Indicator
2024-11-20

;;;;;;;;;;;;;//@version=5
indicator("AT Indicator 1.0","",true,max_labels_count = 500)

//~~ Inputs {
int prd         = input.int(12,"Period",minval=1,inline="setting")
float mult      = input.float(2.0,"Mult",minval=0.1,step=.1,inline="setting", tooltip = "Set the Bollinger Band period. \n\nSet the multiplier.")
bool showZigZag = input.bool(true,"ZigZag",inline="zigzag")
string signal   = input.string("Signal","", ,inline="zigzag")
string dev      = input.string("ZigZag","", ,inline="zigzag", tooltip = "Enable the ZigZag Bollinger Signals. \n\nSelect if you only want to display the signals or the Peak Signal Distance between each signal. \n\nThe Signal Distance can be calculated using the ZigZag, High/Low, or Close.")


//~~ Types & Variables {
//Types
type ZigZag
    int     x1
    float   y1
    float   diff
type ATTrend
    float s
    color c
type Alerts
    bool Long = false
    bool Short = false
    bool LongTp = false
    bool ShortTp = false

var zz  = ZigZag.new(array.new(),array.new(),array.new())
var sbt = ATTrend.new(0.0,na)
alerted = Alerts.new() 
//Variables
int b      = bar_index
float bbup = ta.sma(high,prd)+ta.stdev(high,prd)*mult
float bbdn = ta.sma(low,prd)-ta.stdev(low,prd)*mult
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}

//~~ Methods{
//ZigZag
method zigzag(ZigZag z,c,p,l)=>
    y2 = dev=="ZigZag"?sbt.s:
         dev=="High/Low"?p:
         close
    if z.x1.size()>0
        x1 = z.x1.get(0)
        y1 = z.y1.get(0)
        z.diff.unshift(math.abs(y2-y1))
        // Commented out the line.new to hide the dotted lines
        // line.new(x1,y1,b,y2,color=color.new(color.gray,0),style=line.style_dashed)
        
        // Adjust the vertical positioning of the labels
        buy_label_y = low - 0.02 * (high - low)  // Position below the low of the candle
        sell_label_y = high + 0.02 * (high - low) // Position above the high of the candle
        
        // Set the label text to "BUY" or "SELL"
        label_text = l ? "BUY" : "SELL"
        label_y = l ? buy_label_y : sell_label_y
        
        // Set the label style
        label_style = l ? label.style_label_up : label.style_label_down
        
        label.new(b, label_y, label_text, color=c, size=size.small, style=label_style, textcolor=chart.bg_color)
    z.x1.unshift(b)
    z.y1.unshift(y2)
//ATTrend Calculation
method SBT(ATTrend s,cond,val,col,p,l)=>
    s.s := na(bbdn) or na(bbup)?0.0 : close>sbt.s?math.max(sbt.s,bbdn) : close<sbt.s?math.min(sbt.s,bbup) : 0.0
    if cond
        s.s := val
        s.c := col
        if showZigZag
            zz.zigzag(col,p,l)
        alerted.Long  := l?true:false
        alerted.Short := l?false:true
//Run Methods
sbt.SBT(ta.crossover(close,sbt.s),bbdn,color.lime,low,true)
sbt.SBT(ta.crossunder(close,sbt.s),bbup,color.red,high,false)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}

//~~ Alerts {
alertcondition(alerted.Long,"Long Alert","Long Signal")
alertcondition(alerted.Short,"Short Alert","Short Signal")
alertcondition(alerted.LongTp,"Long TP Alert","Long TP")
alertcondition(alerted.ShortTp,"Short TP Alert","Short TP")
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}

// Volume Weighted Average Price (VWAP)
vwapValue = ta.vwap(hlc3)
plot(vwapValue, color=color.blue, title="VWAP")