Swing Stock Market Multi MA Correlation 
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © exlux99

//@version=4
strategy(title = "Multi MA Correlation Stocks", overlay = true,initial_capital = 1000, default_qty_type= strategy.percent_of_equity, default_qty_value = 100, commission_type=strategy.commission.percent,commission_value=0.01)

useha    = input(false, title="Use Heikin Ashi Candles in Algo Calculations")
usesp500    = input(true, title="Use SP500 Candles in Algo Calculations")
usenasdaq    = input(false, title="Use Nasdaq Candles in Algo Calculations")

//
// === /INPUTS ===

// === BASE FUNCTIONS ===

haClose = useha ? security(heikinashi(syminfo.tickerid), timeframe.period, close) : usesp500 ? security(("TVC:SPX"), timeframe.period, close) : usenasdaq ? security(("TVC:NDX"), timeframe.period, close) :close
haOpen  = useha ? security(heikinashi(syminfo.tickerid), timeframe.period, open) : usesp500 ? security(("TVC:SPX"), timeframe.period, open) : usenasdaq ? security(("TVC:NDX"), timeframe.period, open) :open
haHigh  = useha ? security(heikinashi(syminfo.tickerid), timeframe.period, high) : usesp500 ? security(("TVC:SPX"), timeframe.period, high) : usenasdaq ? security(("TVC:NDX"), timeframe.period, high) :high
haLow   = useha ? security(heikinashi(syminfo.tickerid), timeframe.period, low) : usesp500 ? security(("TVC:SPX"), timeframe.period, low) : usenasdaq ? security(("TVC:NDX"), timeframe.period, low) :low


average = (haClose+haOpen+haHigh+haLow)/4

ma(source, length, type) =>
    type == "SMA" ? sma(source, length) : 
     type == "EMA" ? ema(source, length) :
     type == "SMMA (RMA)" ? rma(source, length) : 
     type == "WMA" ? wma(source, length) :
     type == "VWMA" ? vwma(source, length) :
     na
    
ma1_type   = input("SMMA (RMA)"  , ""     , inline="MA #1", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"])
ma1_source = average
ma1_length = input(20     , ""     , inline="MA #1", minval=1)
ma1 = ma(ma1_source, ma1_length, ma1_type)


ma2_type   = input("EMA"  , ""     , inline="MA #2", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"])
ma2_source = average
ma2_length = input(50     , ""     , inline="MA #2", minval=1)
ma2 = ma(ma2_source, ma2_length, ma2_type)

ma3_type   = input("WMA"  , ""     , inline="MA #3", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"])
ma3_source = average
ma3_length = input(100    , ""     , inline="MA #3", minval=1)
ma3 = ma(ma3_source, ma3_length, ma3_type)


long= ma1>ma2 and ma2>ma3
short=ma1<ma2 and ma2<ma3

longexit=ma1<ma2 and ma2<ma3
shortexit=ma1>ma2 and ma2>ma3

longEntry=input(true, title="Long Entries ?")
shortEntry=input(true, title="Short Entries ?")

if(longEntry)
    strategy.entry("long",1,when=long)
    strategy.close("long",when=longexit)
    
if(shortEntry)
    strategy.entry("short",0,when=short)
    strategy.close("short",when=shortexit)


