I'm working on a NinjaTrader strategy where I'm facing an issue with managing the profit target and stop loss for the total position. My strategy creates a new profit target and stop loss for each individual order, but I want to reset these for the total position instead.
In essence, each time a new order is placed, I want to cancel the existing profit target and stop loss orders and set new ones that reflect the total position (sum of all individual orders).
I've attempted to solve this with a method called ResetStopANDProfitTarget, but it's not working as expected. Here's a snippet of the relevant code for reference:
// ... [include other relevant portions of your code here, but avoid too long excerpts]
private void ManageRebuyOrders(int rebuyTicks, int limitTicks, int stopLossTicks)
{
double priceChange = Position.MarketPosition == MarketPosition.Long ? Position.AveragePrice - Close[0] : Close[0] - Position.AveragePrice;
if (priceChange / TickSize >= rebuyTicks && RebuyRepetitions > RebuyCounter && Position.AveragePrice > 0)
{
RebuyCounter++;
string orderName = signalDirection == SignalDirection.Long ? "LongPosition" : "ShortPosition";
if (Position.MarketPosition == MarketPosition.Long)
{
EnterLong(DefaultQuantity, orderName);
ResetStopANDProfitTarget(orderName);
}
else if (Position.MarketPosition == MarketPosition.Short)
{
EnterShort(DefaultQuantity, orderName);
ResetStopANDProfitTarget(orderName);
}
Print("Rebuy: " + orderName);
}
}
private void ResetStopANDProfitTarget(string orderName)
{
foreach (var order in Account.Orders)
{
if ((order.OrderType == OrderType.Limit && order.OrderState == OrderState.Working) ||
(order.OrderType == OrderType.StopLimit && order.OrderState == OrderState.Accepted))
{
CancelOrder(order);
Print("Cancel Order: " + order.Name);
}
}
SetProfitTarget(orderName, CalculationMode.Ticks, StopLossProfitTicks, false);
SetStopLoss(orderName, CalculationMode.Ticks, StopLossTicks, false);
}
// ... [any additional relevant code]
I would really appreciate any insights or suggestions on how to resolve this issue. Specifically, I'm looking for guidance on how to correctly cancel existing profit target and stop loss orders and set new ones based on the total position size after each new order is executed.
Thanks in advance for your help!

Comment