namespace NinjaTrader.NinjaScript.Strategies
{
public class PleaseHelpStrategy : Strategy
{
private double initialEntryPrice;
private double profitTargetPriceInitial;
private double stopLossPriceInitial;
private double profitTargetPriceAdd;
private double stopLossPriceAdd;
private double profitTargetPrice;
private double stopLossPrice;
private double atrValue;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Trying to use unadjusted atr"; //fading the strategy and using a trailing stop
Name = "MarkII10PFractal";
Calculate = Calculate.OnBarClose;
EntriesPerDirection = 2;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = true;
ExitOnSessionCloseSeconds = 30;
IsFillLimitOnTouch = false;
MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
OrderFillResolution = OrderFillResolution.Standard;
Slippage = 0;
StartBehavior = StartBehavior.WaitUntilFlat;
TimeInForce = TimeInForce.Gtc;
TraceOrders = false;
RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
StopTargetHandling = StopTargetHandling.PerEntryExecution;
BarsRequiredToTrade = 20;
IsInstantiatedOnEachOptimizationIteration = true;
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;
if (CurrentBar < 20)
return;
// Calculate ATR
atrValue = ATR(14)[0];
bool goLongSignal = //(my long entry criteria)
bool goShortSignal = //(my short entry criteria)
bool market_open = ToTime(Time[0]) >= 093000 && ToTime(Time[0]) <= 160000;
if (Position.MarketPosition == MarketPosition.Flat)
{
if (goShortSignal&& market_open)
{
// Initial entry short logic
EnterShort("InitialEntryShort");
double stopLimitPrice = Close[0] - atrValue;
// Submit or re-submit additional entry short order
EnterShortStopLimit(1, stopLimitPrice, stopLimitPrice, "AdditionalEntryShort");
}
if (goLongSignal && market_open)
{
// Initial entry long logic
EnterLong("InitialEntryLong");
double stopLimitPrice = Close[0] + atrValue;
// Submit or re-submit additional entry long order
EnterLongStopLimit(1, stopLimitPrice, stopLimitPrice, "AdditionalEntryLong");
}
}
if (Position.MarketPosition == MarketPosition.Short && Position.Quantity == 1)
{
double stopLimitPrice = initialEntryPrice - atrValue;
// Submit or re-submit additional entry short order
EnterShortStopLimit(1, stopLimitPrice, stopLimitPrice, "AdditionalEntryShort");
}
if (Position.MarketPosition == MarketPosition.Long && Position.Quantity == 1)
{
double stopLimitPrice = initialEntryPrice + atrValue;
// Submit or re-submit additional entry long order
EnterLongStopLimit(1, stopLimitPrice, stopLimitPrice, "AdditionalEntryLong");
}
}
protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
{
if (execution.Order != null && execution.Order.OrderState == OrderState.Filled)
{
if (execution.Order.Name == "InitialEntryLong")
{
initialEntryPrice = execution.Price;
profitTargetPrice = initialEntryPrice + (atrValue * 2);
stopLossPrice = initialEntryPrice - (atrValue * 2);
SetProfitTarget("InitialEntryLong", CalculationMode.Price, profitTargetPrice);
SetStopLoss("InitialEntryLong", CalculationMode.Price, stopLossPrice, false);
}
else if (execution.Order.Name == "InitialEntryShort")
{
initialEntryPrice = execution.Price;
profitTargetPrice = initialEntryPrice - (atrValue * 2);
stopLossPrice = initialEntryPrice + (atrValue * 2);
SetProfitTarget("InitialEntryShort", CalculationMode.Price, profitTargetPrice);
SetStopLoss("InitialEntryShort", CalculationMode.Price, stopLossPrice, false);
}
else if (execution.Order.Name == "AdditionalEntryLong")
{
SetStopLoss("InitialEntryLong", CalculationMode.Price, initialEntryPrice, false);
SetStopLoss("AdditionalEntryLong", CalculationMode.Price, initialEntryPrice, false);
SetProfitTarget("AdditionalEntryLong", CalculationMode.Price, profitTargetPrice);
}
else if (execution.Order.Name == "AdditionalEntryShort")
{
SetStopLoss("InitialEntryShort", CalculationMode.Price, initialEntryPrice, false);
SetStopLoss("AdditionalEntryShort", CalculationMode.Price, initialEntryPrice, false);
SetProfitTarget("AdditionalEntryShort", CalculationMode.Price, profitTargetPrice);
}
}
}
}
}
The problem is that around 50% of the time in backtesting on the NQ futures the additional entries are off by 0.50 - 0.75, maybe that's just slippage, I don't know. And about 10% of the time the adds are off by multiple points. I included some pictures of when the additional entries are off by 1.0 or more. In the pictures, the ATR that the trade is using is hovered over and shown in the data box in the top left so you can see that the add spots are off if you use the ATR that is given. Please tell me how I can fix the script so that it always adds right where it should. Thank you.

Comment