Automatic dynamic support resistance

#Defines Support resistance areas, good for scalping reversals or continuations.  Untested, and forgetful of the author

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);

No comments :

Post a Comment