Best practise: positions mismatch between strategy / broker

Questions about MultiCharts and user contributed studies.
Fredi
Posts: 45
Joined: 01 Nov 2013
Been thanked: 2 times

Best practise: positions mismatch between strategy / broker

Postby Fredi » 12 May 2014

Most of the Brokers do not support Native One-Cancels-Others group, at the moment.
In order to minimize this OCO-Orders Risk (see https://www.multicharts.com/trading-sof ... rders_Risk) I'm testing some codes - without
success up to now.

Situation: 1 Symbol, 1 Chart, Tick-based, SA-Mode.

Code: Select all

auto = getappinfo(aiStrategyAuto);
if auto = 1 then begin
MisQty = MarketPosition_at_Broker - MarketPosition_at_Broker_for_The_Strategy;
StrAlarm = "";

if (MisQty <> 0) and (MisQty <> PrevMisQty) then begin

if MisQty < 0 then begin
StrAlarm = "--> Unwished Short-Positionen=";
PlaceMarketOrder(true, true, AbsValue(MisQty));
end else begin
StrAlarm = "--> Unwished Long-Positionen=";
PlaceMarketOrder(false, true, AbsValue(MisQty));
end;

Print(StrDateTime + StrAlarm + NumToStr(MisQty,0) + " Broker=" + NumToStr(MarketPosition_at_Broker,0)
+ " Strategy=" + NumToStr(MarketPosition_at_Broker_for_The_Strategy,0)
+ " MP*Contracts=" + NumToStr(marketposition * currentcontracts,0));
Alert(StrDateTime + StrAlarm);
end;

PrevMisQty = MisQty;
end;
If I open orders manually (means testing unwished positions) the code is closing the "unwished"
orders correctly. However: when the strategy is closing a position, the value of "MarketPosition_at_Broker"
is changing earlier as the value of "MarketPosition_at_Broker_for_The_Strategy" - and the
above code will be triggered wrongly.

How can I make it work? Thanks in advance.

Regards,
Fredi

User avatar
Andrew MultiCharts
Posts: 1587
Joined: 11 Oct 2011
Has thanked: 931 times
Been thanked: 559 times

Re: Best practise: positions mismatch between strategy / bro

Postby Andrew MultiCharts » 12 May 2014

Hello Fredi,

Please open one of the prebuilt synchronizers' codes. You will see there are 2 time inputs to control syncronization, so something similar needs to be implemented in your code:

Code: Select all

Input :
TimeOutMS(250), // time spent since BrokerMarketPosition changed and we start correct position
LatencyMS(500); // time spent since we start correct StrategyMarketPosition and return to monitoring mode
In fact you could simply add the !From Broker To Strategy MP Synchronizer! signal to the chart with your primary exiting signal that would do the trick.

Fredi
Posts: 45
Joined: 01 Nov 2013
Been thanked: 2 times

Re: Best practise: positions mismatch between strategy / bro

Postby Fredi » 13 May 2014

Hello Andrew,

thank you for your information.

As I understand it: "!From Broker To Strategy MP Synchronizer!" and "ChangeMarketPosition" does not send order to the broker? Is this right? If YES, then this is no solution for my case, because my intension is to close real positions, which are wrongly opened (based on Simulated OCO) during high market volatility (or Crash) and connection loss.

Therefore, my idea, I have to check a possible mismatch and have to correct such status with "PlaceMarketOrder".
It seems, that the code below makes the job:

Code: Select all

auto = getappinfo(aiStrategyAuto);

if auto = 1 then begin
MisQty = MarketPosition_at_Broker - MarketPosition_at_Broker_for_The_Strategy;
if (MisQty = 0) then begin
bar_delay = 0;
bar_start = currentbar;

// Position differs, start tick-counter //
end else bar_delay = currentbar - bar_start;
StrAlarm = "";

// Correction after 3 Bars (= Ticks) //
if (MisQty <> 0) and (bar_delay > 3) then begin
bar_delay = 0;
if MisQty < 0 then begin
StrAlarm = "--> Unwished Short-Positionen=";
PlaceMarketOrder(true, true, AbsValue(MisQty));
end else begin
StrAlarm = "--> Unwished Long-Positionen=";
PlaceMarketOrder(false, true, AbsValue(MisQty));
end;

Print(StrDateTime + StrAlarm + NumToStr(MisQty,0) + " Broker=" + NumToStr(MarketPosition_at_Broker,0)
+ " Strategy=" + NumToStr(MarketPosition_at_Broker_for_The_Strategy,0)
+ " MP*Contracts=" + NumToStr(marketposition*currentcontracts,0));
Alert(StrDateTime + StrAlarm);
end;
end;
For delay I`m using bars (in my case equal to ticks) instead of time. It seems for me a better way, because the handshake/communication between Broker and MC is based on tick-changes. Do you agree and what minimum tick-delay
I can take (in order to react as fast as possible)?

How other users are handling this very critical OCO-Order-risk (expierence / code)?
I am looking forward to feedbacks, suggestions and ideas.

Regards,
Fredi

User avatar
Andrew MultiCharts
Posts: 1587
Joined: 11 Oct 2011
Has thanked: 931 times
Been thanked: 559 times

Re: Best practise: positions mismatch between strategy / bro

Postby Andrew MultiCharts » 13 May 2014

As I understand it: "!From Broker To Strategy MP Synchronizer!" and "ChangeMarketPosition" does not send order to the broker? Is this right? If YES, then this is no solution for my case, because my intension is to close real positions, which are wrongly opened (based on Simulated OCO) during high market volatility (or Crash) and connection loss.
Fredi, i believe you misunderstand the goal of the synchronizer. Its goal is to synchronize the strategy position with broker position. In does emulate an order execution for strategy auto trading engine when it sees the position at broker is different. But the idea is that after they are synchronized, another signal that you developed for exiting manual entries will work correctly with manual entry.

What you are trying to do with the PlaceMarketOrder is another way to reach the same goal, but it will only operate market orders. I see no reason why this approach would be better that usage of !From Broker To Strategy MP Synchronizer! + regular exiting signal script with SetStopLoss, SetProfitTarget or any other exit orders.

Fredi
Posts: 45
Joined: 01 Nov 2013
Been thanked: 2 times

Re: Best practise: positions mismatch between strategy / bro

Postby Fredi » 15 May 2014

Henry, thank you. I think I get it.

In my focus is standing the OCO risk. That means, that at the broker side a unwished
position is possible, not caused from the MC script.
So the signal "!From Strategy To Broker MP Synchronizer!" is the suitable approach for me.

Now the biggest problem for me is to find the right settings for TimeOutMS and LatencyMS.
Because of different market situation, or in case of step by step fillings, the reaction of
"!From Strategy To Broker MP Synchronizer!" is sometimes to slow and sometimes
too fast. That results in a lot of signals (= real orders), which I don't want.

Henry, do you have any idea, how I can solve this problem? How other users are manging
the OCO risk?

Regards,
Fredi

User avatar
Andrew MultiCharts
Posts: 1587
Joined: 11 Oct 2011
Has thanked: 931 times
Been thanked: 559 times

Re: Best practise: positions mismatch between strategy / bro

Postby Andrew MultiCharts » 15 May 2014

Henry, thank you.
Henry, do you have any idea...
Fredi, you have been discussing the case with Andrew. Not only in this thread, by the way... ;-)
Now the biggest problem for me is to find the right settings for TimeOutMS and LatencyMS.
Because of different market situation, or in case of step by step fillings, the reaction of
"!From Strategy To Broker MP Synchronizer!" is sometimes to slow and sometimes
too fast. That results in a lot of signals (= real orders), which I don't want.
Configuring a strategy inputs is absolutely individual for different cases and for different users.
Andrew, do you have any idea, how I can solve this problem? How other users are managing
the OCO risk?
I am not sure i completely understand what exact OCO risks you have in this case. Please give a specific example.

Fredi
Posts: 45
Joined: 01 Nov 2013
Been thanked: 2 times

Re: Best practise: positions mismatch between strategy / bro

Postby Fredi » 15 May 2014

Andrew, sorry for my incorrect form of address.

What do I mean by OCO risk? Via MC I'm sending always 2 orders (Stop + Profit = OCO-group)
to the broker. Because my broker doesn't support native OCO-groups the following case is
possible: one order of them, i.e. the profit target, is filled. But before the other
order will be cancelled by MC, based on a very fast market, the other order (in the example
the stop order) will be filled, also. In this case there is an open, unwished and uncontrolled
order at broker side.

In such a situation "!From Strategy To Broker MP Synchronizer!" checks the market position of
the broker and of the strategy. In the above case there is difference so that "!From...!" sends
a market order to the broker, without affecting the strategy. That's perfect.

The problem now: positions are generated from the strategy (MP*currentshares <> 0) and in some cases
the feedback of the broker is too slow (with other words: delay of MarketPosition_at_Broker <> 0 too big).
That results, as I mentioned, in a lot of signals (= real orders) from "!From Strategy To Broker MP Synchronizer!",
which I don't want (settings: TimeOutMS = 3000 ms and LatencyMS = 3000 ms).

The trick is now to find the right settings for TimeOutMS and LatencyMS: not be choosen too small, in order
to close rapidly unwished order, based on the simulated OCO mechanism. On the other hand not too fast,
in order to don't open/close a lot of orders, based on the regular orders from the strategy.

In the moment, I don't see how to solve this situation.
May be there exits another solution.

Regards,
Fredi

User avatar
Andrew MultiCharts
Posts: 1587
Joined: 11 Oct 2011
Has thanked: 931 times
Been thanked: 559 times

Re: Best practise: positions mismatch between strategy / bro

Postby Andrew MultiCharts » 16 May 2014

When OCO is not supported natively by broker, there is always a chance both bracket orders can be filled.

Fredi
Posts: 45
Joined: 01 Nov 2013
Been thanked: 2 times

Re: Best practise: positions mismatch between strategy / bro

Postby Fredi » 16 May 2014

Based on that possibility (both bracket orders are filled), it is particularly important to
look for a solution. In my opinion is that the reason, may be at all, that there
exits a signal like "!From Strategy To Broker MP Synchronizer!".

It's hard for me to believe, that there is no solution or workaround from MC users, in order to
handle this extreme critical situation.

Andrew, do you have any idea / approach / workaround for a solution?

In any case, thank you very much for your support.

Regards,
Fredi

User avatar
Andrew MultiCharts
Posts: 1587
Joined: 11 Oct 2011
Has thanked: 931 times
Been thanked: 559 times

Re: Best practise: positions mismatch between strategy / bro

Postby Andrew MultiCharts » 16 May 2014

Based on that possibility (both bracket orders are filled), it is particularly important to
look for a solution. In my opinion is that the reason, may be at all, that there
exits a signal like "!From Strategy To Broker MP Synchronizer!".

It's hard for me to believe, that there is no solution or workaround from MC users, in order to
handle this extreme critical situation.

Andrew, do you have any idea / approach / workaround for a solution?
If the OCO group is not supported natively (it is emulated locally by MC), there is no workaround or protection from overfill if market is too fast, brackets are close to market price and there are latency in broker API.

Neither "!From Strategy To Broker MP Synchronizer!", nor "!From Broker To Strategy MP Synchronizer!" signals will help in this case. Their only goal was to sync auto and manual trading.

Fredi
Posts: 45
Joined: 01 Nov 2013
Been thanked: 2 times

Re: Best practise: positions mismatch between strategy / bro

Postby Fredi » 18 May 2014

Andrew, beside you, this topic seems not to be very interesting for other users.

Nevertheless - I try a new approach, in the hope to find a "suboptimal" solution, at least.

Of course, I don't know the latency time of the broker API. However I can't imagine, that this
time is > 5 ... 10 seconds.

On the other hand, so my understanding, the MC script calculation is performed in events (based on
ticks). If the market is very slow (time between ticks is very long) it is possible to initiate a new
calculation of the MC script with the combination of LastBarOnChart_s / RecalcLastBarAfter().

My idea is to combine two conditions:
(a) a minimum number of ticks (= events/price changes occurred) with
(b) a minimum time delay (= covering max latency time of broker API)
The hypothesis is now: after this both condition the strategy is getting a feedback from
the broker about the positions, which have to be normally identical.

An example: there is a difference in positions of strategy / broker. If after a minimum number
of ticks and period of time [(number of ticks > 10) AND (time_delay > 10 sec)] this difference
still exists, then a function is being initialized (like "!From Strategy To Broker MP Synchronizer!"
with PlaceMarketOrder) in order to close the open positions, which are based on overfilling.

Is this a reasonable approach or what did I do wrong?

Regards,
Fredi

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Best practise: positions mismatch between strategy / bro

Postby TJ » 18 May 2014

::
Is this a reasonable approach or what did I do wrong?

Regards,
Fredi
My "Best Practise" is not to trade the thin markets,
or, not to autotrade the market when it is thin (except in exits).

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: Best practise: positions mismatch between strategy / bro

Postby JoshM » 18 May 2014

It's hard for me to believe, that there is no solution or workaround from MC users, in order to handle this extreme critical situation.
It's a critical situation if you're scalping a highly active and volatile instrument with a broker latency that can be a few seconds. I'm not that brave :], so I don't need a workaround for this.
Because my broker doesn't support native OCO-groups the following case is possible: one order of them, i.e. the profit target, is filled. But before the other
order will be cancelled by MC, based on a very fast market, the other order (in the example the stop order) will be filled, also. In this case there is an open, unwished and uncontrolled order at broker side.
An example: there is a difference in positions of strategy / broker. If after a minimum number of ticks and period of time [(number of ticks > 10) AND (time_delay > 10 sec)] this difference still exists, then a function is being initialized (like "!From Strategy To Broker MP Synchronizer!" with PlaceMarketOrder) in order to close the open positions, which are based on overfilling.

Is this a reasonable approach or what did I do wrong?
I think this is an unreasonable approach, if you don't mind me saying so.

If your strategy is so highly dependent on speed that an unprotected position for a few seconds can be an 'extreme critical situation', you're better off not using a fixed time delay of 10 seconds. The drawback of a fixed time delay is that you will need to set it to an arbitrarily high value to reduce the amount of false positives.

As an alternative, you could assume that the time between ticks approaches a normal distribution. If you then calculate the average time between ticks, add three times the standard deviation, you can be reasonably sure that any crossing of that threshold is quite uncommon.

In effect you'll then also be using a moving average of time between ticks, which will accommodate for slow periods in the market when a 10 second delay might not be enough.

Fredi
Posts: 45
Joined: 01 Nov 2013
Been thanked: 2 times

Re: Best practise: positions mismatch between strategy / bro

Postby Fredi » 19 May 2014

Thanks for your interest.

I would like to be more clearly: I'm trading the FDAX. However I think, the instrument is not important,
because the settings of SL & target depending very often on the volatility. So it's more a question of
the strateyg. In my case there are 2 possible differences between SL/Target (A) 20 ticks and (B) 4 ticks.

I have two main goals in the planned protection routine:
1. Closing unwished, unprotected position in crash situations
(if both - SL & target - instead of only one of them are filled)
2. Getting a minmimum of false positives
(therefore this high time value of 10 seconds)

In my tests case 1 is working perfectly with the approach. Case 2 up to now, also; however this, and only
this, is in my eyes the critical part, if no, or not enough ticks are occuring in this 10 seconds.


JoshM: your suggestion (average time between ticks and MA) could help. The problem: there are dramatic
differences of avarage times, depending of the time of day. Based on your idea, I'm thinking about a
solution of at least 1 tick in each of this 10 seconds, instead of only waiting for at least 10 ticks
in 10 seconds.

Regards,
Fredi

User avatar
Andrew MultiCharts
Posts: 1587
Joined: 11 Oct 2011
Has thanked: 931 times
Been thanked: 559 times

Re: Best practise: positions mismatch between strategy / bro

Postby Andrew MultiCharts » 19 May 2014

On the other hand, so my understanding, the MC script calculation is performed in events (based on
ticks). If the market is very slow (time between ticks is very long) it is possible to initiate a new
calculation of the MC script with the combination of LastBarOnChart_s / RecalcLastBarAfter().
IntraBarOrderGeneration + RecalcLastBarAfter
An example: there is a difference in positions of strategy / broker. If after a minimum number
of ticks and period of time [(number of ticks > 10) AND (time_delay > 10 sec)] this difference
still exists, then a function is being initialized (like "!From Strategy To Broker MP Synchronizer!"
with PlaceMarketOrder) in order to close the open positions, which are based on overfilling.

Is this a reasonable approach or what did I do wrong?
What if the position was actually changed at broker (for example closed) at broker, but MC doesn't know about it due to any reason? This logic seems to send a market order (that actually opens a position and the strategy in MC will not know about that position) in this case.

Fredi
Posts: 45
Joined: 01 Nov 2013
Been thanked: 2 times

Re: Best practise: positions mismatch between strategy / bro

Postby Fredi » 20 May 2014

Andrew, maybe I don't understand your question correctly. But this is exactly the goal 1, I mentioned
above: if the connection to the broker works correctly and the MC strategy doesn't initiate this position
(with other words: has nothing to do with this position), then the routine / logic will send a market
order and openes a position. And yes, strategy in MC will not know about that position.

Possible open positions from the MC strategy are still open, because only the mismatched positions between
Broker / strategy will be considered.

PS: if MC strategy should / has to know about this position, but doesn't regonize that at this time - for
whatever reason - then a mismatch will arise at a later point and will be corrected, also (is part of
goal 2, mentioned above).

User avatar
Andrew MultiCharts
Posts: 1587
Joined: 11 Oct 2011
Has thanked: 931 times
Been thanked: 559 times

Re: Best practise: positions mismatch between strategy / bro

Postby Andrew MultiCharts » 20 May 2014

Andrew, maybe I don't understand your question correctly. But this is exactly the goal 1, I mentioned
above: if the connection to the broker works correctly and the MC strategy doesn't initiate this position
(with other words: has nothing to do with this position), then the routine / logic will send a market
order and openes a position. And yes, strategy in MC will not know about that position.
Yes, i understand now.


Return to “MultiCharts”