Count two green dots after a red for custom study


Category:
0
0

Hello, I am just a self-taught scripter here and i am wondering if  you can help me finish my chart study script about a condition counter, I am kinda stuck on the last part where i have to use a reccursive command with condition counter? I tried to find examples from the search results here and i cant seem to figure it out.

Anyway, I have attached a picture to help me with my question. Basically, I am trying to plot scan a 2nd green dot after a red dot only. Then resets and plot the next 2nd green dot again after the red dot and so on.

Here’s the script I have so far.

declare lower;

input length = 14;

input price = close;

input averageType = AverageType.WILDERS;

def NetChgAvg = MovingAverage(averageType, price – price[1], length);

def TotChgAvg = MovingAverage(averageType, AbsValue(price – price[1]), length);

def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;

def RSI = 50 * (ChgRatio + 1);

rec highpeak = if (RSI <= RSI [1] and RSI [1] >= RSI [2]) then RSI [1] else highpeak [1];
def toppeak = highpeak;

rec lowpeak = if (RSI >= RSI [1] and RSI [1] <= RSI [2]) then RSI [1] else lowpeak [1];
def bottompeak = lowpeak;
#———————–
#————————

#————————

def greendot =
(RSI crosses above toppeak or RSI > toppeak)

;

#—————————–

def reddot =
(RSI crosses below bottompeak or RSI < bottompeak)

;

rec counter =
if greendot then + 1
else if counter[1] == 1 or counter[1] < 3 then counter[1] + 0
else + 0
;

plot count = counter == 2;

Attachments:
Marked as spam
Posted by (Questions: 3, Answers: 0)
Asked on April 25, 2020 11:55 pm
106 views
0
Private answer

I see that you have chart study request so I have moved this out of the "Strategy Guide" topic and into the "Chart Studies" topic.

Actually there is a very simple way to build this without using a recursive variable. Which is great because there are tools on the platform that do not support recursive variables. Recursion should only be used in cases where it is impossible to accomplish something without it.

So you have a pattern. One red dot followed by two green dots. We use the index of each variable to reference previous bars. Index references go inside bracket at the very end of a variable name: [0]. The default index is zero and references the current bar being evaluated by the study. When the index is omitted from a variable it means you are referencing the current bar. If you want to reference the previous bar you use an index of [1].

So we need a green dot on the current bar, a green dot on the previous bar, and a red dot on the bar before that:

def twoGreensAfterRed = greenDot and greenDot[1] and redDot[2];

Done, and no recursion required. For each bar on the chart, that one line of code will look at the most recent 3 bars and return true when that exact pattern is found.

Marked as spam
Posted by (Questions: 37, Answers: 4087)
Answered on April 26, 2020 8:40 am