Truth be told, I have a love-hate relationship with TOS strategies. If you’ve ever tried to code a strategy exit with either a price target or a stop loss you have undoubtedly felt this frustration. The core of the problem was that exit strategies did not know the price at which the long or short entry was executed. To work around this you had to code the entire entry into the exit and even then that did not necessarily work properly.
History aside, I was working to create stop type exit strategies for one of our members, when I discovered the following (recent?) function addition to thinkScript: entryPrice(). Like mana from heaven, the TOS programmers heard our prayers. As you might have already guessed, this is the solution to the aforementioned problem. With relative ease you can create long and short exits with price targets and stop-losses. I’ve slammed a few together here to illustrate the point. Below is an image of today’s /ES session with a custom strategy of mine and the example stop losses set to target = 3.0 and stop-loss = 1.5 (Strategy Report Here).
# TS_Stop_LX # http://www.thinkscripter.com # Last update 02 Sep 2010 declare LONG_EXIT; input stop = 2.5; input target = 3.0; def entry = entryPrice(); def exit = if (high >= (entry + target) or low <= (entry - stop)) then 1 else 0; def exitPrice = if (high >= (entry + target)) then (entry + target) else (entry - stop); addOrder(exit, exitPrice); setColor(color.white);
# TS_Stop_SX # http://www.thinkscripter.com # Last update 02 Sep 2010 declare SHORT_EXIT; input stop = 2.5; input target = 3.0; def entry = entryPrice(); def exit = if (low <= (entry - target) or high >= (entry + stop)) then 1 else 0; def exitPrice = if (low <= (entry - target)) then (entry - target) else (entry + stop); addOrder(exit, exitPrice); setColor(color.white);





