#.........................................
# ref: peak/valley code , robert post#10
# https://usethinkscript.com/threads/zigzag-high-low-with-supply-demand-zones-for-thinkorswim.172/#post-7048
#.........................................
#mod atcsam
# Inputs
input fast = 5;
input fasttype = AverageType.SIMPLE;
input slow = 10;
input slowtype = AverageType.SIMPLE;
input displace = 0;
input showBreakoutSignals = no;
input addlabels = yes;
#input agg = aggregationPeriod.THIRTY_MIN;
def price1 = close;#(period=agg);
def price2 = close;#(period=agg);
## SMA plots
plot SMAFast = MovingAverage(fasttype, price1[-displace], fast);
plot SMASlow = MovingAverage(slowtype, price2[-displace], slow);
# Signals plots
plot UpSignal = SMAFast crosses above SMASlow;
plot DownSignal = SMAFast crosses below SMASlow;
UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);
## Look and feel settings
SMAFast.SetDefaultColor(GetColor(1));
SMASlow.SetDefaultColor(GetColor(3));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
SMAFast.AssignValueColor(if SMAFast >= SMASlow then Color.YELLOW else Color.YELLOW);
SMASlow.AssignValueColor(if SMAFast >= SMASlow then Color.DARK_GREEN else Color.RED);
SMASlow.SetLineWeight(3);
AddLabel(addlabels, if SMAFast >= SMASlow then "System UP" else "System DOWN", if SMAFast >= SMASlow then Color.GREEN else Color.RED);
### Peak and Valley code
input min_percent_change = 5;
input length = 10;
def na = Double.NaN;
def bn = BarNumber();
def lastBar = HighestAll(if IsNaN(close) then 0 else bn);
def offset = Min(length - 1, lastBar - bn);
def peak = high > Highest(high[1], length - 1) and high == GetValue(Highest(high, length), -offset);
def valley = low < Lowest(low[1], length - 1) and low == GetValue(Lowest(low, length), -offset);
# set to 0 if peak and valley happen on same bar
def pv = if bn == 1 then 0
else if peak and valley then 0
else if peak then 1
else if valley then -1
else pv[1];
def peak2 = if bn == 1 then 0
else if peak and pv[1] != -1 then 0
else peak;
def valley2 = if bn == 1 then 0
else if valley and pv[1] != 1 then 0
else valley;
input color_peak_valley_bars = yes;
input color_remaining_bars = yes;
AssignPriceColor(
if (peak and color_peak_valley_bars) then Color.LIME
else if (valley and color_peak_valley_bars) then Color.MAGENTA
else if (peak and !color_peak_valley_bars) then Color.CURRENT
else if (valley and !color_peak_valley_bars) then Color.CURRENT
else if color_remaining_bars then Color.GRAY
else Color.CURRENT);
def peak_line = if bn == 1 then 0
else if peak2 then close
else if valley2[1] then 0
else peak_line[1];
def valley_line = if bn == 1 then 0
else if valley2 then close
else if peak2[1] then 0
else valley_line[1];
plot zp = if peak_line > 0 then peak_line else na;
zp.SetDefaultColor(Color.RED);
zp.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
plot zv = if valley_line > 0 then valley_line else na;
zv.SetDefaultColor(Color.GREEN);
zv.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
#----------------------------
input gain_drop_stats = yes;
def gain = if pv[1] == 0 then 0
else if peak2 then Round(close - valley_line, 2)
else 0;
def gain_per = if pv[1] == 0 then 0 else Round( 100 * gain / valley_line, 1);
def is_gain_big = (gain_per >= min_percent_change);
AddChartBubble((gain_drop_stats and peak2 and valley_line > 0 and gain_per > 1.30), high,
gain + "\n" +
gain_per + " %"
, ( if is_gain_big then Color.GREEN else Color.GRAY), yes);
#----------------------------
def drop = if pv[1] == 0 then 0
else if valley2 then Round(peak_line - close, 2)
else 0;
def drop_per = if pv[1] == 0 then 0 else Round( 100 * drop / peak_line, 1);
def is_drop_big = (drop_per >= min_percent_change);
AddChartBubble((gain_drop_stats and valley2 and peak_line > 0 and drop_per > 1.30), low,
drop + "\n" +
drop_per + " %"
, ( if is_drop_big then Color.RED else Color.GRAY), no);
#
AddLabel(yes, if (SMAFast >= SMASlow and zp < SMASlow) then "Poss. Entry on Pullback" else " ", if (SMAFast >= SMASlow and zp < SMASlow) then Color.GREEN else Color.RED);
AddLabel(yes, if (SMAFast < SMASlow and zv > SMASlow) then "Poss. Entry on Pullback" else " ", if (SMAFast < SMASlow and zv > SMASlow) then Color.RED else Color.GREEN);
def Buy = price1 >= SMAFast and SMAFast >= SmaFast [1];
def Sell = Price1 <= SMAFast and SMAFast <= SMAFast [1];
#AddOrder(OrderType.BUY_TO_OPEN, Buy);
#AddOrder(OrderType.SELL_TO_CLOSE, Sell);
#Alerts
Alert(UpSignal, "System Up!", Alert.BAR, Sound.Ding);
Alert(DownSignal, "System Down!", Alert.BAR, Sound.Ding);
# End Code