Loop through data series

Questions about MultiCharts and user contributed studies.
User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Loop through data series

Postby JoshM » 27 Jul 2011

I’m writing a function to use in a strategy that gets tested in the Portfolio Backtester. Each symbol in my portfolio may or may not have more than one data series.

For example:
  • Symbol1: Data1, Data2, Data3
    Symbol2: Data1, Data2
    Symbol3: only Data1
Now I want to loop through the data series that are attached to an instrument, so I was thinking about something like..

Code: Select all

for value3 = 5 downto 1 begin
if Close Data(value3) > 0 then
Print("Symbol of data ", value3, " is ", symbol data(value3));
end;
..with the assumption that 'Close Data(value3)' would equal null if the specified data is not on the chart. Sadly, that doesn't work. :)
  • How can I access the number of data series in use for a instrument, without running into the ‘data series can’t be found – add this to chart’ error message?
An "solution" would be to add to each symbol the highest number of data series that is used by a symbol in the backtest, but I'd rather not do that, since that would be adding needless amount of data in the backtest.

arjfca
Posts: 1292
Joined: 23 Nov 2010
Has thanked: 725 times
Been thanked: 223 times

Re: Loop through data series

Postby arjfca » 28 Jul 2011

Hello John

How about a global variable value with a specific name for each study

In an external signal file, use these code to setup global variables

Var: IntVal (0);
IntVal = GVsetNamedInt ("StudyOne", 3); // 3 data series to look for
IntVal = GVsetNamedInt ("StudyTwo", 2); // 2 data series to look for

Fo each specific study, use

Var: HowManyDataSeries (0);
Var: IntVal (0);

HowManyDataSeries = GVGetNamedInt ("StudyTwo",1) // Rem: 1 will be your default value

This is just an exemple.

You could also have an array created using name of study and #data series. When testing, look trough your Array using your study name and get your # of data series to test

Good luck
Martin

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

Re: Loop through data series

Postby JoshM » 28 Jul 2011

Hello John

How about a global variable value with a specific name for each study

In an external signal file, use these code to setup global variables

Var: IntVal (0);
IntVal = GVsetNamedInt ("StudyOne", 3); // 3 data series to look for
IntVal = GVsetNamedInt ("StudyTwo", 2); // 2 data series to look for

Fo each specific study, use

Var: HowManyDataSeries (0);
Var: IntVal (0);

HowManyDataSeries = GVGetNamedInt ("StudyTwo",1) // Rem: 1 will be your default value

This is just an exemple.

You could also have an array created using name of study and #data series. When testing, look trough your Array using your study name and get your # of data series to test

Good luck
Martin
Thanks Martin! Very helpful and I've incorporated this solution by setting the GlobalVariables in a separate script. :)

In solving this problem I came across something else I don't thoroughly understand. According to the help manual (EasyLanguage Essentials), this is a good use of once:

Code: Select all

Var: Counter(0);
once ( CurrentBar = 1 ) and ( Counter = 0 ) begin
Counter = 1000;
end;
Because there is no need to set the GlobalVariables on each bar and then extract the number of data series (since this will remain constant for each bar), I was thinking about using Once to get better performance. However, this code..

Code: Select all

Var: HowManyDataSeries (0), tmp(""), tmpCounter(0);
Var: IntVal (0);

if CurrentBar = 1 then begin
ClearDebug;
Print("HowManyDataSeries: ", HowManyDataSeries);
end;

// Get number of data series
once (CurrentBar = 1 and HowManyDataSeries = 0) begin
//if CurrentBar = 1 then begin
Print("Getting data series...");
tmp = "StudyTwo";

MyTestFunction2;

if tmp = "StudyOne" then
HowManyDataSeries = GVGetNamedInt("StudyOne", 1)
else if tmp = "StudyTwo" then
HowManyDataSeries = GVGetNamedInt("StudyTwo", 1);

tmpCounter = tmpCounter + 1;
end;

if LastBarOnChart_s = true then begin
Print("Number of data series for this symbol", howManyDataSeries, " number of times set: ", tmpCounter);
end;
Gives the following output:
HowManyDataSeries: 0.00
Number of data series for this symbol 0.00 number of times set: 0.00
Number of data series for this symbol 0.00 number of times set: 0.00
Number of data series for this symbol 0.00 number of times set: 0.00
Number of data series for this symbol 0.00 number of times set: 0.00
Changing the
  • once (CurrentBar = 1 and HowManyDataSeries = 0) begin
to another 'once' statement will yield the same results (actually multiple once variations all give the erroneous output displayed above).

Off course, I can use
  • if CurrentBar = 1 then begin ....
and that does give the good results (so it's not really a problem but more an "exploration"), but I'd like to understand why Once does not work here. It can't be related to the GlobalVariables, since using CurrentBar = 1 does give the correct results.

arjfca
Posts: 1292
Joined: 23 Nov 2010
Has thanked: 725 times
Been thanked: 223 times

Re: Loop through data series

Postby arjfca » 28 Jul 2011

Your code:

Code: Select all

Var: Counter(0);
once ( CurrentBar = 1 ) and ( Counter = 0 ) begin
Counter = 1000;
end;
I never used CurrentBar = 1. You want to know if the current bar = 1. Your way, you assigned the value 1 to CurrentBar.

So maybe your code could be Var:

Code: Select all

Counter(0);
once ( If CurrentBar = 1 ) and ( Counter = 0 ) begin
Counter = 1000;
end;
Martin


Return to “MultiCharts”