Could you please help on what is the issue with this script.. i don't get any signals.. How do we ensure ATR value is at the time of entry as opposed being calculated everyr bar.. The below is for ES futuers and at 5 minute
Code:
#Input for ATR Stop Loss and Take Profit
input ATRStopFactor = 4; # ATRX4 for Stop Loss
input ATRTakeProfitFactor = 8; # ATRX12 for Take Profit
input ATRPeriod = 7;
input ATRaverageType = AverageType.WILDERS;
# ATR Calculation
def atr = MovingAverage(ATRaverageType, TrueRange(high, close, low), ATRPeriod);
#########################################################
#MACD INPUT VALUES
########################################################
input fastLength = 8;#12;20
input slowLength = 21;#26;40
input macdLength = 5;#15;
input averageType = AverageType.EXPONENTIAL;
def Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);
def Avg = MovingAverage(averageType, Value, macdLength);
def Diff = Value - Avg;
#########################################################
# EMA
#########################################################
input periodS = 21;#8;
input periodL = 50;#21;
input periodML = 50;
input periodSL = 200;
input EMAaverageType = AverageType.weighted;
def emaS = MovingAverage(EMAaverageType, close, periodS);
def emaL = MovingAverage(EMAaverageType, close, periodL);
def emaML = MovingAverage(EMAaverageType, close, periodML);
def emaSL = MovingAverage(EMAaverageType, close, periodSL);
#########################################################
# Trailing_Long_Exit/Trailing_Short_Exit
#########################################################
def entryPrice = entryPrice();
def price = if IsNaN(entryPrice[1]) then entryPrice else if !IsNaN(entryPrice) then Max(CLOSE, price[1]) else entryPrice;
#########################################################
#buy/sell logic
#########################################################
def Buy = emaS > emaL and close >emasl and Avg > 0 and Value > Avg;
def Sell = emaS < emaL and close <emasl and Avg < 0 and Value < Avg;
# ATR Stop Loss and Take Profit Levels
def longStop = price - ATRStopFactor;
def longTakeProfit = entryPrice + atr * ATRTakeProfitFactor;
def shortStop = price + ATRStopFactor;
def shortTakeProfit = entryPrice - atr * ATRTakeProfitFactor;
# Place Orders with ATR-based stop loss and take profit logic
AddOrder(OrderType.BUY_to_open, Buy, open[-1], tradeSize = 2,tickColor = Color.GREEN, arrowColor = Color.GREEN, name = "LONG_ENTRY_ORDER");
addOrder(OrderType.BUY_TO_CLOSE, high >= shortStop or low <= shortTakeProfit, open[-1], tradeSize = 2, tickColor = Color.RED, arrowColor = Color.RED,name = "SHORT_EXIT_ORDER");
AddOrder(OrderType.SELL_TO_CLOSE, low <= longStop or high >= longTakeProfit, open[-1], tradeSize = 2,tickColor = Color.RED, arrowColor = Color.RED,name = "LONG_EXIT_ORDER");
AddOrder(OrderType.SELL_to_open, Sell, open[-1], tradeSize = 2,tickColor = Color.GREEN, arrowColor = Color.GREEN, name = "SHORT_ENTRY_ORDER");