Over / Under Trading When Autotrading with MC

Questions about MultiCharts and user contributed studies.
Spaceant
Posts: 254
Joined: 30 May 2009
Has thanked: 1 time
Been thanked: 3 times

Over / Under Trading When Autotrading with MC

Postby Spaceant » 06 Jul 2010

Today, I experienced the first "fatal error" of MC in autotrading which executed TWO sell orders within a minute. This is supposed to be "selling X contracts" to close a position of X contracts before market closing, but MC executed it twice resulting a short position. Fortunately, I sit in front of the computer and close the short position manually.

DOES ANYONE EXPERIENCE SIMILAR THING LIKE WHAT I DO?

I am thinking to code a "precautionary strategy" to check the position every bar, version the position that it should be as hown in the MC chart. Close "over" positions when it detects that; and buy / sell short additional contracts to match the position as it should be.

But I don't know how to start the code..... any input and perhaps someone may have coded it already!!??

User avatar
Dave Masalov
Posts: 1712
Joined: 16 Apr 2010
Has thanked: 51 times
Been thanked: 489 times

Postby Dave Masalov » 07 Jul 2010

Dear Spaceant,

You can write such "precautionary strategy" by means of EasyLanguage. Basically, you need to compare the marketposition and MarketPosition_at_Broker values to compare the actual position at the broker with the position shown on the chart. To do what you want you need to make sure that marketposition and MarketPosition_at_Broker values are the same.

janus
Posts: 835
Joined: 25 May 2009
Has thanked: 63 times
Been thanked: 104 times

Re: Over / Under Trading When Autotrading with MC

Postby janus » 07 Jul 2010

Spaceant, what type of sell order are you using? If you just want to cover your longs say "sell all contracts". better still, if you don't know or don't care whether you are short, long or flat, and you want to cover everything regardless then say:

sell all contracts;
buytocover all contracts;

Either one or the other or neither will be executed depending on your market position. It does no harm to have both commands.

Spaceant
Posts: 254
Joined: 30 May 2009
Has thanked: 1 time
Been thanked: 3 times

Postby Spaceant » 07 Jul 2010

The one that I experienced was something strange:
marketposition = 0 ;
MarketPosition_at_Broker = -3 ;
MarketPosition_at_Broker_for_The_Strategy = 0;

I have tested it today using "buy to cover", but it didn't work as marketposition is zero!! Perhaps, I should use "Buy" to forcefully close the position....... will test

Spaceant
Posts: 254
Joined: 30 May 2009
Has thanked: 1 time
Been thanked: 3 times

Postby Spaceant » 07 Jul 2010

Does anyone know what is the meaning of marketposition_checked??

Spaceant
Posts: 254
Joined: 30 May 2009
Has thanked: 1 time
Been thanked: 3 times

Postby Spaceant » 07 Jul 2010

The one that I experienced was something strange:
marketposition = 0 ;
MarketPosition_at_Broker = -3 ;
MarketPosition_at_Broker_for_The_Strategy = 0;

I have tested it today using "buy to cover", but it didn't work as marketposition is zero!! Perhaps, I should use "Buy" to forcefully close the position....... will test
It works!

For another case:
marketposition = 1 ;
MarketPosition_at_Broker = 0 ;
MarketPosition_at_Broker_for_The_Strategy = 3;

The MarketPosition_at_broker should be 3. Due to whatever reason, no position has been entered, I have tried to code this situation and others in a precautionary strategy:
Inputs:Opt(1);
Vars: MP(0),MP_At_Bk(0),MP_For_S(0),MP_Chk(0),Diff(0),AbsQty(0);

MP=marketposition;
MP_At_Bk=MarketPosition_at_Broker;
MP_For_S=MarketPosition_at_Broker_for_The_Strategy;
MP_Chk=marketposition_checked;

Diff=MP_At_Bk - MP_For_S;
AbsQty=absvalue(Diff);

If Diff<>0 then begin

If MP=1 then begin
If Diff>0 then Sell ("LX-Chk") Diff contracts next bar at market;
If Diff<0 then Buy ("LE-Chk") AbsQty contracts next bar at market;
End; {MP=1}

If MP=-1 then begin
If Diff>0 then Sell Short ("SE-Chk") Diff contracts next bar at market;
If Diff<0 then Buy to Cover ("SX-Chk") AbsQty contracts next bar at market;
End; {MP=-1}

If MP=0 then begin
If Diff>0 then Sell Short ("LX(0)-Chk") Diff contracts next bar at market;
If Diff<0 then Buy ("SX(0)-Chk") AbsQty contracts next bar at market;
End; {MP=0}

End; {Diff}
I have tested MP=0 while there is a +ve / -ve position, in which the code works. BUT, it doesn't seem working for MP=1 or -1

Can anyone tell me what has been wrong?

User avatar
Dave Masalov
Posts: 1712
Joined: 16 Apr 2010
Has thanked: 51 times
Been thanked: 489 times

Postby Dave Masalov » 08 Jul 2010

Dear Spaceant,

This code should make the position at broker the same as it is on the chart:

Code: Select all

Inputs:Opt(1);
Vars: MP(0),MP_At_Bk(0),Diff(0),AbsQty(0);

MP = MarketPosition * CurrentContracts;
MP_At_Bk = MarketPosition_at_Broker;

Diff = MP_At_Bk - MP;
AbsQty = Absvalue(Diff);

If (GetAppInfo(aistrategyauto) = 1) and (GetAppInfo(airealtimecalc) = 1) and (Diff<>0) then begin

If Diff>0 then PlaceMarketOrder(False, True, AbsQty);
If Diff<0 then PlaceMarketOrder(True, True, AbsQty);

End; {Diff}

Spaceant
Posts: 254
Joined: 30 May 2009
Has thanked: 1 time
Been thanked: 3 times

Postby Spaceant » 08 Jul 2010

Dave, thanks!

what is the meaning for PlaceMarketOrder nad its syntax as I can't find it in the help manual?

User avatar
Dave Masalov
Posts: 1712
Joined: 16 Apr 2010
Has thanked: 51 times
Been thanked: 489 times

Postby Dave Masalov » 09 Jul 2010

Dear Spaceant,

PlaceMarketOrder sends a market order, which is not showed (and not counted) on a chart, to a broker

It's syntax can be understood from the hint window in PowerLanguage Editor.

janus
Posts: 835
Joined: 25 May 2009
Has thanked: 63 times
Been thanked: 104 times

Postby janus » 10 Jul 2010

Dave, some questions on this supposedly new feature.

1. Why was PlaceMarketOrder introduced?
2. I presume none of the standard orders will know about any open orders generated by PlaceMarketOrder, including the "sell all contracts" and "buytocover all contracts" to cover all open positions regardless of whether the situation is long, short or flat. Correct?
3. If the answer to 2 is yes, are there equivalents to "sell all contracts" and "buytocover all contracts"?

User avatar
Dave Masalov
Posts: 1712
Joined: 16 Apr 2010
Has thanked: 51 times
Been thanked: 489 times

Postby Dave Masalov » 13 Jul 2010

Dear janus,

1. To give the possibility to synchronize the position at broker with the position on the chart. To make the position at broker the same as it is on the chart.
2. Yes
3. No


Return to “MultiCharts”