-
Notifications
You must be signed in to change notification settings - Fork 2
/
dailydsi.py
256 lines (218 loc) · 8.47 KB
/
dailydsi.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import pickle
import sys
import traceback
from datetime import timedelta, time
import backtrader as bt
import pandas as pd
from backtrader.utils import AutoOrderedDict
from dsicache.allobjects import loadobjects, dsiD_lts
from indicators.oddenhancers import DSIndicator
from mygoogle.sprint import GoogleSprint
from mytelegram.raven import Raven
from tradingschedule import lastclosingtime
raven = Raven()
debug = True
cachepath = r"dsicache/day/dsi.obj"
with open(cachepath, "rb") as file:
cache = pickle.load(file)
lasttimestamp = max([v["lasttimestamp"] for k, v in cache.items() if v["lasttimestamp"] is not None])
if lasttimestamp == lastclosingtime:
msg = f"Cache Update\n Timeframe: Days\n Compression: 1\n Updated Till: {lasttimestamp}"
print(msg)
raven.send_all_clients(msg)
raven.stop()
sys.exit()
daysago500 = lastclosingtime.date() - timedelta(days=500)
sessionstart = time(hour=9, minute=15)
sessionend = time(hour=15, minute=30)
with open("dsicache/day/tickers.obj", "rb") as file:
tickers: pd.DataFrame = pickle.load(file)
filename = "dsicache/day/dsi.obj"
g = GoogleSprint()
wb = g.gs.open("Demand Supply Daily")
ws = wb.worksheet("Zones1")
finaldf = None
class DailyUpdate(bt.Strategy):
def __init__(self):
self.resource = AutoOrderedDict()
self.states = self.deserialize()
nonce = 0
while nonce < len(self.datas):
dsidata = self.datas[nonce] # Day 1
trenddata = self.datas[nonce + 1] # Week 1
curvedata = self.datas[nonce + 2] # Month 1
dname = dsidata._dataname
try:
savedstate = self.states[dname] if self.states else None
except KeyError:
savedstate = None
self.resource[dname].datas.daily = dsidata
self.resource[dname].datas.weekly = trenddata
self.resource[dname].datas.monthly = curvedata
self.resource[dname].ds = DSIndicator(dsidata, trenddata, curvedata, savedstate=savedstate)
nonce += 3
def next(self):
global finaldf
if self.data0.datetime.datetime(0) == lastclosingtime:
allrows = list()
for dname, val in self.resource.items():
ds: DSIndicator = val.ds
close = val.datas.daily.close[0]
atr = ds.atr[0]
try:
d1 = ds.demandzones[0]
d1_entry = d1.entry
d1_stoploss = d1.sl
d1_score = d1.score
ptd = close - d1_entry
atd = round(ptd / atr, 2)
dstrength = d1.strength
dtbase = d1.timeatbase
dratio = d1.ratio if d1.ratio != float("inf") else "Anant"
dcurve = d1.location
dtrend = d1.trend
dtest = d1.testcount
except IndexError:
d1_entry = None
d1_stoploss = None
d1_score = None
ptd = None
atd = None
dstrength = None
dtbase = None
dratio = None
dcurve = None
dtrend = None
dtest = None
try:
s1 = ds.supplyzones[0]
s1_entry = s1.entry
s1_stoploss = s1.sl
s1_score = s1.score
pts = s1_entry - close
ats = round(pts / atr, 2)
sstrength = s1.strength
stbase = s1.timeatbase
sratio = s1.ratio if s1.ratio != float("inf") else "Anant"
scurve = s1.location
strend = s1.trend
stest = s1.testcount
except IndexError:
s1_entry = None
s1_stoploss = None
s1_score = None
pts = None
ats = None
sstrength = None
stbase = None
sratio = None
scurve = None
strend = None
stest = None
row = {
"Ticker": dname.split("_")[0],
"D Trend": dtrend,
"D Curve": dcurve,
"D Time": dtbase,
"D Strength": dstrength,
"D Test": dtest,
"D Ratio": dratio,
"D ATR": atd,
"D Points": ptd,
"D Stoploss": d1_stoploss,
"D Entry": d1_entry,
"D Score": d1_score,
"Last Close": close,
"S Score": s1_score,
"S Entry": s1_entry,
"S Stoploss": s1_stoploss,
"S Points": pts,
"S ATR": ats,
"S Ratio": sratio,
"S Test": stest,
"S Strength": sstrength,
"S Time": stbase,
"S Curve": scurve,
"S Trend": strend,
}
allrows.append(row)
df = pd.DataFrame(allrows)
df.fillna("", inplace=True)
if finaldf is None:
finaldf = df
else:
finaldf = finaldf.append(df, ignore_index=True)
finaldf.drop_duplicates(inplace=True)
g.update_sheet(ws, finaldf)
self.cerebro.runstop()
def serialize(self):
blocks = {dname: val.ds.getstate() for dname, val in self.resource.items()}
if self.states:
self.states.update(blocks)
else:
self.states = blocks
with open(filename, "wb") as file:
pickle.dump(self.states, file)
def deserialize(self):
try:
with open(filename, "rb") as file:
dsidict = pickle.load(file)
return dsidict
except (EOFError, FileNotFoundError):
return None
def stop(self):
self.serialize()
olderthan500days = tickers[tickers.date < daysago500]
newerthan500days = tickers[tickers.date >= daysago500]
while len(olderthan500days):
subset50 = olderthan500days[:50]
olderthan500days = olderthan500days[50:]
print(subset50)
cerebro = bt.Cerebro(runonce=False)
cerebro.addstrategy(DailyUpdate)
cerebro.addcalendar("NSE")
store = bt.stores.IBStore(port=7496, _debug=debug)
for element in subset50.iterrows():
ticker = element[1]
data0 = store.getdata(dataname=ticker.btsymbol, fromdate=daysago500,
sessionstart=sessionstart,
sessionend=sessionend,
historical=True, timeframe=bt.TimeFrame.Days)
cerebro.adddata(data0)
cerebro.resampledata(data0, timeframe=bt.TimeFrame.Weeks)
cerebro.resampledata(data0, timeframe=bt.TimeFrame.Months)
try:
thestrats = cerebro.run(stdstats=False)
except Exception as e:
print(subset50)
print(traceback.format_exc())
raven.send_all_clients(list(subset50.symbol))
raven.send_all_clients(traceback.format_exc())
while len(newerthan500days):
subset1 = newerthan500days[:1]
newerthan500days = newerthan500days[1:]
print(subset1)
cerebro = bt.Cerebro(runonce=False)
cerebro.addstrategy(DailyUpdate)
cerebro.addcalendar("NSE")
store = bt.stores.IBStore(port=7496, _debug=debug)
for element in subset1.iterrows():
ticker = element[1]
data0 = store.getdata(dataname=ticker.btsymbol, fromdate=ticker.date,
sessionstart=sessionstart,
sessionend=sessionend,
historical=True, timeframe=bt.TimeFrame.Days)
cerebro.adddata(data0)
cerebro.resampledata(data0, timeframe=bt.TimeFrame.Weeks)
cerebro.resampledata(data0, timeframe=bt.TimeFrame.Months)
try:
thestrats = cerebro.run(stdstats=False)
except Exception as e:
print(subset1)
print(traceback.format_exc())
raven.send_all_clients(list(subset1.symbol))
raven.send_all_clients(traceback.format_exc())
loadobjects()
msg = f"Cache Update\n Timeframe: Days\n Compression: 1\n Updated Till: {dsiD_lts}"
raven.send_all_clients(msg)
raven.stop()