QuantBot 3:Ultimate MA Crossover
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © TheQuantBot

//@version=4
study("QuantBot 3:Ultimate MA Crossover",overlay = true)

mav = input(title="Moving Average Type", defval="SMA", options=["SMA", "EMA", "HMA", "WMA", "TMA", "VAR", "WWMA", "ZLEMA", "TSF"],group = "Fast Moving Average",inline = "a")
length =input(20, "Moving Average Length", minval=1,group = "Fast Moving Average")
color_ = input(color.maroon,"",inline = "a",group = "Fast Moving Average")
src = input(close,"source",group = "Fast Moving Average")

mav2 = input(title="Moving Average Type", defval="SMA", options=["SMA", "EMA", "HMA", "WMA", "TMA", "VAR", "WWMA", "ZLEMA", "TSF"],group = "Slow Moving Average",inline = "b")
length2 =input(50, "Moving Average Length", minval=1,group = "Slow Moving Average")
color2_ = input(color.purple,"",inline = "b",group = "Slow Moving Average")
src2 = input(close,"source",group = "Slow Moving Average")

valpha=2/(length+1)
vud1=src>src[1] ? src-src[1] : 0
vdd1=src<src[1] ? src[1]-src : 0
vUD=sum(vud1,9)
vDD=sum(vdd1,9)
vCMO=nz((vUD-vDD)/(vUD+vDD))
VAR=0.0
VAR:=nz(valpha*abs(vCMO)*src)+(1-valpha*abs(vCMO))*nz(VAR[1])
wwalpha = 1/ length
WWMA = 0.0
WWMA := wwalpha*src + (1-wwalpha)*nz(WWMA[1])
zxLag = length/2==round(length/2) ? length/2 : (length - 1) / 2
zxEMAData = (src + (src - src[zxLag]))
ZLEMA = ema(zxEMAData, length)
lrc = linreg(src, length, 0)
lrc1 = linreg(src,length,1)
lrs = (lrc-lrc1)
TSF = linreg(src, length, 0)+lrs
getMA(src, length) =>
    ma = 0.0
    if mav == "SMA"
        ma := sma(src, length)
        ma

    if mav == "HMA"
        ma := hma(src, length)
        ma
        
    if mav == "EMA"
        ma := ema(src, length)
        ma

    if mav == "WMA"
        ma := wma(src, length)
        ma

    if mav == "TMA"
        ma := sma(sma(src, ceil(length / 2)), floor(length / 2) + 1)
        ma

    if mav == "VAR"
        ma := VAR
        ma

    if mav == "WWMA"
        ma := WWMA
        ma

    if mav == "ZLEMA"
        ma := ZLEMA
        ma

    if mav == "TSF"
        ma := TSF
        ma
    ma

getMA2(src, length) =>
    ma = 0.0
    if mav2 == "SMA"
        ma := sma(src, length)
        ma

    if mav2 == "HMA"
        ma := hma(src, length)
        ma
        
    if mav2 == "EMA"
        ma := ema(src, length)
        ma

    if mav2 == "WMA"
        ma := wma(src, length)
        ma

    if mav2 == "TMA"
        ma := sma(sma(src, ceil(length / 2)), floor(length / 2) + 1)
        ma

    if mav2 == "VAR"
        ma := VAR
        ma

    if mav2 == "WWMA"
        ma := WWMA
        ma

    if mav2 == "ZLEMA"
        ma := ZLEMA
        ma

    if mav2 == "TSF"
        ma := TSF
        ma
    ma


MAvg1=getMA(src, length)
ma1 = plot(MAvg1,title = "Fast Moving Average",linewidth = 2,color = color_)

MAvg2=getMA2(src2, length2)
ma2 = plot(MAvg2,title = "Slow Moving Average",linewidth = 3,color = color2_)

fillcolor = MAvg1>MAvg2?color.green:color.red
fill(ma1,ma2,color = fillcolor ,transp = 85)

long = MAvg1 > MAvg2 and MAvg1[1] < MAvg2[1]
short = MAvg1 < MAvg2 and MAvg1[1] > MAvg2[1]


////////////////////////////////////////////////////////////////////////////////
buyline = 0.0
sellline = 0.0 
strategy_position_avg_price = 0.0
strategy_position_size = 0
longtpline = 0.0
shorttpline = 0.0

broker = input("Angel Broking","Broker",options = ["Alice Blue","Angel Broking","Fyers","Profitmart","Upstox","Zerodha","Paper Trading"],inline = "l1",group = "|--------- QuantBot Automation Settings --------| |============================================||=== Help +91 8016200637 (Whatsapp, Telegram) |")
segment = input("Equity","Segment",options = ["Equity","Futures","Options","Commodity"],inline = "l1",group = "|--------- QuantBot Automation Settings --------| |============================================||=== Help +91 8016200637 (Whatsapp, Telegram) |")
exchange = syminfo.prefix
strike = "Automatic"//,"Strike",options = ["Automatic","Manual"])
quantity = input(100,"Quantity (IMPORTANT: Enter no of Lots for Futures and options)",minval = 1,group = "|--------- QuantBot Automation Settings --------| |============================================||=== Help +91 8016200637 (Whatsapp, Telegram) |")
pmd = input(false,"Pyramiding",group = "|--------- QuantBot Automation Settings --------| |============================================||=== Help +91 8016200637 (Whatsapp, Telegram) |"),pyramid = pmd == false?"False":"True"
ticker = syminfo.ticker
ltp = tostring(close)
action = strategy_position_size > strategy_position_size[1]?"Buy":strategy_position_size < strategy_position_size[1]?"Sell":"Default Value"//-------------------------------------------------------------------------------------
net_position = strategy_position_size > 0 ? "Long":strategy_position_size < 0? "Short":"Flat" 
validity = input("Day","Validity",options = ["Day","IOC"],inline = "l1",group = "|--------- QuantBot Automation Settings --------| |============================================||=== Help +91 8016200637 (Whatsapp, Telegram) |")
variety = input("Regular","Order Type ----------------",options = ["Regular","Bracket","Cover","AMO"],inline = "op",group = "|--------- QuantBot Automation Settings --------| |============================================||=== Help +91 8016200637 (Whatsapp, Telegram) |")
product_type = input("Intraday","",options = ["Intraday","Delivery"],inline = "op",group = "|--------- QuantBot Automation Settings --------| |============================================||=== Help +91 8016200637 (Whatsapp, Telegram) |")
type = input("Market","", options = ["Market","Limit","Stop-market","Stop-limit"],inline = "op",group = "|--------- QuantBot Automation Settings --------| |============================================||=== Help +91 8016200637 (Whatsapp, Telegram) |")
limit_price = input(0.0,"Limit Price ------(Only for Limit/SL order*)",inline = "lp",group = "|--------- QuantBot Automation Settings --------| |============================================||=== Help +91 8016200637 (Whatsapp, Telegram) |")
stop_loss = input(0.0,"Stop Loss --(Only For BO,CO, SL-M and SL*)",inline = "sl",group = "|--------- QuantBot Automation Settings --------| |============================================||=== Help +91 8016200637 (Whatsapp, Telegram) |")
take_profit = input(0.0,"Take Profit -------(Only For BO Order type*)",inline = "tp",group = "|--------- QuantBot Automation Settings --------| |============================================||=== Help +91 8016200637 (Whatsapp, Telegram) |")
lpp = input(false,"Percentage (%)",inline = "lp",group = "|--------- QuantBot Automation Settings --------| |============================================||=== Help +91 8016200637 (Whatsapp, Telegram) |")
limit_price_percentage = lpp?"True":"False"
slp = input(false,"Percentage (%)",inline = "sl",group = "|--------- QuantBot Automation Settings --------| |============================================||=== Help +91 8016200637 (Whatsapp, Telegram) |")
stop_loss_percentage = slp?"True":"False"
tpp = input(false,"Percentage (%)",inline = "tp",group = "|--------- QuantBot Automation Settings --------| |============================================||=== Help +91 8016200637 (Whatsapp, Telegram) |")
take_profit_percentage = tpp?"True":"False"
near_by = input(100,"Minimum difference between two strike prices",group = "|--------- QuantBot Automation Settings --------| |============================================||=== Help +91 8016200637 (Whatsapp, Telegram) |")
diff_atm = input(1,"Distance from ATM option",group = "|--------- QuantBot Automation Settings --------| |============================================||=== Help +91 8016200637 (Whatsapp, Telegram) |")
option_side = input("Buy","Option Buy or sell",options = ["Buy","Sell"],group = "|--------- QuantBot Automation Settings --------| |============================================||=== Help +91 8016200637 (Whatsapp, Telegram) |")
expiry = input("Current Week","Expiry",options = ["Current Week","Next Week", "Current Month", "Next Month"],group = "|--------- QuantBot Automation Settings --------| |============================================||=== Help +91 8016200637 (Whatsapp, Telegram) |")


if long[1]
    strategy_position_size := quantity
else if short[1]
    strategy_position_size := -quantity
else if short[1] and false // only long Exit
    strategy_position_size := 0
else if long[1] and false // only short exit
    strategy_position_size := 0
else
    strategy_position_size := strategy_position_size[1]




onlylong = long[1] and strategy_position_size[1] == 0
onlyshort = short[1] and strategy_position_size[1] == 0

onlylongexit = false
onlyshortexit = false

shortexitlong = long[1] and strategy_position_size[1] < 0
longexitshort = short[1] and strategy_position_size[1] > 0


// Automation alret handeling

if onlylong
    strategy_position_avg_price := close[1]
else if shortexitlong
    strategy_position_avg_price := close[1]
else if onlyshort
    strategy_position_avg_price := close[1]
else if longexitshort
    strategy_position_avg_price := close[1]
else
    strategy_position_avg_price := strategy_position_avg_price[1]

// plot(strategy_position_avg_price)



if onlylong
    //Send alert for buy
    alert('{"broker":"'+broker+'","segment":"'+segment+'","exchange":"'+exchange +'","strike":"'+strike+'","quantity":'+tostring(quantity)+',"pyramid":"'+pyramid+'","ticker":"'+syminfo.ticker+'","ltp":'+tostring(close)+',"action":"'+"Buy"+'","net_position":"'+"Long"+'","validity":"'+validity+'","variety":"'+variety+'","product_type":"'+product_type+'","type":"'+type+'","limit_price_percentage":"'+limit_price_percentage+'","stop_loss_percentage":"'+stop_loss_percentage+'","take_profit_percentage":"'+take_profit_percentage+'","limit_price":'+tostring(limit_price)+',"stop_loss":'+tostring(stop_loss)+',"take_profit":'+tostring(take_profit)+',"option_side":"'+option_side+'","near_by":'+tostring(near_by)+',"diff_atm":'+tostring(diff_atm)+',"expiry":"'+ expiry +'"}')

else if shortexitlong
    // send alert for short exit and make flat
    alert('{"broker":"'+broker+'","segment":"'+segment+'","exchange":"'+exchange +'","strike":"'+strike+'","quantity":'+tostring(2*quantity)+',"pyramid":"'+pyramid+'","ticker":"'+syminfo.ticker+'","ltp":'+tostring(close)+',"action":"'+"Buy"+'","net_position":"'+"Long"+'","validity":"'+validity+'","variety":"'+variety+'","product_type":"'+product_type+'","type":"'+type+'","limit_price_percentage":"'+limit_price_percentage+'","stop_loss_percentage":"'+stop_loss_percentage+'","take_profit_percentage":"'+take_profit_percentage+'","limit_price":'+tostring(limit_price)+',"stop_loss":'+tostring(stop_loss)+',"take_profit":'+tostring(take_profit)+',"option_side":"'+option_side+'","near_by":'+tostring(near_by)+',"diff_atm":'+tostring(diff_atm)+',"expiry":"'+ expiry +'"}')

else if onlyshort
    //Send alert for sell 
    alert('{"broker":"'+broker+'","segment":"'+segment+'","exchange":"'+exchange +'","strike":"'+strike+'","quantity":'+tostring(quantity)+',"pyramid":"'+pyramid+'","ticker":"'+syminfo.ticker+'","ltp":'+tostring(close)+',"action":"'+"Sell"+'","net_position":"'+"Short"+'","validity":"'+validity+'","variety":"'+variety+'","product_type":"'+product_type+'","type":"'+type+'","limit_price_percentage":"'+limit_price_percentage+'","stop_loss_percentage":"'+stop_loss_percentage+'","take_profit_percentage":"'+take_profit_percentage+'","limit_price":'+tostring(limit_price)+',"stop_loss":'+tostring(stop_loss)+',"take_profit":'+tostring(take_profit)+',"option_side":"'+option_side+'","near_by":'+tostring(near_by)+',"diff_atm":'+tostring(diff_atm)+',"expiry":"'+ expiry +'"}')

else if longexitshort
    // send alert for long exit and make flat
    alert('{"broker":"'+broker+'","segment":"'+segment+'","exchange":"'+exchange +'","strike":"'+strike+'","quantity":'+tostring(2*quantity)+',"pyramid":"'+pyramid+'","ticker":"'+syminfo.ticker+'","ltp":'+tostring(close)+',"action":"'+"Sell"+'","net_position":"'+"Short"+'","validity":"'+validity+'","variety":"'+variety+'","product_type":"'+product_type+'","type":"'+type+'","limit_price_percentage":"'+limit_price_percentage+'","stop_loss_percentage":"'+stop_loss_percentage+'","take_profit_percentage":"'+take_profit_percentage+'","limit_price":'+tostring(limit_price)+',"stop_loss":'+tostring(stop_loss)+',"take_profit":'+tostring(take_profit)+',"option_side":"'+option_side+'","near_by":'+tostring(near_by)+',"diff_atm":'+tostring(diff_atm)+',"expiry":"'+ expiry +'"}')

else if onlylongexit 
    //Send alert for buy
    alert('{"broker":"'+broker+'","segment":"'+segment+'","exchange":"'+exchange +'","strike":"'+strike+'","quantity":'+tostring(quantity)+',"pyramid":"'+pyramid+'","ticker":"'+syminfo.ticker+'","ltp":'+tostring(close)+',"action":"'+"Sell"+'","net_position":"'+"Flat"+'","validity":"'+validity+'","variety":"'+variety+'","product_type":"'+product_type+'","type":"'+type+'","limit_price_percentage":"'+limit_price_percentage+'","stop_loss_percentage":"'+stop_loss_percentage+'","take_profit_percentage":"'+take_profit_percentage+'","limit_price":'+tostring(limit_price)+',"stop_loss":'+tostring(stop_loss)+',"take_profit":'+tostring(take_profit)+',"option_side":"'+option_side+'","near_by":'+tostring(near_by)+',"diff_atm":'+tostring(diff_atm)+',"expiry":"'+ expiry +'"}')

else if onlyshortexit
    //Send alert for buy
    alert('{"broker":"'+broker+'","segment":"'+segment+'","exchange":"'+exchange +'","strike":"'+strike+'","quantity":'+tostring(quantity)+',"pyramid":"'+pyramid+'","ticker":"'+syminfo.ticker+'","ltp":'+tostring(close)+',"action":"'+"Buy"+'","net_position":"'+"Flat"+'","validity":"'+validity+'","variety":"'+variety+'","product_type":"'+product_type+'","type":"'+type+'","limit_price_percentage":"'+limit_price_percentage+'","stop_loss_percentage":"'+stop_loss_percentage+'","take_profit_percentage":"'+take_profit_percentage+'","limit_price":'+tostring(limit_price)+',"stop_loss":'+tostring(stop_loss)+',"take_profit":'+tostring(take_profit)+',"option_side":"'+option_side+'","near_by":'+tostring(near_by)+',"diff_atm":'+tostring(diff_atm)+',"expiry":"'+ expiry +'"}')



plotshape(onlylong?close:na,title = "Long Entry",location = location.belowbar,color = color.green,text = "Long Entry",textcolor = color.white,size = size.normal,style = shape.labelup)
plotshape(shortexitlong?close:na,title = "Short Exit and Long Entry",location = location.belowbar,color = color.green,text = "Short Exit and Long Entry",textcolor = color.white,size = size.normal,style = shape.labelup)
plotshape(onlyshortexit?close:na,title = "Short Exit",location = location.belowbar,color = color.green,text = "Short Exit",textcolor = color.white,size = size.normal,style = shape.labelup)

plotshape(onlyshort ?close:na,title = "Short Entry",location = location.abovebar,color = color.red,text = "Short Entry",textcolor = color.white,size = size.normal,style = shape.labeldown)
plotshape(longexitshort?close:na,title = "Long Exit and Short Entry",location = location.abovebar,color = color.red,text = "Long Exit and Short Entry",textcolor = color.white,size = size.normal,style = shape.labeldown)
plotshape(onlylongexit?close:na,title = "Long Exit",location = location.abovebar,color = color.red,text = "Long Exit",textcolor = color.white,size = size.normal,style = shape.labeldown)
