crazy orders: can you help me to analyze my issue?  [SOLVED]

Questions about MultiCharts and user contributed studies.
auato
Posts: 89
Joined: 23 Nov 2016
Has thanked: 18 times

crazy orders: can you help me to analyze my issue?

Postby auato » 11 Apr 2017

Hi,
Yesterday, again, started crazy orders for which I cannot give a clear explanation. It's already the third time it happens... unfortunately with real money :(
In backtest all run perfectly! :)
I don't know if the problem is produced by my scripts or by my bank interface plugin or something else. I am grooping in the dark so any idea will be really appreciated..

Yesterday my script, which is not turned on for autotrading, entered correctly short @9:04am. If during day it doesn’t reach the profit target or stop loss, it exits from the position forcedly at the end of daily session around 5:30pm. It doesn’t execute the orders directly and autotrading mode is not anabled. It only runs on the chart and sets a global variable GVSetNamedInt (obtained by currentposition*marketposition) and then this GV is passed to another script, with autotrading turned on, and this second script decides to entry or exit position or to stay flat by checking the differences between marketoprdeposition_atBroker and the GV passed from the first script. To place an order it uses Placemarketorder .
When it reached the end of daily session (about 5:30pm) the first and not trading script closed the short position and transferred the value by GV to the real trading script but as you can see below a lot of orders buy and sell ware sent (attached in the "order and position tracker" output including orders, position history and log).

I also attach two printscreens and the code for the generating script and the second script for autotranding:

first script (no autotrade):

Code: Select all

[intrabarordergeneration=true]
....
....


//Profit target
if marketposition=1 then if (close this bar/entryprice-1) >= PT_perc_long then Sell ("BB_PT_Long") next bar at open;
if marketposition=-1 then if (entryprice/close this bar-1) >= PT_perc_short then buytocover ("BB_PT_Short") next bar at open;

//Stop Loss
if marketposition=1 then if (entryprice-close this bar) >= SL_Long then Sell ("BB_SL_Long") next bar at open;
if marketposition=-1 then if (close this bar-entryprice) >= SL_Short then buytocover ("BB_SL_Short") next bar at open;


IF time = 1734 and marketposition=1 then Sell ("ForceFinalLong") next bar at open ;
if time = 1734 and marketposition=-1 THEN buytocover ("ForceFinalShort") next bar at open;

GVSetNamedInt("GV_1", currentcontracts*marketposition);

second script (autotrading AA):

Code: Select all

[IntrabarOrderGeneration=True]

var: mp(0), GV_1(0), GV_2(0), GV_3(0), Result(0), ;

mp = MarketPosition_at_Broker;

GV_1=GVGetNamedInt("GV_1", -999);
GV_2=GVGetNamedInt("GV_2", -999);
GV_3=GVGetNamedInt("GV_3", -999);
Result=GV_1+GV_2+GV_3;

if mp=2 and mp<>Result then Begin
if Result=1 then PlaceMarketOrder(false, false, 1);
if Result=0 then PlaceMarketOrder(false, false, 2);
if Result=-1 then PlaceMarketOrder(false, true, 3);
if Result=-2 then PlaceMarketOrder(false, true, 4);
End;

if mp=1 and mp<>Result then Begin
if Result=2 then PlaceMarketOrder(true, true, 1);
if Result=0 then PlaceMarketOrder(false, false, 1);
if Result=-1 then PlaceMarketOrder(false, true, 2);
if Result=-2 then PlaceMarketOrder(false, true, 3);
End;

if mp=0 and mp<>Result then Begin
if Result=2 then PlaceMarketOrder(true, true, 2);
if Result=1 then PlaceMarketOrder(true, true, 1);
if Result=-1 then PlaceMarketOrder(false, true, 1);
if Result=-2 then PlaceMarketOrder(false, true, 2);
End;

if mp=-1 and mp<>Result then Begin
if Result=2 then PlaceMarketOrder(true, true, 3);
if Result=1 then PlaceMarketOrder(true, true, 2);
if Result=0 then PlaceMarketOrder(true, false, 1);
if Result=-2 then PlaceMarketOrder(false, true, 1);
End;

if mp=-2 and mp<>Result then Begin
if Result=2 then PlaceMarketOrder(true, true, 4);
if Result=1 then PlaceMarketOrder(true, true, 3);
if Result=0 then PlaceMarketOrder(true, false, 2);
if Result=-1 then PlaceMarketOrder(true, false, 1);
End;

Of course,as you can see, in the Log a lot of 3CAS and 3DUP and LRE messages from bank plugin I received to alert me about insufficinet fund (luckily :P ) and repetitive attempts to cancel orders.
I can provide further details.

Thank you
auato
Attachments
Log.txt
(75.1 KiB) Downloaded 354 times
PositionHistory.txt
(3.9 KiB) Downloaded 340 times
orders.txt
(40.75 KiB) Downloaded 354 times
2.JPG
(171.29 KiB) Downloaded 790 times
1.JPG
(158.93 KiB) Downloaded 790 times

auato
Posts: 89
Joined: 23 Nov 2016
Has thanked: 18 times

Re: crazy orders: can you help me to analyze my issue?

Postby auato » 11 Apr 2017

meanwhile I'm doing some considerations about my problem. As I have IOB enabled for the first and the second script, both execute tick-based calculations. Suppose that in the past we were correctly at -1 and then we reached "time=1734" (first script), so the exit is triggered (in my case is triggered a buytocover). So, in the first script marketposition becomes 0 and also "GV_1" passes from -1 to 0. The second script receives the new value for "GV_1=0" and consequently also the variable "Result" becames 0. In this case the second script performs the line of code below:

Code: Select all

if mp=-1 and mp<>Result then Begin
if Result=0 then PlaceMarketOrder(true, false, 1);
Maybe, but it's just an assumption of mine, that the buytocover is not immediately filled by the broker and the variable "mp" is still equal to -1 and immediately after arrives a second tick which allows a new calculation (as the script is a IOB script). The new calculation meets the same condition above with "mp=-1" and so the second script performs another time the command line like above, generating a new buytocover by the same PlaceMarketOrder command. And like so you can have also more then one... but two, three, four unexpected orders

Is it possible and correct this my interpretation?

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

Re: crazy orders: can you help me to analyze my issue?

Postby TJ » 11 Apr 2017

What are your chart resolutions?

auato
Posts: 89
Joined: 23 Nov 2016
Has thanked: 18 times

Re: crazy orders: can you help me to analyze my issue?

Postby auato » 11 Apr 2017

Hi @TJ
it's 5 minutes

maxmax68
Posts: 163
Joined: 20 Nov 2012
Has thanked: 55 times
Been thanked: 48 times

Re: crazy orders: can you help me to analyze my issue?

Postby maxmax68 » 11 Apr 2017

Hello auato,

PlaceMarketOrder sends market order to the broker but is invisible for the trading engine of the chart,
so does not change the market position of the chart.
To change the market position of the chart you have to use it with ChangeMarketPosition.

Hope this could be useful.

Best regards
Massimo

P.S.
Maybe, but it's just an assumption of mine, that the buytocover is not immediately filled by the broker and the variable "mp" is still equal to -1 and immediately after arrives a second tick which allows a new calculation (as the script is a IOB script). The new calculation meets the same condition above with "mp=-1" and so the second script performs another time the command line like above, generating a new buytocover by the same PlaceMarketOrder command. And like so you can have also more then one... but two, three, four unexpected orders
Yes, you should try to generate only one order and then wait a few second before check again broker position.
Last edited by maxmax68 on 11 Apr 2017, edited 1 time in total.

auato
Posts: 89
Joined: 23 Nov 2016
Has thanked: 18 times

Re: crazy orders: can you help me to analyze my issue?

Postby auato » 11 Apr 2017

Hi Massimo, thank you. Are you suggesting me to change all PlaceMarketOrder with ChangeMarketPosition?

maxmax68
Posts: 163
Joined: 20 Nov 2012
Has thanked: 55 times
Been thanked: 48 times

Re: crazy orders: can you help me to analyze my issue?  [SOLVED]

Postby maxmax68 » 11 Apr 2017

You can use ChangeMarketPosition if you want chart in synch with your position.
Probably your solution is to insert a delay before the new check of broker position and new PlaceMarketOrder.


Return to “MultiCharts”