Friday, May 15, 2009

Trade automation in ThinkOrSwim using 'Strategies'

Here is a moving average cross strategies where a moving average crosses over another ex. a 10 day simple moving average crosses over a 30 day simple moving average.

You may want to try this for creating a mechanical trade system.

To create a Strategy follow these image instructions (Click on image to zoom in)

Choose 'Edit Strategies'


Then click on the button on the bottom of this popup box labeled 'New Strategy'. (Click on this image to zoom in)


Then choose a new name for your strategy, delete the default TOS code "declare Long_Entry;" and copy/paste the code into that area. Each LONG_ENTRY and SHORT_ENTRY should have their own startegy and strategy name for ease of use.

Strategy Overview

There are four basic strategy types. Each strategy script must be declared to be one (and only one) of the following:

-LONG_ENTRY
-LONG_EXIT
-SHORT_ENTRY
-SHORT_EXIT

For each trade direction you want to use, you need at least one entry strategy and one exit strategy. For a long only method, you would need at least one “LONG_ENTRY” strategy and one “LONG_EXIT” strategy. If you go long and short, you need at least one of each of the four types. I say “at least” above because you could have more than one entry or exit strategy per trade direction, i.e. one “LONG_ENTRY” and two “LONG_EXIT” strategies, where one exit might represent a profit target and the other one a stop loss.



Here is some customizable code that you can use as a template. We will add this to a chart that uses SMA studies. Where SMA1 is the shorter of the two moving averages and SMA2 is the longer of the two moving averages.

Possible Name: MACrossoverLE

#-------------------------------------------begin code
declare LONG_ENTRY;

input SMA1length=10;

input SMA2length=30;

def SMA1=reference SimpleMovingAvg(length=SMA1length);

def SMA2=reference SimpleMovingAvg(length=SMA2length);

addOrder(SMA1>SMA2);

#-------------------------------------------end code

Short Entry Code:

Possible Name: MACrossoverSE
#-------------------------------------------begin code
declare SHORT_ENTRY;

input SMA1length=10;

input SMA2length=30;

def SMA1=reference SimpleMovingAvg(length=SMA1length);

def SMA2=reference SimpleMovingAvg(length=SMA2length);

addOrder(SMA1 < SMA2);
#-------------------------------------------end code
blog comments powered by Disqus