I have a strategy for example named "Strategy01" and I open 3 charts: CL M5, GC M5 and ES M5. I run the strategy on each chart, and as a result I have 3 working instances of one strategy. All instances of strategy work on the same account Sim101.
There is some logic for opening a positions and let's imagin, each instance of strategy of three open a position. And as a result we have 3 opened positions.
Then I have a code where I control unrealized PnL for whole Account and if the value of unrealized PnL will hit some value, for example 40$, I want to close all opened positions but I want to leave also all strategies working. Here below is a snippet of my code:
double unrealizedPNL = Position.MarketPosition == MarketPosition.Flat ? 0 : Math.Round(Account.Get(AccountItem.UnrealizedProfitLoss, Currency.UsDollar), 2) - Math.Round(Account.Get(AccountItem.Commission, Currency.UsDollar), 2); if (unrealizedPNL >= 40) { foreach (var position in Account.Positions) { if (position.MarketPosition != MarketPosition.Flat) { try { position.Close(); } catch (Exception ex) { Print("There is an error during closing the position : " + ex.ToString()); } } } }
How can I close all opened positions without disabling the strategies?
Comment