bigwin 发表于 2024-10-16 13:40:31

Pine Script代码,改成MQL5代码。 Tradingview改成MT5 指标

Pine Script代码,改成MQL5代码:

//@version=5

const int maxDistanceToLastBar = 5000
const int labelCooldown = 8
const int KDELimit = 300

indicator("RSI短线", shorttitle = "RSI短线",overlay = true, max_labels_count = 500)

rsiLengthInput = input.int(14, minval = 1, title="RSI Length", group="RSI Settings")
rsiSourceInput = input.source(close, "Source", group="RSI Settings")

highPivotLen = input.int(21,"High Pivot Length", minval = 1, group = "Pivots", display = display.none)
lowPivotLen = input.int(21, "Low Pivot Length", minval = 1, group = "Pivots", display = display.none)

float KDEBandwidth = input.float(2.71828, "Bandwidth", group = "KDE", tooltip = "This setting sets the smoothness of the KDE function output.")
int KDEStep = input.int(100, "Nº Bins", minval = 1, group = "KDE", tooltip = "The number of elements the KDE Probability array will have. Higher settings will result in greater precision.")

bearishColor = input.color(#f23646, "High Pivots", group = "Style", inline = "col", display = display.none)
bullishColor = input.color(#089981, "Low Pivots", group = "Style", inline = "col", display = display.none)

rsi = ta.rsi(rsiSourceInput, rsiLengthInput)

//#region KDE
gaussian (float distance, float bandwidth = 1.0) => 1.0 / math.sqrt(2.0 * math.pi) * math.pow(math.e, -0.5 * math.pow(distance / bandwidth, 2.0))

kde (array<float> arr, float bandwidth, int steps) =>
    arrSize = arr.size()
    arrRange = arr.range()
    arrMin = arr.min()
    stepCount = arrRange / steps
   
    densityRange = array.new<float>(steps * 2)
    for i = 0 to (steps * 2) - 1
      densityRange.set(i, arrMin + i * stepCount)
   
    xArr = array.new<float>()
    yArr = array.new<float>()
    for i = 0 to densityRange.size() - 1
      float temp = 0
      for j = 0 to arr.size() - 1
            temp += gaussian(densityRange.get(i) - arr.get(j), 1.0 / bandwidth)

      
      xArr.push(densityRange.get(i))
      yArr.push(1.0 / arrSize * temp)
   
//#endregion

//#region Pivots

prefixSum (array<float> arr, int l, int r) =>
    arr.get(r) - (l == 0 ? 0 : arr.get(l - 1))

float MidKDEHigh = na
float MidKDELow = na

var array<float> KDEHighX = na
var array<float> KDEHighY = na
var array<float> KDEHighYSum = array.new<float>()

var array<float> KDELowX = na
var array<float> KDELowY = na
var array<float> KDELowYSum = array.new<float>()

highPivot = ta.pivothigh(highPivotLen, highPivotLen)
lowPivot = ta.pivotlow(lowPivotLen, lowPivotLen)

var highPivotRSIs = array.new<float>()
var lowPivotRSIs = array.new<float>()

if not na(highPivot)
    if highPivotRSIs.size() > KDELimit
      highPivotRSIs.remove(0)
    highPivotRSIs.push(rsi)

    = kde(highPivotRSIs, KDEBandwidth, KDEStep)
    KDEHighX := KDEHighX1
    KDEHighY := KDEHighY1
   
    KDEHighYSum.clear()
    temp = 0.0
    for i = 0 to KDEHighY.size() - 1
      temp += KDEHighY.get(i)
      KDEHighYSum.push(temp)

    MidKDEHigh := array.get(KDEHighX, array.indexof(KDEHighY, array.max(KDEHighY)))

if not na(lowPivot)
    if lowPivotRSIs.size() > KDELimit
      lowPivotRSIs.remove(0)
    lowPivotRSIs.push(rsi)

    = kde(lowPivotRSIs, KDEBandwidth, KDEStep)
    KDELowX := KDELowX1
    KDELowY := KDELowY1

    KDELowYSum.clear()
    temp = 0.0
    for i = 0 to KDELowY.size() - 1
      temp += KDELowY.get(i)
      KDELowYSum.push(temp)

    MidKDELow := array.get(KDELowX, array.indexof(KDELowY, array.max(KDELowY)))
//#endregion

//#region KDE Optimization
f_lin_interpolate(float x0, float x1, float y0, float y1, float x) =>
    y0 + (x - x0) * (y1 - y0) / (x1 - x0)

float lowProb = na
float maxLowProb = na
float highProb = na
float maxHighProb = na

if last_bar_index - maxDistanceToLastBar < bar_index
    if highPivotRSIs.size() > 0
      highXIndexL = array.binary_search_leftmost(KDEHighX, rsi)
      highXIndexR = math.min(array.binary_search_rightmost(KDEHighX, rsi), KDEHighX.size() - 1)
      nearestIndex = (math.abs(rsi - KDEHighX.get(highXIndexL)) < math.abs(rsi - KDEHighX.get(highXIndexR))) ? highXIndexL : highXIndexR
      highProb := prefixSum(KDEHighYSum, 0, nearestIndex)
   
    if lowPivotRSIs.size() > 0
      lowXIndexL = array.binary_search_leftmost(KDELowX, rsi)
      lowXIndexR = math.min(array.binary_search_rightmost(KDELowX, rsi), KDELowX.size() - 1)
      nearestIndex = (math.abs(rsi - KDELowX.get(lowXIndexL)) < math.abs(rsi - KDELowX.get(lowXIndexR))) ? lowXIndexL : lowXIndexR
      lowProb := prefixSum(KDELowYSum, nearestIndex, KDELowYSum.size() - 1)


diffToHighKDE = math.abs(rsi - MidKDEHigh)
diffToLowKDE = math.abs(rsi - MidKDELow)
//#endregion

//#region Draw Pivots
color curColor = na
if (not na(KDELowY)) and (not na(KDEHighY))

    if lowProb > KDELowY.sum() * 0.75
      curColor := bullishColor
    else if highProb > KDEHighY.sum() * 0.75
      curColor := bearishColor
//barcolor(curColor)

atr = ta.atr(50)

long = na(curColor) and curColor == bullishColor and barstate.isconfirmed
short = na(curColor) and curColor == bearishColor and barstate.isconfirmed

plotarrow(long ? 1 : na, "Possible Bullish Pivot", bullishColor, bullishColor, minheight = 20, maxheight = 20)
plotarrow(short ? -1 : na, "Possible Bearish Pivot", bearishColor, bearishColor, minheight = 20, maxheight = 20)

页: [1]
查看完整版本: Pine Script代码,改成MQL5代码。 Tradingview改成MT5 指标