【问题标题】:Pine script - how to test strategy with different conditionsPine 脚本 - 如何在不同条件下测试策略
【发布时间】:2020-04-27 15:52:03
【问题描述】:

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

    试试:

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

    【讨论】:

      猜你喜欢
      • 2021-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-11
      相关资源
      最近更新 更多