forked from nickmccullum/algorithmic-trading-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinance.py
More file actions
628 lines (529 loc) · 26.8 KB
/
finance.py
File metadata and controls
628 lines (529 loc) · 26.8 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
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
from datetime import datetime
import matplotlib.pyplot as plt
import requests
import pandas as pd
import math
from scipy import stats
import numpy as np
import pandas as pd
import os
import random
import copy
import matplotlib.pyplot as plt
import pandas
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split, cross_val_score, TimeSeriesSplit
from sklearn.metrics import accuracy_score, confusion_matrix
import joblib
from sklearn import tree
from lumibot.brokers import Alpaca
from lumibot.backtesting import YahooDataBacktesting
from lumibot.strategies.strategy import Strategy
from lumibot.traders import Trader
from alpaca_trade_api import REST
from timedelta import Timedelta
import time
def fetch_stock_data(api_key, ticker, start_date, end_date, timeframe):
date_range = [('2023-06-08', '2024-09-08')]#, ('2023-03-09', '2023-05-08'), ('2023-05-09', '2023-7-08'), ('2023-7-09', '2023-9-08'), ('2023-9-09', '2023-11-08')
for (start_date, end_date) in date_range:
url = f"https://api.polygon.io/v2/aggs/ticker/{ticker}/range/{timeframe}/{start_date}/{end_date}?adjusted=true&sort=asc&limit=50000"
params = {
'apiKey': api_key
}
try:
response['results'] = response['results'] + requests.get(url, params=params).json()['results']
except:
response = requests.get(url, params=params).json()
# time.sleep(20)
if requests.get(url, params=params).status_code == 200:
return response
else:
print(f"Error: {requests.get(url, params=params).status_code}")
return None
def plot_graph(data):
timestamps = [datetime.fromtimestamp(result['t'] / 1000) for result in data['results']]
closing_prices = [result['c'] for result in data['results']]
plt.plot(timestamps, closing_prices)
plt.show()
# Parameters
api_key = 'B6aVjCLff5E3KTflUXWVlD7sV3W128hd'
ticker = 'SPY'
start_date = '2023-06-08'
end_date = '2023-08-08'
timeframe = '30/minute'
# Fetch data
def analyse(list):
TechIndicator = copy.deepcopy(list)
# Relative Strength Index
# Avg(PriceUp)/(Avg(PriceUP)+Avg(PriceDown)*100
# Where: PriceUp(t)=1*(Price(t)-Price(t-1)){Price(t)- Price(t-1)>0};
# PriceDown(t)=-1*(Price(t)-Price(t-1)){Price(t)- Price(t-1)<0};
def rsi(values):
up = values[values>0].mean()
down = -1*values[values<0].mean()
return 100 * up / (up + down)
# Add Momentum_1D column for all 15 stocks.
# Momentum_1D = P(t) - P(t-1)
for stock in range(len(TechIndicator)):
TechIndicator[stock]['Momentum_1D'] = (TechIndicator[stock]['Close']-TechIndicator[stock]['Close'].shift(1)).fillna(0)
TechIndicator[stock]['RSI_14D'] = TechIndicator[stock]['Momentum_1D'].rolling(center=False, window=14).apply(rsi).fillna(0)
### Calculation of Volume (Plain)m
for stock in range(len(TechIndicator)):
TechIndicator[stock]['Volume_plain'] = TechIndicator[stock]['Volume'].fillna(0)
TechIndicator[0].tail()
def bbands(price, length=30, numsd=2):
""" returns average, upper band, and lower band"""
#ave = pd.stats.moments.rolling_mean(price,length)
ave = price.rolling(window = length, center = False).mean()
#sd = pd.stats.moments.rolling_std(price,length)
sd = price.rolling(window = length, center = False).std()
upband = ave + (sd*numsd)
dnband = ave - (sd*numsd)
return np.round(ave,3), np.round(upband,3), np.round(dnband,3)
for stock in range(len(TechIndicator)):
TechIndicator[stock]['BB_Middle_Band'], TechIndicator[stock]['BB_Upper_Band'], TechIndicator[stock]['BB_Lower_Band'] = bbands(TechIndicator[stock]['Close'], length=20, numsd=1)
TechIndicator[stock]['BB_Middle_Band'] = TechIndicator[stock]['BB_Middle_Band'].fillna(0)
TechIndicator[stock]['BB_Upper_Band'] = TechIndicator[stock]['BB_Upper_Band'].fillna(0)
TechIndicator[stock]['BB_Lower_Band'] = TechIndicator[stock]['BB_Lower_Band'].fillna(0)
def aroon(df, tf=25):
aroonup = []
aroondown = []
x = tf
while x< len(df['Date']):
aroon_up = ((df['High'][x-tf:x].tolist().index(max(df['High'][x-tf:x])))/float(tf))*100
aroon_down = ((df['Low'][x-tf:x].tolist().index(min(df['Low'][x-tf:x])))/float(tf))*100
aroonup.append(aroon_up)
aroondown.append(aroon_down)
x+=1
return aroonup, aroondown
for stock in range(len(TechIndicator)):
listofzeros = [0] * 25
up, down = aroon(TechIndicator[stock])
aroon_list = [x - y for x, y in zip(up,down)]
if len(aroon_list)==0:
aroon_list = [0] * TechIndicator[stock].shape[0]
TechIndicator[stock]['Aroon_Oscillator'] = aroon_list
else:
TechIndicator[stock]['Aroon_Oscillator'] = listofzeros+aroon_list
for stock in range(len(TechIndicator)):
TechIndicator[stock]["PVT"] = (TechIndicator[stock]['Momentum_1D']/ TechIndicator[stock]['Close'].shift(1))*TechIndicator[stock]['Volume']
TechIndicator[stock]["PVT"] = TechIndicator[stock]["PVT"]-TechIndicator[stock]["PVT"].shift(1)
TechIndicator[stock]["PVT"] = TechIndicator[stock]["PVT"].fillna(0)
def abands(df):
#df['AB_Middle_Band'] = pd.rolling_mean(df['Close'], 20)
df['AB_Middle_Band'] = df['Close'].rolling(window = 20, center=False).mean()
# High * ( 1 + 4 * (High - Low) / (High + Low))
df['aupband'] = df['High'] * (1 + 4 * (df['High']-df['Low'])/(df['High']+df['Low']))
df['AB_Upper_Band'] = df['aupband'].rolling(window=20, center=False).mean()
# Low *(1 - 4 * (High - Low)/ (High + Low))
df['adownband'] = df['Low'] * (1 - 4 * (df['High']-df['Low'])/(df['High']+df['Low']))
df['AB_Lower_Band'] = df['adownband'].rolling(window=20, center=False).mean()
for stock in range(len(TechIndicator)):
abands(TechIndicator[stock])
TechIndicator[stock] = TechIndicator[stock].fillna(0)
TechIndicator[0].tail()
columns2Drop = ['Momentum_1D', 'aupband', 'adownband']
for stock in range(len(TechIndicator)):
TechIndicator[stock] = TechIndicator[stock].drop(labels = columns2Drop, axis=1)
TechIndicator[0].head()
def STOK(df, n):
df['STOK'] = ((df['Close'] - df['Low'].rolling(window=n, center=False).mean()) / (df['High'].rolling(window=n, center=False).max() - df['Low'].rolling(window=n, center=False).min())) * 100
df['STOD'] = df['STOK'].rolling(window = 3, center=False).mean()
for stock in range(len(TechIndicator)):
STOK(TechIndicator[stock], 4)
TechIndicator[stock] = TechIndicator[stock].fillna(0)
def psar(df, iaf = 0.02, maxaf = 0.2):
length = len(df)
dates = (df['Date'])
high = (df['High'])
low = (df['Low'])
close = (df['Close'])
psar = df['Close'][0:len(df['Close'])]
psarbull = [None] * length
psarbear = [None] * length
bull = True
af = iaf
ep = df['Low'][0]
hp = df['High'][0]
lp = df['Low'][0]
for i in range(2,length):
if bull:
psar[i] = psar[i - 1] + af * (hp - psar[i - 1])
else:
psar[i] = psar[i - 1] + af * (lp - psar[i - 1])
reverse = False
if bull:
if df['Low'][i] < psar[i]:
bull = False
reverse = True
psar[i] = hp
lp = df['Low'][i]
af = iaf
else:
if df['High'][i] > psar[i]:
bull = True
reverse = True
psar[i] = lp
hp = df['High'][i]
af = iaf
if not reverse:
if bull:
if df['High'][i] > hp:
hp = df['High'][i]
af = min(af + iaf, maxaf)
if df['Low'][i - 1] < psar[i]:
psar[i] = df['Low'][i - 1]
if df['Low'][i - 2] < psar[i]:
psar[i] = df['Low'][i - 2]
else:
if df['Low'][i] < lp:
lp = df['Low'][i]
af = min(af + iaf, maxaf)
if df['High'][i - 1] > psar[i]:
psar[i] = df['High'][i - 1]
if df['High'][i - 2] > psar[i]:
psar[i] = df['High'][i - 2]
if bull:
psarbull[i] = psar[i]
else:
psarbear[i] = psar[i]
#return {"dates":dates, "high":high, "low":low, "close":close, "psar":psar, "psarbear":psarbear, "psarbull":psarbull}
#return psar, psarbear, psarbull
df['psar'] = psar
#df['psarbear'] = psarbear
#df['psarbull'] = psarbull
for stock in range(len(TechIndicator)):
psar(TechIndicator[stock])
# ROC = [(Close - Close n periods ago) / (Close n periods ago)] * 100
for stock in range(len(TechIndicator)):
TechIndicator[stock]['ROC'] = ((TechIndicator[stock]['Close'] - TechIndicator[stock]['Close'].shift(12))/(TechIndicator[stock]['Close'].shift(12)))*100
TechIndicator[stock] = TechIndicator[stock].fillna(0)
for stock in range(len(TechIndicator)):
TechIndicator[stock]['VWAP'] = np.cumsum(TechIndicator[stock]['Volume'] * (TechIndicator[stock]['High'] + TechIndicator[stock]['Low'])/2) / np.cumsum(TechIndicator[stock]['Volume'])
TechIndicator[stock] = TechIndicator[stock].fillna(0)
for stock in range(len(TechIndicator)):
TechIndicator[stock]['Momentum'] = TechIndicator[stock]['Close'] - TechIndicator[stock]['Close'].shift(4)
TechIndicator[stock] = TechIndicator[stock].fillna(0)
def CCI(df, n, constant):
TP = (df['High'] + df['Low'] + df['Close']) / 3
CCI = pd.Series((TP - TP.rolling(window=n, center=False).mean()) / (constant * TP.rolling(window=n, center=False).std())) #, name = 'CCI_' + str(n))
return CCI
for stock in range(len(TechIndicator)):
TechIndicator[stock]['CCI'] = CCI(TechIndicator[stock], 20, 0.015)
TechIndicator[stock] = TechIndicator[stock].fillna(0)
for stock in range(len(TechIndicator)):
new = (TechIndicator[stock]['Volume'] * (~TechIndicator[stock]['Close'].diff().le(0) * 2 -1)).cumsum()
TechIndicator[stock]['OBV'] = new
#Keltner Channel
def KELCH(df, n):
KelChM = pd.Series(((df['High'] + df['Low'] + df['Close']) / 3).rolling(window =n, center=False).mean(), name = 'KelChM_' + str(n))
KelChU = pd.Series(((4 * df['High'] - 2 * df['Low'] + df['Close']) / 3).rolling(window =n, center=False).mean(), name = 'KelChU_' + str(n))
KelChD = pd.Series(((-2 * df['High'] + 4 * df['Low'] + df['Close']) / 3).rolling(window =n, center=False).mean(), name = 'KelChD_' + str(n))
return KelChM, KelChD, KelChU
for stock in range(len(TechIndicator)):
KelchM, KelchD, KelchU = KELCH(TechIndicator[stock], 14)
TechIndicator[stock]['Kelch_Upper'] = KelchU
TechIndicator[stock]['Kelch_Middle'] = KelchM
TechIndicator[stock]['Kelch_Down'] = KelchD
TechIndicator[stock] = TechIndicator[stock].fillna(0)
for stock in range(len(TechIndicator)):
TechIndicator[stock]['EMA'] = TechIndicator[stock]['Close'].ewm(span=3,min_periods=0,adjust=True,ignore_na=False).mean()
TechIndicator[stock] = TechIndicator[stock].fillna(0)
for stock in range(len(TechIndicator)):
TechIndicator[stock]['TEMA'] = (3 * TechIndicator[stock]['EMA'] - 3 * TechIndicator[stock]['EMA'] * TechIndicator[stock]['EMA']) + (TechIndicator[stock]['EMA']*TechIndicator[stock]['EMA']*TechIndicator[stock]['EMA'])
for stock in range(len(TechIndicator)):
TechIndicator[stock]['HL'] = TechIndicator[stock]['High'] - TechIndicator[stock]['Low']
TechIndicator[stock]['absHC'] = abs(TechIndicator[stock]['High'] - TechIndicator[stock]['Close'].shift(1))
TechIndicator[stock]['absLC'] = abs(TechIndicator[stock]['Low'] - TechIndicator[stock]['Close'].shift(1))
TechIndicator[stock]['TR'] = TechIndicator[stock][['HL','absHC','absLC']].max(axis=1)
TechIndicator[stock]['ATR'] = TechIndicator[stock]['TR'].rolling(window=14).mean()
TechIndicator[stock]['NATR'] = (TechIndicator[stock]['ATR'] / TechIndicator[stock]['Close']) *100
TechIndicator[stock] = TechIndicator[stock].fillna(0)
def DMI(df, period):
df['UpMove'] = df['High'] - df['High'].shift(1)
df['DownMove'] = df['Low'].shift(1) - df['Low']
df['Zero'] = 0
df['PlusDM'] = np.where((df['UpMove'] > df['DownMove']) & (df['UpMove'] > df['Zero']), df['UpMove'], 0)
df['MinusDM'] = np.where((df['UpMove'] < df['DownMove']) & (df['DownMove'] > df['Zero']), df['DownMove'], 0)
df['plusDI'] = 100 * (df['PlusDM']/df['ATR']).ewm(span=period,min_periods=0,adjust=True,ignore_na=False).mean()
df['minusDI'] = 100 * (df['MinusDM']/df['ATR']).ewm(span=period,min_periods=0,adjust=True,ignore_na=False).mean()
df['ADX'] = 100 * (abs((df['plusDI'] - df['minusDI'])/(df['plusDI'] + df['minusDI']))).ewm(span=period,min_periods=0,adjust=True,ignore_na=False).mean()
for stock in range(len(TechIndicator)):
DMI(TechIndicator[stock], 14)
TechIndicator[stock] = TechIndicator[stock].fillna(0)
columns2Drop = ['UpMove', 'DownMove', 'ATR', 'PlusDM', 'MinusDM', 'Zero', 'EMA', 'HL', 'absHC', 'absLC', 'TR']
for stock in range(len(TechIndicator)):
TechIndicator[stock] = TechIndicator[stock].drop(labels = columns2Drop, axis=1)
for stock in range(len(TechIndicator)):
TechIndicator[stock]['26_ema'] = TechIndicator[stock]['Close'].ewm(span=26,min_periods=0,adjust=True,ignore_na=False).mean()
TechIndicator[stock]['12_ema'] = TechIndicator[stock]['Close'].ewm(span=12,min_periods=0,adjust=True,ignore_na=False).mean()
TechIndicator[stock]['MACD'] = TechIndicator[stock]['12_ema'] - TechIndicator[stock]['26_ema']
TechIndicator[stock] = TechIndicator[stock].fillna(0)
def MFI(df):
# typical price
df['tp'] = (df['High']+df['Low']+df['Close'])/3
#raw money flow
df['rmf'] = df['tp'] * df['Volume']
# positive and negative money flow
df['pmf'] = np.where(df['tp'] > df['tp'].shift(1), df['tp'], 0)
df['nmf'] = np.where(df['tp'] < df['tp'].shift(1), df['tp'], 0)
# money flow ratio
df['mfr'] = df['pmf'].rolling(window=14,center=False).sum()/df['nmf'].rolling(window=14,center=False).sum()
df['Money_Flow_Index'] = 100 - 100 / (1 + df['mfr'])
for stock in range(len(TechIndicator)):
MFI(TechIndicator[stock])
TechIndicator[stock] = TechIndicator[stock].fillna(0)
def ichimoku(df):
# Turning Line
period9_high = df['High'].rolling(window=9,center=False).max()
period9_low = df['Low'].rolling(window=9,center=False).min()
df['turning_line'] = (period9_high + period9_low) / 2
# Standard Line
period26_high = df['High'].rolling(window=26,center=False).max()
period26_low = df['Low'].rolling(window=26,center=False).min()
df['standard_line'] = (period26_high + period26_low) / 2
# Leading Span 1
df['ichimoku_span1'] = ((df['turning_line'] + df['standard_line']) / 2).shift(26)
# Leading Span 2
period52_high = df['High'].rolling(window=52,center=False).max()
period52_low = df['Low'].rolling(window=52,center=False).min()
df['ichimoku_span2'] = ((period52_high + period52_low) / 2).shift(26)
# The most current closing price plotted 22 time periods behind (optional)
df['chikou_span'] = df['Close'].shift(-22) # 22 according to investopedia
for stock in range(len(TechIndicator)):
ichimoku(TechIndicator[stock])
TechIndicator[stock] = TechIndicator[stock].fillna(0)
def WillR(df):
highest_high = df['High'].rolling(window=14,center=False).max()
lowest_low = df['Low'].rolling(window=14,center=False).min()
df['WillR'] = (-100) * ((highest_high - df['Close']) / (highest_high - lowest_low))
for stock in range(len(TechIndicator)):
WillR(TechIndicator[stock])
TechIndicator[stock] = TechIndicator[stock].fillna(0)
def MINMAX(df):
df['MIN_Volume'] = df['Volume'].rolling(window=14,center=False).min()
df['MAX_Volume'] = df['Volume'].rolling(window=14,center=False).max()
for stock in range(len(TechIndicator)):
MINMAX(TechIndicator[stock])
TechIndicator[stock] = TechIndicator[stock].fillna(0)
def KAMA(price, n=10, pow1=2, pow2=30):
''' kama indicator '''
''' accepts pandas dataframe of prices '''
absDiffx = abs(price - price.shift(1) )
ER_num = abs( price - price.shift(n) )
ER_den = absDiffx.rolling(window=n,center=False).sum()
ER = ER_num / ER_den
sc = ( ER*(2.0/(pow1+1)-2.0/(pow2+1.0))+2/(pow2+1.0) ) ** 2.0
answer = np.zeros(sc.size)
N = len(answer)
first_value = True
for i in range(N):
if sc[i] != sc[i]:
answer[i] = np.nan
else:
if first_value:
answer[i] = price[i]
first_value = False
else:
answer[i] = answer[i-1] + sc[i] * (price[i] - answer[i-1])
return answer
for stock in range(len(TechIndicator)):
TechIndicator[stock]['KAMA'] = KAMA(TechIndicator[stock]['Close'])
TechIndicator[stock] = TechIndicator[stock].fillna(0)
columns2Drop = ['26_ema', '12_ema','tp','rmf','pmf','nmf','mfr']
for stock in range(len(TechIndicator)):
TechIndicator[stock] = TechIndicator[stock].drop(labels = columns2Drop, axis=1)
return TechIndicator
def get_AI():
os.chdir('./finished_files/input/ETFs')
list = os.listdir()
number_files = len(list)
#filenames = [x for x in os.listdir("./Stocks/") if x.endswith('.txt') and os.path.getsize(x) > 0]
filenames = random.sample([x for x in os.listdir() if x.endswith('.txt')
and os.path.getsize(os.path.join('',x)) > 0], 8)
data = []
# for filename in filenames:
filename = 'spy.us.txt'
df = pd.read_csv(os.path.join('',filename), sep=',')
label, _, _ = filename.split(sep='.')
df['Label'] = label
df['Date'] = pd.to_datetime(df['Date'])
data.append(df)
TechIndicator = analyse(data)
def get_data(stock):
y = []
extra_columns = ['RSI_14D', 'Volume_plain', 'Aroon_Oscillator', 'VWAP', 'Momentum', 'OBV', 'MACD']
x = stock.drop(columns=['Date', 'Label', 'KAMA', 'OpenInt','BB_Middle_Band', 'BB_Upper_Band', 'BB_Lower_Band', 'AB_Upper_Band', 'AB_Lower_Band', 'Kelch_Upper', 'Kelch_Middle', 'Kelch_Down', 'TEMA', 'NATR', 'plusDI', 'minusDI', 'ADX', 'MIN_Volume', 'MAX_Volume', 'ichimoku_span1', 'ichimoku_span2', 'chikou_span', 'WillR', 'CCI','PVT', 'AB_Middle_Band', 'STOK', 'STOD', 'psar', 'ROC', 'Money_Flow_Index', 'turning_line', 'standard_line']).iloc[:-2*48]
thresholds = [-0.4, -0.32, -0.24, -0.16, -0.08, 0, 0.08, 0.16, 0.24, 0.32, 0.4]
last_percentage_change = 0
for row in range(len(stock) - 2*48):
for i in range(2*48):
percentage_change = (stock['Close'][row + i] - stock['Close'][row]) / stock['Close'][row]
if abs(last_percentage_change) < abs(percentage_change):
last_percentage_change = percentage_change
if last_percentage_change <= thresholds[0]:
class_label = 10
elif last_percentage_change <= thresholds[1]:
class_label = 9
elif last_percentage_change <= thresholds[2]:
class_label = 8
elif last_percentage_change <= thresholds[3]:
class_label = 7
elif last_percentage_change <= thresholds[4]:
class_label = 4
elif last_percentage_change <= thresholds[5]:
class_label = 5
elif last_percentage_change <= thresholds[6]:
class_label = 4
elif last_percentage_change <= thresholds[7]:
class_label = 3
elif last_percentage_change <= thresholds[8]:
class_label = 2
else:
class_label = 1
y.append(class_label)
y = pd.Series(y, name='Label')
return x, y
def train_ai(lst):
all_x = []
all_y = []
for stock in range(len(lst)):
x, y = get_data(lst[stock])
all_x.append(x)
all_y.append(y)
lst[stock] = lst[stock].fillna(0, inplace=True)
all_x = pd.concat(all_x, ignore_index=True)
all_y = pd.concat(all_y, ignore_index=True)
columns = [column for column in all_x.columns]
model = DecisionTreeClassifier()
x_train, x_test, y_train, y_test = train_test_split(all_x, all_y)
model.fit(x_train, y_train)
predictions = model.predict(x_test)
score = accuracy_score(y_test, predictions)
cm = confusion_matrix(y_test, predictions)
print(f"Test accuracy score: {score}")
print(f"Confusion Matrix:\n{cm}")
# for i in range(len(x_test) - 1):
# pred = model.predict(x_test.iloc[i:i+1])
# actual = y_test.iloc[i]
# # if pred != actual:
# print(f'Price: {x_test["Close"].iloc[i]:.2f}, Prediction: {pred[0]}, Actual: {actual} --- Next Price: {x_test["Close"].iloc[i+1]:.2f}')
return model
model = train_ai(TechIndicator)
return model
trading_bot = get_AI()
joblib.dump(trading_bot, 'trading-ai.joblib')
def handle_data(data):
data_list = []
df = pd.DataFrame()
results = data['results']
df['Label'] = pd.Series([data['ticker'] for i in range(len(results))])
df['Date'] = pd.Series([datetime.fromtimestamp(result['t']/1000) for result in results])
df['Open'] = pd.Series([float(result['o']) for result in results])
df['High'] = pd.Series([float(result['h']) for result in results])
df['Low'] = pd.Series([float(result['l']) for result in results])
df['Close'] = pd.Series([float(result['c']) for result in results])
df['Volume'] = pd.Series([float(result['v']) for result in results])
df['OpenInt'] = pd.Series([float(0) for result in results])
df.set_index(pd.RangeIndex(start=0, stop=len(df)), inplace=True)
data_list.append(df)
# print(df)
# Deepcopy might not be necessary, depends on your data structure
TechIndicator2 = analyse(data_list)
return TechIndicator2[0]
API_KEY = "PK126IQGNA1GFHAKH7RS"
API_SECRET = "HIwjwC6V4xmqd193perPNPBes33GEgzSOzZU4Xsn"
BASE_URL = "https://paper-api.alpaca.markets"
ALPACA_CREDS = {
"API_KEY":API_KEY,
"API_SECRET": API_SECRET,
"PAPER": True
}
data = fetch_stock_data(api_key, ticker, start_date, end_date, timeframe)
df = handle_data(data)
class MLTrader(Strategy):
def initialize(self, ticker:str="SPY", cash_at_risk:float=.5):
self.ticker = ticker
self.sleeptime = "30M"
self.last_trade = None
self.last_order = None
self.cash_at_risk = cash_at_risk
self.api = REST(base_url=BASE_URL, key_id=API_KEY, secret_key=API_SECRET)
def position_sizing(self):
cash = self.get_cash()
last_price = self.get_last_price(self.ticker)
quantity = round(cash * self.cash_at_risk / last_price,0)
return cash, last_price, quantity
def get_sentiment(self):
# print(df['Date'])
condition = 'neutral'
today = self.get_datetime().strftime('%Y-%m-%d %H:%M:%S')
if int(today[11:13]) > 9:
today_row = df[df['Date'] == today]
if today_row is not None:
# Get the prediction for today's data
probability = trading_bot.predict(today_row.drop(columns=['Date', 'Label', 'KAMA', 'OpenInt','BB_Middle_Band', 'BB_Upper_Band', 'BB_Lower_Band', 'AB_Upper_Band', 'AB_Lower_Band', 'Kelch_Upper', 'Kelch_Middle', 'Kelch_Down', 'TEMA', 'NATR', 'plusDI', 'minusDI', 'ADX', 'MIN_Volume', 'MAX_Volume', 'ichimoku_span1', 'ichimoku_span2', 'chikou_span', 'WillR', 'CCI','PVT', 'AB_Middle_Band', 'STOK', 'STOD', 'psar', 'ROC', 'Money_Flow_Index', 'turning_line', 'standard_line']))
print(f"For {today}: Prediction: {probability}")
if probability >= 10:
condition = "positive"
elif probability <= 1:
condition = "negative"
else:
print("Gay")
return condition
def runnit(self, cash, last_price, quantity):
sentiment = self.get_sentiment()
if cash > last_price:
if sentiment == "positive":
order = self.create_order(
self.ticker,
quantity,
"buy",
type="bracket",
take_profit_price=last_price*1.02,
stop_loss_price=last_price*.99
)
self.submit_order(order)
self.last_order = order
self.last_trade = "buy"
elif sentiment == "negative":
order = self.create_order(
self.ticker,
quantity,
"sell",
type="bracket",
take_profit_price=last_price*.98,
stop_loss_price=last_price*1.01
)
self.submit_order(order)
self.last_order = order
self.last_trade = "sell"
def on_trading_iteration(self):
cash, last_price, quantity = self.position_sizing()
if self.last_order:
if self.last_order.side == "buy":
if self.last_order.stop_loss_price > last_price or self.last_order.take_profit_price < last_price:
self.last_order = None
self.runnit(cash, last_price, quantity)
elif self.last_order.side == "sell":
if self.last_order.stop_loss_price < last_price or self.last_order.take_profit_price > last_price:
self.last_order = None
self.runnit(cash, last_price, quantity)
elif self.last_order is None:
self.runnit(cash, last_price, quantity)
start_date = datetime(2023,7,1,11)
end_date = datetime(2023,9,7,7)
broker = Alpaca(ALPACA_CREDS)
strategy = MLTrader(name='mlstrat', broker=broker,
parameters={"ticker":ticker,
"cash_at_risk":1})
strategy.backtest(
YahooDataBacktesting,
start_date,
end_date,
parameters={"ticker":ticker, "cash_at_risk":1}
)
# trader = Trader()
# trader.add_strategy(strategy)
# trader.run_all()
print(handle_data(data))