Problems with Brokersync

Questions about MultiCharts and user contributed studies.
User avatar
siscop
Posts: 197
Joined: 09 Jan 2011
Has thanked: 34 times
Been thanked: 29 times

Problems with Brokersync

Postby siscop » 26 Mar 2015

I was long on „DD“ and wrote an Exitstrategy.
After I came back I was surprised with this.
Can someone pls help me explain what is wrong with my code so this will not happen again?
There is no entry signal so why did it enter a position?
DD Verlust.png
(119.94 KiB) Downloaded 538 times

Code: Select all

inputs:
AtrLen(20),
AtrMultiTarget(2),
AtrMultiLoss(4);

variables:
Menge(0),
ProfitTargetSumme(0),
StoppLossSumme(0),
iiatr(0),
TPPreis(0),
SLPreis(0);

Menge=MarketPosition_at_Broker;
//BrokerSync
if marketposition=0 then
ChangeMarketPosition(Menge,AvgEntryPrice_at_Broker);

iiatr=AvgTrueRange(AtrLen);

//Exitbedingung
condition50=c>o;
condition51=c<o;

// SL TP berechnen
ProfitTargetSumme=Menge*AtrMultiTarget*iiatr;
StoppLossSumme=Menge*AtrMultiLoss*iiatr;
SetStopPosition;
SetProfitTarget(ProfitTargetSumme);
SetStopLoss(StoppLossSumme);
TPPreis=AvgEntryPrice_at_Broker+AtrMultiTarget*iiatr;
SLPreis=AvgEntryPrice_at_Broker-AtrMultiLoss*iiatr;

//Exit
if marketposition=1 then
begin
if condition50 then
sell next bar at market;
end;

if marketposition=-1 then
begin
if condition51 then
buytocover next bar at market;
end;


Print(" Datum ",date," Menge ",Menge, " MarketPosition_at_Broker ", MarketPosition_at_Broker,
" EntryPreis ",AvgEntryPrice_at_Broker," TP ",ProfitTargetSumme," StoppLossSumme ",StoppLossSumme,
" TPPreis ",TPPreis," SLPreis ",SLPreis );

orion
Posts: 250
Joined: 01 Oct 2014
Has thanked: 65 times
Been thanked: 104 times

Re: Problems with Brokersync

Postby orion » 26 Mar 2015

There is no entry signal so why did it enter a position?
I am not sure I understand that question. You were long when you started and your code uses ChangeMarketPosition to manipulate the market position.

The keyword MarketPosition is updated via order fill reports coming from the broker while the keyword MarketPosition_at_Broker is updated via position update reports coming from the broker. These two channels can be out of sync. Caution is advised when using ChangeMarketPosition in your code. Don't use these strategy sync keywords if you don't understand the details of what they do.

User avatar
siscop
Posts: 197
Joined: 09 Jan 2011
Has thanked: 34 times
Been thanked: 29 times

Re: Problems with Brokersync

Postby siscop » 26 Mar 2015

I had to restart MC so „DD“ was long and no strategy was handling this open position.
So I wrote a Exitstrategy for this market. I had to enter Marketposition_at_Broker and ChangeMarketPosition so the Strategy would consider the actual Position as is – Long.

It seemed to be wrong as this was my live account and the sum wasn`t that little.
So to avoid the next expensive lesson I would be interested how to change the code so the strategy would sync up with the broker once and just exit accordingly and not reenter a new one.

orion
Posts: 250
Joined: 01 Oct 2014
Has thanked: 65 times
Been thanked: 104 times

Re: Problems with Brokersync

Postby orion » 26 Mar 2015

If you are looking to have the code sync only at start, sample code here will do the job. However, I would still advise users to invest time in understanding the details of these keywords rather than using boiler plate code without understanding what is going on.


Return to “MultiCharts”