커뮤니티

수식 전환 부탁드립니다

프로필 이미지
seayun1
2023-10-31 13:41:05
1184
글번호 173563
답변완료
안녕하세요 항상 감사합니다 트레이딩뷰 수식전환부탁드립니다 indicator('Nadaraya-Watson: Rational Quadratic Kernel (Non-Repainting)', overlay=true, timeframe="") // Settings src = input.source(close, 'Source') h = input.float(8., 'Lookback Window', minval=3., tooltip='The number of bars used for the estimation. This is a sliding value that represents the most recent historical bars. Recommended range: 3-50') r = input.float(8., 'Relative Weighting', step=0.25, tooltip='Relative weighting of time frames. As this value approaches zero, the longer time frames will exert more influence on the estimation. As this value approaches infinity, the behavior of the Rational Quadratic Kernel will become identical to the Gaussian kernel. Recommended range: 0.25-25') x_0 = input.int(25, "Start Regression at Bar", tooltip='Bar index on which to start regression. The first bars of a chart are often highly volatile, and omission of these initial bars often leads to a better overall fit. Recommended range: 5-25') smoothColors = input.bool(false, "Smooth Colors", tooltip="Uses a crossover based mechanism to determine colors. This often results in less color transitions overall.", inline='1', group='Colors') lag = input.int(2, "Lag", tooltip="Lag for crossover detection. Lower values result in earlier crossovers. Recommended range: 1-2", inline='1', group='Colors') size = array.size(array.from(src)) // size of the data series // Further Reading: // The Kernel Cookbook: Advice on Covariance functions. David Duvenaud. Published June 2014. // Estimation of the bandwidth parameter in Nadaraya-Watson kernel non-parametric regression based on universal threshold level. Ali T, Heyam Abd Al-Majeed Hayawi, Botani I. Published February 26, 2021. kernel_regression(_src, _size, _h) => float _currentWeight = 0. float _cumulativeWeight = 0. for i = 0 to _size + x_0 y = _src[i] w = math.pow(1 + (math.pow(i, 2) / ((math.pow(_h, 2) * 2 * r))), -r) _currentWeight += y*w _cumulativeWeight += w _currentWeight / _cumulativeWeight // Estimations yhat1 = kernel_regression(src, size, h) yhat2 = kernel_regression(src, size, h-lag) // Rates of Change bool wasBearish = yhat1[2] > yhat1[1] bool wasBullish = yhat1[2] < yhat1[1] bool isBearish = yhat1[1] > yhat1 bool isBullish = yhat1[1] < yhat1 bool isBearishChange = isBearish and wasBullish bool isBullishChange = isBullish and wasBearish // Crossovers bool isBullishCross = ta.crossover(yhat2, yhat1) bool isBearishCross = ta.crossunder(yhat2, yhat1) bool isBullishSmooth = yhat2 > yhat1 bool isBearishSmooth = yhat2 < yhat1 // Colors color c_bullish = input.color(#3AFF17, 'Bullish Color', group='Colors') color c_bearish = input.color(#FD1707, 'Bearish Color', group='Colors') color colorByCross = isBullishSmooth ? c_bullish : c_bearish color colorByRate = isBullish ? c_bullish : c_bearish color plotColor = smoothColors ? colorByCross : colorByRate // Plot plot(yhat1, "Rational Quadratic Kernel Estimate", color=plotColor, linewidth=2) // Alert Variables bool alertBullish = smoothColors ? isBearishCross : isBearishChange bool alertBearish = smoothColors ? isBullishCross : isBullishChange // Alerts for Color Changes alertcondition(condition=alertBullish, title='Bearish Color Change', message='Nadaraya-Watson: {{ticker}} ({{interval}}) turned Bearish ▼') alertcondition(condition=alertBearish, title='Bullish Color Change', message='Nadaraya-Watson: {{ticker}} ({{interval}}) turned Bullish ▲') // Non-Displayed Plot Outputs (i.e., for use in other indicators) plot(alertBearish ? -1 : alertBullish ? 1 : 0, "Alert Stream", display=display.none)
지표
답변 1
프로필 이미지

예스스탁 예스스탁 답변

2023-11-01 11:08:20

안녕하세요 예스스탁입니다. input : hh(8),r(8.),x_0(25),smoothColors(false),lag(2); input : c_bullish(Green),c_bearish(Red); var : src(0),i(0),y(0),w1(0),w2(0); var : _currentWeight1(0),_currentWeight2(0),_cumulativeWeight1(0),_cumulativeWeight2(0); var : yhat1(0),yhat2(0); var : wasBearish(False),wasBullish(False); var : isBearish(False),isBullish(False); var : isBearishChange(False),isBullishChange(False); var : isBullishCross(False),isBearishCross(False); var : isBullishSmooth(False),isBearishSmooth(False); src = C; _currentWeight1 = 0; _cumulativeWeight1 = 0; _currentWeight2 = 0; _cumulativeWeight2 = 0; for i = 0 to 1 + x_0 { y = src[i] ; w1 = pow(1 + (pow(i, 2) / ((pow(hh, 2) * 2 * r))), -r); _currentWeight1 = _currentWeight1 + y*w1; _cumulativeWeight1 = _cumulativeWeight1+ w1; w2 = pow(1 + (pow(i, 2) / ((pow(hh-lag, 2) * 2 * r))), -r); _currentWeight2 = _currentWeight2 + y*w2; _cumulativeWeight2 = _cumulativeWeight2+ w2; } yhat1 = _currentWeight1 / _cumulativeWeight1; yhat2 = _currentWeight2 / _cumulativeWeight2; wasBearish = yhat1[2] > yhat1[1]; wasBullish = yhat1[2] < yhat1[1]; isBearish = yhat1[1] > yhat1; isBullish = yhat1[1] < yhat1; isBearishChange = isBearish and wasBullish; isBullishChange = isBullish and wasBearish; isBullishCross = CrossUp(yhat2, yhat1); isBearishCross = CrossDown(yhat2, yhat1); isBullishSmooth = yhat2 > yhat1; isBearishSmooth = yhat2 < yhat1; // Colors var : colorByCross(0),colorByRate(0),plotColor(0); colorByCross = iff(isBullishSmooth , c_bullish , c_bearish); colorByRate = iff(isBullish , c_bullish , c_bearish); plotColor = iff(smoothColors , colorByCross , colorByRate); // Plot plot1(yhat1, "Rational Quadratic Kernel Estimate",plotColor); 즐거운 하루되세요 > seayun1 님이 쓴 글입니다. > 제목 : 수식 전환 부탁드립니다 > 안녕하세요 항상 감사합니다 트레이딩뷰 수식전환부탁드립니다 indicator('Nadaraya-Watson: Rational Quadratic Kernel (Non-Repainting)', overlay=true, timeframe="") // Settings src = input.source(close, 'Source') h = input.float(8., 'Lookback Window', minval=3., tooltip='The number of bars used for the estimation. This is a sliding value that represents the most recent historical bars. Recommended range: 3-50') r = input.float(8., 'Relative Weighting', step=0.25, tooltip='Relative weighting of time frames. As this value approaches zero, the longer time frames will exert more influence on the estimation. As this value approaches infinity, the behavior of the Rational Quadratic Kernel will become identical to the Gaussian kernel. Recommended range: 0.25-25') x_0 = input.int(25, "Start Regression at Bar", tooltip='Bar index on which to start regression. The first bars of a chart are often highly volatile, and omission of these initial bars often leads to a better overall fit. Recommended range: 5-25') smoothColors = input.bool(false, "Smooth Colors", tooltip="Uses a crossover based mechanism to determine colors. This often results in less color transitions overall.", inline='1', group='Colors') lag = input.int(2, "Lag", tooltip="Lag for crossover detection. Lower values result in earlier crossovers. Recommended range: 1-2", inline='1', group='Colors') size = array.size(array.from(src)) // size of the data series // Further Reading: // The Kernel Cookbook: Advice on Covariance functions. David Duvenaud. Published June 2014. // Estimation of the bandwidth parameter in Nadaraya-Watson kernel non-parametric regression based on universal threshold level. Ali T, Heyam Abd Al-Majeed Hayawi, Botani I. Published February 26, 2021. kernel_regression(_src, _size, _h) => float _currentWeight = 0. float _cumulativeWeight = 0. for i = 0 to _size + x_0 y = _src[i] w = math.pow(1 + (math.pow(i, 2) / ((math.pow(_h, 2) * 2 * r))), -r) _currentWeight += y*w _cumulativeWeight += w _currentWeight / _cumulativeWeight // Estimations yhat1 = kernel_regression(src, size, h) yhat2 = kernel_regression(src, size, h-lag) // Rates of Change bool wasBearish = yhat1[2] > yhat1[1] bool wasBullish = yhat1[2] < yhat1[1] bool isBearish = yhat1[1] > yhat1 bool isBullish = yhat1[1] < yhat1 bool isBearishChange = isBearish and wasBullish bool isBullishChange = isBullish and wasBearish // Crossovers bool isBullishCross = ta.crossover(yhat2, yhat1) bool isBearishCross = ta.crossunder(yhat2, yhat1) bool isBullishSmooth = yhat2 > yhat1 bool isBearishSmooth = yhat2 < yhat1 // Colors color c_bullish = input.color(#3AFF17, 'Bullish Color', group='Colors') color c_bearish = input.color(#FD1707, 'Bearish Color', group='Colors') color colorByCross = isBullishSmooth ? c_bullish : c_bearish color colorByRate = isBullish ? c_bullish : c_bearish color plotColor = smoothColors ? colorByCross : colorByRate // Plot plot(yhat1, "Rational Quadratic Kernel Estimate", color=plotColor, linewidth=2) // Alert Variables bool alertBullish = smoothColors ? isBearishCross : isBearishChange bool alertBearish = smoothColors ? isBullishCross : isBullishChange // Alerts for Color Changes alertcondition(condition=alertBullish, title='Bearish Color Change', message='Nadaraya-Watson: {{ticker}} ({{interval}}) turned Bearish ▼') alertcondition(condition=alertBearish, title='Bullish Color Change', message='Nadaraya-Watson: {{ticker}} ({{interval}}) turned Bullish ▲') // Non-Displayed Plot Outputs (i.e., for use in other indicators) plot(alertBearish ? -1 : alertBullish ? 1 : 0, "Alert Stream", display=display.none)