Hello
I’m building an indicator to break even a position that already has stops. When it reaches a certain profit point, I want to modify the stops (it can be one or more stop orders). The changeOrder method isn’t available for indicators. How can I do this?
I’ve been looking into Account.Change(…) being able to do this, but I don’t know how to implement it.
Thanks.
#region GetLiveStopLimit(tag)
private NinjaTrader.Cbi.Order GetLiveStopLimit(string tag)
{
return myAccount.Orders
.Where(o => o.Name == tag &&
o.OrderType == OrderType.StopLimit &&
(o.OrderState == OrderState.Working || o.OrderState == OrderState.Accepted))
.OrderByDescending(o => o.Time)
.FirstOrDefault();
}
#endregion
#region Amend Order
private void AmendOrder(string orderTag, double newLimit, double newStop, int newQty)
{
TriggerCustomEvent(_ =>
{
//
Find latest modifiable StopLimit order with matching name
NinjaTrader.Cbi.Order order = GetLiveStopLimit(orderTag);
// Fallback if SL3 not found
if (order == null && orderTag == "SL3")
{
Print($"⚠️ '{orderTag}' not found or inactive — trying fallback 'SL2'...");
order = GetLiveStopLimit("SL2");
}
if (order == null)
{
Print($"❌ Amend failed — no modifiable order found for '{orderTag}' (or fallback).");
return;
}
// ✅ Round to tick size
newLimit = Instrument.MasterInstrument.RoundToTickSize(newLimit);
newStop = Instrument.MasterInstrument.RoundToTickSize(newStop);
// ✅ Get live market price based on order side
marketPrice = order.OrderAction == OrderAction.Sell
? GetCurrentBid()
: GetCurrentAsk();
// ✅ Validate Stop Price vs Market (real-time safe)
if (order.OrderAction == OrderAction.Sell && newStop > marketPrice)
{
Print($"❌ Invalid Sell Stop: {newStop} > market {marketPrice}");
newStop = marketPrice - TickSize; // Optional auto-correction
}
if (order.OrderAction == OrderAction.Buy && newStop < marketPrice)
{
Print($"❌ Invalid Buy Stop: {newStop} < market {marketPrice}");
newStop = marketPrice + TickSize; // Optional auto-correction
}
// ✅ Limit/Stop logic check
if ((order.OrderAction == OrderAction.Buy && newLimit < newStop) ||
(order.OrderAction == OrderAction.Sell && newLimit > newStop))
{
Print($"⚠️ Invalid limit-stop relationship: Limit {newLimit}, Stop {newStop}, Side {order.OrderAction}");
return;
}
if (newQty <= 0)
{
Print("⚠️ Invalid quantity — must be greater than 0.");
return;
}
// ✅ Apply changes
order.LimitPriceChanged = newLimit;
order.StopPriceChanged = newStop;
order.QuantityChanged = newQty;
if (ShowPrints)
{
Print($"🔧 Amending '{order.Name}' (tag: '{orderTag}')");
Print($"➡️ Limit: {newLimit}, Stop: {newStop}, Qty: {newQty}, Market: {marketPrice}");
}
myAccount.Change(new[] { order });
}, null);
}
#endregion
This code assumes 2 stops SL2 and SL3 - you can adjust to your needs.
I would love to know why some of the code is in one font and half is in another - AI not knowing quite what to do I guess!
Please Help Me with this bro!!!