Detect positions liquidated by broker

Questions about MultiCharts .NET and user contributed studies.
christephan
Posts: 17
Joined: 04 Dec 2014
Has thanked: 2 times
Been thanked: 3 times

Detect positions liquidated by broker

Postby christephan » 24 Dec 2014

I have several strategies (each on its charts) running on the same symbol. And if many strategies have positions, sometimes margin is not enough.

In simple, if I can have 2 lots for intraday and 1 lot for overnight. And 2 strategies are running separately, each could Buy at most 1 lot.
For open position before market normal open, it doesn't have problems because MC could detect the order failure automatically and handles it right.

But if I have 2 lots intraday, and times goes into 15 minutes prior to normal market close, then 1 of the 2 lots will be force-liquidated by broker. But the 2 strategies doesn't know that and when the strategies both sell their positions, I have 1 short position while the strategies think we are flat.

Here is the time line: (ECBOT time, for example)
8:30 Normal Market Open
10:00 Strategy 1 - Buy 1 lot
11:00 Strategy 2 - Buy 1 lot
15:00 Broker - Sell 1 lot (Because of overnight margin not enough)
15:15 Normal Market Close
17:00 Overnight Market Open
18:00 Strategy 1 - Sell 1 lot
18:00 Strategy 2 - Sell 1 lot

Then I will have a short position until I notice it and to close it manually. What a great risk~

Any idea?

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Detect positions liquidated by broker

Postby Henry MultiСharts » 24 Dec 2014

Hello christephan,

You can use IStrategyPerformance - MarketPositionAtBroker to get the actual broker position.
Please also see Trading from Multiple Charts on One Instrument.

christephan
Posts: 17
Joined: 04 Dec 2014
Has thanked: 2 times
Been thanked: 3 times

Re: Detect positions liquidated by broker

Postby christephan » 24 Dec 2014

Henry~

I know MarketPositionAtBroker. But it's expected to be different with strategy position when multiple strategies are running on the same symbol.

I have two ideas for now~
One is to create a global object to hold the current "MC position". To update the position whenever my strategies are trading. And then we could compare the "MC position" against "broker position".
But the problem for this method is how to filter out manual trading. Is it possible to monitor manual trading sent by MC? Or is it possible to force all orders generated in MC to update the "MC position" global object?

Another way is an easy fix: Just block Sell/BuyToCover orders to open a position at broker if near normal market close, or in overnight market.
That is something like:
If MarketPostionAtBroker > 1 then Sell.Send(1);

But I have a question here, what will happen if at least two strategies are checking broker position and sending Sell orders at the same time?

For example,
If current position at broker is 1, and Strategy A and Strategy B both want to send a Sell order at this bar close CalcBar(). How is the execution flow in MC? Do A and B execute one by one? or in parallel?
That is, will A and B both get broker position as 1, and then each sell 1 lot, and result in short instead of flat also?

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Detect positions liquidated by broker

Postby Henry MultiСharts » 26 Dec 2014

christephan,

Please use MarketPostionAtBrokerForTheStrategy instead - it returns Market Position requested from the broker for the current strategy.


Return to “MultiCharts .NET”