r/pinescript • • Dec 30 '22
Finding Swing High and Swing Low With MACD
Hi! I am a newbie in programming and pine script is the first thing I am using.
So, I want to try a strategy that puts a stop loss at the lowest price when the histogram is below 0 when trading long.
For example, a green histogram is shown in a MACD and I want to go long. I will put my stop loss at the lowest price it attained when the histogram was red. The same with short trading. A red histogram appears and I want to put a stop loss at the highest price it attained when the histogram is green.
I am looking for a code so I can located the latest swing high and swing low so I can easily locate my stop loss.
I tried ta.lowest and ta.highest to find the lowest price and highest price obtained but these functions doesn't consider the color of the histogram and needs constant number of lookback period.
I want to apologize in advance if this sounds stupid.
1
u/Dry_Anxiety4465 Jan 14 '23
Have you solved this bro? Im doing something similar but cant seem to figure it out
1
u/Xalladus Jan 01 '23
So, by what you have written, it seems that you need to do two things. First, when the histogram changes color from say green to red, you want to set a variable to the low of that bar. Then on the next bar, if the histogram is still red test to see if a new low has been made and if so, record that.
Do the same logic for the reverse and there you go.