-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSuper_Trend.py
More file actions
61 lines (48 loc) · 2.16 KB
/
Super_Trend.py
File metadata and controls
61 lines (48 loc) · 2.16 KB
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
'''
Implementation of the super trend indicator
https://bonfida.medium.com/introduction-of-super-trend-strategy-3b8c420abbd0
The indicator changes state when a closing price crosses a price threshold
The price threshold is set using the ATR and multipler.
The treshold moves only in the direction of the trend unless it is broken.
'''
ATR_PERIOD = 24
ATR_MULTIPLIER = 3.0
def initialize(state):
state.super_trend = 0 # current trend direction
state.limit = 0 # current threshold price
''' The update function for the super trend indicator '''
def super_trend(state, price, atr, multipler) :
# calculate the new trend reversal price
limit = -state.super_trend * multipler * atr + price
# set the state the first time that the bot is run
if state.super_trend == 0:
state.limit = limit
# check if the trend has changed
if price >= state.limit and state.super_trend != 1:
state.super_trend = 1
# reset reversal price
state.limit = -state.super_trend * multipler * atr + price
elif price < state.limit and state.super_trend != -1:
state.super_trend = -1
# reset reversal price
state.limit = -state.super_trend * multipler * atr + price
else:
# only ratchet limit price if its going in the trends direction
if state.super_trend == +1:
state.limit = max(state.limit, limit)
elif state.super_trend == -1:
state.limit = min(state.limit, limit)
# return the current trend reversal price and the trenc direction
return state.limit, state.super_trend
@schedule(interval="1h", symbol="BTCUSDT")
def handler(state, data):
# initalize super trend indicator with historic data the first time the bot is run
if state.super_trend == 0:
atr = data.atr(ATR_PERIOD)
for p, v in zip(data.select("close"), atr):
super_trend(state, p, v, ATR_MULTIPLIER)
# update super trend indicator
level, direction = super_trend(state, data.close_last, data.atr(ATR_PERIOD)[-1], ATR_MULTIPLIER)
with PlotScope.root(data.symbol):
plot_line("level", level)
plot_line("direction", direction, data.symbol)