Understanding arrays/ get Y when X condition is met

Questions about MultiCharts and user contributed studies.
ScottishSpeculator
Posts: 47
Joined: 03 Jan 2023
Been thanked: 3 times

Understanding arrays/ get Y when X condition is met

Postby ScottishSpeculator » 05 Mar 2023

Hello guys.

Still learning... Anyone got any good examples or materials on arrays? Need to brush up on this. I think the solution to a problem at moment involves arrays.

Let's say you want to look back at an indicator and select a value based on some condition. Say for example purposes volatility value and the condition may be the highest or lowest value over a look back period. Easy enough you can use something like :

Code: Select all

input: lookback(10); var: StdDev(0), lowest_std(0); StdDev = StdDev(close, lookback); lowest_std = lowest(stdDev, lookback);

However what if I want to also know the close price which corresponds to the lowest StdDev value and have this as a different variable you could plot? How do you go about this? Obtaining Y for X which has conditions , is the answer array?

thanks in advance

User avatar
Polly MultiCharts
Posts: 202
Joined: 20 Jul 2022
Has thanked: 1 time
Been thanked: 56 times

Re: Understanding arrays/ get Y when X condition is met

Postby Polly MultiCharts » 13 Mar 2023

Hi ScottishSpeculator,

Please refer to this example of arrays usage:

Code: Select all

once cleardebug; input: lookback(10); var: StdD(0), lowest_std(0); StdD = StdDev(close, lookback); lowest_std = lowest(StdD , lookback); array: ar_Close[](0); array: ar_StdDev[](0); Array_SetMaxIndex(ar_Close, Symbol_currentbar); ar_Close[Symbol_currentbar] = Symbol_close; Array_SetMaxIndex(ar_StdDev, Symbol_currentbar); ar_StdDev[Symbol_Currentbar] = lowest_std; plot1(ar_Close[Symbol_currentbar], "Symbol_close"); plot2(ar_StdDev[Symbol_Currentbar], "lowest_std"); //(1) outputs value //print(" i = ", Symbol_CurrentBar:0:0, " | Close = ", symbol_close:0:8, " | lowest_std = ", lowest_std:0:8); //(2) ounput arrays if LastBarOnChart then begin for value1 = 1 to Symbol_Currentbar begin print(" i = ", value1:0:0, " | Close = ", ar_Close[value1]:0:8, " | lowest_std = ", ar_StdDev[value1]:0:8); end; end;
In this example, you can also just output the ‘Close’ and ‘lowest_std’ values without using arrays. In that case you need to comment out the lines using ‘//(1) outputs value’.

ScottishSpeculator
Posts: 47
Joined: 03 Jan 2023
Been thanked: 3 times

Re: Understanding arrays/ get Y when X condition is met

Postby ScottishSpeculator » 13 Mar 2023

Hey Polly

Are you able to attach some more comments to the lines of code so I can follow what each line is doing, is that too much trouble? Still trying to learn arrays. I read you couldn't plot arrays so this seems conflicting. What I can't see from the comments/ code is how ar_close is not just plotting all the closes vs closes for when lowest_std is true.

User avatar
Polly MultiCharts
Posts: 202
Joined: 20 Jul 2022
Has thanked: 1 time
Been thanked: 56 times

Re: Understanding arrays/ get Y when X condition is met

Postby Polly MultiCharts » 15 Mar 2023

ScottishSpeculator,

Here are dynamic array declarations:

Code: Select all

//dynamic array declarations array: ar_Close[](0); array: ar_StdDev[](0);
For more info about array declarations please refer to this page.

Dynamic arrays cannot automatically increase their capacity, they need to call the function for this:

Code: Select all

Array_SetMaxIndex(ar_Close, Symbol_currentbar);
Here is more info about it.

Also please note that the indicators are calculated sequentially on each bar, and Symbol_currentbar will have a new Current Bar value on every calculation, that is, increase by one, and fill a new array cell with values.

As for

Code: Select all

ar_Close[Symbol_currentbar] = Symbol_close;
and

Code: Select all

ar_StdDev[Symbol_Currentbar] = lowest_std;
The function ‘lowest_std = lowest(StdD , lookback)’ returns a numeric value, not a bool, so if you need to additionally calculate the extrema of the ‘lowest value’ function, that is, the lowest value among the lowest value, and put it in an array, then you will need to create a special function for that.

ScottishSpeculator
Posts: 47
Joined: 03 Jan 2023
Been thanked: 3 times

Re: Understanding arrays/ get Y when X condition is met

Postby ScottishSpeculator » 15 Mar 2023

Polly thanks for your reply. Yeah the function is probably what I am after. Ill look into your links more,


Return to “MultiCharts”