Variable Assignment is Distorting Values

Questions about MultiCharts and user contributed studies.
multichartsUser23
Posts: 15
Joined: 02 Mar 2018
Has thanked: 3 times

Variable Assignment is Distorting Values

Postby multichartsUser23 » 28 Jan 2019

Hi guys,

Came across a weird issue where assigning a value to a variable seems to distort the past values of that value when these past values are referenced. The first batch of code below outputs values different from the values output by the second batch of code, and I have no idea why.

Code: Select all

vars: Bandwidth(0,data4), BulgeLine(0), SqueezeLine(0), x(0);
Bandwidth = BollingerBandwidth(Close,20,2,-2)data4;
BulgeLine = Highest(Bandwidth,50);
SqueezeLine = Lowest(Bandwidth,50);

if date = 1181210 and time = 1126 then begin
print(date,time,"||",BulgeLine:0:4,"||", o data4,"||",h data4,"||",l data4,"||",c data4);
for x = 0 to 5-1 begin
print(x, ", ", [b]Bandwidth[/b][x]:0:4);
end;
end;

BollingerLESeconds = BulgeLine-SqueezeLine;


Code: Select all

vars: Bandwidth(0,data4), BulgeLine(0), SqueezeLine(0), x(0);
Bandwidth = BollingerBandwidth(Close,20,2,-2)data4;
BulgeLine = Highest(Bandwidth,50);
SqueezeLine = Lowest(Bandwidth,50);

if date = 1181210 and time = 1126 then begin
print(date,time,"||",BulgeLine:0:4,"||", o data4,"||",h data4,"||",l data4,"||",c data4);
for x = 0 to 5-1 begin
print(x, ", ", BollingerBandwidth(Close,20,2,-2)data4[x]:0:4);
end;
end;

BollingerLESeconds = BulgeLine-SqueezeLine;

The second set of output values is the correct one, and the one that I want. However, it is SO much slower than the code in the first snippet above. Prohibitively slow in the grand scheme of things. It seems pretty weird that I shouldn't be able to assign this value to a variable and enjoy the speed and good coding convention that variable assignment offers without having to accept inaccurate values.

Can some please advise me on what to do?

Thanks a lot!

User avatar
Anna MultiCharts
Posts: 560
Joined: 14 Jul 2017
Has thanked: 42 times
Been thanked: 140 times

Re: Variable Assignment is Distorting Values

Postby Anna MultiCharts » 31 Jan 2019

Hello, multichartsUser23!

In the first script you output the variable values on previous bars, and in the second – the function values on previous bars. Depending on the type of the function – simple or series – the values of it might be different.
We’d recommend introducing Print commands to also see what values are sent to the variable and function on each calculation.

multichartsUser23
Posts: 15
Joined: 02 Mar 2018
Has thanked: 3 times

Re: Variable Assignment is Distorting Values

Postby multichartsUser23 » 26 Feb 2019

Hi Anna,

I don't think I understand. You seem to be blaming the function's series/simple attribute, but the function is used in both cases (in one case the function is used when assigning the variable, and the second one the function itself is used, as you correctly note), so I don't understand why the series/simple attribute would be a consideration in one scenario and not the other.

Moreover, the function being used is a default MC function seen in the calculation of Bollinger Bands.

I had used print statements to see what was going on, and only the second example above where I use the function (and not the variable holding that function) used the correct values. And when I saw "correct" values, I mean the values identical to those values printed in the same situation in which instead of data4, this calculation is being carried out on data3 (ie. before the introduction of the 10-second data stream that I'm sure you're familiar with). The incorrect values that are printed seem to come from the off-minute (meaning the 10-second bar is at something other than "xx:xx:00" like "xx:xx:30") data from the first data stream.

Also, I should note that this code works absolutely fine and does not present these problems to me when working with equities. It is only when working with futures that this occurs (meaning variable assignment does not present this problem). Please advise on how I can have the variable assignment, with its speed and efficiency, deliver the same results as the function to which it is assigned as seen in the second example) when you have a moment.

Thanks!

User avatar
Anna MultiCharts
Posts: 560
Joined: 14 Jul 2017
Has thanked: 42 times
Been thanked: 140 times

Re: Variable Assignment is Distorting Values

Postby Anna MultiCharts » 28 Feb 2019

Hello, multichartsUser23!

BollingerBandwidth is not found among the pre-built functions in MultiCharts. There’s only BollingerBand function, which is a Simple function, and all functions that are referenced in it are also Simple ones.
Please attach your workspace and BollingerBandwidth’s script so that we could check this on our end.


Return to “MultiCharts”