Reputation: 7
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)
Upvotes: 0
Views: 6473
Reputation: 470
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/
Upvotes: 0
Reputation: 1043
Maybe that you could try something like this:
plot(hhCondition and not hhCondition[1] ? 1 : 0, "Swing High found", color.green, offset = -2)
Upvotes: 0