Pine脚本-如何在不同条件下测试策略

问题描述 投票:0回答:1

希望您在这种气候下一切都很好。我对Tradingview和Pine Script很陌生,想对策略进行回测,但是我在编码部分苦苦挣扎。我要在以下情况下输入多头头寸:-5 EMA跨越10 EMA-RSI大于50-ADX大于25

我想在以下情况下输入空头头寸:-5 EMA在10 EMA下交叉-RSI小于50-ADX小于25

当EMA再次交叉时,该头寸应平仓。我试图自己做(见下文),但没有成功。如果有人可以给我一些指导,我将不胜感激。

和平:)

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/

//@version=4
strategy("full kit", overlay=true)
ema5 = ema (close, 5)
ema10 = ema (close, 10)
rsi14 = rsi (close, 14)
adxlen = input(14, title="ADX Smoothing")
dilen = input(14, title="DI Length")
dirmov(len) =>
    up = change(high)
    down = -change(low)
    plusDM = na(up) ? na : (up > down and up > 0 ? up : 0)
    minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)
    truerange = rma(tr, len)
    plus = fixnan(100 * rma(plusDM, len) / truerange)
    minus = fixnan(100 * rma(minusDM, len) / truerange)
    [plus, minus]
adx(dilen, adxlen) =>
    [plus, minus] = dirmov(dilen)
    sum = plus + minus
    adx = 100 * rma(abs(plus - minus) / (sum == 0 ? 1 : sum), adxlen)
sig = adx(dilen, adxlen)
plot(sig, color=color.red, title="ADX")


long = crossover(ema(close, 5), sma(close, 10)),rsi14 
short = crossunder(ema(close, 5), sma(close, 10))

plot(ema5, title="5", color=#000000, linewidth=3)
plot(ema10, title="10", color=#002200, linewidth=2)

start = timestamp(2010,6,1,0,0)
end = timestamp(2019,6,1,0,0)

if time >= start and time <=end
    strategy.entry("long", strategy.long, 1000.0, when = long)
    strategy.entry("short", strategy.short, 1000.0, when = short)

strategy.close("long", when = short)
strategy.close("short", when = long)
pine-script algorithmic-trading trading
1个回答
0
投票

尝试:

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/

//@version=4
strategy("full kit", overlay=true)
ema5 = ema (close, 5)
ema10 = ema (close, 10)
rsi14 = rsi (close, 14)
adxlen = input(14, title="ADX Smoothing")
dilen = input(14, title="DI Length")
dirmov(len) =>
    up = change(high)
    down = -change(low)
    plusDM = na(up) ? na : (up > down and up > 0 ? up : 0)
    minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)
    truerange = rma(tr, len)
    plus = fixnan(100 * rma(plusDM, len) / truerange)
    minus = fixnan(100 * rma(minusDM, len) / truerange)
    [plus, minus]
adx(dilen, adxlen) =>
    [plus, minus] = dirmov(dilen)
    sum = plus + minus
    adx = 100 * rma(abs(plus - minus) / (sum == 0 ? 1 : sum), adxlen)
sig = adx(dilen, adxlen)
plot(sig, color=color.red, title="ADX")


long = crossover(ema5, ema10) and rsi14 > 50 and sig > 25
short = crossunder(ema5, ema10) and rsi14 < 50 and sig < 25
closeLong = crossunder(ema5, ema10)
closeShort = crossover(ema5, ema10)

plot(ema5, title="5", color=#000000, linewidth=3)
plot(ema10, title="10", color=#002200, linewidth=2)

start = timestamp(2010,6,1,0,0)
end = timestamp(2019,6,1,0,0)

if time >= start and time <=end
    strategy.entry("long", strategy.long, 1000.0, when = long)
    strategy.entry("short", strategy.short, 1000.0, when = short)

strategy.close("long", when = closeLong)
strategy.close("short", when = closeShort)

// Debugging.
plotchar(long, "long", "▲", location.top)
plotchar(short, "short", "▼", location.top)
© www.soinside.com 2019 - 2024. All rights reserved.