Trend Following with Moving Averages

LonesomeTheBlue · study · 43 行 · 点赞 2,560 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © LonesomeTheBlue
 
//@version=4
study("Trend Following with Moving Averages", resolution = "")
src = input(defval = close, title = "Source")
smooth = input(defval = true, title = "Smooth?")
masrc = input(defval = "EMA", title = "Moving Average Type", options = ["EMA", "SMA", "RMA", "WMA", "VWMA"])
 
getdata(src, len)=>
    ret = masrc == "EMA" ? ema(src, len) : masrc == "SMA" ? sma(src, len) : masrc == "RMA" ? rma(src, len) : masrc == "WMA" ? wma(src, len) : vwma(src, len)
    
ma1 = getdata(src, input(defval =   8, title ="Length 1", minval = 1))
ma2 = getdata(src, input(defval =  13, title ="Length 2", minval = 1))
ma3 = getdata(src, input(defval =  21, title ="Length 3", minval = 1))
ma4 = getdata(src, input(defval =  34, title ="Length 4", minval = 1))
ma5 = getdata(src, input(defval =  55, title ="Length 5", minval = 1))
ma6 = getdata(src, input(defval =  89, title ="Length 6", minval = 1))
ma7 = getdata(src, input(defval = 144, title ="Length 7", minval = 1))
ma8 = getdata(src, input(defval = 233, title ="Length 8", minval = 1))
ma9 = getdata(src, input(defval = 377, title ="Length 9", minval = 1))
 
getmult(l)=>
    ret = l == 1 ? 1 : l == 2 ? 2 : l == 3 ? 4 : l == 4 ? 8 : l == 5 ? 16 : l == 6 ? 32 : l == 7 ? 64 : l == 8 ? 128 : 256
 
getma(l)=>
    ret = l == 1 ? ma1 : l == 2 ? ma2 : l == 3 ? ma3 : l == 4 ? ma4 : l == 5 ? ma5 : l == 6 ? ma6 : l == 7 ? ma7 : l == 8 ? ma8 : ma9
    
getval(l)=>
    ma = getma(l)
    mult = getmult(l)
    ret = (mult * (close > ma ? 1 : -1))
 
dir = 0.0
for x = 1 to 9
    dir := dir + (getval(x))
if smooth
    dir := ema(dir, 3)
 
mid = plot(0, color = color.gray)
col = dir > 0 ? color.lime : color.red
lev = plot(dir, color = col)
fill(mid, lev, color = col, transp = 40)

← 返回列表