how can it calculate base on closed bar?  [SOLVED]

Questions about MultiCharts and user contributed studies.
kiyi0317
Posts: 40
Joined: 13 Apr 2016
Has thanked: 17 times
Been thanked: 1 time

how can it calculate base on closed bar?

Postby kiyi0317 » 21 Sep 2016

dear all,

I have a script work for multi data instruments, the entry and exit are correct during back test. but when the script running for real time trade, the orders will be submit on every data1 bar close as usual, but the calculation base on the indicators of the current bar on data2, data3, data4 and data5, Not the last closed bar!? how I can make it work to calculate for the closed bar instead of the current bar (not yet close)?

FYI, IOG disable and IntraBarPersist does not use in the script.

thanks
YT

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

Re: how can it calculate base on closed bar?

Postby TJ » 21 Sep 2016

dear all,

I have a script work for multi data instruments, the entry and exit are correct during back test. but when the script running for real time trade, the orders will be submit on every data1 bar close as usual, but the calculation base on the indicators of the current bar on data2, data3, data4 and data5, Not the last closed bar!? how I can make it work to calculate for the closed bar instead of the current bar (not yet close)?

FYI, IOG disable and IntraBarPersist does not use in the script.

thanks
YT
What are the chart resolution for each data series?

kiyi0317
Posts: 40
Joined: 13 Apr 2016
Has thanked: 17 times
Been thanked: 1 time

Re: how can it calculate base on closed bar?

Postby kiyi0317 » 21 Sep 2016


What are the chart resolution for each data series?
range bar
data1 10pts
data2 30pts
data3 50pts
data4 100pts
data5 200pts

kiyi0317
Posts: 40
Joined: 13 Apr 2016
Has thanked: 17 times
Been thanked: 1 time

Re: how can it calculate base on closed bar?

Postby kiyi0317 » 21 Sep 2016

more strange is that another similar script has being used, it never have the problem in the past, the closed bar has been calculated; but here right now the script also have the same problem, the current bar of data2,3,4 (not yet close) have being calculate for orders. it make me loss profit. please do help asap is much appreciate.

YT

kiyi0317
Posts: 40
Joined: 13 Apr 2016
Has thanked: 17 times
Been thanked: 1 time

Re: how can it calculate base on closed bar?

Postby kiyi0317 » 22 Sep 2016

I am not looking for same result, but entry and exit both are not correct, not calculate base on the last closed bar. And it has been running correctly before, it just got the problem within these days. the question should be what was updated in the platform.

tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 81 times
Contact:

Re: how can it calculate base on closed bar?

Postby tony » 22 Sep 2016

dear all,

I have a script work for multi data instruments, the entry and exit are correct during back test. but when the script running for real time trade, the orders will be submit on every data1 bar close as usual, but the calculation base on the indicators of the current bar on data2, data3, data4 and data5, Not the last closed bar!? how I can make it work to calculate for the closed bar instead of the current bar (not yet close)?

FYI, IOG disable and IntraBarPersist does not use in the script.

thanks
YT
Please don't take this the wrong way, but in the future make sure your questions are clear so others can follow along and help. I'm kind of understanding what you are asking. You are saying that in live trading you want to base calculations on the close of Data2, Data3, etc and NOT the current bar which has not yet closed?

Make sure you are referencing close[1] of data2 versus close of data2 to call the prior bar for that data series. Also look at the real time history matching setting. That could be impacting your results.

And lastly, note that with more than one data series as you are using, backtesting results can be skewed because if you are calculating intrabar, with data2, etc those will only calculate with OHLC because bar magnifier is not available for those series.

kiyi0317
Posts: 40
Joined: 13 Apr 2016
Has thanked: 17 times
Been thanked: 1 time

Re: how can it calculate base on closed bar?

Postby kiyi0317 » 22 Sep 2016

Please don't take this the wrong way, but in the future make sure your questions are clear so others can follow along and help. I'm kind of understanding what you are asking. You are saying that in live trading you want to base calculations on the close of Data2, Data3, etc and NOT the current bar which has not yet closed?

Make sure you are referencing close[1] of data2 versus close of data2 to call the prior bar for that data series. Also look at the real time history matching setting. That could be impacting your results.

And lastly, note that with more than one data series as you are using, backtesting results can be skewed because if you are calculating intrabar, with data2, etc those will only calculate with OHLC because bar magnifier is not available for those series.

I am sorry if my message is not explained clear enough. and thank you for your reply.

my original problem is that once data1 bar close, the calculation should be start and base on the details of data2 closed bar, not the data2 current bar. is it true?

I do expect that once data1 bar close, the calculation start and base on those closed bar on data2, data3 and data4, etc.

but the situation is, once data1 bar close, it calculated base on the data2 current bar and data3 current bar and so on....

the setting of real time history matching has clicked as always.
I had never use the intrabar calculation on back testing, that is why I looking for base on closed bar. and the exact problem right now is the calculation base on intrabar with data2, data3, data4, etc.

please help me out.

tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 81 times
Contact:

Re: how can it calculate base on closed bar?

Postby tony » 22 Sep 2016

Can you show your signal so we can better identify what's going on?

kiyi0317
Posts: 40
Joined: 13 Apr 2016
Has thanked: 17 times
Been thanked: 1 time

Re: how can it calculate base on closed bar?

Postby kiyi0317 » 22 Sep 2016

Can you show your signal so we can better identify what's going on?
same case with below example;

variables: var0 ( 0 ), var1 ( 0 ), var2 ( 0 ), var3 ( 0 ),
var4 ( 0, data2 ), var5 ( 0, data2 ), var6 ( 0, data2 ), var7 ( 0, data2 ),
var8 ( 0, data3 ), var9 ( 0, data3 ), var10( 0, data3 ), var11( 0, data3 );

Value1 = Stochastic( H, L, C, Length, 3, 3, 1, var0, var1, var2, var3 ) of data1;
Value2 = Stochastic( H, L, C, Length, 3, 3, 1, var4, var5, var6 , var7 ) of data2;
Value3 = Stochastic( H, L, C, Length, 3, 3, 1, var8, var9, var10, var11 ) of data3;

if MarketPosition_at_Broker <= 0 then begin;
if condition2 or condition3 or condition5 or condition6 then
Buy ( "buyentry1" ) Tradesize contracts next bar at market;
end;

if MarketPosition_at_Broker >= 0 then begin;
if condition8 or condition9 or condition11 or condition12 then
sellshort ( "sellentry1" ) Tradesize contracts next bar at market;
end;

if MarketPosition > 0 and conditionx then begin;
if conditiony or conditionz then sell ("exitbuy") next bar at market;
end;

if MarketPosition < 0 and conditionx then begin;
if conditiony or conditionz then buytocover ("exitsell") next bar at market;
end;

is it enough?

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

Re: how can it calculate base on closed bar?

Postby TJ » 22 Sep 2016

Can you show your signal so we can better identify what's going on?
same case with below example;
variables: var0 ( 0 ), var1 ( 0 ), var2 ( 0 ), var3 ( 0 ),
var4 ( 0, data2 ), var5 ( 0, data2 ), var6 ( 0, data2 ), var7 ( 0, data2 ),
var8 ( 0, data3 ), var9 ( 0, data3 ), var10( 0, data3 ), var11( 0, data3 );
Value1 = Stochastic( H, L, C, Length, 3, 3, 1, var0, var1, var2, var3 ) of data1;
Value2 = Stochastic( H, L, C, Length, 3, 3, 1, var4, var5, var6 , var7 ) of data2;
Value3 = Stochastic( H, L, C, Length, 3, 3, 1, var8, var9, var10, var11 ) of data3;
if MarketPosition_at_Broker <= 0 then begin;
if condition2 or condition3 or condition5 or condition6 then
Buy ( "buyentry1" ) Tradesize contracts next bar at market;
end;
if MarketPosition_at_Broker >= 0 then begin;
if condition8 or condition9 or condition11 or condition12 then
sellshort ( "sellentry1" ) Tradesize contracts next bar at market;
end;
if MarketPosition > 0 and conditionx then begin;
if conditiony or conditionz then sell ("exitbuy") next bar at market;
end;
if MarketPosition < 0 and conditionx then begin;
if conditiony or conditionz then buytocover ("exitsell") next bar at market;
end;
is it enough?

See post #1, #2 and #5
[FAQ] How to Post Codes (... so that people can read)
viewtopic.php?f=16&t=11713

kiyi0317
Posts: 40
Joined: 13 Apr 2016
Has thanked: 17 times
Been thanked: 1 time

Re: how can it calculate base on closed bar?

Postby kiyi0317 » 22 Sep 2016

Code: Select all

variables: var0 ( 0 ), var1 ( 0 ), var2 ( 0 ), var3 ( 0 ),
var4 ( 0, data2 ), var5 ( 0, data2 ), var6 ( 0, data2 ), var7 ( 0, data2 ),
var8 ( 0, data3 ), var9 ( 0, data3 ), var10( 0, data3 ), var11( 0, data3 );

Value1 = Stochastic( H, L, C, Length, 3, 3, 1, var0, var1, var2, var3 ) of data1;
Value2 = Stochastic( H, L, C, Length, 3, 3, 1, var4, var5, var6 , var7 ) of data2;
Value3 = Stochastic( H, L, C, Length, 3, 3, 1, var8, var9, var10, var11 ) of data3;

if MarketPosition_at_Broker <= 0 then begin;
if condition2 or condition3 or condition5 or condition6 then
Buy ( "buyentry1" ) Tradesize contracts next bar at market;
end;

if MarketPosition_at_Broker >= 0 then begin;
if condition8 or condition9 or condition11 or condition12 then
sellshort ( "sellentry1" ) Tradesize contracts next bar at market;
end;

if MarketPosition > 0 and conditionx then begin;
if conditiony or conditionz then sell ("exitbuy") next bar at market;
end;

if MarketPosition < 0 and conditionx then begin;
if conditiony or conditionz then buytocover ("exitsell") next bar at market;
end;

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

Re: how can it calculate base on closed bar?

Postby TJ » 22 Sep 2016


same case with below example;

::

is it enough?

If you follow my advice in reading #5,
your error will be evident immediately.

tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 81 times
Contact:

Re: how can it calculate base on closed bar?

Postby tony » 22 Sep 2016

Code: Select all

variables: var0 ( 0 ), var1 ( 0 ), var2 ( 0 ), var3 ( 0 ),
var4 ( 0, data2 ), var5 ( 0, data2 ), var6 ( 0, data2 ), var7 ( 0, data2 ),
var8 ( 0, data3 ), var9 ( 0, data3 ), var10( 0, data3 ), var11( 0, data3 );

Value1 = Stochastic( H, L, C, Length, 3, 3, 1, var0, var1, var2, var3 ) of data1;
Value2 = Stochastic( H, L, C, Length, 3, 3, 1, var4, var5, var6 , var7 ) of data2;
Value3 = Stochastic( H, L, C, Length, 3, 3, 1, var8, var9, var10, var11 ) of data3;

if MarketPosition_at_Broker <= 0 then begin;
if condition2 or condition3 or condition5 or condition6 then
Buy ( "buyentry1" ) Tradesize contracts next bar at market;
end;

if MarketPosition_at_Broker >= 0 then begin;
if condition8 or condition9 or condition11 or condition12 then
sellshort ( "sellentry1" ) Tradesize contracts next bar at market;
end;

if MarketPosition > 0 and conditionx then begin;
if conditiony or conditionz then sell ("exitbuy") next bar at market;
end;

if MarketPosition < 0 and conditionx then begin;
if conditiony or conditionz then buytocover ("exitsell") next bar at market;
end;
I don't see what condition8, 9, etc equals. I do see value 2 and value3 are based on current data2, data3 values. If you want to reference the prior bar close for value2 and value3 then you need declare a variable and then reference the prior bar. For example var12(0, data2) and then var12=Stochastic( H, L, C, Length, 3, 3, 1, var4, var5, var6 , var7 ) of data2; and when using var12 in a conditional statement, reference var12[1]

kiyi0317
Posts: 40
Joined: 13 Apr 2016
Has thanked: 17 times
Been thanked: 1 time

Re: how can it calculate base on closed bar?

Postby kiyi0317 » 22 Sep 2016


If you follow my advice in reading #5,
your error will be evident immediately.

TJ, thank you for your reminder. will make it better

kiyi0317
Posts: 40
Joined: 13 Apr 2016
Has thanked: 17 times
Been thanked: 1 time

Re: how can it calculate base on closed bar?

Postby kiyi0317 » 22 Sep 2016


I don't see what condition8, 9, etc equals. I do see value 2 and value3 are based on current data2, data3 values. If you want to reference the prior bar close for value2 and value3 then you need declare a variable and then reference the prior bar. For example var12(0, data2) and then var12=Stochastic( H, L, C, Length, 3, 3, 1, var4, var5, var6 , var7 ) of data2; and when using var12 in a conditional statement, reference var12[1]
Tony, first of all, thank you for your help and reply.

sorry for the mess coding info, currently I am using var6 in the condition statement. is it not correct?

var12=Stochastic( H, L, C, Length, 3, 3, 1, var4, var5, var6 , var7 ) of data2;

I do understand above. I try to use var6[1] in the statement before but it just be execute after var6 close, which means delay. anyway, I will test what you suggest.

kiyi0317
Posts: 40
Joined: 13 Apr 2016
Has thanked: 17 times
Been thanked: 1 time

Re: how can it calculate base on closed bar?

Postby kiyi0317 » 23 Sep 2016

I modified the script, hope that will be easily to understand. please let me know if you could find any mistake. much appreciate for your help.

Code: Select all

inputs: Length(14), OverSold(20), OverBought(80), Tradesize(1);

variables: var0(0), var1(0), SlowKd1(0), var3(0),
var4(0, data2), var5(0, data2), SlowKd2(0, data2), var7(0, data2),
var8(0, data3), var9(0, data3), SlowKd3(0, data3), var11(0, data3),
STOd1(0, data1), STOd2(0, data2), STOd3(0, data3),
buyin1(false), buyin2(false), buyin3(false),
sellout1(false), sellout2(false), sellout3(false);

STOd1 = Stochastic( H, L, C, Length, 3, 3, 1, var0, var1, SlowKd1, var3 ) of data1;
STOd2 = Stochastic( H, L, C, Length, 3, 3, 1, var4, var5, SlowKd2, var7 ) of data2;
STOd3 = Stochastic( H, L, C, Length, 3, 3, 1, var8, var9, SlowKd3, var11 ) of data3;

//buy entry
buyin1 = SlowKd1 crosses over OverSold ;
buyin2 = SlowKd1 crosses over OverSold and SlowKd2 > OverSold and SlowKd2 < OverBought;
buyin3 = SlowKd1 crosses over OverSold and SlowKd3 > OverSold and SlowKd3 < OverBought;


//sell entry
sellout1 = SlowKd1 crosses under OverBought ;
sellout2 = SlowKd1 crosses under OverBought and SlowKd2 > OverSold and SlowKd2 < OverBought;
sellout3 = SlowKd1 crosses under OverBought and SlowKd3 > OverSold and SlowKd3 < OverBought;


//buy entry
if MarketPosition_at_Broker <= 0 then begin;
if buyin2 or buyin3 then
Buy ( "buyentry1" ) Tradesize contracts next bar at market;
end;

if MarketPosition_at_Broker <= 1 then begin;
if buyin1 then begin;
if (SlowKd2 > OverSold) or (SlowKd3 > OverSold) then
Buy ( "buyentry2" ) Tradesize contracts next bar at market;
end;
end;


//sell entry
if MarketPosition_at_Broker >= 0 then begin;
if sellout2 or sellout3 then
sellshort ( "sellentry1" ) Tradesize contracts next bar at market;
end;

if MarketPosition_at_Broker >= -1 then begin;
if sellout1 then begin;
if (SlowKd2 < OverBought) or (SlowKd3 < OverBought) then
sellshort ( "sellentry2" ) Tradesize contracts next bar at market;
end;
end;


//exit
if MarketPosition > 0 then begin;
if (SlowKd2 crosses under OverSold) or (SlowKd3 crosses under OverSold) then
sell ("exitbuy") next bar at market;
end;

if MarketPosition < 0 then begin;
if (SlowKd2 crosses over OverBought) or (SlowKd3 crosses over OverBought) then
buytocover ("exitsell") next bar at market;
end;

kiyi0317
Posts: 40
Joined: 13 Apr 2016
Has thanked: 17 times
Been thanked: 1 time

Re: how can it calculate base on closed bar?

Postby kiyi0317 » 28 Sep 2016

any one can help?

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: how can it calculate base on closed bar?

Postby JoshM » 29 Sep 2016

I have a script work for multi data instruments, the entry and exit are correct during back test. but when the script running for real time trade, the orders will be submit on every data1 bar close as usual, but the calculation base on the indicators of the current bar on data2, data3, data4 and data5, Not the last closed bar!? how I can make it work to calculate for the closed bar instead of the current bar (not yet close)?
If I understand you correctly, the following explanation would apply:

When a MultiCharts script calculates on historical data -- whether this being a backtest or an indicator -- then the `Close` keyword refers to the bar's closing price. And then `Close` returns the price of the last (or current) closed bar.

But when a script calculates on real-time data, the `Close` keyword returns the current price of the bar. This kind of makes sense because in real time, MultiCharts cannot be sure whether the current price update is the last of the price bar. And so each incoming price tick 'extends' the bar's closing price.

This would explain the behaviour that you're seeing, namely that on historical data the closing price is used by the script but in live trading the current price of the different data series.

So to experiment, you might want to try changing:

Code: Select all

STOd2 = Stochastic( H, L, C, Length, 3, 3, 1, var4, var5, SlowKd2, var7 ) of data2;
STOd3 = Stochastic( H, L, C, Length, 3, 3, 1, var8, var9, SlowKd3, var11 ) of data3;
..to:

Code: Select all

STOd2 = Stochastic( H, L, C[1], Length, 3, 3, 1, var4, var5, SlowKd2, var7 ) of data2;
STOd3 = Stochastic( H, L, C[1], Length, 3, 3, 1, var8, var9, SlowKd3, var11 ) of data3;

kiyi0317
Posts: 40
Joined: 13 Apr 2016
Has thanked: 17 times
Been thanked: 1 time

Re: how can it calculate base on closed bar?  [SOLVED]

Postby kiyi0317 » 29 Sep 2016


So to experiment, you might want to try changing:

Code: Select all

STOd2 = Stochastic( H, L, C, Length, 3, 3, 1, var4, var5, SlowKd2, var7 ) of data2;
STOd3 = Stochastic( H, L, C, Length, 3, 3, 1, var8, var9, SlowKd3, var11 ) of data3;
..to:

Code: Select all

STOd2 = Stochastic( H, L, C[1], Length, 3, 3, 1, var4, var5, SlowKd2, var7 ) of data2;
STOd3 = Stochastic( H, L, C[1], Length, 3, 3, 1, var8, var9, SlowKd3, var11 ) of data3;
dear Josh,
appreciate for your help, let's try. thanks

brgds
YT


Return to “MultiCharts”