forked from mpiccirilli/trading_bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
175 lines (130 loc) · 6.7 KB
/
main.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
import base64
import utils
import config
import db
import emails
import pandas as pd
import aiohttp
import asyncio
def main(event, context):
"""Triggered from a message on a Cloud Pub/Sub topic.
Args:
event (dict): Event payload.
context (google.cloud.functions.Context): Metadata for the event.
"""
pubsub_message = base64.b64decode(event['data']).decode('utf-8')
####################################
########## Check tickers ###########
####################################
if pubsub_message == 'Ticker':
print('Getting list of predefined tickers')
defaultTickers = pd.read_csv('tickers.txt')
defaultTickerSet = set(defaultTickers['tickers'].values.tolist())
print("Going to Wikipedia to get list of current tickers in S&P500")
wikiTickers = utils.get_sp500_tickers()
# Need to filter out anything with a period.
filteredWikiTickers = [x for x in wikiTickers if '.' not in x]
# Now double check to make sure there's a max of 4 letters, remove otherwise
finalWikiTickers = [x for x in filteredWikiTickers if len(x)<=4]
wikiTickerSet = set(finalWikiTickers)
# Check if website tickers are different:
defaultNotInWiki = defaultTickerSet.difference(wikiTickerSet)
wikiNotInDefault = wikiTickerSet.difference(defaultTickerSet)
removeTicker = pd.DataFrame({'RemoveTickers': list(defaultNotInWiki)})
addTicker = pd.DataFrame({'AddTickers': list(wikiNotInDefault)})
if ((len(removeTicker)>0) | (len(addTicker)>0)):
print('Need to change some tickers..')
html, subject = emails.ticker_check_email(addTicker.to_html(), removeTicker.to_html())
response = emails.send_email(request=pubsub_message, html_content=html, subject=subject)
print(response)
else:
print("No tickers to change")
########################################
########## Run trading algo ###########
########################################
if pubsub_message == 'Trading':
# Get Tickers:
print('getting tickers')
tickers = pd.read_csv('tickers.txt')
tickers = tickers['tickers'].values.tolist()
print('getting access token')
newAccess = utils.get_access_token()
access_token = newAccess['access_token']
expires_in = newAccess['expires_in']
print('Running the algo..')
hist_stock_data = utils.get_stocks(token=access_token, tickers=tickers, expires_in=expires_in)
print('Calculating trade metrics..')
trade_metric_df = utils.calc_trade_metrics(stock_data=hist_stock_data)
print('Shape of trade metrics: ', trade_metric_df.shape)
print("Getting buy/sell symbols...")
(algoBuys, algoSells) = utils.find_trades(data_frame=trade_metric_df, token=access_token, tickers=tickers)
print('Submit the orders!')
# This is the old way - the slow way!
#(buys, sells) = utils.make_trades(positionsToBuy=algoBuys, positionsToSell=algoSells, token=access_token)
# Async order submissions
orderStart = pd.to_datetime('today')
asyncio.run(utils.make_trades_async(buySymbolsList=algoBuys, sellSymbolsList=algoSells, token=access_token))
orderEnd = pd.to_datetime('today')
print('Time took to send orders: ', (orderEnd - orderStart))
buyToday = hist_stock_data[(hist_stock_data['symbol'].isin(algoBuys) &
(hist_stock_data['datetime']==pd.to_datetime('today').strftime('%Y-%m-%d')))]['close'].sum()
sellToday = hist_stock_data[(hist_stock_data['symbol'].isin(algoSells) &
(hist_stock_data['datetime']==pd.to_datetime('today').strftime('%Y-%m-%d')))]['close'].sum()
maxNeeded = hist_stock_data[(hist_stock_data['datetime']==pd.to_datetime('today').strftime('%Y-%m-%d'))]['close'].sum()
print('Approx amount bought today: ', round(buyToday,2))
print('Approx amount sold today: ', round(sellToday,2))
print('Maximum possible needed: ', round(maxNeeded,2))
print('Trading bot deployed')
#######################################
# Save todays trades to DB #
#######################################
if pubsub_message == 'MorningTrades':
print('getting access token')
newAccess = utils.get_access_token()
access_token = newAccess['access_token']
expires_in = newAccess['expires_in']
print('Pulling and saving todays trades...')
today = pd.to_datetime('today').strftime('%Y-%m-%d')
todaysTrades = utils.get_historical_trades_DF(start_date=today, end_date=today, token=access_token)
print('There were {} trades today..'.format(todaysTrades.shape[0]))
print('Saving trades to the DB..')
db.save_trades_gbq(ordersDF=todaysTrades)
print('Done saving todays trades..')
html, subject = emails.daily_trades(tradesDF=todaysTrades)
response = emails.send_email(pubsub_message, html_content=html, subject=subject)
print('Email response: ', response)
print('Done saving and send todays trades...')
#######################################
# Shut it down! #
#######################################
if pubsub_message == 'Kill':
# Get Tickers:
print('getting tickers')
tickers = pd.read_csv('tickers.txt')
tickers = tickers['tickers'].values.tolist()
print('getting access token')
newAccess = utils.get_access_token()
access_token = newAccess['access_token']
orderStart = pd.to_datetime('today')
failures = utils.shut_it_down(token=access_token, tickers=tickers)
orderEnd = pd.to_datetime('today')
print('Trades that failed: ', failures)
print('Time took to send orders: ', (orderEnd - orderStart))
#######################################
# Update Refresh Token #
#######################################
if pubsub_message == 'Refresh Token':
newAccess = utils.get_access_token()
access_token = newAccess['access_token']
# Get a new token
newRefreshToken = utils.get_new_refresh_token(token=access_token)
print('New creds: ', newRefreshToken)
configFile = open("config.py").read().splitlines()
newString = "TD_REFRESH_TOKEN=\'{}\'".format(newRefreshToken['refresh_token'])
print(newString)
# Location of token string
configFile[1] = newString
with open('config.py', 'w') as f:
for item in configFile:
f.write("%s\n" % item)
print('Saved new refresh token')