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

Pine script - how to test strategy with different conditions

希望你在这种气候下一切都好。我对 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)

尝试:

// 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)