Passing array to function such as average or RSI?

Questions about MultiCharts and user contributed studies.
maisatomai
Posts: 83
Joined: 18 Mar 2013
Has thanked: 11 times

Passing array to function such as average or RSI?

Postby maisatomai » 23 Apr 2014

Hi

Code: Select all

value10=average(HighDaily[0],2);
value11=(HighDaily[0]+HighDaily[1])/2;
This works as value10 has same value as value11

Code: Select all

value10=average(HighDaily[0],3);
value11=(HighDaily[0]+HighDaily[1]+HighDaily[2])/3;
This don't work as value10 has different value as value11

HighDaily is of type Array. Anyone know why and how can I fix it? I hope to convert array into a PriceSeries as I suspect we need to pass PriceSeries into average for it to work well. I need this to work as I need it to use on other functions like RSI

maisatomai
Posts: 83
Joined: 18 Mar 2013
Has thanked: 11 times

Re: Passing array to function such as average or RSI?

Postby maisatomai » 23 Apr 2014

I tried doing something like this but it invalid

Code: Select all

value12=RSI(HighDaily,3)[0];
I think it because HighDaily is an array which is NumericSimple and RSI need NumericSeries. How do I create a numericseries?

I think if all this fail I will just create my own algo to calculate things like RSI and average

bowlesj3
Posts: 2180
Joined: 21 Jul 2007
Has thanked: 227 times
Been thanked: 429 times

Re: Passing array to function such as average or RSI?

Postby bowlesj3 » 23 Apr 2014

If HighDaily is an array it will not work. Your index references in an array work from element 0 to 1 to 2 and have nothing to do with the time of the bar. NumericSeries relates to price bars and the [0], [1] and [2] work back in time which is totally different. Numeric series relates to anything to do with the internal price bar tables that MC automatically sets up for you so you can access historic price bar related info. You can look inside all the functions you are using and see it they are numeric series. I am surprised the average function will even allow you to compile an array going into it.

Looking at the code in the Average function.

Code: Select all

inputs:
PriceValue( numericseries ),
Len( numericsimple ) ;

Average = Summation( PriceValue, Len ) / Len ;
It makes sense that HighDaily will give you an error when you try to submit it to the RSI function which is numeric series. Now if you changed HighDaily to a variable and put in an statement HighDaily=close then it would force HighDaily to be numeric series and the RSI statement would work.

You can hover your cursor over the variable and it will tell you if it is numeric simple or numeric series.

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

Re: Passing array to function such as average or RSI?

Postby JoshM » 26 Apr 2014

(..) I am surprised the average function will even allow you to compile an array going into it. (..)
Out of curiosity I had to test this. :]

Code: Select all

Arrays:
priceArray[100](0);

Variables:
myAverage(0);

myAverage = Average(priceArray, 20);
Gives a compile error (like it should):

Code: Select all

------ Compiled with error(s): ------
Incorrect argument type.
line 6, column 20
* * * * * * *

To answer the title of this thread ("Passing array to function such as average or RSI?"), create a function like:

Code: Select all

Inputs:
Price[MaxSize](NumericArray);

Variables:
x(0);

// Output array
for x = 0 to Array_GetMaxIndex(Price) begin

Print("Price = ", NumToStr(Price[x], 4));

end;
Then an indicator like:

Code: Select all

Arrays:
myPriceArray[9](0);

Variables:
y(0);

once (LastBarOnChart_s = true) begin

// Populate array
for y = 0 to 9 begin

myPriceArray[y] = Close[y];

end;

// Passing (copy of) array to function
test_Function_April_2014(myPriceArray);

end;
And you get an output like:

Code: Select all

Price = 1.4897
Price = 1.4924
Price = 1.4924
Price = 1.4928
Price = 1.4926
Price = 1.4931
Price = 1.4934
Price = 1.4929
Price = 1.4926
Price = 1.4926


Return to “MultiCharts”