Hi,
I am using below code in my indicator to flatten my account. But the position didn’t get closed.
Below code on OnOrderUpdate event method.
Collection<Cbi.Instrument> instrumentsToClose = new Collection();
instrumentsToClose.Add(curPosition.Instrument);
if(curPosition.MarketPosition != MarketPosition.Flat){
curPosition.Account.Flatten(instrumentsToClose);
Print(DateTime.Now.ToString(“yyyy-MM-dd HH:mm:ss.fff”) + " OUA2 : ANC " +" " + orderExecuted.Account.Name+ " " + “Flatten all posistion : " + curPosition.Instrument.FullName );
}
Print(DateTime.Now.ToString(“yyyy-MM-dd HH:mm:ss.fff”) + " OUA2 : ANC " +” " + orderExecuted.Account.Name+ " " + String.Format(“Position : {0} at {1} Qty {2}”, curPosition.MarketPosition, curPosition.AveragePrice, curPosition.Quantity));
Logs
2025-06-26 07:30:32.538 OUA2 : ANC BX67622-03 Flatten all posistion : NQ SEP25
2025-06-26 07:30:32.538 OUA2 : ANC BX67622-03 Position : Short at 22577.75 Qty 12025-06-26 07:48:54:626|1|64|Flatten account=‘BX67622-03’
Aren’t these commands specific to Strategies or Add-Ons? and not available from Indicators
Edge
3
@Nandhumca
You’ll need to define what account your working with upfront.
I would also suggest to add bool check for connection status!
//Private Variables
private bool isConnected = false; //Conditional Check for IsConnected
private Account myAccount: //Account to use for Orders
//Example of hard coded account name and isConneted condition placed in State.DataLoaded
isConnected = { Connection.Connections.FirstOrDefault(x => x.Status == ConnectionStatus.Connected) != null; }
if(isConnected) { lock (Account.All) myAccount = Account.All.FirstOrDefault(a => a.Name == "Sim101"); }
//Example Method to Flatten Everything
public void GoFlat()
{
if(!isConnected) { Print("GoFlat | ACCOUNT NOT CONNECTED!"); return; }
if(myAccount == null) { Print("GoFlat | Account NULL!"); return; }
if(Instrument == null ) { Print("GoFlat | Instrument NULL!"); return; }
Collection<Cbi.Instrument> instrumentsToCancel = new Collection<Instrument>();
instrumentsToCancel.Add(Instrument);
myAccount.Flatten(instrumentsToCancel);
Print("Flatten EveryThing for | " + myAccount.ToString() + " | " + Instrument.FullName.ToString() );
}
Be Safe in this Crazy World!
