Shifting Values within a Variable

Questions about MultiCharts and user contributed studies.
trader0311
Posts: 50
Joined: 28 Sep 2014
Has thanked: 7 times

Shifting Values within a Variable

Postby trader0311 » 20 Sep 2015

Hi,

I have a variable that is conditional meaning that sometimes values are calculated for the variable and sometimes they are not depending on if my condition is true. When the condition is not true generally the last value that the variable produced is used over and over until the condition is true and another value is added (or I can add in a zero). To illustrate please see the string of numbers below:

1, 2, 3, 4, 5, 5, 5, 5, 5, 6, 7, 8

As you can see in this string the number 5 is repeated. The problem I am facing is that I want to apply an analysis technique, like a moving average, to this string of numbers contained in a variable. The consecutive 5's will screw up the analysis technique you apply because of this "flat spot" (ie the consecutive 5's).

My question related as to how I eliminate these redundant numbers or flat spots so I can properly apply different analysis techniques or functions to the variable. When done properly the string of numbers would read 1,2,3,4,5,6,7,8.

Ideally I would like to do this within a variable since arrays can be difficult to apply analysis techniques to. I had the idea of shifting all the values in the variable to the right by one bar when my condition was not true. If that was possible, the shift in the numbers would eliminate this gap of redundant numbers in my string. Would anyone have an idea regarding how I can accomplish this shift in the variable values using powerlanguage? Any insight would be greatly appreciated. This has had me stumped for the longest time.

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

Re: Shifting Values within a Variable

Postby TJ » 21 Sep 2015

Hi,

I have a variable that is conditional meaning that sometimes values are calculated for the variable and sometimes they are not depending on if my condition is true. When the condition is not true generally the last value that the variable produced is used over and over until the condition is true and another value is added (or I can add in a zero). To illustrate please see the string of numbers below:

1, 2, 3, 4, 5, 5, 5, 5, 5, 6, 7, 8

As you can see in this string the number 5 is repeated. The problem I am facing is that I want to apply an analysis technique, like a moving average, to this string of numbers contained in a variable. The consecutive 5's will screw up the analysis technique you apply because of this "flat spot" (ie the consecutive 5's).

My question related as to how I eliminate these redundant numbers or flat spots so I can properly apply different analysis techniques or functions to the variable. When done properly the string of numbers would read 1,2,3,4,5,6,7,8.

Ideally I would like to do this within a variable since arrays can be difficult to apply analysis techniques to. I had the idea of shifting all the values in the variable to the right by one bar when my condition was not true. If that was possible, the shift in the numbers would eliminate this gap of redundant numbers in my string. Would anyone have an idea regarding how I can accomplish this shift in the variable values using powerlanguage? Any insight would be greatly appreciated. This has had me stumped for the longest time.
You have to use an array.

You also need a temp variable.
You can transfer the array into this temp variable before applying your analysis processing.

trader0311
Posts: 50
Joined: 28 Sep 2014
Has thanked: 7 times

Re: Shifting Values within a Variable

Postby trader0311 » 22 Sep 2015

Thanks TJ.

I have not had much experience with arrays but will definitely give it a shot.

I was doing some reading on loops and was thinking it might be possible to create a loop that, when the condition to add a number to the variable was not true, that the loop would execute on every value in the loop moving each of them to the right ( value1 = value1[1]) or something like that.

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

Re: Shifting Values within a Variable

Postby TJ » 22 Sep 2015

Thanks TJ.

I have not had much experience with arrays but will definitely give it a shot.

I was doing some reading on loops and was thinking it might be possible to create a loop that, when the condition to add a number to the variable was not true, that the loop would execute on every value in the loop moving each of them to the right ( value1 = value1[1]) or something like that.
That can work too... the devil is in the detail.


Return to “MultiCharts”