orders are using buy and sell levels from previous bar

Questions about MultiCharts and user contributed studies.
faschim
Posts: 36
Joined: 14 Jul 2009
Has thanked: 2 times

orders are using buy and sell levels from previous bar

Postby faschim » 01 Feb 2013

I have a strategy that uses intrabarordergeneration=true (and bar magnifier using 1 tick) and 3 data series, namely data1=5 minute bars, data2=5 second bars, data3=5 minutes bars again (overlaid on data2 chart for visual tracking only). Other pertinent info is:

* Multicharts version 8.0 build 5620
* Broker is IB
* using live simulated trading
* symbol = ES

The strategy submits limit orders with price levels given by the variables buyLevel and sellLevel. The actual orders that get placed are using values of these variables for a previous bar and not the latest as calculated at the close of the bar. Those levels should be active for the next bar. In other words its almost like its using buyLevel[1] and sellLevel[1]. Below is a strategy that I created to illustrate the issue. If you run this with the setup as described and compare the output of the print statement with the order price levels in IB or in the Order and Position Tracker for that matter, you will see what I mean.

Code: Select all

[IntrabarOrderGeneration = True]

variables: intrabarpersist int statusOfBar_data1(0), intrabarpersist int statusOfBar_data2(0),
   intrabarpersist double avgBarRange(0.0), intrabarpersist double sellLevel(0.0),
   intrabarpersist double buyLevel(0.0), intrabarpersist bool isInitialized(false);

// barstatus of data1 and data2
statusOfBar_data1 = barstatus(1);    
statusOfBar_data2 = barstatus(2);

// calculate buy and sell limit levels
if statusOfBar_data1 = 2 then begin
   avgBarRange = XAverage(high-low,5);
   sellLevel = close + (2*avgBarRange);
   buyLevel = close - (2*avgBarRange);
   isInitialized = true;
end;

if (getappinfo(aiRealTimeCalc) > 0) then 
   print("time_s(of data2)=",(time_s of data2),",  barstatus(1)=",statusOfBar_data1,",  barstatus(2)=",statusOfBar_data2,",  buyLevel=",buyLevel,",  sellLevel=",sellLevel);
   
// submit orders
if isInitialized then begin
   buy("buy(1)") next bar at buyLevel limit;
   sellshort("sell(1)") next bar at sellLevel limit;
end;


Is it related to the issues surrounding the following barstatus(2) behavior
https://www.multicharts.com/pm/viewissue ... _no=MC-235

Or am I missing something in the code above?

User avatar
JoshM
Posts: 2080
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1525 times
Been thanked: 1483 times
Contact:

Re: orders are using buy and sell levels from previous bar

Postby JoshM » 02 Feb 2013

faschim wrote:I have a strategy that uses intrabarordergeneration=true (and bar magnifier using 1 tick) and 3 data series, namely data1=5 minute bars, data2=5 second bars, data3=5 minutes bars again (overlaid on data2 chart for visual tracking only).

What were the results when you switched the data series (Data1 = 5 seconds, Data2 = 5 minutes)?

What is your goal (i.e., what are you trying to achieve)? I can see in the code what your current implementation is, but this might not be a correct implementation depending on what you're trying to achieve. For example, why do you need three data series when your code suggests that one data series with IOG turned on will also suffice?

faschim wrote:If you run this with the setup as described and compare the output of the print statement with the order price levels in IB or in the Order and Position Tracker for that matter, you will see what I mean.

I suppose you mean the wrong order prices, correct? If not, can you explain what we will see then in the Order and Position Tracker that is wrong? (An annotated screenshot would be helpful) That saves us time to reproduce your issue and search for errors during market hours.

faschim
Posts: 36
Joined: 14 Jul 2009
Has thanked: 2 times

Re: orders are using buy and sell levels from previous bar

Postby faschim » 03 Feb 2013

Untitled.jpg
Untitled.jpg (94.81 KiB) Viewed 115 times
I will experiment a bit more on monday and try switching the data series around amongst other things.

Keep in mind that the code I posted was only to illustrate the issue and as a strategy it doesn't do anything sensible. The real strategy has 2000 lines of code so wasn't about to post that. For a variety of reasons. However the example code replicates the issue I was seeing. Note that in the real code I do make use of data1 and data2. The data3 overlay isn't used in the code but is used for chart cosmetics if you will.

I could of course be making a very simple oversight with the code or misunderstanding some standard behaviour of multicharts. At this point however it would appear to me that with this particular code, its producing wrong order levels in IB. They are essentially stale order levels (lagged by 1 bar). Attached is a screen shot from a spreadsheet I knocked up to illustrate what I was seeing. Will produce some screenshots on monday.

Untitled.jpg
Untitled.jpg (94.81 KiB) Viewed 115 times

User avatar
TJ
Posts: 6551
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 966 times
Been thanked: 1892 times

Re: orders are using buy and sell levels from previous bar

Postby TJ » 03 Feb 2013

faschim wrote:I have a strategy that uses intrabarordergeneration=true (and bar magnifier using 1 tick) and 3 data series, namely data1=5 minute bars, data2=5 second bars, data3=5 minutes bars again (overlaid on data2 chart for visual tracking only). Other pertinent info is:

* Multicharts version 8.0 build 5620
* Broker is IB
* using live simulated trading
* symbol = ES
...Or am I missing something in the code above?


You must make data1 the fastest resolution of all data series.
These users thanked the author TJ for the post:
faschim

faschim
Posts: 36
Joined: 14 Jul 2009
Has thanked: 2 times

Re: orders are using buy and sell levels from previous bar

Postby faschim » 04 Feb 2013

Thanks TJ. If I switch data1 and data2 around and change the code accordingly it works ok. For completeness I have attached a screenshot during live simulated trading that shows the original issue.

order levels.png
order levels.png (406.26 KiB) Viewed 116 times


Note that if I simply comment out the line

Code: Select all

statusOfBar_data = barstatus(2)

without switiching around the data series it also works. Sort of anyway. I need barstatus on both series so its not really a solution or workaround. Commenting out barstatus(2) also points back to this
https://www.multicharts.com/pm/viewissue ... _no=MC-235

Is making data1 the highest frequency data series officially documented anywhere or is it more unofficially known that if you don't it causes problems. I don't recall running across this anywhere.


Return to “MultiCharts”