简体   繁体   English

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

[英]Pine script - how to test strategy with different conditions

Hope you are all well during this climate.希望你们在这种气候下一切都好。 I'm quite new to Tradingview and Pine Script and wanted to run a backtest on a strategy but I struggling with the coding part.我对 Tradingview 和 Pine Script 还很陌生,想对策略进行回测,但我在编码部分苦苦挣扎。 I want to enter a long position with the following conditions: - The 5 EMA crosses over the 10 EMA - RSI is greater than 50 - ADX is greater than 25我想在以下条件下输入多头 position: - 5 EMA 穿过 10 EMA - RSI 大于 50 - ADX 大于 25

I want to enter a short position when: - The 5 EMA crosses under the 10 EMA - RSI is less than 50 - ADX is less than 25我想在以下情况下做空 position: - 5 EMA 穿过 10 EMA - RSI 小于 50 - ADX 小于 25

The position should close when the EMA's crosses over again.当 EMA 再次交叉时,position 应该关闭。 I attempt to do this myself (see below) but not with much success.我自己尝试这样做(见下文),但没有取得多大成功。 I would appreciate if anyone could give me with some guidance.如果有人能给我一些指导,我将不胜感激。

Peace:)和平:)

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

Try:尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM