Boise, Idaho
Experience: Intermediate
Platform: TOS
Trading: Stocks
Posts: 37 since Aug 2013
Thanks Given: 56
Thanks Received: 9
|
Hi All,
I'm trying to create a strategy with a simple buy condition (close crosses above EMA 34) and with an exit based on meeting a target or hitting stop loss. The strategy however recaculated the Target and stop loss each time the buy criteria is met even when an active position is open. Is there a way to pause the Target/stop loss calculation when there is an open position?
The script is posted below.
Any help is appreciated.
Thanks!
AP
def ATR = MovingAverage(AverageType.WILDERS, TrueRange(high, close, low), 14);
def EMA34 = ExpAverage(close, 34);
#BuySignal
def Buy = Close Crosses above EMA34;
# Buy Target
def BuyTarget = if (Buy) then open[-1] + 5*ATR else BuyTarget[1] ;
Plot BT=BuyTarget;
# Buy StopLoss
def BuyStopLoss = if (Buy) then open[-1]-2*ATR else BuyStopLoss[1];
Plot BSL=BuyStopLoss;
#Orders
AddOrder(OrderType.BUY_TO_OPEN, Buy and tradinghours==1, open[-1], tradeSize = 1, tickcolor = GetColor(1), arrowcolor = GetColor(1), name="Buy");
AddOrder(OrderType.SELL_TO_CLOSE, (low crosses below BuyStopLoss) or flatatclose==1, BuyStopLoss, 1, tickcolor = GetColor(1), arrowcolor = GetColor(2));
AddOrder(OrderType.SELL_TO_CLOSE, (high crosses above BuyTarget) or flatatclose==1, open[-1], 1, tickcolor = GetColor(1), arrowcolor = GetColor(3));
|