I'm coding a strategy for NinjaTrader 8 and encountering an issue with order management.
I've implemented logic to evaluate the number of contracts for each operation, ensuring I never exceed a specified dollar value per trade. This part is working as intended. However, the problem arises when exiting trades with a certain number of contracts.
For instance, when attempting to exit a trade with 10 contracts, the strategy sometimes sells 11 contracts instead, leaving an open order on the chart. This open order has an entry point at the previous exit and no Take Profit (TP) or Stop Loss (SL) set. I'm attaching a picture that shows the issue, where an operation with 17 contracts was closed with 18 contracts, leaving one open order.
Interestingly, if I disable this feature and simply run the strategy with a fixed number of contracts, the problem does not occur. I'm also attaching the log for the operation shown in the picture to help you evaluate the issue.
Here's the relevant part of my code for managing the operations:
csharp// Long Entry
if (Close[2] > entryLevel && Close[1] > entryLevel && Close[0] <= entryLevel && !entradaRealizada && Position.MarketPosition == Cbi.MarketPosition.Flat && !riskManagementTriggered)
{
if (SelectedTargetMode == TargetMode.Fib764And1146)
{
int contractsTP1 = (contracts + 1) / 2;
int contractsTP2 = contracts / 2;
EnterLong(contractsTP1, "Long TP1");
EnterLong(contractsTP2, "Long TP2");
}
else
{
EnterLong(contracts, SelectedTargetMode == TargetMode.Fib764 ? "Long TP1" : "Long TP2");
}
entradaRealizada = true;
currentStopPrice = bufferFib0;
}
// Manage Long Position
if (Position.MarketPosition == Cbi.MarketPosition.Long)
{
if (SelectedTargetMode == TargetMode.Fib764And1146)
{
int contractsTP1 = (contracts + 1) / 2;
int contractsTP2 = contracts / 2;
// Verify if the stop price is valid before sending the order
if (currentStopPrice < GetCurrentBid())
{
ExitLongStopMarket(contractsTP1, currentStopPrice, "Stop Loss TP1", "Long TP1");
ExitLongStopMarket(contractsTP2, currentStopPrice, "Stop Loss TP2", "Long TP2");
}
else
{
ExitLong(contractsTP1, "Stop Loss TP1", "Long TP1");
ExitLong(contractsTP2, "Stop Loss TP2", "Long TP2");
}
ExitLongLimit(contractsTP1, bufferFib764, "Take Profit TP1", "Long TP1");
ExitLongLimit(contractsTP2, bufferFib1146, "Take Profit TP2", "Long TP2");
}
else
{
if (currentStopPrice < GetCurrentBid())
{
ExitLongStopMarket(contracts, currentStopPrice, "Stop Loss", SelectedTargetMode == TargetMode.Fib764 ? "Long TP1" : "Long TP2");
}
else
{
ExitLong(contracts, "Stop Loss", SelectedTargetMode == TargetMode.Fib764 ? "Long TP1" : "Long TP2");
}
ExitLongLimit(contracts, SelectedTargetMode == TargetMode.Fib764 ? bufferFib764 : bufferFib1146, "Take Profit", SelectedTargetMode == TargetMode.Fib764 ? "Long TP1" : "Long TP2");
}
if (UseBreakEven && Close[0] >= bufferFib764)
{
if (currentStopPrice != Position.AveragePrice)
{
currentStopPrice = Position.AveragePrice;
if (currentStopPrice < GetCurrentBid())
{
ExitLongStopMarket(contracts, currentStopPrice, "Stop Loss", SelectedTargetMode == TargetMode.Fib764 ? "Long TP1" : "Long TP2");
}
else
{
ExitLong(contracts, "Stop Loss", SelectedTargetMode == TargetMode.Fib764 ? "Long TP1" : "Long TP2");
}
}
}
}
private int CalculateContracts()
{
if (Contracts == ContractsMode.MaxLossPerTrade)
{
double entryLevel = SelectedEntryMode == EntryMode.Fib382 ? bufferFib382 : bufferFib236;
double stopLossTicks = Math.Abs(entryLevel - bufferFib0) / TickSize;
double stopLossAmount = stopLossTicks * TickSize * Instrument.MasterInstrument.PointValue;
if (stopLossAmount <= 0)
{
Print("Error: Stop loss amount is zero or negative. Cannot calculate contracts.");
return 0;
}
int contracts = (int)Math.Floor(MaxLossPerTrade / stopLossAmount);
return Math.Max(1, contracts);
}
return ContractsPerTrade;
}
What can I do to fix this issue?
Thank you for your assistance.

Comment