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);






entryprice() is a good start, but it really needed to provide a set of return values, like:
entryprice().open
entryprice().barnumber
.stop
.target
.variable1
.variable5
The challenge being to capture a technical indicator condition at the entryprice barnumber such as a trailing volatility measurement) and then be able to manage the position with a lot more realistic control than a fixed or relative value computed on each subsequent bar. Of course, using a more realistic position management technique (such as closing the position in three stages or adjusting to volatility or using a Parabolic SAR given specific criteria of price action, etc.) means building a lot of logic that, as you point out, has to be carried forward and computed with every bar by the long entry, short entry, long exit and short exit scripts. This no doubt burdens the platform and it burdens the thinkScript developer with all the redundant code to keep in sync. Blecch!
wow amaaaaazing i’ve been dreaming about this for the longest time really. gotta say,
hell, its about time…
hope the next update would let us customize the number of ticks or shrink the lower studies to our needs =)
Hello,
I was excited to see the entryPrice() function is available but I must be doing something wrong with it (I’m a real newbie). In trying to declare the definition I typed in
def entry=entryPrice();
and immediately got an error reported: “No such function”. Any idea as to what I’m doing wrong?
Also, if you’ve got the time, I’d sure like the code to simply print this price somewhere on the chart. I was thinking of either a line or a bubble call-out that’d print my entry price on the graph
Thanks!
Make sure you are using this code in a strategy and not a study (there is a big difference). Unfortunately, you cannot draw anything within strategy codes like lines or bubbles.
-Eric
Great find! You are very skilled at pulling gold out of the raw TOS ore.
Also, I like what you’ve done to the place. Your site looks great.
Thanks Pro.
-Eric
Eric, Your screenshot looks very similar to the screenshot I grabbed of an (international) automated trading contestant’s winning algorithm! Imagine an Expert Advisor for TOS, one that would keep the roundtrips to a minimum and still be reasonably profitable. Could such a dream ever come true?
Great work, as always. You’re on the cutting edge, Eric.
Steve