//-- This source code is subject to the terms of the Mozilla Public License 2.0
//-- This is a example of study, for presentation only
//@version=4
//==建立新研究==//
study(title = "MACD", shorttitle = "MACD")
ResolutionString = input("", title = "商品分辨率", type = input.resolution, confirm = false)
//==設定輸入==//
fast_length = input(12, title = "快速期數", type = input.integer)
slow_length = input(26, title = "慢速期數", type = input.integer)
source = input(close, title = "數據來源", type = input.source)
signal = input( 9, title = "信號期數", type = input.integer, minval = 1, maxval = 50)
sma_source = input(false, title = "需要改成用SMA計算MACD麼?", type = input.bool)
sma_signal = input(false, title = "需要改成用SMA計算信號麼?", type = input.bool)
//==設定顏色==//
col_grow_above = #26A69A, col_fall_above = #B2DFDB, col_macd = #0094FF
col_grow_below = #FFCDD2, col_fall_below = #EF5350, col_signal = #FF6A00
//==算計式==//
fast_ma = sma_source ? sma(source, fast_length) : ema(source, fast_length)
slow_ma = sma_source ? sma(source, slow_length) : ema(source, slow_length)
macd = fast_ma - slow_ma
_signal = sma_signal ? sma(macd, signal) : ema(macd, signal)
columns = macd - signal
divergence = columns[1] < columns ? col_grow_above : col_fall_above
convergence = columns[1] < columns ? col_grow_below : col_fall_below
col_columns = (columns >= 0 ? divergence : convergence)
//==繪製樣式==//
plot(columns, title = "柱狀圖", color = col_columns, transp= 0, style = plot.style_columns)
plot( macd, title = "MACD", color = col_macd, transp = 0)
plot(_signal, title = "信號線", color = col_signal, transp = 0)