【问题标题】:Convert supertrend from pinescript to javascript将 supertrend 从 pinescript 转换为 javascript
【发布时间】:2022-11-12 06:23:31
【问题描述】:

首先感谢您的帮助,这是一个复杂的问题!

我有一个将 pinescript 脚本转换为 javascript 的爱好,然后我用它来运行我在 js 中构建的 backtester infra。刚开始我正在使用许多脚本成功地执行此操作,问题不会是我发送蜡烛错误或其他什么。

我 95% 确定我的问题在于没有正确执行 ATR。我对这个功能有点搞砸了,有点接近但不准确。我很确定我转换了超级趋势部分是正确的。

让我们从 KivancOzbilgic 的 supertrend 的 pinescript 脚本开始:

//@version=4
study("Supertrend", overlay = true, format=format.price, precision=2, resolution="")

Periods = input(title="ATR Period", type=input.integer, defval=10)
src = input(hl2, title="Source")
Multiplier = input(title="ATR Multiplier", type=input.float, step=0.1, defval=3.0)
changeATR= input(title="Change ATR Calculation Method ?", type=input.bool, defval=true)
showsignals = input(title="Show Buy/Sell Signals ?", type=input.bool, defval=true)
highlighting = input(title="Highlighter On/Off ?", type=input.bool, defval=true)
atr2 = sma(tr, Periods)
atr= changeATR ? atr(Periods) : atr2
up=src-(Multiplier*atr)
up1 = nz(up[1],up)
up := close[1] > up1 ? max(up,up1) : up
dn=src+(Multiplier*atr)
dn1 = nz(dn[1], dn)
dn := close[1] < dn1 ? min(dn, dn1) : dn
trend = 1
trend := nz(trend[1], trend)
trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trend
upPlot = plot(trend == 1 ? up : na, title="Up Trend", style=plot.style_linebr, linewidth=2, color=color.green)
buySignal = trend == 1 and trend[1] == -1
plotshape(buySignal ? up : na, title="UpTrend Begins", location=location.absolute, style=shape.circle, size=size.tiny, color=color.green, transp=0)
plotshape(buySignal and showsignals ? up : na, title="Buy", text="Buy", location=location.absolute, style=shape.labelup, size=size.tiny, color=color.green, textcolor=color.white, transp=0)
dnPlot = plot(trend == 1 ? na : dn, title="Down Trend", style=plot.style_linebr, linewidth=2, color=color.red)
sellSignal = trend == -1 and trend[1] == 1
plotshape(sellSignal ? dn : na, title="DownTrend Begins", location=location.absolute, style=shape.circle, size=size.tiny, color=color.red, transp=0)
plotshape(sellSignal and showsignals ? dn : na, title="Sell", text="Sell", location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.red, textcolor=color.white, transp=0)
mPlot = plot(ohlc4, title="", style=plot.style_circles, linewidth=0)
longFillColor = highlighting ? (trend == 1 ? color.green : color.white) : color.white
shortFillColor = highlighting ? (trend == -1 ? color.red : color.white) : color.white
fill(mPlot, upPlot, title="UpTrend Highligter", color=longFillColor)
fill(mPlot, dnPlot, title="DownTrend Highligter", color=shortFillColor)
alertcondition(buySignal, title="SuperTrend Buy", message="SuperTrend Buy!")
alertcondition(sellSignal, title="SuperTrend Sell", message="SuperTrend Sell!")
changeCond = trend != trend[1]
alertcondition(changeCond, title="SuperTrend Direction Change", message="SuperTrend has changed direction!")

这是我在javascript中的实现

// candles example (I provide 100 candles)
const candles = [{high: 10, low: 8, close: 9, open: 8.5}, ...]

static ATR = async (candles, multiplier, limit) => {
    const lows = candles.map((candle) => +candle.low)
    const highs = candles.map((candle) => +candle.high)
    const closes = candles.map((candle) => +candle.close)
    let TRResults = []
    for (let x = 1; x < candles.length; x++) TRResults.push(Math.max(highs[x] - lows[x], Math.abs(highs[x] - closes[x - 1]), Math.abs(lows[x] - closes[x - 1])))
    let RMA_TR_Results = [TRResults[0]]
    const alpha = 1 / limit
    for (let x = 1; x < TRResults.length; x++) RMA_TR_Results.push((alpha * TRResults[x]) + ((1 - alpha) * RMA_TR_Results[RMA_TR_Results.length - 1]))
    return RMA_TR_Results[RMA_TR_Results.length - 1] * multiplier
}

static superTrend = async(candles, multiplier, limit) => {
  let upperBands = []
  let lowerBands = []
  let superTrends = []
  for (let i = 0; i < candles.length; i++) {
    if (i >= limit * 4) {
      const lastCandle = +candles[i - 1].close
      const currentCandling = +candles[i].close
      const candlesATR = await this.ATR(candles.slice(i - (limit * 4), limit * 4), multiplier, limit)
      const basicUpperBand = ((+candles[i].high + +candles[i].low) / 2) - candlesATR
      const basicLowerBand = ((+candles[i].high + +candles[i].low) / 2) + candlesATR
      if (i === limit * 4) {
        upperBands.push(basicUpperBand)
        lowerBands.push(basicLowerBand)
        superTrends.push(true)
      } else {
        const lastUpperBand = upperBands[upperBands.length - 1]
        const lastLowerBand = lowerBands[lowerBands.length - 1]
        upperBands.push(lastCandle > lastUpperBand ? Math.max(basicUpperBand, lastUpperBand) : basicUpperBand)
        lowerBands.push(lastCandle < lastLowerBand ? Math.min(basicLowerBand, lastLowerBand) : basicLowerBand)
        const lastSuperTrend = superTrends[superTrends.length - 1]
        superTrends.push(!lastSuperTrend && currentCandling > lastLowerBand ? true : lastSuperTrend && currentCandling < lastUpperBand ? false : lastSuperTrend)
      }
    }
  }
  return superTrends[superTrends.length - 1]
}

// Running the super trend
const supertrendResult = await superTrend(candles, 2, 14)

再次感谢任何帮助!

这是我正在使用的更多资源:

RMA Calculation(在 pinescript 中)

    pine_rma(source, length) =>
    alpha = 1 / length
    sum = 0.0
    sum := na(sum[1]) ? 
         ta.sma(source, length) : 
         alpha * source + (1 - alpha) * nz(sum[1])
    plot(pine_rma(close, 15))

TR Calculation

   true range = max[(high - low), abs(high - previous close), abs (low - previous close)]

【问题讨论】:

    标签: javascript pine-script algorithmic-trading tradingview-api


    【解决方案1】:

    我发现了我的问题。它的拼接方式

    const candlesATR = await this.ATR(candles.slice(i - (limit * 4), limit * 4), multiplier, limit)
    

    应该:

    const candlesATR = await this.ATR(candles.slice(i - (limit * 4), i), multiplier, limit)
    

    【讨论】:

      猜你喜欢
      • 2022-12-05
      • 2021-11-08
      • 2021-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-14
      • 2022-11-22
      相关资源
      最近更新 更多