I am fairly new to coding and currently trying to work on this strategy. I'm encountering an issue with my NinjaTrader strategy where long positions I enter are immediately closing at a 1-tick loss. Additionally, the entry price for these positions is being returned as 0.
protected override void OnBarUpdate() {
if (CurrentBar < BarsRequiredToTrade)
return;
if ((Times[0][0].TimeOfDay > new TimeSpan(6, 40, 0))
&& (Times[0][0].TimeOfDay < new TimeSpan(11, 30, 0))
&& (slowEMA1[0] > slowEMA2[0])
&& (CrossAbove(fastEMA1, fastEMA2, 1)))
{
EnterLong();
entryPrice = Position.AveragePrice;
atr1 = ATR(Close, 14);
atrPrice = (atr1[0] * 1.5);
var atrStop = Convert.ToInt32(entryPrice - atrPrice);
var atrTarget = Convert.ToInt32(entryPrice + atrPrice);
SetProfitTarget(CalculationMode.Price, atrTarget);
SetStopLoss(CalculationMode.Price, atrStop);
Print("Long Entry Triggered");
Print("Entry Price: " + entryPrice);
Print("Profit Target: " + atrTarget);
Print("Stop Loss: " + atrStop);
Print("ATR Price: " + atrPrice);
Print("ATR Value: " + atr1);
}
}
I've verified that the strategy fulfills the entry conditions and calls the EnterLong() function. I've also checked for errors in stop-loss and take-profit calculations within the OnBarUpdate.
Here is what was outputted from the print statements:
Long Entry Triggered
Entry Price: 0
Profit Target: 1
Stop Loss: -1
ATR Price: 1.41738909800935
ATR Value: ATR(MES JUN24 (1 Minute),14)
I am currently running Version 8.1.2.1 of NinjaTrader, and I have tried backtesting this using the playback option in NinjaTrader.
Any insight and help would be greatly appreciated!
Comment