I am trying to use IOrders to store my entry prices and then use those values to exit after target/stop condition is triggered.
But when I backtest the below code and when I print the entry1price, entry2price, diff to output: I just get 3 zeros on the output window plus the stop gets trigged immediately on the same bar, but the price never dropped 100 points. What’s causing this?…
(note: This doesn’t make sense to me, but the code works if I change ‘<’ to ‘>’ in the condition that triggers the stop….also this is a multi instrument strategy)
Can someone please give me a bit advice on how to fix this
Thanks for your time in advance

#region Variables
private IOrder entryOrder1 = null;
private IOrder entryOrder2 = null;
private double entry1Price = 0;
private double entry2Price = 0;
#endregion
protected override void OnBarUpdate()
{
currentdiff = closes[1][0] – closes[2][0];
//this is the entry
If (condition1
&& entryOrder1 == null
&& entryOrder2 == null
{
entryOrder1 = EnterLong(); entryOrder2 = EnterShort();
}
//this to calculate the difference between my entry prices on two instruments
If (entryOrder1 != null && entryOrder2 != null)
{
if (entryOrder1.Filled > 0 && entryOrder2.Filled > 0)
{
entry1Price = entryOrder1.AvgFillPrice; entry2Price = entryOrder2.AvgFillPrice;
diff = entry1Price - entry2Price;
}
//this is the profit target
//currentdiff is calculated on each bar
If(currentdiff >= diff + 100
&& entryOrder1 != null && entryOrder2 != null)
{
ExitLong(); ExitShort();
entryOrder1 = null; entryOrder2 = null;
Print(entry1Price); Print(entry2Price);
Print(diff);
}
//this is the stop
If(currentdiff <= diff - 100
&& entryOrder1 != null && entryOrder2 != null)
{
ExitLong(); ExitShort();
entryOrder1 = null; entryOrder2 = null;
Print(entry1Price); Print(entry2Price);
Print(diff);
}
}
else {}

Comment