Trend Following Bar

LonesomeTheBlue · study · 53 行 · 点赞 2,109 · 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 Bar", precision=0)
matype= input('EMA', title = "MA Type", options =['EMA', 'SMA', 'RMA', 'WMA', 'VWMA'])
prd = input(20, title = "Period to Check Trend", minval = 5)
rateinp = input(1, title = "Trend Channel Rate %", minval = 0.1, step = 0.1)
ulinreg = input(true, title = "Use Linear Regression")
linprd = input(10, title = "Linear Regression Period", minval = 2)
 
rate = rateinp / 100
pricerange = highest(300) - lowest(300)
chan = pricerange * rate
    
gettrend(len)=>
    masrc = matype == 'EMA' ? ema(close, len) : matype == 'RMA' ? rma(close, len) : matype == 'VWMA' ? vwma(close, len) : matype == 'WMA' ? wma(close, len) : sma(close, len)
    ma = ulinreg ? linreg(masrc, linprd, 0) : masrc
    
    hh = highest(ma, prd)
    ll = lowest(ma, prd)
    diff = abs(hh - ll)
    
    trend = iff(diff > chan, iff(ma > ll + chan, 1, iff(ma < hh - chan, -1, 0)), 0)
    _ret = trend * diff / chan
    _ret
    
getcol(trend)=>
    _ret = trend >= 10.0 ? #00FF00ff : trend >= 9.0 ? #00FF00ef : trend >= 8.0 ? #00FF00df : trend >= 7.0 ? #00FF00cf : trend >= 6.0 ? #00FF00bf :
       trend >= 5.0 ? #00FF00af : trend >= 4.0 ? #00FF009f : trend >= 3.0 ? #00FF008f : trend >= 2.0 ? #00FF007f : trend >= 1.0 ? #00FF006f :  
       trend <= -10.0 ? #FF0000ff : trend <= -9.0 ?  #FF0000ef : trend <= -8.0 ?  #FF0000df : trend <= -7.0 ?  #FF0000cf : trend <= -6.0 ?  #FF0000bf :
       trend <= -5.0 ?  #FF0000af : trend <= -4.0 ?  #FF00009f : trend <= -3.0 ?  #FF00008f : trend <= -2.0 ?  #FF00007f : trend <= -1.0 ?  #FF00006f :
       na
 
plot(05, color = getcol(gettrend(5)), style = plot.style_columns, histbase = 00)
plot(10, color = getcol(gettrend(10)), style = plot.style_columns, histbase = 05)
plot(15, color = getcol(gettrend(15)), style = plot.style_columns, histbase = 10)
plot(20, color = getcol(gettrend(20)), style = plot.style_columns, histbase = 15)
plot(25, color = getcol(gettrend(25)), style = plot.style_columns, histbase = 20)
plot(30, color = getcol(gettrend(30)), style = plot.style_columns, histbase = 25)
plot(35, color = getcol(gettrend(35)), style = plot.style_columns, histbase = 30)
plot(40, color = getcol(gettrend(40)), style = plot.style_columns, histbase = 35)
plot(45, color = getcol(gettrend(45)), style = plot.style_columns, histbase = 40)
plot(50, color = getcol(gettrend(55)), style = plot.style_columns, histbase = 45)
plot(55, color = getcol(gettrend(60)), style = plot.style_columns, histbase = 50)
plot(60, color = getcol(gettrend(65)), style = plot.style_columns, histbase = 55)
plot(65, color = getcol(gettrend(70)), style = plot.style_columns, histbase = 60)
plot(70, color = getcol(gettrend(75)), style = plot.style_columns, histbase = 65)
plot(75, color = getcol(gettrend(80)), style = plot.style_columns, histbase = 70)
plot(80, color = getcol(gettrend(85)), style = plot.style_columns, histbase = 75)
plot(85, color = getcol(gettrend(90)), style = plot.style_columns, histbase = 80)
plot(90, color = getcol(gettrend(95)), style = plot.style_columns, histbase = 85)
plot(95, color = getcol(gettrend(100)), style = plot.style_columns, histbase = 90)

← 返回列表