-
Notifications
You must be signed in to change notification settings - Fork 0
/
interp.py
385 lines (206 loc) · 10.4 KB
/
interp.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# -*- coding: utf-8 -*-
"""
Created on Fri Aug 28 23:22:15 2020
@author: Michael
"""
import numpy as np
import matplotlib.pyplot as plt
from lineCalc import *
from shapely.geometry import Polygon
from SupportResistanceMethod import getData
class interp:
def __init__(self,ticket,domain):
sma50,sma200,prices = self.get50and200(ticket, domain)
sma50 = sma50[:len(sma200)]
result,angles,intersections,polygon_areas,polygons,diff = self.getAnalysis(sma200,sma50)
self.ticket = ticket
self.domain = domain
self.sma50 = sma50
self.sma200 = sma200
self.prices = prices
self.result = result
self.angles = angles
self.intersections = intersections
self.polygon_areas = polygon_areas
self.polygons = polygons
self.diff = diff
def SMA(self, x, w):
return np.convolve(x, np.ones(w), 'valid') / w
def get50and200(self, ticket,domain):
#e and t are burner variables because we dont need them
prices,e,t = getData(ticket, domain)
sma50 = self.SMA(prices,50)
sma200 = self.SMA(prices,200)
return sma50,sma200,prices
"""
Think of ways to make indicators to see if the stock is a good buy.
Idea 1 : Project the data onto other scales (log,x^2,2^x,poly) and see which one looks most like a straight line (best fit)
Idea 2 : Idea for estimating the amount of money put into each position. Use the angle between the two moving averages as an indicator to
how much money should be placed into the position.
Idea 3: Use polyfit on the data set with n = 1-10 and check the value of the dervative at the last point of the interpolated curves
"""
# why
#this method takes in two plots and returns the intersection points of the curves as well as the angle at which they intersect
#input: two lists
#output: list of indicies which are the intersection points
def getIntersections(self, shortMa, longMa):
intersections = []
#have to find which moving average is above at the start of the data set
#highest = 1 : short list starts on top
#highest = 0 : long list starts on top
highest = None
if shortMa[0] > longMa[0]:
highest = 1
else:
highest = 0
# count through the data sets and check to see when the points intersect
for i in range(len(shortMa)-1):
if highest == 1:
if shortMa[i] < longMa[i]:
print(i)
intersections.append(int(i))
highest = 0 #reset the highest so that the longerlist is on top
elif highest == 0:
if longMa[i] < shortMa[i]:
intersections.append(int(i))
highest = 1
else:
print("Highest not set at index " , i)
break
return intersections
#method that finds the angle of intersection of each of the intersection points
def getAngles(self, shortMa,longMa,intersections):
angles = []
localRange = 5
x = np.arange(0,len(shortMa),1)
for i in intersections:
#case for when the intersection point is too close to the start of the data set
if i < localRange:
#checks to make sure that the function at least has some data points to make a line
if i <= 2:
pass
else:
#get average intersection lines for this intersection
mShort,bShort = np.polyfit(x[:i],shortMa[:i],1)
mLong,bLong = np.polyfit(x[:i],longMa[:i],1)
#convert to angle relative to x axis
mShort = np.arctan(mShort)
mLong = np.arctan(mLong)
angles.append((mShort,mLong,i))
else:
#get average intersection lines for this intersection
mShort,bShort = np.polyfit(x[i-localRange:i],shortMa[i-localRange:i],1)
mLong,bLong = np.polyfit(x[i-localRange:i],longMa[i-localRange:i],1)
angles.append((mShort,mLong,i))
return angles
def areaAnalysis(self, shortMa,longMa,intersections):
#makes them the same length (idk if we need this but its here)
#basically the idea for this is to return a list of the areas between the curves for each interval (between intersection points)
polygon_areas = []
polygons = []
for i in range(len(intersections)-1):
thisPolygon = []
#go to the actual x position (time)
if intersections[i] < 1:
pass
else:
"""
start = intersections[i-1]
end = intersections[i]
"""
start = intersections[i]
end = intersections[i+1]
#runs foreward through the subsection, taking the whichever value is on top first
for f in range(start,end+1,):
if shortMa[f] >= longMa[f]:
thisPoint = [f,shortMa[f]]
else:
thisPoint = [f,longMa[f]]
thisPolygon.append(thisPoint)
#once you hit the end of this subset, loop backwards and get all of the data points on the bottom plot
for b in range(end,start-1,-1):
if shortMa[b] <= longMa[b]:
thisPoint = [b,shortMa[b]]
else:
thisPoint = [b,longMa[b]]
thisPolygon.append(thisPoint)
polygons.append(thisPolygon)
polygon = Polygon(thisPolygon)
area = polygon.area
polygon_areas.append(area)
return polygon_areas,polygons
def getAnalysis(self, shortMa,longMa):
"""
##Things I want this method to return##
1. Number of intersections of the data set
2. Total area above/beneath plots
#interpretation of results#
a large area before intersection and a steep intersection angle result in the highest confidence
a small area before intersection and small intersection angle result in the lowest confidence
"""
#makes them the same length
if len(shortMa) < len(longMa):
longMa = longMa[:len(shortMa)]
elif len(longMa) < len(shortMa):
shortMa = shortMa[:len(longMa)]
intersections = self.getIntersections(shortMa,longMa)
lines = self.getAngles(shortMa,longMa,intersections)
plt.show()
diff = []
for l in lines:
diff.append(l[0]-l[1])
print("The intersection at " , l[2], " has a slope difference of " , abs(l[0]-l[1]))
polygon_areas,polygons = self.areaAnalysis(shortMa, longMa, intersections)
i = 0
for p in polygon_areas:
print("The polygon at intrval ", i, " has \nArea ", p)
i+=1
#normalzie both data sets
diff = self.normalizeData(diff)
polygon_areas = self.normalizeData(polygon_areas)
compare = self.relationship(diff,polygon_areas)
return compare,lines,intersections, polygon_areas,polygons,diff
def normalizeData(self, data):
return (data - np.min(data)) / (np.max(data) - np.min(data))
def relationship(self, diff,polygon_areas):
result = []
"""
*thing to rememeber about this*
we want to compare the angle to the area of the section that
came directly before the intersection. this means that when adding them
in index form (like below) you cant just add a[i]+b[i], one of the indexes
probbably has to be staggered
"""
for i in range(len(polygon_areas)):
#ACTUAL EQUATION GOES HERE
"""
R = (A+D)/2
"""
result.append((polygon_areas[i]+diff[i+1])/2)
return result
#window is 5m per unit so 5m*2000 = a week
ticket = "BTC-USD"
interpObj = interp(ticket,2000)
polygons = interpObj.polygons
angles = interpObj.angles
diff = interpObj.diff
index = interpObj.result
plt.title(ticket)
plt.plot(interpObj.sma50)
plt.plot(interpObj.sma200)
#plots the individual intersections points in red on the graph with the data sets
for i in interpObj.intersections:
plt.plot(i,interpObj.sma50[i], marker='o', markersize=3, color="red")
plt.show()
angleTitle = "Angle difference: " , len(interpObj.diff)
plt.title(angleTitle)
plt.plot(interpObj.diff)
plt.show()
polyTitle = "Polygon area: ", len(interpObj.polygon_areas)
plt.title(polyTitle)
plt.plot(interpObj.polygon_areas)
plt.show()
plt.plot(interpObj.result)
plt.title("Interp Index")
plt.plot(interpObj.result)
#maybe make a method outside of this class(or in this class) to display the info graphically instead of doing this bullshit