【问题标题】:Adding a functioning custom indicator to a tradingview strategy将功能正常的自定义指标添加到交易视图策略
【发布时间】:2019-12-22 21:29:04
【问题描述】:

总的来说,我对 pine 脚本和编码非常陌生,我找不到将工作指标的代码添加到 tradingview 上的交叉策略的方法。

如果您能指出正确的方向,将不胜感激。

我尝试将指标的代码粘贴到策略中,然后将“INDICATOR_NAME”(代码如下 sn-p)更改为“adxvma_period”,但 tradingview 无法识别。

这里是我需要添加指标的地方

longCondition = crossover(close, INDICATOR_NAME(close, 7))
if (longCondition)
    strategy.entry("LONG", strategy.long)

这是指标的代码

study(title="ADX Volatility Moving Average", shorttitle="ADXVMA", overlay=true)
adxvma_period = input(7, minval=1)


ups=0.0
downs=0.0
index=0.0
adxvma=0.0
trend=0
up=0.0
down=0.0

k=1.0/adxvma_period

volatility = atr(200)

currentUp=max(close[0] - close[1], 0)
currentDown=max(close[1] - close[0], 0)

up:=(1-k)*nz(up[1]) + k*currentUp[0]
down:=(1-k)*nz(down[1]) + k*currentDown[0]

sum = up[0] + down[0]

fractionUp=0.0
fractionDown=0.0

if sum > 0.0
    fractionUp := up[0]/sum
    fractionDown := down[0]/sum

ups:=(1-k)*nz(ups[1])+k*fractionUp
downs:=(1-k)*nz(downs[1])+k*fractionDown

normDiff = abs(ups[0] - downs[0])
normSum = ups[0] + downs[0]


normFraction=if normSum > 0.0
    normDiff/normSum

index:=(1-k)*nz(index[1]) + k*normFraction
ups:=(1-k)*nz(ups[1]) + k*fractionUp
downs:=(1-k)*nz(downs[1]) + k*fractionDown

epsilon = 0.1 * nz(volatility[1])
hhp = highest(index,adxvma_period)[1]
llp = lowest(index,adxvma_period)[1]

hhv = max(index[0],hhp)
llv = min(index[0],llp)

vIndex=0.0

if (hhv-llv)>0.0
    vIndex:=(index[0]-llv)/(hhv-llv)

adxvma:=(1 - k*vIndex)*nz(adxvma[1]) + k*vIndex*close[0]

lcolor=lime

if (nz(trend[1])>-1 and adxvma[0]> nz(adxvma[1]))
    trend:=1
    lcolor=black
else
    if (nz(trend[1])<1 and adxvma[0]<nz(adxvma[1]))
        trend:=-1
        lcolor:=red
    else
        trend:=0
        lcolor:=yellow




plot( adxvma, color= lcolor, linewidth=3)

【问题讨论】:

    标签: pine-script


    【解决方案1】:
    //@version=3
    //@author=LucF, for PineCoders (original NT ADXVMA indie by Fat Tails on futures.io, Pine port by tr_rlstreet on TV)
    
    // This code runs as a strategy, which cannot generate alerts.
    // If you want to use the alerts it must be converted to an indicator (study).
    // To do so:
    //      1. Swap the following 2 lines by commenting the first and uncommenting the second.
    //      2. Comment out the last 4 lines containing the strategy() calls.
    //      3. Save and voilà.
    strategy(title="ADX Volatility Moving Average", shorttitle="ADXVMA [Strat]", overlay=true, pyramiding=0, initial_capital=100000, default_qty_type=strategy.percent_of_equity, default_qty_value=100)
    // study("ADX Volatility Moving Average", shorttitle="ADXVMA [Indicator]", overlay=true)
    
    // —————————— Colors
    MyGreenRaw = color(#00FF00,0),  MyGreenMedium = color(#00FF00,50),  MyGreenDark = color(#00FF00,75),    MyGreenDarkDark = color(#00FF00,92)
    MyRedRaw = color(#FF0000,0),    MyRedMedium = color(#FF0000,30),    MyRedDark = color(#FF0000,75),      MyRedDarkDark = color(#FF0000,90)
    
    // —————————— Inputs
    LongsOnly = input(false,"Longs only")
    ShortsOnly = input(false,"Shorts only")
    ShowTriggers = input(false, "Show Entry/Exit triggers")
    ShowTradedBackground = input(true, "Show Traded Background")
    adxvma_period = input(7, minval=1)
    
    // —————————— Date range filtering
    DateFilter = input(false, "══════ Date Range Filtering ══════")
    FromYear = input(1900, "From Year", minval = 1900)
    FromMonth = input(1, "From Month", minval = 1, maxval = 12)
    FromDay = input(1, "From Day", minval = 1, maxval = 31)
    ToYear = input(2999, "To Year", minval = 1900)
    ToMonth = input(1, "To Month", minval = 1, maxval = 12)
    ToDay = input(1, "To Day", minval = 1, maxval = 31)
    FromDate = timestamp(FromYear, FromMonth, FromDay, 00, 00)
    ToDate = timestamp(ToYear, ToMonth, ToDay, 23, 59)
    TradeDateIsAllowed() => DateFilter ? (time >= FromDate and time <= ToDate) : true
    
    // —————————— ADXVMA
    ups=0.0
    downs=0.0
    index=0.0
    adxvma=0.0
    trend=0
    up=0.0
    down=0.0
    
    k=1.0/adxvma_period
    
    volatility = atr(200)
    
    currentUp=max(close[0] - close[1], 0)
    currentDown=max(close[1] - close[0], 0)
    
    up:=(1-k)*nz(up[1]) + k*currentUp[0]
    down:=(1-k)*nz(down[1]) + k*currentDown[0]
    
    sum = up[0] + down[0]
    
    fractionUp=0.0
    fractionDown=0.0
    
    if sum > 0.0
        fractionUp := up[0]/sum
        fractionDown := down[0]/sum
    
    ups:=(1-k)*nz(ups[1])+k*fractionUp
    downs:=(1-k)*nz(downs[1])+k*fractionDown
    
    normDiff = abs(ups[0] - downs[0])
    normSum = ups[0] + downs[0]
    
    
    normFraction=if normSum > 0.0
        normDiff/normSum
    
    index:=(1-k)*nz(index[1]) + k*normFraction
    ups:=(1-k)*nz(ups[1]) + k*fractionUp
    downs:=(1-k)*nz(downs[1]) + k*fractionDown
    
    epsilon = 0.1 * nz(volatility[1])
    hhp = highest(index,adxvma_period)[1]
    llp = lowest(index,adxvma_period)[1]
    
    hhv = max(index[0],hhp)
    llv = min(index[0],llp)
    
    vIndex=0.0
    
    if (hhv-llv)>0.0
        vIndex:=(index[0]-llv)/(hhv-llv)
    
    adxvma:=(1 - k*vIndex)*nz(adxvma[1]) + k*vIndex*close[0]
    
    lcolor=lime
    
    if (nz(trend[1])>-1 and adxvma[0]> nz(adxvma[1]))
        trend:=1
        lcolor=black
    else
        if (nz(trend[1])<1 and adxvma[0]<nz(adxvma[1]))
            trend:=-1
            lcolor:=red
        else
            trend:=0
            lcolor:=yellow
    
    // —————————— Entries/Exits
    InLong = false
    InShort = false
    EnterLong = TradeDateIsAllowed() and not InLong[1] and crossover(close, adxvma[1])
    EnterShort = TradeDateIsAllowed() and not InShort[1] and crossunder(close, adxvma[1])
    InLong := (InLong[1] and not EnterShort[1]) or (EnterLong[1] and not ShortsOnly)
    InShort := (InShort[1] and not EnterLong[1]) or (EnterShort[1] and not LongsOnly)
    
    // —————————— Plots
    plot( adxvma, color= lcolor, linewidth=3)
    // ————— Entry/Exit markers
    plotshape( ShowTriggers and not ShortsOnly and EnterLong, style=shape.triangleup, location=location.belowbar, color=MyGreenRaw, size=size.small)
    plotshape( ShowTriggers and not LongsOnly and EnterShort, style=shape.triangledown, location=location.abovebar, color=MyRedRaw, size=size.small)
    // ————— Exits when printing only longs or shorts
    plotshape( ShowTriggers and ShortsOnly and InShort[1] and EnterLong, style=shape.triangleup, location=location.belowbar, color=MyRedMedium, transp=70, size=size.small)
    plotshape( ShowTriggers and LongsOnly and InLong[1] and EnterShort, style=shape.triangledown, location=location.abovebar, color=MyGreenMedium, transp=70, size=size.small)
    // ————— Background
    bgcolor( color=ShowTradedBackground? InLong and not ShortsOnly?MyGreenDarkDark: InShort and not LongsOnly? MyRedDarkDark:na:na)
    
    // —————————— Alerts
    alertcondition( EnterLong or EnterShort, title="1. Reverse", message="Reverse")
    alertcondition( EnterLong, title="2. Long", message="Long")
    alertcondition( EnterShort, title="3. Short", message="Short")
    
    // —————————— Strategy calls
    strategy.entry("Long", strategy.long, when=EnterLong and not ShortsOnly)
    strategy.entry("Short", strategy.short, when=EnterShort  and not LongsOnly)
    strategy.close("Short", when=EnterLong and ShortsOnly)
    strategy.close("Long", when=EnterShort and LongsOnly)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-19
      • 2014-12-09
      • 2016-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多