如何跟踪指标走势和多种条件进行策略或研究?



如果这个问题显得多余,我很抱歉。我正在尝试创建一个使用三个确认的策略或研究

1:三个均线交叉2:相对强弱指数从超卖/超买状态和向上/向下(超过OB或OS水平)和3: MACD执行交叉。

我使用pine脚本创建所有指示器没有问题,但是,我已经可以告诉所有这些条件不会在同一时间排队。我希望一旦所有这些条件都满足,即使不是立即(手动交易这个策略已经证明它永远不会),我也能建立多头头寸,但是我找不到一种方法来在不同的时间验证所有这些条件。我认为我可以使用barssince()函数来锁定其中一个条件发生的期间,然后为我使用布尔输入条件,但是当放入策略形式时,它不会产生任何数据。

到目前为止我写的是:

// 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("REDUX", overlay = true)
//This indicator will use 3 ema crossing, RSI failure swings, and MACD Crosses to open a long position. ATR will be used to determine stop losses.
//USER INPUTS - EMAS
emaSource = input(title = "EMA Source", type = input.source, defval = hlc3)
ema1Length = input(title = "1st EMA Length", type = input.integer, defval = 5)
ema2Length = input(title = "2nd EMA Length", type = input.integer, defval = 8)
ema3Length = input(title = "3rd EMA Length", type = input.integer, defval = 13)
ema4Length = input(title = "4th EMA Length", type = input.integer, defval = 144)
ema5Length = input(title = "5th EMA Length", type = input.integer, defval = 233)
//USER INPUTS - RSI
rsiOB = input(title = "RSI Overbought Level", type = input.integer, defval = 70)
rsiOS = input(title = "RSI Oversold Level", type = input.integer, defval = 30)
rsiLength = input(title = "RSI Length", type = input.integer, defval = 14)
rsiSource = input(title = "RSI Source", type = input.source, defval = hlc3)
//USER INPUTS - MACD 
macdSource = input(title = "MACD Source", type = input.source, defval = hlc3)
macdFast = input(title = "MACD Slow Length", type = input.integer, defval = 12)
macdSlow = input(title = "MACD Slow Length", type = input.integer, defval = 26)
macdSignalPeriod = input(title = "MACD Signal Period", type = input.integer, defval = 9)
//USER INPUTS - ATR
atrLength = input(title = "ATR Length", type = input.integer, defval = 14)
atrLookback = input(title = "ATR Lookback", type = input.integer, defval = 7)
atrMultiplier = input(title = "ATR Multiplier", type = input.float, defval = 1.0)
//CREATE - EMAS
ema1 = ema(emaSource, ema1Length)
ema2 = ema(emaSource, ema2Length)
ema3 = ema(emaSource, ema3Length)
ema4 = ema(emaSource, ema4Length)
ema5 = ema(emaSource, ema5Length)
//CREATE - RSI
rsiValue = rsi(rsiSource, rsiLength)
//CREATE - MACD
fastMA = ema(close, macdFast)
slowMA = ema(close, macdSlow)
macdLine = fastMA - slowMA
signalLine = sma(macdLine, macdSignalPeriod)
//CREATE - ATR
atr = atr(atrLength)
highestHigh = highest(high, atrLookback)
lowestLow = lowest(low, atrLookback)
//CREATE - ATR TRAILING STOP LOSS
longStop = lowestLow - atr * atrMultiplier
shortStop = highestHigh - atr * atrMultiplier
//CALCULATING - EMA CROSSING
emaLong = crossover(ema1, ema2) and ema2 > ema3 
emaShort = crossunder(ema1, ema2) and ema2 < ema3 
emaLongTrigger = barssince(emaLong)
emaShortTrigger = barssince(emaShort)
//CALCULATING - RSI MOVEMENT
isRsiOB = rsiValue >= rsiOB 
isRsiOS = rsiValue <= rsiOS
isRsiUp = rsiValue > rsiOS
isRsiDown = rsiValue < rsiOB
rsiLongTrigger = barssince(isRsiOS)
rsiShortTrigger = barssince(isRsiOB)
rsiIsUp = isRsiOS[rsiLongTrigger] and isRsiUp //RSI has moved from OS to above (Bullish)
rsiIsDown = isRsiOB[rsiShortTrigger] and isRsiDown //RSI has moved from OB to below (Bearish)
//CALCULATING - MACD CROSS
macdBullCross = crossover(signalLine, macdLine)
macdBearCross = crossunder(signalLine, macdLine)
macdBullTrigger = barssince(macdBullCross)
macdBearTrigger = barssince(macdBearCross)
//LONG CONDITIONS
long = emaLong[emaLongTrigger] and rsiIsUp and macdBullCross[macdBullTrigger]
short = emaShort[emaShortTrigger] and rsiIsDown and macdBearCross[macdBearTrigger]
//ENTRIES AND EXITS
strategy.entry("Long", 250, when = long)
strategy.close("Long", when = short)
strategy.entry("Short", 250, when = short)
strategy.close("Short", when = long)

谢谢大家。

Barssince应该可以做到。我通常首先定义单独的条件:

// example using true, substitute your own conditions.
long_1 = true
long_2 = true
long_3 = true

然后把它们放在一起:

long = long_1 and barssince(long_2) < 5 and barssince(long_3) < 10

你显然可以在不同的部分构建它们。或顺序。

long = true
long := long and long_1
long := long and barssince(long_2) <=2
long := long and barssince(long_3) < 6

如果这不是最佳实践,我道歉,这就是我加入多个条件的方式,它对我来说很好。

最新更新