0

I'm making simple Swing High and Swing Low finder/alert and everything works grate, except there are a lot of cases that Swing High comes after another Swing high.

Logic is simple - a swing high is defined if two candles before have been lower than this one and two candles after are lower as well

What I want to achieve is to plot swing high only if previous plot has not been swing high.

Here will be the code

//@version=4
study("Swing Point Finder")

hhCondition = high[4] < high[2] and high[3] < high[2] and high[1] < high[2] and high < high[2]
llCondition = low[4] > low[2] and low[3] > low[2] and low[1] > low[2] and low > low[2]


plot(hhCondition ? 1 : 0, "Swing High found", color.green, offset = -2)
plot(llCondition ? 1 : 0, "Swing Low found", color.red, offset = -2)
PineCoders-LucF
  • 8,288
  • 2
  • 12
  • 21
  • I still have not found a solution for what I'm looking for here, but I found an interesting conditional I can add to this test - I can check is hhCondition highest value in lets say last 10 bars. `hhCondition = high[4] < high[2] and high[3] < high[2] and high[1] < high[2] and high < high[2] and high[2] > highest(nz(high[3]), 10)` That eliminates a lot of noise. Credit goes for this post https://stackoverflow.com/questions/53074111/highest-high-of-the-last-n-days-not-n-days-ago – Mārtiņš Līgotnis Jun 16 '20 at 21:29

2 Answers2

0

Maybe that you could try something like this:

plot(hhCondition and not hhCondition[1] ? 1 : 0, "Swing High found", color.green, offset = -2)
alexgrover
  • 1,023
  • 1
  • 4
  • 6
  • thanks for suggestion, but, sorry, no change in result. By the way is there array formed from plot cases at all? In documentation I can only find that [x] is only capable to hold a value, but in my case I need to find repeated candle pattern... – Mārtiņš Līgotnis Jun 16 '20 at 20:47
0

yea good catch, I've run into this same issue in multiple scripts I've written.

What'd I would suggest here is creating a variable that tracks all pivot points.

isPivot == hhCondition or llCondition

Then, on a new pivot, compare it against the previous one. something like this for example:

plotThisHigh = hhCondition
if (hhCondition)
    sameDirection = valuewhen(isPivot, hhCondition, 0)
    plotThisHigh := sameDirection ? false : true

duplicate this for checking previous pivot lows as well. I'd recommend using a different variable for plotting (plotThisHigh as opposed to resassigning hhCondition) because if there are say three hhConditions in a row (with no llConditions in between) then if you reassign hhCondition the logic will no longer work. does that make sense?

I've written more details in this blog post if you need...hope it helps https://marketscripters.com/how-to-work-with-pivots-in-pine-script/

André
  • 380
  • 4
  • 9