Linear Regression with MA filter

Arjan_Eagle · study · 58 行 · 点赞 5 · TradingView 原页

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

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Arjan_Eagle
//@version=6 
 
indicator(title = 'Linear Regression with MA filter', shorttitle = 'LRMF', overlay = true)
 
// Inputs
src = input.source(close, group = 'Linear Regression Settings')
LinLenght = input.int(25, group = 'Linear Regression Settings')
MAsource = input.source(close, group = 'Ma Settings')
MAlenght = input.int(defval = 40, step = 1, group = 'Ma Settings')
 
// Ma Switch
string i_maType = input.string(defval = "EMA", title = "MA type", options = ["SMA", "RMA", "WMA", "VWMA", "TEMA", "DEMA", "HMA", "ALMA", "EMA"])
float ma = switch i_maType
    "SMA" => ta.sma(MAsource, MAlenght)
    "RMA" => ta.rma(MAsource, MAlenght)
    "WMA" => ta.wma(MAsource, MAlenght)
    "VWMA" => ta.vwma(MAsource, MAlenght)
    "TEMA" =>
        ema1 = ta.ema(MAsource, MAlenght)
        ema2 = ta.ema(ema1, MAlenght)
        ema3 = ta.ema(ema2, MAlenght)
        3 * (ema1 - ema2) + ema3
    "DEMA" =>
        ema1 = ta.ema(MAsource, MAlenght)
        ema2 = ta.ema(ema1, MAlenght)
        2 * ema1 - ema2
    "HMA" => ta.wma(2 * ta.wma(MAsource, MAlenght / 2) - ta.wma(MAsource, MAlenght), math.floor(math.sqrt(MAlenght)))
    "ALMA" => ta.alma(MAsource, MAlenght, 0.85, 6)
    => ta.ema(MAsource, MAlenght)
 
// Calculation
LinR = ta.linreg(src, LinLenght, 0)
col = LinR > ma ? color.green : color.red
 
 
// Plotting
 
// Plotshape
Green = ta.crossover(LinR, ma)
Red = ta.crossunder(LinR, ma)
 
plot(LinR, title = 'Linear Regression with MA filter', color = col, linewidth = 1)
plot(ma, title = 'MA', color = color.gray, linewidth = 1)
barcolor(color = col, display = display.none)
plotshape(
    Green, title = 'Long', 
    style = shape.triangleup, 
    location = location.belowbar, 
    color = color.green, 
    size = size.tiny) 
plotshape(
    Red, title = 'Short', 
    style = shape.triangledown, 
    location = location.abovebar, 
    color = color.red, 
    size = size.tiny)

← 返回列表