0

我是 pine 脚本的新手,并且在使用最后一个 MACD 向上交叉的起始时段跟踪 RSI 的最大百分比跌幅时遇到问题。

它应该在策略顶部显示最大的下降枢轴#,并且它确实开始工作,但是在我正在测试的期间中途用不正确的枢轴#替换它。

我究竟做错了什么?

截屏

这是代码:

//@version=5

strategy(title="biggest RSI drop% since MACD cross to uptrend", format=format.price, max_bars_back = 4999, precision=2, calc_on_every_tick=true)

// ====================
// INITIALIZE VARIABLES
// ====================

// RSI
Grp1 ="RSI OPTIONS"
showRSI = input(true, "Show RSI     ", "", "1", Grp1)
showPivotPercents = input(true, "Show Pivot Percents", "", "1", Grp1)
lengthRSI = input(14, "RSI Length  ", "", "2", Grp1)
RSIHighLimit = input(80, "RSI High Band ", "", "3", Grp1)
RSILowLimit = input(20, "RSI Low Band", "", "3", Grp1)

// PIVOTS
var bool RSIPivot = na
var int RSIPivotPoint1 = na
var float RSIPivotPoint = na
var float RSIDropValue = na
var int RSIPivotTime = na
var int RSIPivotCount = 0
RSIPivot := false //Resets to flase if pivot was found on previous bar

// MACD
Grp3 = "MACD OPTIONS"
showMACDUpCross = input(true, "Show MACD Up Cross    ", "", "1", Grp3) // Shows potential entry point for buy (after one full up & downtrend)
showMACDDownCross = input(true, "Show MACD Down Cross    ", "", "1", Grp3)
showMACDcolor  = input(true, "Show MACD Color Bands", "", "1", Grp3) // Show MACD red and green color bands for up/downtrends

// ============
// CALCULATIONS
// ============

// CALC RSI
RSISignal = ta.rsi(close, lengthRSI)

// CALC MACD
[MACD, MACDSignal, MACDHist] = ta.macd(close, 12, 26, 9)

// CALC RANGE & RESET PIVOT COUNT
BarsSinceMACDXOver = ta.barssince(ta.crossover(MACD, MACDSignal))
if BarsSinceMACDXOver == 0
    RSIPivotCount := 0

// IDENTIFY RSI PIVOTS: FINDS LOW RSI PIVOTS AND RECORDS THE CHART VALUE, TIME AND COUNT SINCE MACD UPCROSS
if ta.pivotlow(RSISignal, 1, 1) and BarsSinceMACDXOver >= 1 
    RSIPivot := true
    RSIPivotPoint := RSISignal[1]
    RSIPivotTime := time[1]
    RSIPivotCount += 1

// MEASURE RSI PIVOT AMOUNT: MEASURES THE DIFFERENCE FROM LATEST DETECTED PIVOT BACK BARS UNTIL VALUE REVERSED
if RSIPivot //if pivot is detected 1 bar back then run loop to calc amount of drop from previous price reversal 
    for i = 2 to BarsSinceMACDXOver //runs loop from 1 bar before pivot to beginning of period to reference bar hist[] number to check against 
        if RSISignal[i-1] <= RSISignal[i] //checks if previous 
            RSIDropValue := RSISignal[1] - RSISignal[i]
        else
            break

// FIND BIGGEST RSI PIVOT
if RSIPivot and RSIPivotCount == 1 // SETS THE FIRST RSI PIVOT POINT [X] LOCATION AS THE BIGGEST WHEN IT'S FIRST DETECTED (FOR TREND LINE)
    RSIPivotPoint1 := RSIPivotCount
if RSIPivot and RSIPivotCount > 1 and RSIDropValue < RSIDropValue[RSIPivotPoint1] // CHECKS IF A NEW RSI PIVOT IS BIGGER THE LOCATION [X] TO THAT (FOR TREND LINE) 
    RSIPivotPoint1 := RSIPivotCount


// =========
// PLOT DATA
// =========

//PLOT RSI: WHITE CHART LINE
plot(showRSI ? RSISignal : na, "RSI", color=color.new(color.white, 0))

// PLOT MACD: YELLOW VERICAL LINE FOR MACD UPCROSS, ORANGE VERICAL LINE FOR MACD DOWNCROSS, GREEN AND RED TREND FILL
plot(showMACDUpCross and ta.crossover(MACD, MACDSignal) ? 100 : na, "MACD Up Trend", style=plot.style_histogram, color=color.new(color.yellow, 0), linewidth = 2)
plot(showMACDDownCross and ta.crossunder(MACD, MACDSignal) ? RSISignal : na, "MACD Down Trend", style=plot.style_histogram, color=color.new(color.orange, 0), linewidth = 2)
plot(showMACDcolor ? 100 : na, "MACD Trend Fill", style=plot.style_area, offset = 1, color=(ta.barssince(ta.crossunder(MACD, MACDSignal)) > ta.barssince(ta.crossover(MACD, MACDSignal)) ? color.new(color.green, 85) : color.new(color.red, 85)))

// PLOT RANGE COUNTER: DIPLAYS THE BARS SINCE MACD UPCROSS ON BOTTOM RIGHT OF INDICATOR
var RangeCounter = label.new(na, na, color = color.new(color.silver, 75), style = label.style_label_down, textcolor = color.white, size = size.tiny)
if barstate.islast
    label.set_xy(RangeCounter, bar_index, 0)
    label.set_text(RangeCounter, str.tostring(BarsSinceMACDXOver, format.volume))

// PLOT RSI PIVOT DETECTED ON RSI LINE: GREEN DOT ON RSI LINE
plot(RSIPivot ? RSISignal[1] : na, title = "RSI Pivot", color=color.new(color.green, 0), style = plot.style_circles, offset = -1, linewidth = 2)

// PLOT RSI PIVOT % LABEL: GREEN LABEL OVER RSI LINE
RSIPivotDropPercentLabel = label.new(showPivotPercents and RSIPivot ? bar_index[1] : na, (RSISignal[1] + 2), text=str.tostring(RSIDropValue, format.percent), style = label.style_label_down, color = color.new(color.green, 75), textcolor = color.white, size = size.tiny)

// PLOT BIGGEST RSI PIVOT # LABEL: SHOWS THE NUMBER OF THE PIVOT AS IT IS DETECTED AT THE TOP OF THE INDICATOR (CURRENTLY THE 2ND ROW DOWN) FOR DEBUG PURPOSES BUT MAY KEEP IT IN FINAL SCRIPT
RSIPivotCounterLabel = label.new(RSIPivot ? bar_index[1] : na, 100[1], text=str.tostring(RSIPivotCount), style = label.style_none, textcolor = color.white, size = size.tiny)

// PLOT BIGGEST RSI PIVOT # LABEL: SHOULD BE DISPLAYING THE BIGGEST % PIVOT # DETECTED SINCE MACD UPCROSS (YELLOW VERTICAL LINE) ************* BROKEN DUE TO RSIPivotPoint1 VARIABLE?
RSIBiggestPivotLabel = label.new(bar_index[1], 115, text=str.tostring(RSIPivotPoint1), style = label.style_none, textcolor = color.white, size = size.tiny)


//RESET VARIABLES (FOR NEW PERIOD) WHEN MACD CROSSES TO UPTREND
if barstate.isconfirmed and BarsSinceMACDXOver == 0
    RSIPivotPoint1 := na
    RSIPivotPoint := na
    RSIDropValue := na
    RSIPivotTime := na
4

0 回答 0