Up Arrow, Dependent on a Preceding Down Arrow


Category:
0
0

Hi Pete,

I’m trying to create a very simple study that marks the third consecutive higher high candle (or third lower low) with a up (or down) arrow, but only if the prior arrow was in the opposite direction.  In other words, it flips. My scripting abilities are fairly limited, so I’m hoping there’s a simple solution that I can understand and apply to other applications.  I was imagining defining a boolean, ‘count,’ that flips each time an arrow gets drawn, and is a requirement for the following, opposite arrow.  I saw your reference to a recursive variable in another thread, but wasn’t able to find documentation I could understand.  Any help would be appreciated.  Here’s my attempt (the doesn’t work):

def count = 1;

plot UP = high is greater than high from 1 bar ago
and high from 1 bar ago is greater than high from 2 bar ago
and high from 2 bar ago is greater than high from 3 bar ago
and count == 0;

plot DOWN = low is less than low from 1 bar ago
and low from 1 bar ago is less than low from 2 bar ago
and low from 2 bar ago is less than low from 3 bar ago
and count == 1;

UP.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);

DOWN.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

Marked as spam
Posted by (Questions: 8, Answers: 3)
Asked on June 16, 2019 5:18 pm
80 views
1
Private answer

You were correct. Recursion is the key. So for each up arrow instance we use recursion to set a variable to 1, then hold that value so long as no down bar is encountered. Once we hit any down bar the recursive variable gets set to zero and we then hold that value until the next up bar appears. Likewise for the down bar tracking.

Then for each of the original up signals, we first check to see if the previous bar's recursive variable for down bar tracking is set to 1 or zero. We only plot an up bar if that previous bar is equal to 1. Which means that last signal plotted on the chart was a down bar.

Sorry if that sounds really complicated. But this is the simplest definition I can provide.

Here is your code:

def up = high is greater than high from 1 bar ago and high from 1 bar ago is greater than high from 2 bar ago and high from 2 bar ago is greater than high from 3 bar ago;
def down = low is less than low from 1 bar ago and low from 1 bar ago is less than low from 2 bar ago and low from 2 bar ago is less than low from 3 bar ago;
rec trackUp = if up then 1 else if !down then trackUp[1] else 0;
rec trackDown = if down then 1 else if !up then trackDown[1] else 0;
plot upArrow = up and trackDown[1] == 1;
upArrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
plot downArrow = down and trackUp[1] == 1;
downArrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

Marked as spam
Posted by (Questions: 37, Answers: 4086)
Answered on June 18, 2019 1:14 pm