计算线性回归PineScrip的皮尔逊R [英] Calculate Pearson's R for Linear Regression PineScript
本文介绍了计算线性回归PineScrip的皮尔逊R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在下面有一个简单的策略,在这个策略中我试图计算皮尔逊R相关值。我从这里获得了代码,并对其进行了一些修改(https://www.tradingview.com/script/CD7yUWRV-Linear-Regression-Trend-Channel/)。
他的代码不包括皮尔逊的R相关性,这对我尝试使用的交易策略非常重要,因为它表明了趋势的强度和方向(向上或向下)。要查看皮尔逊R的工作检查,请添加默认指标线性回归,它将是左下角的数字。我将附加一个屏幕截图作为示例。
如何根据目前已有的代码计算皮尔逊的R值?
我已经在Pinescrip中寻找了具有线性回归的皮尔逊R计算的示例PINE脚本,但没有找到任何东西。
strategy(title="Linear Regression Trend Channel Strategy", overlay=true,initial_capital=1000,commission_type=strategy.commission.percent,commission_value=0.26,default_qty_type=strategy.percent_of_equity,default_qty_value=100)
period = input( 240, "Period" , input.integer, minval=3)//288
deviations = input( 2.0, "Deviation(s)" , input.float , minval=0.1, step=0.1)
extendType = input("Right", "Extend Method", input.string , options=["Right","None"])=="Right" ? extend.right : extend.none
periodMinusOne = period-1
Ex = 0.0, Ey = 0.0, Ex2 = 0.0, Exy = 0.0, for i=0 to periodMinusOne
closeI = nz(close[i]), Ex := Ex + i, Ey := Ey + closeI, Ex2 := Ex2 + (i * i), Exy := Exy + (closeI * i)
ExEx = Ex * Ex, slope = Ex2==ExEx ? 0.0 : (period * Exy - Ex * Ey) / (period * Ex2 - ExEx)
linearRegression = (Ey - slope * Ex) / period
intercept = linearRegression + bar_index * slope
deviation = 0.0, for i=0 to periodMinusOne
deviation := deviation + pow(nz(close[i]) - (intercept - slope * (bar_index[i])), 2.0)
deviation := deviations * sqrt(deviation / periodMinusOne)
startingPointY = linearRegression + slope * periodMinusOne
var line upperChannelLine = na , var line medianChannelLine = na , var line lowerChannelLine = na
line.delete(upperChannelLine[1]), line.delete(medianChannelLine[1]), line.delete(lowerChannelLine[1])
upperChannelLine := line.new(bar_index - period + 1, startingPointY + deviation, bar_index, linearRegression + deviation, xloc.bar_index, extendType, color.new(#FF0000, 0), line.style_solid , 2)
medianChannelLine := line.new(bar_index - period + 1, startingPointY , bar_index, linearRegression , xloc.bar_index, extendType, color.new(#C0C000, 0), line.style_solid , 1)
lowerChannelLine := line.new(bar_index - period + 1, startingPointY - deviation, bar_index, linearRegression - deviation, xloc.bar_index, extendType, color.new(#00FF00, 0), line.style_solid , 2)
if(crossunder(close,line.get_y2(lowerChannelLine)))
strategy.entry("Long", strategy.long)
if(crossover(close,line.get_y2(upperChannelLine)))
strategy.entry("Short", strategy.short)
推荐答案
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © x11joe
// Credit given to @midtownsk8rguy for original source code. I simply modified to add Pearson's R
//@version=4
study("Linear Regression Trend Channel With Pearson's R", "LRTCWPR", true, format.inherit)
period = input( 20, "Period" , input.integer, minval=3)
deviations = input( 2.0, "Deviation(s)" , input.float , minval=0.1, step=0.1)
extendType = input("Right", "Extend Method", input.string , options=["Right","None"])=="Right" ? extend.right : extend.none
periodMinusOne = period-1
Ex = 0.0, Ey = 0.0, Ex2 = 0.0,Ey2 =0.0, Exy = 0.0, for i=0 to periodMinusOne
closeI = nz(close[i]), Ex := Ex + i, Ey := Ey + closeI, Ex2 := Ex2 + (i * i),Ey2 := Ey2 + (closeI * closeI), Exy := Exy + (closeI * i)
ExT2 = pow(Ex,2.0) //Sum of X THEN Squared
EyT2 = pow(Ey,2.0) //Sym of Y THEN Squared
PearsonsR = (Exy - ((Ex*Ey)/period))/(sqrt(Ex2-(ExT2/period))*sqrt(Ey2-(EyT2/period)))
ExEx = Ex * Ex, slope = Ex2==ExEx ? 0.0 : (period * Exy - Ex * Ey) / (period * Ex2 - ExEx)
linearRegression = (Ey - slope * Ex) / period
intercept = linearRegression + bar_index * slope
deviation = 0.0, for i=0 to periodMinusOne
deviation := deviation + pow(nz(close[i]) - (intercept - slope * (bar_index[i])), 2.0)
deviation := deviations * sqrt(deviation / periodMinusOne)
startingPointY = linearRegression + slope * periodMinusOne
var label pearsonsRLabel = na
label.delete(pearsonsRLabel[1])
pearsonsRLabel := label.new(bar_index,startingPointY - deviation*2,text=tostring(PearsonsR), color=color.black,style=label.style_labeldown,textcolor=color.white,size=size.large)
var line upperChannelLine = na , var line medianChannelLine = na , var line lowerChannelLine = na
line.delete(upperChannelLine[1]), line.delete(medianChannelLine[1]), line.delete(lowerChannelLine[1])
upperChannelLine := line.new(bar_index - period + 1, startingPointY + deviation, bar_index, linearRegression + deviation, xloc.bar_index, extendType, color.new(#FF0000, 0), line.style_solid , 2)
medianChannelLine := line.new(bar_index - period + 1, startingPointY , bar_index, linearRegression , xloc.bar_index, extendType, color.new(#C0C000, 0), line.style_solid , 1)
lowerChannelLine := line.new(bar_index - period + 1, startingPointY - deviation, bar_index, linearRegression - deviation, xloc.bar_index, extendType, color.new(#00FF00, 0), line.style_solid , 2)
好的,我在下面发布了我的解决方案。我是从YouTube上的一段视频中得到这个想法的,https://www.youtube.com/watch?v=2B_UW-RweSE
这真的帮了我一个大忙。希望这对TradingView上需要它的其他人有所帮助!
这篇关于计算线性回归PineScrip的皮尔逊R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文