Is there any way to retrieve or store or otherwise get access to old values when OnOrderUpdate fires?
I have an Indicator that creates orders for multiple accounts on the click of a button. When an order fills, it then creates the SL and TP orders for all of the respective accounts. Now, I need to get it to where when I move an order on the Chart Trader, which only allows manipulation of one Account at a time, it moves the orders for the other accounts, as well.
I’m thinking that I can put something into OnOrderUpdate to sense when an order change has been made, but then, how do I see what the old value for the Stop Price was? Or does OnOrderUpdate report what both the old and new values are?
Thanks in advance!
Short answer: no. The update hands you the order as it is now. The help guide’s Order page says its properties “will always reflect the current state of an order”, so by the time your handler runs, StopPrice already holds the new price, and the old one is gone unless you kept it.
You place the stops yourself, so you already hold every Order reference: keep each one with its stop price in a dictionary. When an update shows a price different from the one you stored, the difference is the move, and you apply it to the others. Three things make it work in practice:
- The echo. When you move the other accounts’ stops, their own updates come back through the same handler, and each would look like a manual move and get mirrored again. Store the new price before you call Change; when that order’s update arrives it matches what you stored and is ignored.
- Settled states only. A dragged order goes through ChangePending and ChangeSubmitted before it is back at Accepted or Working with its new price, so act on those two only. Some stops, held by the broker or simulated, only ever reach Accepted, so take both.
- Threads. Updates from different accounts can arrive at the same time, so lock the dictionary. Compare orders as objects, not by OrderId, which the same page says can change during an order’s life.
A sketch, for one bracket at a time across your accounts:
private readonly Dictionary<Order, double> lastStop = new Dictionary<Order, double>();
private readonly object stopLock = new object();
// where you create each account's stop, after CreateOrder and before Submit:
// lock (stopLock) lastStop[stopOrder] = stopPrice; // the price you passed to CreateOrder
private void OnAccountOrderUpdate(object sender, OrderEventArgs e)
{
List<Order> toMove = new List<Order>();
lock (stopLock)
{
double old;
if (!lastStop.TryGetValue(e.Order, out old))
return; // not one of our stops
if (Order.IsTerminalState(e.OrderState))
{
lastStop.Remove(e.Order); // filled, cancelled or rejected
return;
}
if (e.OrderState != OrderState.Accepted && e.OrderState != OrderState.Working)
return; // change still in flight
double now = e.Order.StopPrice;
if (Math.Abs(now - old) < e.Order.Instrument.MasterInstrument.TickSize / 2)
return; // no move, or the echo of our own change
double delta = now - old;
lastStop[e.Order] = now;
foreach (Order other in lastStop.Keys.ToList())
{
if (other == e.Order)
continue;
double target = other.Instrument.MasterInstrument.RoundToTickSize(lastStop[other] + delta);
lastStop[other] = target; // stored first, so its update reads as no move
other.StopPriceChanged = target;
toMove.Add(other);
}
}
foreach (Order o in toMove)
o.Account.Change(new[] { o });
}
This keeps each account’s distance (same move in ticks). If you’d rather put every stop on the same price, set target to now; you then only need the stored value for the echo check. Targets work the same way with LimitPrice and LimitPriceChanged. If you can have several brackets open at once, keep one dictionary per click so a move only reaches its own group.
After I made my initial post, I thought about it a little bit more, and came up with this:
private void OnOrderUpdate(object sender, OrderEventArgs oea)
{
Print("Account - " + oea.Order.Account.ToString() + ", Order Name: " + oea.Order.Name.ToString() + ", Stop Price = " + oea.Order.StopPrice.ToString() + ", Limit Price = " + oea.Order.LimitPrice.ToString() + ", oldStop = " + oldStop.ToString() + ", oldLimit = " + oldLimit.ToString());
if (oea.Order.Account == theAccount1)
{
if (oea.Order.Name.Contains("Target") && oea.Order.LimitPrice != oldLimit)
{
foreach (Account acct in activeAccountsArray)
{
foreach (Order ord in acct.Orders)
{
if (ord.Name.Contains("Target") && ord.LimitPrice != oea.Order.LimitPrice)
{
ord.LimitPriceChanged = oea.Order.LimitPrice;
oldLimit = oea.Order.LimitPrice;
acct.Change(new[] { ord } );
}
}
}
}
else if (oea.Order.Name.Contains("Exit") && oea.Order.StopPrice != oldStop)
{
foreach (Account acct in activeAccountsArray)
{
foreach (Order ord in acct.Orders)
{
if (ord.Name.Contains("Exit") && ord.StopPrice != oea.Order.StopPrice)
{
ord.StopPriceChanged = oea.Order.StopPrice;
oldStop = oea.Order.StopPrice;
acct.Change(new[] { ord } );
}
}
}
}
}
}
activeAccountsArray holds all of the Accounts that I am actively trading, and Chart Trader is selected to the first one in the array, whose value is also set to theAccount1.
oldStop and oldLimit are set to the values of the original SL and TP in OnExecutionUpdate when the orders are initially placed.
It seems to work great! I can move the orders on Chart Trader, and watch the orders move on the other windows which have the other accounts selected. I also get the appropriate output in the Logs and Orders tabs.
Thanks for your help! 
Nice, and simpler than mine, since you only ever drag on the first account. One guard worth adding at the top of both inner loops before this runs on a live account:
if (ord.Instrument != oea.Order.Instrument || Order.IsTerminalState(ord.OrderState))
continue;
The name test also matches orders your indicator didn’t place, an ATM’s Target1 for instance, on any instrument those accounts trade, and as written the loop would move their limit to this instrument’s price. And Account.Orders can hold orders that are already filled or cancelled, so from the second bracket of the day the loop would also try to change the first bracket’s dead orders.
1 Like