将有效的自定义指标添加到交易视图策略

时间:2019-08-16 14:41:47

标签: pine-script

我一般对pine脚本和编码都是陌生的,我找不到一种方法将工作指标的代码添加到tradingview的交叉策略上。

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

我尝试将指标的代码粘贴到策略中,然后将“ adxvma_period”的“ INDICATOR_NAME”(下面的代码段)更改了,但是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)

1 个答案:

答案 0 :(得分:-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)