Monday, February 29, 2016



# Plot areas of potential support / resistance based on major peaks and valleys.
# Changing "magnitude" determines the granularity of detected peaks or valleys.
# A low magnitude value will plot minor price swings, while a high magnitude value
# will only plot major price swings.
# A magnitude value of 2 means that a high must be greater than the 2 candles 
# before and after it to be considered a peak.  Likewise for the lows to be a valley.
# http://www.researchtrade.com/forum/read.php?7,2258,page=18
# Robert Payne


input magnitude = 5;

# define and plot the most recent peak
def peak = high >= Highest(high[1], magnitude) and high >= Highest(high[-magnitude], magnitude);
def peakvalue = if BarNumber() < magnitude then Double.NaN else if peak then high else peakvalue[1];
plot peakline = peakvalue;
     peakline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
     peakline.SetDefaultColor(Color.GREEN);

# extend the current peak line to the right edge of the chart
def countp = if IsNaN(peak) and !IsNaN(peak[1]) then 1 else countp[1] + 1;
plot peakext = if IsNaN(peak) then GetValue(peakline, countp) else Double.NaN;
     peakext.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
     peakext.SetDefaultColor(Color.GREEN);

# continue the previous peak as a dashed line
def oldpeak = if BarNumber() < magnitude then Double.NaN else if peak then peakvalue[1] else oldpeak[1];
plot oldpeakline = oldpeak;
     oldpeakline.SetPaintingStrategy(PaintingStrategy.DASHES);
     oldpeakline.SetDefaultColor(Color.GREEN);

# define and plot the most recent valley
def valley = low <= Lowest(low[1], magnitude) and low <= Lowest(low[-magnitude], magnitude);
def valleyValue = if BarNumber() < magnitude then Double.NaN else if valley then low else valleyValue[1];
plot valleyline = valleyValue;
     valleyline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
     valleyline.SetDefaultColor(Color.PINK);

# extend the current valley line to the right edge of the chart
def countt = if IsNaN(valley) and !IsNaN(valley[1]) then 1 else countt[1] + 1;
plot valleyext = if IsNaN(valley) then GetValue(valleyline, countt) else Double.NaN;
     valleyext.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
     valleyext.SetDefaultColor(Color.PINK);

# continue the previous valley as a dashed line
def oldvalley = if BarNumber() < magnitude then Double.NaN else if valley then valleyValue[1] else oldvalley[1];
plot oldvalleyline = oldvalley;
     oldvalleyline.SetPaintingStrategy(PaintingStrategy.DASHES);
     oldvalleyline.SetDefaultColor(Color.PINK);

0 comments: