I implemented a strategy that reverses the amount of open contracts and there is always a part of the position wrongly closed.
The growth of the contracts shall be 1, 2, 4, 8, 16 etc.
But the logic reverses somehow the existing amount of contracts in the other direction, results in.
1, 3, 6, 8, 20 etc.
The implementation starts with opening the first order:
EnterLong(DefaultQuantity,"buyOrder");
followed by (On execution update)
if (execution.Order.OrderAction == OrderAction.Buy)
{
// Set stop loss
double stopOrderPrice = execution.Order.AverageFillPrice - BoxSize * TickSize * 4;
ExitLongStopMarket(0, true, execution.Order.Quantity, stopOrderPrice, "Stop loss", execution.Order.FromEntrySignal);
// Set profit target 1
double profitTarget = execution.Order.AverageFillPrice + ProfitFactor * BoxSize * TickSize * 4; ;
ExitLongLimit(0, true, execution.Order.Quantity, profitTarget, "Profit target", execution.Order.FromEntrySignal);
}
When the reverse shall happen, the Stop Loss of the first position is hit, the following code is called:
if (order.OrderAction == OrderAction.Sell) // long position
{
order.Account.CancelAllOrders(order.Instrument);
//SetProfitTarget("sellOrder", CalculationMode.Pips, BoxSize * ProfitFactor);
//SetStopLoss("sellOrder", CalculationMode.Pips, BoxSize, false);
EnterShort(DefaultQuantity * (int)Math.Pow(2, reversesCount), "sellOrder");
reversesCount++;
}
with respective stop loss setting
if (execution.Order.OrderAction == OrderAction.SellShort)
{
// Set stop loss
double stopOrderPrice = execution.Order.AverageFillPrice + BoxSize * TickSize * 4;
ExitShortStopMarket(0, true, execution.Order.Quantity, stopOrderPrice, "Stop loss", execution.Order.FromEntrySignal);
// Set profit target 1
double profitTarget = execution.Order.AverageFillPrice - ProfitFactor * BoxSize * TickSize * 4; ;
ExitShortLimit(0, true, execution.Order.Quantity, profitTarget, "Profit target", execution.Order.FromEntrySignal);
}
The first reverse returns instead of 2 contracts a position with 3 contracts:
The log delivers an opening of 2 contracts
SellStopLimit order is no longer active.
10.02.2025 22:23:51 Strategy 'SzymonTC24DSv2/340536621': Entered internal SubmitOrderManaged() method at 10.02.2025 22:23:51: BarsInProgress=0 Action=BuyToCover OrderType=StopMarket Quantity=1 LimitPrice=0 StopPrice=21834.50 SignalName='Stop loss' FromEntrySignal=''
10.02.2025 22:23:51 Strategy 'SzymonTC24DSv2/340536621': Entered internal SubmitOrderManaged() method at 10.02.2025 22:23:51: BarsInProgress=0 Action=BuyToCover OrderType=Limit Quantity=1 LimitPrice=21828.50 StopPrice=0 SignalName='Profit target' FromEntrySignal=''
Stop loss was triggered at: 10.02.2025 22:28:53
10.02.2025 22:28:53 Strategy 'SzymonTC24DSv2/340536621': Entered internal SubmitOrderManaged() method at 10.02.2025 22:28:53: BarsInProgress=0 Action=Buy OrderType=Market Quantity=2 LimitPrice=0 StopPrice=0 SignalName='buyOrder' FromEntrySignal=''
buy order updated: Submitted
Buy Limit = 21835.75 Sell Limit = 21833.25
buy order updated: Accepted
buy order updated: Working
buy order updated: Filled
BuyStopLimit order is no longer active.
10.02.2025 22:28:53 Strategy 'SzymonTC24DSv2/340536621': Entered internal SubmitOrderManaged() method at 10.02.2025 22:28:53: BarsInProgress=0 Action=Sell OrderType=StopMarket Quantity=2 LimitPrice=0 StopPrice=21832.50 SignalName='Stop loss' FromEntrySignal=''
10.02.2025 22:28:53 Strategy 'SzymonTC24DSv2/340536621': Entered internal SubmitOrderManaged() method at 10.02.2025 22:28:53: BarsInProgress=0 Action=Sell OrderType=Limit Quantity=2 LimitPrice=21838.50 StopPrice=0 SignalName='Profit target' FromEntrySignal=''
Stop loss was triggered at: 10.02.2025 22:30:26
I can't get the reason of the missing of the full closing of the previous position. Does anyone have good ideas for that?
Comment