-
Notifications
You must be signed in to change notification settings - Fork 5
/
BBRSI.pine
58 lines (48 loc) · 2.25 KB
/
BBRSI.pine
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Implementation of a strategy using Bollinger Bands and RSI
// The strategy has been explained in this video: https://youtu.be/pCmJ8wsAS_w
// © 2023 Geraked, Rabist
// Licensed under MIT
// Backtesting result:
// Symbol: EURUSD
// Timeframe: 5 MIN
// Range: 2023-06-18 21:00 — 2023-07-14 11:05
// Percent Profitable: 75.0%
// Total Trades: 88
// Profit Factor: 2.120
//@version=5
strategy("Bollinger Bands and RSI Strategy", "BBRSI", overlay=true)
bbLength = input.int(20, "BB Length", minval=1)
bbMult = input.float(2.0, "BB StdDev", minval=0.001, maxval=50)
rsiLength = input.int(3, "RSI Length", minval=1)
slCoef = input.float(1.5, "SL Coef")
tpCoef = input.float(1.5, "TP Coef")
closeBbBasis = input.bool(true, "Close on BB Basis")
bbBasis = ta.sma(close, bbLength)
dev = bbMult * ta.stdev(close, bbLength)
bbUpper = bbBasis + dev
bbLower = bbBasis - dev
up = ta.rma(math.max(ta.change(close), 0), rsiLength)
down = ta.rma(-math.min(ta.change(close), 0), rsiLength)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
rsiUpper = 70
rsiBasis = 50
rsiLower = 30
longSignal = rsi[1] < rsiLower and close[1] < bbLower and rsi > rsiLower and close > bbLower and rsi < rsiBasis and close < bbBasis
shortSignal = rsi[1] > rsiUpper and close[1] > bbUpper and rsi < rsiUpper and close < bbUpper and rsi > rsiBasis and close > bbBasis
longSL = bbLower - slCoef * (bbBasis - bbLower)
shortSL = bbUpper + slCoef * (bbUpper - bbBasis)
if closeBbBasis
if (ta.crossover(close, bbBasis))
strategy.close(id="long", comment="Close longs")
if (ta.crossunder(close, bbBasis))
strategy.close(id="short", comment="Close shorts")
if longSignal
strategy.entry("long", strategy.long)
inn = strategy.opentrades.entry_price(strategy.opentrades - 1)
longTP = inn + tpCoef * (inn - longSL)
strategy.exit("exit", "long", stop=longSL, limit=longTP, comment_loss="SL long", comment_profit="TP long")
if shortSignal
strategy.entry("short", strategy.short)
inn = strategy.opentrades.entry_price(strategy.opentrades - 1)
shortTp = inn - tpCoef * (shortSL - inn)
strategy.exit("exit", "short", stop=shortSL, limit=shortTp, comment_loss="SL short", comment_profit="TP short")