-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRSI.py
128 lines (102 loc) · 3.55 KB
/
RSI.py
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
"""
Title: RSI - Relative Strength Index (NSE)
Description: This trading strategy executes every hour to decide
how to trade the stocks (100%) based on Relative Strength Index (RSI)
calculated for 15 days lookback period
Style tags: Systematic
Asset class: HAL
Dataset: NSE
"""
from blueshift.api import( symbol,
order_target_percent,
schedule_function,
date_rules,
time_rules,
)
""" Start of the strategy to define things and schedule rebalance """
def initialize(context):
# universe selection
# context.stock = symbol("HAL") # the var name such as 'stock' here is arbitary
# context.stock = symbol("TATAELXSI")
context.stock = symbol("AAPL")
# context.stock = symbol("ADANIENT") # best
# we need the past lookpack period data only at starting (day1)?
context.flag = True
context.lookback = 15
# call rebalance function every one hour
schedule_function(rebalance,
date_rules.every_day(),
time_rules.every_nth_hour(1))
# schedule_function(rebalance,
# date_rules.every_day(),
# time_rules.every_minute())
def rebalance(context,data):
"""
A function to rebalance the portfolio, passed on to the call
of schedule_function above.
"""
if context.flag:
px = data.history(context.stock, 'close', context.lookback , '1d')
init_rsi(px, context) # to initialize prev_avrg params in context
rsi = get_rsi(context, data)
if rsi < 30:
signal = -1 # short/ sell
elif rsi > 70:
signal = 1 # long/ buy
else:
signal = 0 # hold no position
order_target_percent(context.stock, signal)
# to initialize prev_avrg params in context
def init_rsi(px, context):
context.flag = False # to only run once
gains = []
losses = []
print("in init_rsi: ")
for i in range(len(px)):
print(i, px[i])
if i==0:
continue
diff = px[i] - px[i-1]
if diff > 0:
gains.append(diff)
elif diff < 0:
losses.append(abs(diff))
try:
avrg_gain = sum(gains) / len(gains)
except:
avrg_gain = 0
try:
avrg_loss = sum(losses) / len(losses)
except:
avrg_loss = 0
context.prev_avrg_gain = avrg_gain
context.prev_avrg_loss = avrg_loss
def get_rsi(context, data):
# we get price as a pandas series
# can access individual prices through index like array
# print(price)
prev_price = data.history(context.stock, 'close', 1, '1d')
curr_price = data.current(context.stock, 'close')
diff = curr_price - prev_price[0]
gain=0
loss=0
if diff > 0:
gain = diff
elif diff < 0:
loss = abs(diff)
lookback = context.lookback
# print("lookback: ", lookback)
# not dividing by lookback here
avrg_gain = context.prev_avrg_gain * (lookback - 1) + gain
avrg_loss = context.prev_avrg_loss * (lookback - 1) + loss
# updating prev_avrg
context.prev_avrg_gain = avrg_gain / lookback
context.prev_avrg_loss = avrg_loss / lookback
# calculating rsi
try:
alpha = avrg_gain / avrg_loss
rsi = 100 - (100 / (1 + alpha))
except:
rsi = 100
print("rsi: ", rsi)
return rsi