I am working in an strategy with two data series running OnEachTick. It has also breakeven conditions that works.
All stuff works perfect with just one contract. However, when entries have more than one, all works ok until the script uses breakeven one time. Then, in next entry, error occurs and strategy stops just in the moment of entry
"Stop price can´t be changed below the market"
So, breakeven conditions try to change stop before conditions required. I have made a lot of changes, prints and nothing works... I suppose there is a problem resetting any variable but I don´t find the solution. Can you please give me a clue?
These are the lines of code
#region Strategy Variables
private double stopShortPrice;
private double targetShortPrice;
private double entryShortPrice;
private double entryShortObj;
private int NroShorts;
#endregion
else if (State == State.Realtime)
{
if (entryShortOrder != null)
entryShortOrder = GetRealtimeOrder(entryShortOrder);
if (stopShortOrder != null)
stopShortOrder = GetRealtimeOrder(stopShortOrder);
if (targetShortOrder != null)
targetShortOrder = GetRealtimeOrder(targetShortOrder);
}
OnBarUpdate
if IsFirstTickOfBar
&& Conditions[1]
{
entryShortPrice =...
NroShorts=
entryShortOrder = (EnterShortLimit(0, true, NroShorts, entryShortPrice, @"S1"));
}
#region BREAKEVEN
if (BarsInProgress == 0)
{
if (CurrentBar > barNumberOfEntryShort + 1
&& stopShortOrder != null
&& stopShortPrice > Position.AveragePrice)
{
if (Close[0] <= (Position.AveragePrice - ((Position.AveragePrice - targetShortPrice) * 0.5))
{
ChangeOrder(stopShortOrder, stopShortOrder.Quantity, 0, Position.AveragePrice);
barNumberOfEntryShort = 0;
}
}
}
#endregion
OnExecutionUpdate
if (entryShortOrder != null && entryShortOrder == execution.Order)
{
if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
{
sumShortFilled += execution.Quantity;
if (execution.Order.OrderState == OrderState.PartFilled)
{
stopShortOrder = ...
targetShortOrder = ...
}
else if (execution.Order.OrderState == OrderState.Filled && sumShortFilled == execution.Order.Filled)
{
stopShortPrice = ...
targetShortPrice = ...
stopShortOrder = ExitShortStopMarket(0, true, execution.Order.Filled, stopShortPrice, "Stop S1", "S1");
targetShortOrder = ExitShortLimit(0, true, execution.Order.Filled, targetShortPrice, "Target S1", "S1");
barNumberOfEntryShort = CurrentBar;
}
if (execution.Order.OrderState != OrderState.PartFilled && sumShortFilled == execution.Order.Filled)
{
entryShortOrder = null;
sumShortFilled = 0;
}
}
Comment