AutoTrade - Sync Mode - bug?

Questions about MultiCharts and user contributed studies.
SUPER
Posts: 646
Joined: 03 Mar 2007
Has thanked: 106 times
Been thanked: 84 times

AutoTrade - Sync Mode - bug?

Postby SUPER » 14 Apr 2009

It seems when a system is set to trade in Sync mode it generates buy and short order at same time irrespective of the system logic.

For comparison, create two charts with same code allow one to autotrade in sync mode and observe trades on both charts as they develop. Soon you will find that the trades do not match on both charts.

Test Code:

Code: Select all


[IntrabarOrderGeneration = true]

Vars: intrabarpersist BuySetUp(false),ShortSetUp(false),IntraBarPersist Mp(0);
Vars: intrabarpersist OpenValue(0);

Mp=MarketPosition;
Value1=Average(C[1],3);
OpenValue=Open;

if C[1] > Value1 then begin
BuySetUp=true;
end;

if C[1] < Value1 then begin
ShortSetUp=true;
end;

If Mp=1 then begin
BuySetUp=false;
end;

If Mp=-1 then begin
ShortSetUp=false;
end;


If Mp<>1 and BuySetUp then buy next bar at OpenValue Limit;
If Mp<>-1 and ShortSetUp then SellShort next bar at OpenValue Limit;


SetExitonClose;

//Print(CurrentBar,",",Value1,",",BuySetUp,",",ShortSetUp,",",OpenValue,",",Mp);



*** Code works perfectely in Async Mode.
Attachments
Sync-Test.JPG
(245.13 KiB) Downloaded 709 times

User avatar
Andrew Kirillov
Posts: 1589
Joined: 28 Jul 2005
Has thanked: 2 times
Been thanked: 31 times
Contact:

Postby Andrew Kirillov » 15 Apr 2009

We made extensive testing and unfortunately this is not a bug. This is the way TWS works. I assume you can't see the same problem on ASYNC mode since you use time conversion of unfilled orders.
Here is why the results different. We’ve reproduced the same issue and analyzed logs. Please see the attached picture.
The clearly show that we send two identical orders with different IDs within 15 milliseconds. One of orders will be filled (id=678) and other will not. Please note that TWS accepts two orders and confirms. It means that we don’t control it any more.
In a minute we must cancel order with id=679 since new orders are coming.
Summary:
TWS doesn’t execute the same orders sent with 15 ms interval. There are lots of reasons why it happens. It could be runaway price or system issues, but this is a fact.
Attachments
topic=6249_TradingServerLog.PNG
(33.68 KiB) Downloaded 701 times

SUPER
Posts: 646
Joined: 03 Mar 2007
Has thanked: 106 times
Been thanked: 84 times

Postby SUPER » 15 Apr 2009

Andrew,

Please try to run the code I have provided and you will notice that as soon as Sync Mode automation is started "two" orders are sent at the same time, a buy and sell short, which should not happen as the system logic does not allow buy and short conditions to exist on same bar.

When "Async" mode is initiated it sends correct order a buy or sell short as per the system logic and that works irrespective of conversion criteria options.

Will appreciate your comments.

Regards
Super

User avatar
Andrew Kirillov
Posts: 1589
Joined: 28 Jul 2005
Has thanked: 2 times
Been thanked: 31 times
Contact:

Postby Andrew Kirillov » 16 Apr 2009

Super,
This is not a program fault. This is misunderstanding.
- When you enable auto-trading in ASYNC mode you have long or short market position on your chart (buy/sell arrows on history) and this is why the program sends 1 order only.

- When you enable auto-trading in SYNC mode your market position is flat since we force the system to rest it to 0. You have no arrows on the chart. So the program should send two orders according to your script’s logic to enter either long or short.

If you want to have the strategy to behave identical in ASYNC and SYNC mode add this piece of code to your strategy script:


if LastBarOnChart_s and 1=getappinfo(airealtimecalc) then begin
If Mp<>1 and BuySetUp then buy next bar at OpenValue Limit;
If Mp<>-1 and ShortSetUp then SellShort next bar at OpenValue Limit;
end;

SUPER
Posts: 646
Joined: 03 Mar 2007
Has thanked: 106 times
Been thanked: 84 times

Postby SUPER » 16 Apr 2009

Andrew,

Many thanks for your reply.

I tried your impoved code but it did not make any difference the problem is when the code is run as soon as Sync mode is started both "BuySetup" and "ShortSetUp" are set to true on first tick and it is here where the problem lies. Because of this both buy and short orders are submitted at same time.


Print (CurrentBar,",",C[1],",",Value1,",",BuySetUp,",",ShortSetUp,",",OpenValue,",",Mp);

now on the print out of the first tick I get
(612.00, 843.75, 843.92, True, True, 843.75, 0);

you can see from the print statement that the BuySetUp can not be true as 843.75 is not greater than 843.92, don't understand why "BuySetUp" turns to true.

Will appreciate your comments.

Regards
Super

SUPER
Posts: 646
Joined: 03 Mar 2007
Has thanked: 106 times
Been thanked: 84 times

Postby SUPER » 16 Apr 2009

Duplicate post-deleted
Last edited by SUPER on 17 Apr 2009, edited 1 time in total.

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

Postby TJ » 16 Apr 2009

maybe it is this?


if C[1] > Value1 then begin
BuySetUp=true;
ShortSetUp=FALSE;
end;

SUPER
Posts: 646
Joined: 03 Mar 2007
Has thanked: 106 times
Been thanked: 84 times

Postby SUPER » 16 Apr 2009

maybe it is this?


if C[1] > Value1 then begin
BuySetUp=true;
ShortSetUp=FALSE;
end;
Thanks TJ,

You are a genius, your solution solves the problem.

However there is still unanswered question why does BuySetUp turn true on first tick?

Regards
Super

SUPER
Posts: 646
Joined: 03 Mar 2007
Has thanked: 106 times
Been thanked: 84 times

Postby SUPER » 16 Apr 2009

Duplicate post
Last edited by SUPER on 17 Apr 2009, edited 1 time in total.

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

Postby TJ » 16 Apr 2009

try this on your debug effort...




[IntrabarOrderGeneration = true]
Vars: intrabarpersist BuySetUp(false),ShortSetUp(false),IntraBarPersist Mp(0);
Vars: intrabarpersist OpenValue(0);
Mp=MarketPosition;
Value1=Average(C[1],3);
OpenValue=Open;
Print(CurrentBar,",",Value1,",",BuySetUp,",",ShortSetUp,",",OpenValue,",",Mp);
if C[1] > Value1 then begin
BuySetUp=true;
end;
Print(CurrentBar,",",Value1,",",BuySetUp,",",ShortSetUp,",",OpenValue,",",Mp);
if C[1] < Value1 then begin
ShortSetUp=true;
end;
Print(CurrentBar,",",Value1,",",BuySetUp,",",ShortSetUp,",",OpenValue,",",Mp);
If Mp=1 then begin
BuySetUp=false;
end;
Print(CurrentBar,",",Value1,",",BuySetUp,",",ShortSetUp,",",OpenValue,",",Mp);
If Mp=-1 then begin
ShortSetUp=false;
end;
Print(CurrentBar,",",Value1,",",BuySetUp,",",ShortSetUp,",",OpenValue,",",Mp);
If Mp<>1 and BuySetUp then buy next bar at OpenValue Limit;
If Mp<>-1 and ShortSetUp then SellShort next bar at OpenValue Limit;
Print(CurrentBar,",",Value1,",",BuySetUp,",",ShortSetUp,",",OpenValue,",",Mp);
SetExitonClose;
//Print(CurrentBar,",",Value1,",",BuySetUp,",",ShortSetUp,",",OpenValue,",",Mp);

SUPER
Posts: 646
Joined: 03 Mar 2007
Has thanked: 106 times
Been thanked: 84 times

Postby SUPER » 16 Apr 2009

Tj, Tried your suggestion with little modificaiton and you can see from the print statement that on first tick both BuySetUp and ShortSetUp remain true.


[IntrabarOrderGeneration = true]
Vars: intrabarpersist BuySetUp(false),ShortSetUp(false),IntraBarPersist Mp(0);
Vars: intrabarpersist OpenValue(0);



Mp=MarketPosition;
Value1=Average(C[1],3);
OpenValue=Open;
if LastBarOnChart then Print(1,",",CurrentBar,",",Value1,",",BuySetUp,",",ShortSetUp,",",OpenValue,",",Mp);
if C[1] > Value1 then begin
BuySetUp=true;
end;
if LastBarOnChart then Print(2,",",CurrentBar,",",Value1,",",BuySetUp,",",ShortSetUp,",",OpenValue,",",Mp);
if C[1] < Value1 then begin
ShortSetUp=true;
end;
if LastBarOnChart then Print(3,",",CurrentBar,",",Value1,",",BuySetUp,",",ShortSetUp,",",OpenValue,",",Mp);
If Mp=1 then begin
BuySetUp=false;
end;
if LastBarOnChart then Print(4,",",CurrentBar,",",Value1,",",BuySetUp,",",ShortSetUp,",",OpenValue,",",Mp);
If Mp=-1 then begin
ShortSetUp=false;
end;
if LastBarOnChart then Print(5,",",CurrentBar,",",Value1,",",BuySetUp,",",ShortSetUp,",",OpenValue,",",Mp);
if LastBarOnChart then begin
If Mp<>1 and BuySetUp then buy next bar at OpenValue Limit;
If Mp<>-1 and ShortSetUp then SellShort next bar at OpenValue Limit;
End;
if LastBarOnChart then Print(6,",",CurrentBar,",",Value1,",",BuySetUp,",",ShortSetUp,",",OpenValue,",",Mp); SetExitonClose;
if LastBarOnChart then Print(7,",",CurrentBar,",",Value1,",",BuySetUp,",",ShortSetUp,",",OpenValue,",",Mp);

=============

OutPut:
1.00, 634.00, 845.83,TRUE,TRUE, 847.00, 0.00
2.00, 634.00, 845.83,TRUE,TRUE, 847.00, 0.00
3.00, 634.00, 845.83,TRUE,TRUE, 847.00, 0.00
4.00, 634.00, 845.83,TRUE,TRUE, 847.00, 0.00
5.00, 634.00, 845.83,TRUE,TRUE, 847.00, 0.00
6.00, 634.00, 845.83,TRUE,TRUE, 847.00, 0.00
7.00, 634.00, 845.83,TRUE,TRUE, 847.00, 0.00
1.00, 634.00, 845.83,TRUE,TRUE, 847.00, 1.00
2.00, 634.00, 845.83,TRUE,TRUE, 847.00, 1.00
3.00, 634.00, 845.83,TRUE,TRUE, 847.00, 1.00
4.00, 634.00, 845.83,FALSE,TRUE, 847.00, 1.00
5.00, 634.00, 845.83,FALSE,TRUE, 847.00, 1.00
6.00, 634.00, 845.83,FALSE,TRUE, 847.00, 1.00
7.00, 634.00, 845.83,FALSE,TRUE, 847.00, 1.00

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

Postby TJ » 16 Apr 2009

my idea was to put a print statement after every assignment... so that you can track what variables have been manipulated.

SUPER
Posts: 646
Joined: 03 Mar 2007
Has thanked: 106 times
Been thanked: 84 times

Postby SUPER » 17 Apr 2009

if I write my code in following manner (A),(B) and (C) then correct values are assigned right from the first tick:

(A)
BuySetUp= C[1] > Value1;

(B)
if C[1] > Value1 then BuySetUp=true else BuySetUp=false;

(C) (Tj's suggestion)
if C[1] > Value1 then begin
BuySetUp=true;
ShortSetUp=false;
end;

================

Does it mean that following coding technique is not correct?

(D)
if C[1] > Value1 then BuySetUp=true;

Will appreciate views.

Regards
Super

User avatar
Andrew Kirillov
Posts: 1589
Joined: 28 Jul 2005
Has thanked: 2 times
Been thanked: 31 times
Contact:

Postby Andrew Kirillov » 17 Apr 2009

The thing is BuySetUp as well as ShortSetUp are RESET to false only when Mp <> 0. While MP=0 BuySetUp and ShortSetUp will the same as they were in the process of signal calculation on the previous bars:

Mp=MarketPosition;
...
If Mp=1 then begin
BuySetUp=false;
end;

If Mp=-1 then begin
ShortSetUp=false;
end;

When Auto-trading starts after historical calculation, MP = 0. BuySetUp & ShortSetUp have with 99% probability TRUE values, because they have changing in the process of the whole historical calculation and the most important they could be change in True state only since to make them false is necessary that MP<>0.
This is why "on the print out of the first tick I get (612.00, 843.75, 843.92, True, True, 843.75, 0); "

SUPER
Posts: 646
Joined: 03 Mar 2007
Has thanked: 106 times
Been thanked: 84 times

Postby SUPER » 17 Apr 2009

Andrew,

Many thanks for your precise explanation, I was stuck in one frame of mind and without your assistance would have continued to think along the same lines.

Your explanation clearly demonstrates that on has to pay more attention towards resetting of variables if they are to be use system for Automated trade.

I have to admit that the problems I experienced with Sync mode were due to coding issues and MC is working as expected in its current state.

Summary: ORDERS ARE PERFECTLY HANDLED IN MC AUTOMATION.

Thanks & Regards
Super

User avatar
Andrew Kirillov
Posts: 1589
Joined: 28 Jul 2005
Has thanked: 2 times
Been thanked: 31 times
Contact:

Postby Andrew Kirillov » 17 Apr 2009

My pleasure.


Return to “MultiCharts”