forked from FranklineMisango/Lumibot_Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlong_short_strategy.py
458 lines (409 loc) · 19.6 KB
/
long_short_strategy.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
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
import os
import yfinance as yf
import threading
from dotenv import load_dotenv
load_dotenv()
import datetime
import alpaca_trade_api as tradeapi
import time
import pandas as pd
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import csv
from datetime import datetime as dt
from datetime import timedelta
import glob
API_KEY = os.environ.get('APCA_API_KEY_ID')
API_SECRET = os.environ.get('APCA_API_SECRET_KEY')
APCA_API_BASE_URL = "https://paper-api.alpaca.markets"
EMAIL_USER = os.environ.get('EMAIL_ADDRESS')
EMAIL_PASSWORD = os.environ.get('EMAIL_PASSWORD')
EMAIL_RECEIVER = os.environ.get('YOUR_EMAIL_ADDRESS')
#Declaring global variables
global initial_equity
#helper functions to send the emails to the traders
def mail_alert(mail_content, sleep_time):
# The mail addresses and password
sender_address = EMAIL_USER
sender_pass = EMAIL_PASSWORD
# Setup MIME
message = MIMEMultipart()
message['From'] = 'Frankline & Co. HFT Day Trading Bot'
message['To'] = EMAIL_RECEIVER
message['Subject'] = 'Frankline & Co. HFT Important Day Updates'
message['Signature'] = "Making HFT Fun and Profitable"
# The body and the attachments for the mail
message.attach(MIMEText(mail_content, 'plain'))
# Create SMTP session for sending the mail
session = smtplib.SMTP('smtp.gmail.com', 587) # use gmail with port
session.starttls() # enable security
# login with mail_id and password
session.login(sender_address, sender_pass)
text = message.as_string()
session.sendmail(sender_address, EMAIL_RECEIVER, text)
session.quit()
time.sleep(sleep_time)
class LongShort:
def __init__(self):
#TODO - Add more stocks to the stockUniverse with diverse leverage
self.alpaca = tradeapi.REST(API_KEY, API_SECRET, APCA_API_BASE_URL, 'v2')
'''Test with more liquidity stocks
stockUniverse = ['AAPL', 'MSFT', 'NVDA', 'GOOG', 'META', 'ADBE', 'CSCO', 'CRM', 'INTC', 'ORCL',
'JPM', 'BAC', 'GS', 'MS', 'C', 'WFC', 'AXP', 'BLK', 'SCHW', 'SPGI', 'PGNY', 'XCH', 'DRMA',
'JNJ', 'PFE', 'UNH', 'ABT', 'MRK', 'AMGN', 'TMO', 'GILD', 'CVS', 'MDT', 'VVOS', 'GLXG', 'SCNI', 'FDMT', 'SGLY',
'OMEX', 'INBS', 'KSPI', 'HCWC', 'TRIB', 'FUFU', 'MNY', 'MNY', 'NEON', 'BBLG', 'FANH', 'SNOA', 'YIBO',
'AMZN', 'TSLA', 'NKE', 'MCD', 'HD', 'LOW', 'DIS', 'SBUX', 'NFLX', 'PCLN', 'T', 'VZ', 'CMCSA', 'ATVI', 'TTWO', 'SNAP',
'BA', 'CAT', 'HON', 'GE', 'LMT', 'UPS', 'RTX', 'MMM', 'DE', 'NOC', 'EWTX', 'MBLY', 'AUR', 'MSTR', 'XPEV', 'BEKE', 'TAL',
'NIO', 'W', 'UEC', 'JD', 'COHR', 'PYPL', 'AMD', 'AMAT', 'CVNA', 'CRM', 'TSM', 'C', 'ABNB', 'VRT', 'LUMN',
'PG', 'KO', 'PEP', 'WMT', 'COST', 'CAG', 'MDLZ', 'CL', 'SJM', 'GIS',
'XOM', 'CVX', 'BP', 'SLB', 'EOG', 'OXY', 'PXD', 'VLO', 'KMI', 'PSX',
'NEE', 'DUK', 'SO', 'D', 'EXC', 'SRE', 'AEP', 'ED', 'PCG', 'XEL',
'AMT', 'PLD', 'SPG', 'EQIX', 'O', 'VTR', 'DRE', 'AVB', 'PSA', 'WPC']
'''
stockUniverse = [
# Technology
'AAPL', 'MSFT', 'NVDA', 'GOOG', 'META',
# Financials
'JPM', 'BAC', 'GS', 'MS', 'C',
# Healthcare
'JNJ', 'PFE', 'UNH', 'ABT', 'MRK',
# Consumer Discretionary
'AMZN', 'TSLA', 'NKE', 'MCD', 'HD',
# Consumer Staples
'PG', 'KO', 'PEP', 'WMT', 'COST',
# Energy
'XOM', 'CVX', 'BP', 'SLB', 'EOG',
# Industrials
'BA', 'CAT', 'HON', 'GE', 'LMT',
# Communication Services
'DIS', 'CMCSA', 'NFLX', 'T', 'VZ',
# Real Estate
'AMT', 'PLD', 'SPG', 'EQIX', 'O',
# Utilities
'NEE', 'DUK', 'SO', 'D', 'EXC'
]
self.allStocks = [[stock, 0] for stock in stockUniverse]
self.long = []
self.short = []
self.qShort = None
self.qLong = None
self.adjustedQLong = None
self.adjustedQShort = None
self.blacklist = set()
self.longAmount = 0
self.shortAmount = 0
self.timeToClose = None
self.orders_log = []
self.initial_equity = None
def run(self):
#TODO - fix the mail_alert function - renest and refactor the function
self.log_portfolio("start")
orders = self.alpaca.list_orders(status="open")
for order in orders:
self.alpaca.cancel_order(order.id)
print("Waiting for market to open...")
tAMO = threading.Thread(target=self.awaitMarketOpen)
tAMO.start()
tAMO.join()
print("Market opened.")
equity = int(float(self.alpaca.get_account().equity))
buying_power = int(float(self.alpaca.get_account().buying_power))
initial_total_cash_for_trading = equity + buying_power
mail_content = (
'The bot started running on {} at {} UTC.\n'
'Our Total cash available for Trading is : ${:.2f}'
).format(dt.now().strftime('%Y-%m-%d'), dt.now().strftime('%H:%M:%S'), float(initial_total_cash_for_trading))
mail_alert(mail_content, 0)
while True:
clock = self.alpaca.get_clock()
closingTime = clock.next_close.replace(tzinfo=datetime.timezone.utc).timestamp()
currTime = clock.timestamp.replace(tzinfo=datetime.timezone.utc).timestamp()
self.timeToClose = closingTime - currTime
if self.timeToClose < (60 * 15):
print("Market closing soon. Closing positions.")
positions = self.alpaca.list_positions()
for position in positions:
orderSide = 'sell' if position.side == 'long' else 'buy'
qty = abs(int(float(position.qty)))
respSO = []
tSubmitOrder = threading.Thread(target=self.submitOrder, args=(qty, position.symbol, orderSide, respSO))
tSubmitOrder.start()
tSubmitOrder.join()
print("Sleeping until market closes in 15 minutes.")
time.sleep(60 * 15)
self.log_portfolio("end")
self.send_email()
else:
tRebalance = threading.Thread(target=self.rebalance)
tRebalance.start()
tRebalance.join()
time.sleep(60)
def awaitMarketOpen(self):
isOpen = self.alpaca.get_clock().is_open
while not isOpen:
clock = self.alpaca.get_clock()
openingTime = clock.next_open.replace(tzinfo=datetime.timezone.utc).timestamp()
currTime = clock.timestamp.replace(tzinfo=datetime.timezone.utc).timestamp()
timeToOpen = int((openingTime - currTime) / 60)
self.initial_equity = int(float(self.alpaca.get_account().equity))
initial_equity = self.initial_equity
if timeToOpen == 30:
# Add buying power and adjust the quantities for equity and stuff
buying_power = int(float(self.alpaca.get_account().buying_power))
initial_total_cash_for_trading = self.initial_equity + buying_power
# Correcting the string formatting
mail_content = (
f'The market opens in 30 minutes. '
f'Your initial equity (cash) is: ${initial_equity:.2f}. '
f'Our Total cash available Before Trading is: ${initial_total_cash_for_trading:.2f}'
)
mail_alert(mail_content, 60)
print(f"{timeToOpen} minutes til market open.")
equity = int(float(self.alpaca.get_account().equity))
buying_power = int(float(self.alpaca.get_account().buying_power))
initial_total_cash_for_trading = equity + buying_power
print(f'Your initial equity (cash) is: ${initial_equity:.2f}. ')
print(f"Our Total Funding pool with Buying power is : {initial_total_cash_for_trading}")
time.sleep(60)
isOpen = self.alpaca.get_clock().is_open
def rebalance(self):
tRerank = threading.Thread(target=self.rerank)
tRerank.start()
tRerank.join()
orders = self.alpaca.list_orders(status="open")
for order in orders:
self.alpaca.cancel_order(order.id)
print(f"We are taking a long position in: {self.long}")
print(f"We are taking a short position in: {self.short}")
executed = [[], []]
positions = self.alpaca.list_positions()
self.blacklist.clear()
for position in positions:
if self.long.count(position.symbol) == 0:
if self.short.count(position.symbol) == 0:
side = "sell" if position.side == "long" else "buy"
respSO = []
tSO = threading.Thread(target=self.submitOrder, args=(abs(int(float(position.qty))), position.symbol, side, respSO))
tSO.start()
tSO.join()
else:
if position.side == "long":
side = "sell"
respSO = []
tSO = threading.Thread(target=self.submitOrder, args=(int(float(position.qty)), position.symbol, side, respSO))
tSO.start()
tSO.join()
else:
if abs(int(float(position.qty))) == self.qShort:
pass
else:
diff = abs(int(float(position.qty))) - self.qShort
side = "buy" if diff > 0 else "sell"
respSO = []
tSO = threading.Thread(target=self.submitOrder, args=(abs(diff), position.symbol, side, respSO))
tSO.start()
tSO.join()
executed[1].append(position.symbol)
self.blacklist.add(position.symbol)
else:
if position.side == "short":
respSO = []
tSO = threading.Thread(target=self.submitOrder, args=(abs(int(float(position.qty))), position.symbol, "buy", respSO))
tSO.start()
tSO.join()
else:
if int(float(position.qty)) == self.qLong:
pass
else:
diff = abs(int(float(position.qty))) - self.qLong
side = "sell" if diff > 0 else "buy"
respSO = []
tSO = threading.Thread(target=self.submitOrder, args=(abs(diff), position.symbol, side, respSO))
tSO.start()
tSO.join()
executed[0].append(position.symbol)
self.blacklist.add(position.symbol)
respSendBOLong = []
tSendBOLong = threading.Thread(target=self.sendBatchOrder, args=(self.qLong, self.long, "buy", respSendBOLong))
tSendBOLong.start()
tSendBOLong.join()
if respSendBOLong:
respSendBOLong[0][0] += executed[0]
if len(respSendBOLong[0][1]) > 0:
respGetTPLong = []
thread = threading.Thread(target=self.getTotalPrice, args=(self.long, respGetTPLong))
thread.start()
thread.join()
if respGetTPLong and len(respGetTPLong) > 0:
self.qLong = int(self.longAmount // respGetTPLong[0])
else:
print("Error: respGetTPLong is empty or does not contain valid data.")
respSendBOShort = []
tSendBOShort = threading.Thread(target=self.sendBatchOrder, args=(self.qShort, self.short, "sell", respSendBOShort))
tSendBOShort.start()
tSendBOShort.join()
if respSendBOShort:
respSendBOShort[0][0] += executed[1]
if len(respSendBOShort[0][1]) > 0:
respGetTPShort = []
thread = threading.Thread(target=self.getTotalPrice, args=(self.short, respGetTPShort))
thread.start()
thread.join()
if respGetTPShort[0] > 0:
self.adjustedQShort = self.shortAmount // respGetTPShort[0]
else:
self.adjustedQShort = -1
else:
self.adjustedQShort = -1
if self.adjustedQLong is not None and self.adjustedQLong > -1:
self.qLong = int(self.adjustedQLong - self.qLong)
for stock in respSendBOLong[0][0]:
respResendBOLong = []
tResendBOLong = threading.Thread(target=self.submitOrder, args=(self.qLong, stock, "buy", respResendBOLong))
tResendBOLong.start()
tResendBOLong.join()
if self.adjustedQShort is not None and self.adjustedQShort > -1:
self.qShort = int(self.adjustedQShort - self.qShort)
for stock in respSendBOShort[0][0]:
respResendBOShort = []
tResendBOShort = threading.Thread(target=self.submitOrder, args=(self.qShort, stock, "sell", respResendBOShort))
tResendBOShort.start()
tResendBOShort.join()
def rerank(self):
tRank = threading.Thread(target=self.rank)
tRank.start()
tRank.join()
longShortAmount = len(self.allStocks) // 4
self.long = []
self.short = []
for i, stockField in enumerate(self.allStocks):
if i < longShortAmount:
self.short.append(stockField[0])
elif i > (len(self.allStocks) - 1 - longShortAmount):
self.long.append(stockField[0])
# Add buying power and adjust the quantities for equity and stuff
equity = int(float(self.alpaca.get_account().equity))
buying_power = int(float(self.alpaca.get_account().buying_power))
# Calculate total cash for trading without depleting buying power
total_cash_for_trading = equity
if total_cash_for_trading < (equity * 0.45 + equity * 0.55):
total_cash_for_trading += min(buying_power, (equity * 0.45 + equity * 0.55) - equity)
# Long/short ratio of 45/55
self.shortAmount = total_cash_for_trading * 0.45
self.longAmount = total_cash_for_trading - self.shortAmount
respGetTPLong = []
thread = threading.Thread(target=self.getTotalPrice, args=(self.long, respGetTPLong))
thread.start()
thread.join()
respGetTPShort = []
thread = threading.Thread(target=self.getTotalPrice, args=(self.short, respGetTPShort))
thread.start()
thread.join()
self.qLong = int(self.longAmount // respGetTPLong[0])
self.qShort = int(self.shortAmount // respGetTPShort[0])
def getTotalPrice(self, stocks, resp):
totalPrice = 0
for stock in stocks:
try:
stock_data = yf.Ticker(stock)
bars = stock_data.history(period='1d', interval='1m')
if not bars.empty:
totalPrice += bars['Close'].iloc[-1]
else:
print(f"No price data found for {stock}, skipping...")
except Exception as e:
print(f"Failed to download data for {stock}: {e}")
resp.append(totalPrice)
def sendBatchOrder(self, qty, stocks, side, resp):
executed = []
incomplete = []
for stock in stocks:
if self.blacklist.isdisjoint({stock}):
respSO = []
tSubmitOrder = threading.Thread(target=self.submitOrder, args=(qty, stock, side, respSO))
tSubmitOrder.start()
tSubmitOrder.join()
if respSO and not respSO[0]:
incomplete.append(stock)
else:
executed.append(stock)
respSO.clear()
resp.append([executed, incomplete])
def submitOrder(self, qty, stock, side, resp):
if qty is None:
print("Quantity is None, cannot submit order.")
resp.append(False)
return
if qty > 0:
try:
self.alpaca.submit_order(stock, qty, side, "market", "day")
print(f"Market order of | {qty} {stock} {side} | completed.")
self.orders_log.append([stock, qty, side, "completed"])
resp.append(True)
except Exception as e:
print(f"Order of | {qty} {stock} {side} | did not go through: {e}")
self.orders_log.append([stock, qty, side, f"failed: {e}"])
resp.append(False)
else:
print(f"Quantity is 0, order of | {qty} {stock} {side} | not completed.")
self.orders_log.append([stock, qty, side, "not completed: qty is 0"])
resp.append(True)
def log_portfolio(self, time_of_day):
positions = self.alpaca.list_positions()
with open(f'portfolio_{time_of_day}.csv', mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerow(["Symbol", "Qty", "Side"])
for position in positions:
writer.writerow([position.symbol, position.qty, position.side])
def getPercentChanges(self):
length = 10
for i, stock in enumerate(self.allStocks):
data = yf.download(stock[0], period='1d', interval='1m')
if len(data) >= length:
open_price = data.iloc[0]['Open']
close_price = data.iloc[-1]['Close']
self.allStocks[i][1] = (close_price - open_price) / open_price
else:
self.allStocks[i][1] = 0
def rank(self):
tGetPC = threading.Thread(target=self.getPercentChanges)
tGetPC.start()
tGetPC.join()
self.allStocks.sort(key=lambda x: x[1])
def send_email(self):
msg = MIMEMultipart()
msg['From'] = 'Frankline & Co. HFT Day Trading Bot'
msg['To'] = EMAIL_RECEIVER
msg['Subject'] = "Post-Market Daily Trade Report"
msg['Signature'] = "Making HFT Fun and Profitable"
profit = int(float(self.alpaca.get_account().equity)) - self.initial_equity
order_count = len(self.orders_log)
body = (
f"Hello Trader, Attached is the Daily trade report from Day Trading.\n"
f"I executed : {order_count} orders.\n\n"
f"Our P/L today is : ${profit:.2f}"
)
msg.attach(MIMEText(body, 'plain'))
filename = "HFT_Orders.csv"
with open(filename, "w", newline='') as file:
writer = csv.writer(file)
writer.writerow(["Stock", "Quantity", "Side", "Status"])
writer.writerows(self.orders_log)
attachment = open(filename, "rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', f"attachment; filename= {filename}")
msg.attach(part)
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(EMAIL_USER, EMAIL_PASSWORD)
text = msg.as_string()
server.sendmail(EMAIL_USER, EMAIL_RECEIVER, text)
server.quit()
# Run the LongShort class
ls = LongShort()
ls.run()