Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.1 kB
0
Indexable
Never
//@version=4
study(title="QuantNomad - MACD Higher TimeFrame", shorttitle="MACD HTF")

//////////////
/// INPUTS ///

fast_length   = input(title = "MACD Fast Length", type = input.integer, defval = 12)
slow_length   = input(title = "MACD Slow Length", type = input.integer, defval = 26)
signal_length = input(title = "MACD Smoothing",   type = input.integer, minval = 1, maxval = 50, defval = 9)
higher_tf     = input(title = "MACD TimeFrame",   type = input.resolution, defval = '60')

////////////////
// FUNCTIONS ///

mymacd_macd() =>
    src     = close
    fast_ma = sma(src, fast_length)
    slow_ma = sma(src, slow_length)
    macd    = fast_ma - slow_ma
    signal  = sma(macd, signal_length)
    macd
    
mymacd_signal() =>
    // Calculating
    src     = close
    fast_ma = sma(src, fast_length)
    slow_ma = sma(src, slow_length)
    macd    = fast_ma - slow_ma
    signal  = sma(macd, signal_length)
    signal 

mymacd_color() =>
    // Calculating
    src     = close
    fast_ma = sma(src, fast_length)
    slow_ma = sma(src, slow_length)
    macd    = fast_ma - slow_ma
    signal  = sma(macd, signal_length)
    hist    = macd - signal
    (hist>=0 ? (hist[1] < hist ? #26A69A : #B2DFDB) : (hist[1] < hist ? #FFCDD2 : #EF5350) )
    
// Calculate variables on higher timeframe    
macd   = security(syminfo.tickerid, higher_tf, mymacd_macd())
signal = security(syminfo.tickerid, higher_tf, mymacd_signal())
mcolor = security(syminfo.tickerid, higher_tf, mymacd_color())

// macd   = mymacd_macd()
// signal = mymacd_signal()
// mcolor = mymacd_color()


longEntry = crossover(macd, signal)
plotshape(longEntry, title='Long Entry', location=location.bottom, style=shape.labelup, color=color.new(#2C9670, 0), text='Long', textcolor=color.new(color.white, 0))


// Caclulate hist 
hist    = macd - signal

// Plotting 
plot(hist,   title = "Histogram", color = mcolor,  transp=0,  style = plot.style_columns)
plot(macd,   title = "MACD",      color = #0094ff, transp = 0)
plot(signal, title = "Signal",    color = #ff6a00, transp = 0)