-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbollinger-bands.pine
69 lines (55 loc) · 2.3 KB
/
bollinger-bands.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
59
60
61
62
63
64
65
66
67
68
69
//@version=5
// Bollinger Bands: Madrid : 14/SEP/2014 11:07 : 2.0
// This displays the traditional Bollinger Bands, the difference is
// that the 1st and 2nd StdDev are outlined with two colors and two
// different levels, one for each Standard Deviation
src = input(close)
length = input.int(34, minval=1)
mult = input.float(2.0, minval=0.001, maxval=50)
basis = ta.sma(src, length)
dev = ta.stdev(src, length)
dev2 = mult * dev
upper1 = basis + dev
lower1 = basis - dev
upper2 = basis + dev2
lower2 = basis - dev2
colorBasis = src >= basis ? color.blue : color.orange
pBasis = plot(basis, linewidth=2, color=colorBasis)
pUpper1 = plot(upper1, color=color.new(color.blue, 0), style=plot.style_circles)
pUpper2 = plot(upper2, color=color.new(color.blue, 0))
pLower1 = plot(lower1, color=color.new(color.orange, 0), style=plot.style_circles)
pLower2 = plot(lower2, color=color.new(color.orange, 0))
fill(pBasis, pUpper2, color=color.new(color.blue, 80))
fill(pUpper1, pUpper2, color=color.new(color.blue, 80))
fill(pBasis, pLower2, color=color.new(color.orange, 80))
fill(pLower1, pLower2, color=color.new(color.orange, 80))
//@version=5
strategy(title="Bollinger Bands Strategy", overlay=true)
// Define strategy inputs
src2 = input(close, title="Source")
// length = input.int(20, title="BB Length") // Commented out the previous definition of 'length'
mult2 = input.float(2.0, title="Std Deviations")
// Calculate Bollinger Bands
basis2 = ta.sma(src2, length)
dev3 = mult2 * ta.stdev(src2, length)
upper = basis2 + dev3
lower = basis2 - dev3
// Define entry conditions
longCondition = ta.crossover(close, upper)
shortCondition = ta.crossunder(close, lower)
// Define exit conditions
longExitCondition = ta.crossunder(close, basis)
shortExitCondition = ta.crossover(close, basis)
// Place orders and track positions
if longCondition
strategy.entry("Long", strategy.long)
if shortCondition
strategy.entry("Short", strategy.short)
if longExitCondition
strategy.close("Short")
if shortExitCondition
strategy.close("Long")
// Plot Bollinger Bands
plot(basis, title="Basis", color=color.white, linewidth=2)
fill(plot(upper, title="Upper", color=color.green), plot(basis, title="Basis"), color=color.new(color.green, 70))
fill(plot(basis, title="Basis"), plot(lower, title="Lower", color=color.red), color=color.new(color.red, 70))