Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

pine script - How to track indicator movements and mutiple conditions for strategies or study?

I apologize if this question comes off as redundant. I'm trying to create a strategy or study that uses three confirmations

1: a triple EMA cross 2: RSI moves from oversold/overbought conditions and upwards/downwards (past the OB or OS levels) and 3: MACD performs a cross.

I have no problem creating all the indicators using pine script, however, I can already tell that all of these conditions will not line up at the same time. I'd like to be able to open a long position once all of these conditions are met, even if its not immediate (trading this strategy manually has proven it never is) however I can't find a way to verify all of these conditions at various times. I thought I would be able to use the barssince() function to lock in the period in which one of these conditions happen and then use that for me Boolean entry conditions, however it does not produce any data when put into the strategy form.

Here is what I have so far:

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

Thank you all.

question from:https://stackoverflow.com/questions/65896707/how-to-track-indicator-movements-and-mutiple-conditions-for-strategies-or-study

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Barssince should do the trick. I generally define my separate conditons first:

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

Then put them together:

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

You could obviously build them in separate parts. Or procedurally.

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

Apologies if this isn't best practice, this is just how I've been joining multiple conditions, and it works fine for me.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...