I am in a SIM account and would like for an open position to close automatically without my manual intervention, if my unrealized PNL is $25.00 or less. It's my first attempt and I have found following code, that actually compiles but doesn't do anything when PNL is less than or equal to $25.00.
Here is my strategy code that I am attaching to a chart before I open a position:
protected override void OnAccountItemUpdate(Cbi.Account account, Cbi.AccountItem accountItem, double value)
{
Account a = Account.All.First(t => t.Name == "Sim101");
double unrealized = Position.GetUnrealizedProfitLoss(PerformanceUnit.C urrency, Close[0]);
if(State == State.Terminated) return;
if (unrealized <= 25.00)
{
if(Position.MarketPosition == MarketPosition.Long)
{
ExitLong(Position.Quantity);
}
if(Position.MarketPosition == MarketPosition.Short)
{
ExitShort(Position.Quantity);
}
SetState(State.Terminated); return;
}
}
Is it possible to close an open position via code? I don't put bracket orders and would like to create strategies for automatically taking profit and losses. Thanks
John

Comment