Moving Grid Trader - With Alerts
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © activedutyjosh
//@version=5
indicator("Moving Grid Trader - Alerts", "Moving Grid Trader - Alerts", overlay=true)

//direction check
x=color.orange
plotwidth= input(2), title="Width of EMA signel lines"
len = input(21, title="short Term EMA - Default 21")
src = input(close, title="Source")
offset = input(0, title="Offset")
out = ta.ema(src, len)
if(out[1] > out[5]) //check to see if the trend is up
    x:=color.green
else
    x:=color.red
//plot(out, color=x, linewidth=plotwidth, title="Long MA", offset=offset)

y=color.orange
len2 = input(100, title="long Term EMA - Default 100")
out2 = ta.ema(src, len2)
if(out2[1] > out2[5]) //check to see if the trend is up
    y:=color.green
else
    y:=color.red
//plot(out2, color=y, linewidth=plotwidth, title="Long MA", offset=offse

    //macd portion
fast_length = input(title="Fast Length", defval=12)
slow_length = input(title="Slow Length", defval=26)
src2 = input(title="Source", defval=close)
signal_length = input.int(title="Signal Smoothing",  minval = 1, maxval = 50, defval = 9)
sma_source = input.string(title="Oscillator MA Type",  defval="EMA", options=["SMA", "EMA"])
sma_signal = input.string(title="Signal Line MA Type", defval="EMA", options=["SMA", "EMA"])

        // Plot colors
col_macd = input(#2962FF, "MACD Line  ", group="Color Settings", inline="MACD")
col_signal = input(#FF6D00, "Signal Line  ", group="Color Settings", inline="Signal")
col_grow_above = input(#12FF05, "Above   Grow", group="Histogram", inline="Above")
col_fall_above = input(#E30909, "Fall", group="Histogram", inline="Above")
col_grow_below = input(#12FF05, "Below Grow", group="Histogram", inline="Below")
col_fall_below = input(#E30909, "Fall", group="Histogram", inline="Below")
        
        // Calculating
fast_ma = sma_source == "SMA" ? ta.sma(src2, fast_length) : ta.ema(src2, fast_length)
slow_ma = sma_source == "SMA" ? ta.sma(src2, slow_length) : ta.ema(src2, slow_length)
macd = fast_ma - slow_ma
MACDsignal = sma_signal == "SMA" ? ta.sma(macd, signal_length) : ta.ema(macd, signal_length)
hist = macd - MACDsignal
//hline(0, "Zero Line", color=color.new(#787B86, 50))
//plot(hist, title="Histogram", style=plot.style_columns, color=(hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below)))
        //generate variable with the color of the histogram    
colorCheck=(hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below))       

var openorder = false
var buyposition=1000.23
minline = buyposition+(buyposition*0.00005) //0.5 cent for each 100 dollars invested
line1 = buyposition+(buyposition*0.0001) //1 cent for each 100 dollars invested
line2 = buyposition+(buyposition*0.0003) // 3 cent
line3 = buyposition+(buyposition*0.0005) // 5 cent
line4 = buyposition+(buyposition*0.0007) // 7 cent
line5 = buyposition+(buyposition*0.0009) // 9 cent
line6 = buyposition+(buyposition*0.0011) // 11 cent
line7 = buyposition+(buyposition*0.0013) // 13 
line8 = buyposition+(buyposition*0.0015) //15
line9 = buyposition+(buyposition*0.0017)
line10 = buyposition+(buyposition*0.0019)
line11 = buyposition+(buyposition*0.0021) //21
line12 = buyposition+(buyposition*0.0023)
line13 = buyposition+(buyposition*0.0025)
line14 = buyposition+(buyposition*0.0027)
line15 = buyposition+(buyposition*0.0029)
line16 = buyposition+(buyposition*0.0031)
line17 = buyposition+(buyposition*0.0033)
line18 = buyposition+(buyposition*0.0035)
line19 = buyposition+(buyposition*0.0037)
line20 = buyposition+(buyposition*0.0039)
line21 = buyposition+(buyposition*0.0041)
line22 = buyposition+(buyposition*0.0043)
line23 = buyposition+(buyposition*0.0045)
line24 = buyposition+(buyposition*0.0047)
line25 = buyposition+(buyposition*0.0049)
line26 = buyposition+(buyposition*0.0051)
line27 = buyposition+(buyposition*0.0053)
line28 = buyposition+(buyposition*0.0055) //55
line29 = buyposition+(buyposition*0.0057)
line30 = buyposition+(buyposition*0.0059)
line31 = buyposition+(buyposition*0.0061)
line32 = buyposition+(buyposition*0.0063)
line33 = buyposition+(buyposition*0.0065)
line34 = buyposition+(buyposition*0.0067)
line35 = buyposition+(buyposition*0.0069)
line36 = buyposition+(buyposition*0.0071)
line37 = buyposition+(buyposition*0.0071)
line38 = buyposition+(buyposition*0.0073)
line39 = buyposition+(buyposition*0.0075)
line40 = buyposition+(buyposition*0.0077)
line41 = buyposition+(buyposition*0.0079)
line42 = buyposition+(buyposition*0.0081)
line43 = buyposition+(buyposition*0.0083)
line44 = buyposition+(buyposition*0.0085)
line45 = buyposition+(buyposition*0.0087)
line46 = buyposition+(buyposition*0.0089)
line47 = buyposition+(buyposition*0.0091)
line48 = buyposition+(buyposition*0.0093)
line49 = buyposition+(buyposition*0.0095)
line50 = buyposition+(buyposition*0.0097) // 97 cents
line01 = buyposition+(buyposition*0.01) // 1 dollar
stoploss = (buyposition-(buyposition*0.02)) //2 percent stop loss

sell1 = (ta.crossunder (low, line1))
sell2 = (ta.crossunder(low, line2))
sell3 = (ta.crossunder(low, line3))
sell4 = (ta.crossunder(low, line4))
sell5 = (ta.crossunder(low, line5))
sell6 = (ta.crossunder(low, line6))
sell7 = (ta.crossunder(low, line7))
sell8 = (ta.crossunder(low, line8))
sell9 = (ta.crossunder(low, line9))
sell10 = (ta.crossunder(low, line10))
sell11 = (ta.crossunder(low, line11))
sell12 = (ta.crossunder(low, line12))
sell13 = (ta.crossunder(low, line13))
sell14 = (ta.crossunder(low, line14))
sell15 = (ta.crossunder(low, line15))
sell16 = (ta.crossunder(low, line16))
sell17 = (ta.crossunder(low, line17))
sell18 = (ta.crossunder(low, line18))
sell19 = (ta.crossunder(low, line19))
sell20 = (ta.crossunder(low, line20))
sell21 = (ta.crossunder(low, line21))
sell22 = (ta.crossunder(low, line22))
sell23 = (ta.crossunder(low, line23))
sell24 = (ta.crossunder(low, line24))
sell25 = (ta.crossunder(low, line25))
sell26 = (ta.crossunder(low, line26))
sell27 = (ta.crossunder(low, line27))
sell28 = (ta.crossunder(low, line28))
sell29 = (ta.crossunder(low, line29))
sell30 = (ta.crossunder(low, line30))
sell31 = (ta.crossunder(low, line31))
sell32 = (ta.crossunder(low, line32))
sell33 = (ta.crossunder(low, line33))
sell34 = (ta.crossunder(low, line34))
sell35 = (ta.crossunder(low, line35))
sell36 = (ta.crossunder(low, line36))
sell37 = (ta.crossunder(low, line37))
sell38 = (ta.crossunder(low, line38))
sell39 = (ta.crossunder(low, line39))
sell40 = (ta.crossunder(low, line40))
sell41 = (ta.crossunder(low, line41))
sell42 = (ta.crossunder(low, line42))
sell43 = (ta.crossunder(low, line43))
sell44 = (ta.crossunder(low, line44))
sell45 = (ta.crossunder(low, line45))
sell46 = (ta.crossunder(low, line46))
sell47 = (ta.crossunder(low, line47))
sell48 = (ta.crossunder(low, line48))
sell49 = (ta.crossunder(low, line49))
sell50 = (ta.crossunder(low, line50))
sell01 = (ta.crossunder (low, line01))
sellminline = (ta.crossunder(low, minline))
sellstoploss = (ta.crossunder(low, stoploss))

//move grid up if the price keeps climbing
if (close > line50)
    buyposition := low-(low*0.00007)

//buy and sell conditions
buy=(close>open and openorder==false and (colorCheck==col_grow_below or colorCheck==col_grow_above))
sell = ((sellminline or sell01 or sell1 or sell2 or sell3 or sell4 or sell5 or sell6 or sell7 or sell8 or sell9 or sell10 or sell11 or sell12 or sell13 or sell14 or sell15 or sell16 or sell17 or sell18 or sell19 or sell20 or sell21 or sell22 or sell23 or sell24 or sell25 or sell26 or sell27 or sell28 or sell29 or sell30 or sell31 or sell32 or sell33 or sell34 or sell35 or sell36 or sell37 or sell37 or sell39 or sell40 or sell41 or sell42 or sell43 or sell44 or sell45 or sell46 or sell47 or sell48 or sell49 or sell50) and close<open and openorder)
soldstop=(sellstoploss and openorder==true)
eject= (low < stoploss and openorder==true)
sellalert = (sell or soldstop or eject)

//set buy persistant values
if (buy)
    openorder := true
    buyposition := high

//close order if sell alert detected
if (sellalert)
    openorder := false



//Alert conditions
alertcondition(buy, title='Buy Signal', message='{  "message_type": "bot",  "bot_id": 9614178,  "email_token": "73f54f38-a92b-4a8d-bce8-2e16cf28922b",  "delay_seconds": 0}')
alertcondition(sellalert, title='Sell Signal', message='{ "action": "close_at_market_price",  "message_type": "bot",  "bot_id": 9614178,  "email_token": "73f54f38-a92b-4a8d-bce8-2e16cf28922b",  "delay_seconds": 0 }')


//Plotting
plot(minline, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line1, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line2, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line3, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line4, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line5, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line6, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line7, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line8, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line9, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line10, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line10, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line12, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line13, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line14, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line15, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line16, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line17, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line18, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line19, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line20, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line21, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line22, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line23, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line24, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line25, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line26, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line27, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line28, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line29, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line30, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line31, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line32, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line33, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line34, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line35, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line36, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line37, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line38, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line39, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line40, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line41, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line42, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line43, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line44, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line44, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line45, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line46, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line47, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line48, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line49, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
//plot(line50, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
plot(stoploss, color=color.red, linewidth=plotwidth, title="Stoploss", offset=offset)
plot(line01, color=color.green, linewidth=plotwidth, title="Long MA", offset=offset)
plotshape(buy, style=shape.labelup, title="BUY", location=location.belowbar, color=color.white, text="BUY", size=size.auto)
plotshape(sell, style=shape.labeldown, location=location.abovebar, color=color.green, text="MONEY", size=size.auto)
plotshape(soldstop, style=shape.labelup, location = location.belowbar, color=color.red, text="Stopploss", size=size.auto)