UPDATE 3-16-2009: Strategies now work on all charts, not just intraday. See code below!
In Think or Swim’s Think Desktop software, you can create your own indicators. These are called “Studies”. You can also create sets of rules for generating entry and exit signals for trades. These are called “Strategies”. Strategies can’t be used to execute actual trades (at least not yet). However, besides painting an annotation on your charts, you can run a simple and quick backtest with your strategies to see the past results, and get a report of the profit or loss from the trades. Here’s how to use them:
There are four basic strategy types. Each strategy script must be declared to be one (and only one) of the following:
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.
Strategies are triggered by the use of the code:
The order is triggered if condition = 1, and the price specified is the “fill” price. I give an example of this later on. Once you have the ENTRY or EXIT declaration made with an “AddOrder” statement, you have all the required pieces of a strategy. The rest comes in fleshing out the details of your trade signals that will set condition to 1 and decide what price to fill at. One gotcha is that the order will be added on the bar AFTER condition is true.
If you use condition[-1] and price[-1] then the strategy will intuitively appear on the bar you are calculating it from. Those are the strategy basics.
Adding Strategies to Your Chart
You add strategies to charts in a way similar to how you add studies. On your chart, click the button labeled “Studies”, then select “Edit Strategies”. Then you will see this screen:
You can see the available strategies in the list on the left. You can create new strategies or click on an exsisting one to edit it or see the source code. You can also save and load strategies to/from a folder on your computer. You add the strategies to your chart just like you add studies.
In this example, I have added strategies I wrote based on an exponential moving average (EMA) cross. This method trades both long and short, so I need at least 4–one LONG_ENTRY, one LONG_EXIT, one SHORT_ENTRY and one SHORT_EXIT. This is not a recommendation to use this EMA cross method!! It will probably lose you money. I give it as a simple example that is easy to understand.
The next thing to do is to set the global properties for your strategy execution. Click “Properties for All” and you get this form:
First, you can set the number of simultaneous trades to be taken. I leave mine set to 1 for simplicity. Second, choose the size of the trades. For stocks, it’s the number of shares. For futures, if you enter 1 your P&L from the strategy report will be in points. If you want P&L in dollars, enter the dollars per point of your contract, i.e. 50 for ES, 20 for NQ and 5 for YM.
After your strategies are added to the chart, set up the timeframe and duration that you want to run. In this example, I chose ES on a 24 hour chart, 30 min bars over 5 days. Note that strategies may NOT have any plots in them! If you want to see your indicators on the chart, you must also add the matching studies. Because of this, it’s a good practice to code all of your studies with def and rec variables only, and then define plots at the end. Then your studies can be dropped right into a strategy, sans-plots of course. I added two EMA’s to the chart that match those in my strategies. Here’s an overview of how the strategies look on the chart:
I constrained my strategies to enter trades only during market hours and to always exit at the close. These are not automatic settings; I had to create code inside the strategy scripts. Here’s a closeup of two days of trades:
You can see that we went short right after the open on the first day. When the fast EMA crossed under the slow one on a bar closing basis, I went short at the closing price of that bar. The strategy exits at the close of the day. The next day, we get whipsawed with a long entry followed by a short entry. There was actually a signal for a LONG_EXIT at the same time as the SHORT_ENTRY, but the new entry signal takes precedence on the chart. The strategy basically went from long +1 to short -1 in the same instant. This will only happen if your entry signal one direction is the same as the exit signal the other direction. Otherwise you’ll see both when they happen. Finally, we exit the short at the end of the day.
Backtesting Your Strategies
Once you have the strategies set up, you can run a report of all the signals on the chart. To do this, right-click on one of the strategy signal annotations and select “Show Report”:
A quick backtest report will be calculated. The test will only be run for the visible data. If you hide after hours data, it won’t be considered. Also, it will only test back as far as you have data. This is not a big deal for daily charts, but it does mean that the furthest you can go back with intraday time charts is 20 days, and less for tick charts. Here’s a report for my EMA Cross strategies over the 5 day period:
The report shows things like trade time and direction, trade P&L, overall P&L at the time of the trades, highest profit trade, and the overall P&L for the test. Notice that this method did well with a positive profit; almost any trend-following method has been doing well in this market. An EMA Cross method will get disemboweled during a choppy range-bound market, as will any trend-following method. I’m working on an Excel-based post processor that will generate more information, such as win rate, loss rate, expectancy and so forth.
Look for that soon Here’s the post processor!
And that’s it! To help you get started on your own strategies, you can download my Thinkscript Strategy files for the EMA Cross Method and load them into your own list, or use my Strategy Template files as a starting point to make your own.