Secretariat
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © MrBadger

//@version=5
indicator("Secretariat",overlay=true)

// Inputs
high_low_lookback = input(50,"High/Low lookback")
locationForTrendState = input("bottom", "Location for trend state if visible", "location for trendstate if visible")
showTrendState = input(true, "Trend state")
allowEntriesOffFastMA = input(false, "Allow aggresive entries (off fast MA)")
debugMarkers = input(false,"Show debug markers")

//Set MA's
maFast = ta.ema(close,9)
maSlow = ta.wma(close,30)


// Plot MAS
plot(maFast,color=color.red,title="secritariate fast")
plot(maSlow,color=color.navy,title="secritariate slow")


// Bullsih or bearish according to moving averages
maBullish = (maFast > maSlow) 
maBearish = (maFast < maSlow)

// Plot the trendstate
plotshape((maBullish and showTrendState), color=color.green,  location = (locationForTrendState == "bottom" ? location.bottom : location.top), size=size.tiny)
plotshape((maBearish and showTrendState), color=color.red, location =(locationForTrendState == "bottom" ? location.bottom : location.top), size=size.tiny)

//movingStop = maBullish ? ta.lowest(low,12) : ta.highest(high,12)

//plot(movingStop, color=(maBullish ? color.orange : color.blue))
// Should we draw dots?

bool lowestInLookback = na
bool highestInLookback = na

lowestInLookback := (low <= ta.lowest(low,high_low_lookback) ? true : false ) and maBearish
highestInLookback := (high >= ta.highest(high,high_low_lookback)  ? true : false) and maBullish
    


plotshape(lowestInLookback, title="Low dots", style=shape.circle, color=color.red, location=location.belowbar, size=size.tiny)
plotshape(highestInLookback, title="High dots", style=shape.circle, color=color.blue, location=location.abovebar, size=size.tiny)

var string dotStatus = na

sellEntryOnSlow = false
buyEntryOnSlow = false
sellEntryOnFast = false
buyEntryOnFast = false

if lowestInLookback and maBearish
    dotStatus := "PENDING SELL"

if highestInLookback and maBullish
    dotStatus := "PENDING BUY"


if ta.crossunder(close,maSlow) 
    if dotStatus == "PENDING SELL" 
        if close < maSlow
            sellEntryOnSlow := true
            dotStatus :=  "SELL"

if ta.crossover(close,maSlow) 
    if dotStatus == "PENDING BUY" 
        if close > maSlow
            buyEntryOnSlow := true
            dotStatus := "BUY"



if ta.crossunder(close,maFast) and dotStatus == "PENDING SELL" and allowEntriesOffFastMA  and (close < maFast)
    sellEntryOnFast := true 
    dotStatus :=  "SELL"

if ta.crossover(close,maFast) and dotStatus == "PENDING BUY" and allowEntriesOffFastMA and (close > maFast)
    buyEntryOnFast := true
    dotStatus := "BUY"

if (dotStatus == "PENDING BUY" or dotStatus == "BUY" ) and ta.crossunder(maFast,maSlow)
    dotStatus := "NO TRADE"

if (dotStatus == "PENDING SELL" or dotStatus == "SELL") and ta.crossover(maFast,maSlow)
    dotStatus := "NO TRADE"
    
plotshape((ta.crossunder(maFast,maSlow) and debugMarkers  ? maFast : na), title="cancel buys", style=shape.xcross, color=color.red, location=location.absolute)
plotshape((ta.crossover(maFast,maSlow) and debugMarkers  ? maFast  :na), title="cancel sells", style=shape.xcross, color=color.green, location=location.absolute)

plotshape((dotStatus == "PENDING SELL" and debugMarkers ? maFast : na), title="pending sell", style=shape.diamond, color=color.red, location=location.absolute)
plotshape((dotStatus == "PENDING BUY" and debugMarkers ? maFast :na), title="pending buy", style=shape.diamond, color=color.green, location=location.absolute)

// TRADE SIGNALS

sellEntryCandle = (dotStatus == "SELL")
buyEntryCandle =  (dotStatus == "BUY")

plotshape(sellEntryCandle, "Sell signal", style=shape.triangledown, location=location.abovebar, color=color.red, size=size.tiny)
plotshape(buyEntryCandle, "Buy signal", style=shape.triangleup, location=location.belowbar, color=color.green, size=size.tiny)
// END TRADES SIGNALS
alertcondition(sellEntryOnFast,"Secretariat aggressive sell","Secretariat bearish pull back to fast")
alertcondition(sellEntryOnSlow,"Secretariat sell","Secretariat bearish pull back to slow")
alertcondition(buyEntryOnFast,"Secretariat aggressive buy","Secretariat bullish pull back to fast")
alertcondition(buyEntryOnSlow,"Secretariat buy","Secretariat bullish pull back to slow")
dotStatus := ( dotStatus == "BUY" or dotStatus == "SELL" ? "NO TRADE" : dotStatus)
