Define Exit Target in ThinkorSwim Strategy code?

K

kalivibe

New member
VIP
Thanks for all your help. I have another question regarding strategy exits. Again, just learning. I am trying to exit when the first of two actions occurs. When I use this looking for just one parameter it works properly.

input barexit = 2;
def LongExt= LongEnt from barexit bars ago;
AddOrder(OrderType.SELL_TO_CLOSE, LongExt, high, TradeSize);

Where LongEnt is also aready defined.

However when I try this, I don't get an error but it seems like half of the trades are no longer there?

input TargetRatio = 1.015;
input barexit = 2;
def LongTarget = LongEnt * TargetRatio;
def LongExt= LongTarget or LongEnt from barexit bars ago;
AddOrder(OrderType.SELL_TO_CLOSE, LongExt, close, TradeSize);

Any help would be great!
 
K

kalivibe

New member
VIP
In the statement highlighted above, you are trying to set LongTarget as the order entry price * the TargetRatio. However, the variable LongEnt is NOT the order entry price. Recall that you defined that variable as your entry conditions. So, it is a boolean condition. That means it will have a value of either true or false which is interpreted by the computer as either a 1 or a 0.

So, def LongTarget = LongEnt * TargetRatio; will either be 1 * 1.015 or 0 * 1.015. Neither of which is what you intended.

Take a look at this reference regarding the EntryPrice() function.

I believe what you want is something along the lines of this:

Ruby:
input TargetRatio = 1.015;
input barexit = 2;
def LongTarget = EntryPrice() * TargetRatio;
def LongExt= (close >= LongTarget) or (LongEnt from barexit bars ago);
AddOrder(OrderType.SELL_TO_CLOSE, LongExt, close, TradeSize);

Thanks. Is EntryPrice() known by thinkscript? Also can I change (close >= LongTarget) to (high >= LongTarget)? Would I need to change the Sell to close code as well then?
 
K

kalivibe

New member
VIP
Thanks!! Really appreciate it. Sorry for all newbie questions. Is changing the following OK?
(close >= LongTarget) to (high >= LongTarget)? Would I need to change the Sell to close code as well then?
 
RobertPayne

RobertPayne

Member
Thanks!! Really appreciate it. Sorry for all newbie questions. Is changing the following OK?
(close >= LongTarget) to (high >= LongTarget)? Would I need to change the Sell to close code as well then?
Try it. See what happens. I find the best way to learn is to experiment and see what does and doesn't work.
 
K

kalivibe

New member
VIP
Hi - I am still struggling to get this to work. I am trying to basically put in a target and if that is not after bars exit. With the code below it always exits after 2 bars only. Any advice would be great. Thanks.

def TradeSize =100;
input TargetRatio = 1.015;
input TargetRatioShort = .015;
input barexit = 2;
def LongEnt = UpsideBreakout and high[-1] > high;
def LongTarget = EntryPrice () * TargetRatio;
def LongExt= (high >= LongTarget) or (LongEnt from barexit bars ago);

AddOrder(OrderType.BUY_TO_OPEN, LongEnt, high, TradeSize);

AddOrder(OrderType.SELL_TO_CLOSE, LongExt, close, TradeSize);
def ShortEnt= DownsideBreakout and low[-1] < low;
def ShortTarget = EntryPrice () - (TargetRatioShort * EntryPrice ());

def ShortExt = ShortTarget or ShortEnt from barexit bars ago;

AddOrder(OrderType.SELL_TO_OPEN, ShortEnt, low, TradeSize);

AddOrder(OrderType.BUY_TO_CLOSE, ShortExt, close, TradeSize);
 
K

kalivibe

New member
VIP
Wondering if there is anyone that would help me with some programming for payment? I have indicators I have created that I use for entry. I would like to test them using various exit methods . . . taking partial profit at an ATR based target, exiting remainder with a ATR trail stop or based on an indicator.
 
BenTen

BenTen

Administrative
Staff
VIP
@kalivibe I suggest you post the code so our community can take a look and help you out.
 
K

kalivibe

New member
VIP
Thanks. Here is an example. Right now this exits after 2 bars. However I would like to create the exits I described above additionally. Very, very new to attempting to program.

def RBup = close > open and high < high[1] and high[1] < high[2] and low < low[1] and close[1] < open[1] and high[-1] > high;
plot bullish = if RBup then high else Double.NaN;
bullish.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
alert(bullish,"RB Up", alert.Bar,sound.ding);
def RBdown = close < open and low > low[1] and low[1] > low[2] and high > high[1] and close[1] > open[1] and low[-1] < low;
plot bearish = if RBdown then low else Double.NaN;
bearish.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
alert(bearish,"RB Down", alert.Bar, sound.ding);

def TradeSize =100;
input barexit = 2;
def LongEnt = RBup;
def LongExt= LongEnt from barexit bars ago;

AddOrder(OrderType.BUY_TO_OPEN, LongEnt, high, TradeSize);

AddOrder(OrderType.SELL_TO_CLOSE, LongExt, high, TradeSize);
def ShortEnt= RBdown;
def ShortExt = ShortEnt from barexit bars ago;

AddOrder(OrderType.SELL_TO_OPEN, ShortEnt, low, TradeSize);

AddOrder(OrderType.BUY_TO_CLOSE, ShortExt, low, TradeSize);
 
Top