Posts Tagged ‘backtest’

Fixed Stop Loss and Profit Target Strategies for Think or Swim

November 2, 2010

As promised, here are four strategies showing how to implement fixed stop losses and fixed profit targets for TOS. You need to add these to a chart with some entry strategy. I have included two example entries for you to test out if you want. These are called “Justbuy” and “Justsell”. If there are two green bars in a row, it goes long. If two red bars in a row, it goes short. Otherwise, the targets and stops manage the trade.

You can run a backtest on it to see the results, and play with the stops and targets to see if it improves (or worsens!). To me, this is a good quick way to test if you have any edge in an idea. If there is one, it will show up right away in a simple test. If it takes fancy tools and algorithms to squeeze out a drop of an edge, but you don’t have direct market connections and 8+ figures of capital to employ it, then you are fooling yourself and asking for pain.

These strategies are free. Download “Fixed Stops and” from “Released Thinkscript Strategies” on my Google site. As always, leave a comment if you have any questions or need help setting them up!

Messing Around With Cumulative Tick

June 13, 2009

After a comment by Donahchoo on Twitter, I started thinking of ways of looking at cumulative values of the NYSE Tick.

As with many other topics, there has been exhaustive treatment of what the Tick is and the significance thereof. Richard at Move the Markets has a very good Tick article if you want this background info. Dr. Brett Steenbarger talks a lot about how he uses cumulative adjusted Tick; see his blog for more details on what he does with it. Building on this foundation of what the Tick is, I’ll jump right in. This is more of a thought journal of my impressions than an exhaustive treatment of an optimized indicator, so it’s a bit rough around the edges.

I’ve been thinking that the most revealing information comes when we see extremes in the value of the Tick. Many times we hear of the tick being referenced to absolute levels: above +1000 could mean heavy buying, below -1000 heavy selling. The trouble with this is that sometimes you get a spike in the Tick that is unsustainable (and you should fade it), and sometimes you get heavy, extended readings for a long time (that you should be following). But the absolute level of the Tick doesn’t really tell me which is which. Additionally, it seems that when the low of a bar of the Tick stays relatively high (such as the low of a 5 min Tick bar being at +400 and the high at +1200), that means more for strength than having both a high and a low reading in the same bar, as in both +1200 and -1200.

So to define an extreme, I decided to use a different approach. First, I apply two exponential moving averages to create bands. I plotted the EMA(20) of the highs in the Tick, and the EMA(20) of the lows. Then I wrote a script to sum the net extremes ONLY, ignoring any Tick readings happening inside the bands, using this formula:

def htick = high("$TICK");
def ltick = low("$TICK");
def avgh = expaverage(htick,20);
def avgl = expaverage(ltick,20);
def bull = if htick > avgh then htick - avgh else 0;
def bear = if ltick < avgl then ltick - avgl else 0;

rec ctick = if barnumber()==1 then 0 else if IsNaN(htick) OR IsNaN(ltick) then ctick[1] else ctick[1] + bull + bear;

That code will sum only the extreme Tick readings, as defined by our EMA bands. I like this because it forms an adaptive definition for tick extremes, and it also captures the effect of having high Tick highs and lows as I described above, and vice versa for low highs and lows.

I wrote a full indicator to test this out on a 5min chart of the last 3 days in ES. I’ve plotted the indicator below the price chart. I’ve also included a separate plot of the Tick for visualization purposes. The CumTick indicator is doing all it’s own calculations behind the scenes. There are three things going on that I’ll explain:

1. A net sum line (thin line) for the cumulative values of ctick as described in the code above.
2. A 20 period EMA of the net sum line. This is done for smoothing purposes. This line is colored according to values in it’s own past–if the EMA is above the value of the same EMA 4 periods ago, it is green, else red. This lookback is kind of like the way the Fisher transform looks back at it’s own past values, only it uses a lookback period of 1.
3. I have added a cloud, red if the EMA is below zero, and green if it is above zero.


You can see that the red/green EMA does pretty well on choosing the dominant market direction. Zooming in for some detail, I’ve annotated graphically what my code from above is doing:


I wrote a couple of strategies to run a quick backtest on this. I went long when the EMA went from red to green, and opposite for shorts. I also included an ‘exit on close’ to keep this to a daytrading strategy only. Here’s what the trades looked like:


Only a couple of whipsaws, but the entries had some significant retracement at times. With no stop loss or profit target, just reversing according to color and going flat at the end of the day, here’s the results (in ES points) for the 3 day period:

Max trade P/L: 10.00
Total P/L 26.25
Total 41 order(s)

That’s very encouraging! I always want to do a sanity check on new ideas. If it’s not profitable in a simple test, you’re not going to mine gold by tweaking it. I would need to do more work in looking at stops and more backwards data to gain confidence in it, and work on an entry setup to see if there was a smarter way to get in. I welcome comments and any ideas anybody may have, if you’re interested. You can download my indicator file and the strategy files (look for “Cumulative”) in the “Work in Progress” section at my google site.

Thinkscript Strategy Report Tool

April 14, 2009

UPDATE 12-18-2009: I updated the tool to v2 after a TOS update broke it. Get the new version at the link below!

UPDATE 4-24-2009: I updated the Thinkscript Strategy Report file to make it backward compatible with at least Excel 2000. Download again if you have had problems!

In my earlier post, I covered how to create strategies using Thinkscript. I have developed a tool that can calculate some more useful statistics about your trading system results.

After adding a strategy set to a chart, you right-click on one of the signals and choose “show report”, as described in my tutorial. Think Desktop then gives you a report on your strategy results. You can save the summary as a .csv file. The information given in the report is net Profit/Loss and the trade that gave the maximum profit. This info isn’t really adequate to evaluate a trade strategy, at least in my book.

However, since most of the information is there, all it takes is a little Excel macro magic to import the .csv file and calculate important parameters, like win rate, loss rate, average win size, average loss size, largest win streak, largest loss streak, and system expectancy, or expected profit per trade over a very large number of samples.

Once you have this information, you can plug it into my Monte Carlo Trade System Simulator and stress test it. You can see how bad (or good) your results could be in the future if all of these parameters remain constant. Or, you can adjust them and get what-if scenarios. What if I have 10% fewer winners? What if my average profit is less than what I get in this small backtest?

Well, download the Thinkscript Strategy Report tool under Released Tools at my Google site to get started! As always, you have to enable macros for the tool to work.

This release is grovel-ware:
Pleasepleasepleeeeeeeeeaze !!! Oh, please!! I will always honor your memory! I swear!!

Please note: this tool only works if you use single position strategies! If you allow more than one entry per direction in the “Properties for all” preference screen, this tool will not work.