-
Notifications
You must be signed in to change notification settings - Fork 2
/
ATR Mean Reversion Strategy V1.pine
92 lines (90 loc) · 2.98 KB
/
ATR Mean Reversion Strategy V1.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
Script Name: ATR Mean Reversion Strategy V1
Author: Bcullen175
Description: **Long Only Strategy**
When Price drops below the ATR band below it will enter a buy on the next candle open
SL at current price minus ATR* ATR multiplier
TP at Mean EMA or if higher than Mean EMA and current candle low is below previous candle low or if price is above ATR
NB: I would highly recommend a low fee broker (I use ICmarkets raw spread account) due to...
PineScript code:
Pine Script™ strategy
ATR Mean Reversion Strategy V1
Copy code
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
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Bcullen175
//@version=5
strategy("ATR Mean Reversion", overlay=true, initial_capital=100000,default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_type=strategy.commission.percent, commission_value=6E-5) // Brokers rate (ICmarkets = 6E-5)
SLx = input(1.5, "SL Multiplier", tooltip = "Multiplies ATR to widen stop on volatile assests, Higher values reduce risk:reward but increase winrate, Values below 1.2 are not reccomended")
src = input(close, title="Source")
period = input.int(10, "ATR & MA PERIOD")
plot(open+ta.atr(period))
plot(open-ta.atr(period))
plot((ta.ema(src, period)), title = "Mean", color=color.white)
i_startTime = input.time(title="Start Filter", defval=timestamp("01 Jan 1995 13:30 +0000"), group="Time Filter", tooltip="Start date & time to begin searching for setups")
i_endTime = input.time(title="End Filter", defval=timestamp("1 Jan 2099 19:30 +0000"), group="Time Filter", tooltip="End date & time to stop searching for setups")
// Check filter(s)
f_dateFilter = time >= i_startTime and time <= i_endTime
atr = ta.atr(period)
// Check buy/sell conditions
var float buyPrice = 0
buyCondition = low < (open-ta.atr(period)) and strategy.position_size == 0 and f_dateFilter
sellCondition = (high > (ta.ema(close, period)) and strategy.position_size > 0 and close < low[1]) or high > (open+ta.atr(period))
stopDistance = strategy.position_size > 0 ? ((buyPrice - atr)/buyPrice) : na
stopPrice = strategy.position_size > 0 ? (buyPrice - SLx*atr): na
stopCondition = strategy.position_size > 0 and low < stopPrice
// Enter positions
if buyCondition
strategy.entry(id="Long", direction=strategy.long)
if buyCondition[1]
buyPrice := open
// Exit positions
if sellCondition or stopCondition
strategy.close(id="Long", comment="Exit" + (stopCondition ? "SL=true" : ""))
buyPrice := na
// Draw pretty colors
plot(buyPrice, color=color.lime, style=plot.style_linebr)
plot(stopPrice, color=color.red, style=plot.style_linebr, offset=-1)
Expand (44 lines)