-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
624 lines (522 loc) · 24.9 KB
/
run.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
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
from robinhood import Robinhood
import numpy as np
import math
import os
import random
import time
import smtplib
import datetime
from pymongo import MongoClient
import quandl
import requests
try:
import config
print('using local config file')
rhuser = config.rhuser
rhpass = config.rhpass
guser = config.guser
gpass = config.gpass
mongodb_uri = config.mongodb_uri
quandl_key = config.quandl_key
mailgun_api_key = config.mailgun_api_key
mailgun_domain = config.mailgun_domain
diag_email_dest = config.diag_email_dest
except:
print('using environment variable')
rhuser = os.getenv('RHUSER')
rhpass = os.getenv('RHPASS')
guser = os.getenv('GUSER')
gpass = os.getenv('GPASS')
mongodb_uri = os.getenv('MONGODB_URI')
quandl_key = os.getenv('QUANDL_KEY')
mailgun_api_key = os.getenv('MAILGUN_API_KEY')
mailgun_domain = os.getenv('MAILGUN_DOMAIN')
diag_email_dest = os.getenv('DIAG_EMAIL_DEST')
#from https://stackoverflow.com/questions/865618/how-can-i-perform-divison-on-a-datetime-timedelta-in-python
def divtd(td1, td2):
us1 = td1.microseconds + 1000000 * (td1.seconds + 86400 * td1.days)
us2 = td2.microseconds + 1000000 * (td2.seconds + 86400 * td2.days)
return float(us1) / us2
def run_gather_data():
#code that gets and logs performance data
print("Gathering Data")
success = True
rh = Robinhood()
now = datetime.datetime.utcnow()
try:
success = rh.marketOpenCheck()
if not success:
print('markets are closed')
else:
print('markets are open')
except Exception as e:
success = False
print('rh market check error ', str(e))
send_email(mailgun_domain,mailgun_api_key,diag_email_dest,('resiliant-trader data gather error '+str(datetime.datetime.now())),("rh market check error. Unexpected error: "+str(e)))
if success:
try:
success = rh.login(username=rhuser, password=rhpass)
if success:
print('robinhood login succesful')
else:
print('robinhood login unsuccesful')
except Exception as e:
success = False
print('rh login error ', str(e))
send_email(mailgun_domain,mailgun_api_key,diag_email_dest,('resiliant-trader data gather error '+str(datetime.datetime.now())),("rh login error. Unexpected error: "+str(e)))
if success:
try:
client = MongoClient(mongodb_uri)
db = client.get_database()
except Exception as e:
print('mongo login error ', str(e))
success = False
send_email(mailgun_domain,mailgun_api_key,diag_email_dest,('resiliant-trader data gather error '+str(datetime.datetime.now())),("mongo login error. Unexpected error: "+str(e)))
if success:
try:
#get pricing data
spyAskPrice = rh.ask_price('SPY')
spyBidPrice = rh.bid_price('SPY')
spyAvgCost = (spyAskPrice+spyBidPrice)/2
print('spyAvgCost = ', spyAvgCost)
tltAskPrice = rh.ask_price('TLT')
tltBidPrice = rh.bid_price('TLT')
tltAvgCost = (tltAskPrice+tltBidPrice)/2
print('tltAvgCost = ', tltAvgCost)
except Exception as e:
print('etf price error ', str(e))
success = False
send_email(mailgun_domain,mailgun_api_key,diag_email_dest,('resiliant-trader data gather error '+str(datetime.datetime.now())),("etf price error. Unexpected error: "+str(e)))
if success:
try:
#get portfolioValue
portfolioValue = rh.equity()
tltPosition = 0
spyPosition = 0
print('portfolioValue =', portfolioValue)
openPositions = rh.securities_owned()['results']
for position in openPositions:
instrumentURL = position['instrument']
positionTicker = rh.get_url(instrumentURL)['symbol']
positionQuantity = float(position['quantity'])
if (positionTicker == 'SPY'):
spyPosition = positionQuantity
if (positionTicker == 'TLT'):
tltPosition = positionQuantity
print('spyPosition = ',spyPosition)
print('tltPosition = ',tltPosition)
except Exception as e:
print('portfolio value error ', str(e))
success = False
send_email(mailgun_domain,mailgun_api_key,diag_email_dest,('resiliant-trader data gather error '+str(datetime.datetime.now())),("portfolio value error. Unexpected error: "+str(e)))
if success:
try:
#get treasury risk free rate
quandl.ApiConfig.api_key = quandl_key
riskFree = (quandl.get("USTREASURY/BILLRATES.3", rows=1,returns='numpy')[0])[1]
print('riskFree =', riskFree)
except Exception as e:
print('risk free error ', str(e))
success = False
send_email(mailgun_domain,mailgun_api_key,diag_email_dest,('resiliant-trader data gather error '+str(datetime.datetime.now())),("risk free error. Unexpected error: "+str(e)))
if success:
try:
#get last data
lastData = db.rawPrices.find_one(sort=[("timestamp", -1)])
lastTimestamp = lastData['timestamp']
lastSpy = lastData['spy']
lastTlt = lastData['tlt']
lastPortfolio = lastData['portfolio']
lastRiskFree = lastData['annualized90day']
except Exception as e:
print('error getting previous data ', str(e))
success = False
send_email(mailgun_domain,mailgun_api_key,diag_email_dest,('resiliant-trader data gather error '+str(datetime.datetime.now())),("error getting previous data. Unexpected error: "+str(e)))
if success:
try:
# save data
rawData = {
"timestamp":now,
"spy":spyAvgCost,
"tlt":tltAvgCost,
"portfolio":portfolioValue,
"annualized90day":riskFree
}
data_id = db.rawPrices.insert_one(rawData).inserted_id
print("data saved to",data_id)
except Exception as e:
print('data save error ', str(e))
success = False
send_email(mailgun_domain,mailgun_api_key,diag_email_dest,('resiliant-trader data gather error '+str(datetime.datetime.now())),("error calculating change. Unexpected error: "+str(e)))
if success:
try:
# calculate percentage changes
spyChange = (spyAvgCost-lastSpy)/lastSpy
print('spyChange = ',spyChange)
tltChange = (tltAvgCost-lastTlt)/lastTlt
print('tltChange = ',tltChange)
portfolioChange = (portfolioValue-lastPortfolio)/lastPortfolio
print('portfolioChange = ',portfolioChange)
elapsedTime = now - lastTimestamp
year = datetime.timedelta(days=365)
treasuryChange = ((1+(((lastRiskFree+riskFree)/2))/100)**(divtd(elapsedTime,year)))-1
print('treasuryChange = ',treasuryChange)
except Exception as e:
print('error calculating change ', str(e))
success = False
send_email(mailgun_domain,mailgun_api_key,diag_email_dest,('resiliant-trader data gather error '+str(datetime.datetime.now())),("error calculating change. Unexpected error: "+str(e)))
if success:
try:
# save data
percentageData = {
"timestamp":now,
"spy":spyChange,
"tlt":tltChange,
"portfolio":portfolioChange,
"90dayTreasury":treasuryChange
}
data_id = db.percentageMove.insert_one(percentageData).inserted_id
print("data saved to",data_id)
except Exception as e:
print('data save error ', str(e))
success = False
send_email(mailgun_domain,mailgun_api_key,diag_email_dest,('resiliant-trader data gather error '+str(datetime.datetime.now())),("data save error. Unexpected error: "+str(e)))
if success:
try:
# calculate tracking
spyTarget = calcAlloc(rh)
print('spyTarget = ',spyTarget)
tltTarget = 1-spyTarget
print('tltTarget = ',tltTarget)
spyActual = (spyPosition*spyAvgCost)/portfolioValue
tltActual = (tltPosition*tltAvgCost)/portfolioValue
print('spyActual = ',spyActual)
print('tltActual = ',tltActual)
except Exception as e:
print('error calculating tracking ', str(e))
success = False
send_email(mailgun_domain,mailgun_api_key,diag_email_dest,('resiliant-trader data gather error '+str(datetime.datetime.now())),("error calculating tracking. Unexpected error: "+str(e)))
if success:
try:
# save tracking data
trackingData = {
"timestamp":now,
"spyActual":spyActual,
"tltActual":tltActual,
"spyTarget":spyTarget,
"tltTarget":tltTarget
}
data_id = db.tracking.insert_one(trackingData).inserted_id
print("data saved to",data_id)
except Exception as e:
print('tracking data save error ', str(e))
success = False
send_email(mailgun_domain,mailgun_api_key,diag_email_dest,('resiliant-trader data gather error '+str(datetime.datetime.now())),("tracking data save error. Unexpected error: "+str(e)))
def send_email(domain,key,recipient, subject, body):
mailgun_key = key
mailgun_domain = domain
request_url = 'https://api.mailgun.net/v3/'+mailgun_domain+'/messages'
request = requests.post(request_url, auth=('api', mailgun_key), data={
'from': 'trader@resiliant-trader.com',
'to': recipient,
'subject': subject,
'text': body
})
print(request.status_code)
print(request.text)
def recommendInitialTarget(portfolioValue,spyAllocationPercentage,tltAllocationPercentage,spyBuyPrice,tltBuyPrice):
spyTargetAllocation = spyAllocationPercentage*portfolioValue
tltTargetAllocation = tltAllocationPercentage*portfolioValue
spyTargetShares = math.floor(spyTargetAllocation/spyBuyPrice)
tltTargetShares = math.floor(tltTargetAllocation/tltBuyPrice)
return spyTargetShares,tltTargetShares
def recommendTarget(portfolioValue,spyAllocationPercentage,tltAllocationPercentage,spyBuyPrice,tltBuyPrice):
allocationRatio = math.floor(spyAllocationPercentage/tltAllocationPercentage)
tltTargetShares = math.floor(portfolioValue/(tltBuyPrice+(spyBuyPrice*allocationRatio)))
spyTargetShares =math.floor(tltTargetShares*allocationRatio)
return spyTargetShares,tltTargetShares
def targetTotalCost(spyTargetShares,tltTargetShares,spyBuyPrice,tltBuyPrice):
targetPurchaseCost = (spyTargetShares*spyBuyPrice)+(tltTargetShares*tltBuyPrice)
return targetPurchaseCost
def allocationPercentage(shares,cost,totalCost):
percentage = (shares*cost)/totalCost
return percentage
def allocationLoss(spyTarget,spyAchieved,tltTarget,tltAchieved):
loss = math.sqrt((spyTarget-spyAchieved)**2+(tltTarget-tltAchieved)**2)
return loss
def calcAlloc(rh):
spyHist = rh.get_historical_quote('SPY','5minute','week')
tltHist = rh.get_historical_quote('TLT','5minute','week')
spyPrices = spyHist[:,4]
spyVolumes = spyHist[:,5]
tltPrices = tltHist[:,4]
tltVolumes = tltHist[:,5]
spyVWAP = np.average(spyPrices, axis=0, weights=spyVolumes)
tltVWAP = np.average(tltPrices, axis=0, weights=tltVolumes)
spyPricesNorm = spyPrices/spyVWAP
tltPricesNorm = tltPrices/tltVWAP
spyVolatility = np.std(spyPricesNorm)
tltVolatility = np.std(tltPricesNorm)
totalVolatility = spyVolatility+tltVolatility
spyRawAllocation = 1-(spyVolatility/totalVolatility)
spyAllocation = spyRawAllocation
#spyAllocation = 1/(1+math.exp(-20*(spyRawAllocation-.5)))
if spyAllocation > 1:
spyAllocation = 1
if spyAllocation < 0:
spyAllocation = 0
return spyAllocation
def run_trader():
try:
print("running trader at: "+str(datetime.datetime.now()))
message = "running trader at: "+str(datetime.datetime.now())
success = True
rh = Robinhood()
success = rh.marketOpenCheck()
if not success:
print('markets are closed')
message += '\nmarkets are closed'
else:
print('markets are open')
message += '\nmarkets are open'
if success:
success = rh.login(username=rhuser, password=rhpass)
if success:
print('login succesful')
message += '\nlogin succesful'
else:
print('login unsuccesful')
message += '\nlogin unsuccesful'
if success:
#exit extra postions
openPositions = rh.securities_owned()['results']
sellOrders = {}
for position in openPositions:
instrumentURL = position['instrument']
positionTicker = rh.get_url(instrumentURL)['symbol']
positionQuantity = position['quantity']
if (positionTicker != 'SPY') and (positionTicker != 'TLT'):
print('position in ', positionTicker, ' is not needed, selling')
stock_instrument = rh.instruments(positionTicker)[0]
sellOrders[positionTicker] = rh.place_immediate_market_order(instrumentURL,positionTicker,'gfd',positionQuantity,'sell')
if sellOrders == {}:
print('no extra positions found to close')
message += '\nno extra positions found to close'
else:
print(sellOrders)
orderOutcome = 'unresolved'
while orderOutcome != 'resolved':
remainingUnresolved = False
for order in sellOrders:
orderDetail = sellOrders[order]
orderDetail['status'] = rh.check_order_status(orderDetail['url'])
if orderDetail['status'] == 'unresolved':
remainingUnresolved = True
if not remainingUnresolved:
orderOutcome = 'resolved'
else:
print('remaining unresolved orders, waiting')
message += '\nremaining unresolved orders, waiting'
time.sleep(60)
for order in sellOrders:
orderDetail = sellOrders[order]
if orderDetail['status'] == 'failure':
success = False
if not success:
print('unable to sell extra positions correctly')
message += '\nunable to sell extra positions correctly'
if success:
#get portfolio current value
portfolioValue = rh.equity()
print('portfolioValue =', portfolioValue)
message += '\nportfolioValue = '
message += str(portfolioValue)
#allocate portfolio
spyAllocationPercentage = calcAlloc(rh)
tltAllocationPercentage = 1-spyAllocationPercentage
print('spyAllocationPercentage = ', spyAllocationPercentage)
message += '\nspyAllocationPercentage = '
message += str(spyAllocationPercentage)
print('tltAllocationPercentage = ', tltAllocationPercentage)
message += '\ntltAllocationPercentage = '
message += str(tltAllocationPercentage)
spyTargetAllocation = spyAllocationPercentage*portfolioValue
tltTargetAllocation = tltAllocationPercentage*portfolioValue
print('spyTargetAllocation = ', spyTargetAllocation)
message += '\nspyTargetAllocation = '
message += str(spyTargetAllocation)
print('tltTargetAllocation = ', tltTargetAllocation)
message += '\ntltTargetAllocation = '
message += str(tltTargetAllocation)
#get pricing data
spyAskPrice = rh.ask_price('SPY')
spyBidPrice = rh.bid_price('SPY')
spyAvgCost = (spyAskPrice+spyBidPrice)/2
spyBuyPrice = spyAskPrice+(spyAskPrice - spyBidPrice)
spySellPrice = spyBidPrice-(spyAskPrice - spyBidPrice)
print('spyAskPrice = ', spyAskPrice)
message += '\nspyAskPrice = '
message += str(spyAskPrice)
print('spyBidPrice = ', spyBidPrice)
message += '\nspyBidPrice = '
message += str(spyBidPrice)
tltAskPrice = rh.ask_price('TLT')
tltBidPrice = rh.bid_price('TLT')
tltAvgCost = (tltAskPrice+tltBidPrice)/2
tltBuyPrice = tltAskPrice+(tltAskPrice - tltBidPrice)
tltSellPrice = tltBidPrice-(tltAskPrice - tltBidPrice)
print('tltAskPrice = ', tltAskPrice)
message += '\ntltAskPrice = '
message += str(tltAskPrice)
print('tltBidPrice = ', tltBidPrice)
message += '\ntltBidPrice = '
message += str(tltBidPrice)
#recommend position sizes
#[spyTargetShares,tltTargetShares] = recommendTarget(portfolioValue,spyAllocationPercentage,tltAllocationPercentage,spyBuyPrice,tltBuyPrice)
[spyTargetShares,tltTargetShares] = recommendInitialTarget(portfolioValue,spyAllocationPercentage,tltAllocationPercentage,spyBuyPrice,tltBuyPrice)
print('spyTargetShares = ', spyTargetShares)
message += '\nspyTargetShares = '
message += str(spyTargetShares)
print('tltTargetShares = ', tltTargetShares)
message += '\ntltTargetShares = '
message += str(tltTargetShares)
targetPurchaseCost = targetTotalCost(spyTargetShares,tltTargetShares,spyBuyPrice,tltBuyPrice)
spyTargetAllocationPercentage = allocationPercentage(spyTargetShares,spyBuyPrice,targetPurchaseCost)
tltTargetAllocationPercentage = allocationPercentage(tltTargetShares,tltBuyPrice,targetPurchaseCost)
print('spyTargetAllocationPercentage = ',spyTargetAllocationPercentage)
message += '\nspyTargetAllocationPercentage = '
message += str(spyTargetAllocationPercentage)
print('tltTargetAllocationPercentage = ',tltTargetAllocationPercentage)
message += '\ntltTargetAllocationPercentage = '
message += str(tltTargetAllocationPercentage)
targetLoss = allocationLoss(spyTargetAllocationPercentage,spyAllocationPercentage,tltTargetAllocationPercentage,tltAllocationPercentage)
print('target loss = ',targetLoss)
targetRemainingCash = portfolioValue-targetPurchaseCost
print('targetPurchaseCost = ', targetPurchaseCost)
message += '\ntargetPurchaseCost = '
message += str(targetPurchaseCost)
print('targetRemainingCash = ', targetRemainingCash)
message += '\ntargetRemainingCash = '
message += str(targetRemainingCash)
#detemine required rebalancing
spyRequired = spyTargetShares
tltRequired = tltTargetShares
for position in openPositions:
instrumentURL = position['instrument']
positionTicker = rh.get_url(instrumentURL)['symbol']
positionQuantity = float(position['quantity'])
if (positionTicker == 'SPY'):
spyRequired = spyTargetShares-positionQuantity
if (positionTicker == 'TLT'):
tltRequired = tltTargetShares-positionQuantity
print('spyRequired = ',spyRequired)
message += '\nspyRequired = '
message += str(spyRequired)
print('tltRequired = ',tltRequired)
message += '\ntltRequired = '
message += str(tltRequired)
spyInstrumentUrl = (rh.instruments('SPY')[0])['url']
tltInstrumentUrl = (rh.instruments('TLT')[0])['url']
if success:
#sell positions
if spyRequired < 0.0:
print('selling ',-spyRequired,' of SPY')
spySellOrder = rh.place_immediate_market_order(spyInstrumentUrl,'SPY','gfd',-spyRequired,'sell')
print(spySellOrder)
orderOutcome = 'unresolved'
while orderOutcome != 'resolved':
remainingUnresolved = False
orderResponse = rh.check_order_status(spySellOrder['url'])
if orderResponse == 'unresolved':
remainingUnresolved = True
if not remainingUnresolved:
orderOutcome = 'resolved'
else:
print('remaining unresolved spySell, waiting')
message += '\nremaining unresolved spySell, waiting'
time.sleep(60)
if orderResponse == 'failure':
success = False
if not success:
print('unable to sell required spy')
message += '\nunable to sell required spy'
if success:
if tltRequired < 0.0:
print('selling ',-tltRequired,' of TLT')
tltSellOrder = rh.place_immediate_market_order(tltInstrumentUrl,'TLT','gfd',-tltRequired,'sell')
print(tltSellOrder)
orderOutcome = 'unresolved'
while orderOutcome != 'resolved':
remainingUnresolved = False
orderResponse = rh.check_order_status(tltSellOrder['url'])
if orderResponse == 'unresolved':
remainingUnresolved = True
if not remainingUnresolved:
orderOutcome = 'resolved'
else:
print('remaining unresolved tltSell, waiting')
message += '\nremaining unresolved tltSell, waiting'
time.sleep(60)
if orderResponse == 'failure':
success = False
if not success:
print('unable to sell required tlt')
message += '\nunable to sell required tlt'
#buy positions
if success:
if spyRequired > 0.0:
print('buying ',spyRequired,' of SPY')
spyBuyOrder = rh.place_immediate_market_order(spyInstrumentUrl,'SPY','gfd',spyRequired,'buy',round(spyBuyPrice, 3))
print(spyBuyOrder)
orderOutcome = 'unresolved'
while orderOutcome != 'resolved':
remainingUnresolved = False
orderResponse = rh.check_order_status(spyBuyOrder['url'])
if orderResponse == 'unresolved':
remainingUnresolved = True
if not remainingUnresolved:
orderOutcome = 'resolved'
else:
print('remaining unresolved spyBuy, waiting')
message += '\nremaining unresolved spyBuy, waiting'
time.sleep(60)
if orderResponse == 'failure':
success = False
if not success:
print('unable to buy required spy')
message += '\nunable to buy required spy'
if success:
if tltRequired > 0.0:
print('buying ',tltRequired,' of TLT')
tltBuyOrder = rh.place_immediate_market_order(tltInstrumentUrl,'TLT','gfd',tltRequired,'buy',round(tltBuyPrice, 3))
print(tltBuyOrder)
orderOutcome = 'unresolved'
while orderOutcome != 'resolved':
remainingUnresolved = False
orderResponse = rh.check_order_status(tltBuyOrder['url'])
if orderResponse == 'unresolved':
remainingUnresolved = True
if not remainingUnresolved:
orderOutcome = 'resolved'
else:
print('remaining unresolved tltBuy, waiting')
message += '\nremaining unresolved tltBuy, waiting'
time.sleep(60)
if orderResponse == 'failure':
success = False
if not success:
print('unable to buy required tlt')
message += '\nunable to buy required tlt'
if success:
success = rh.logout()
if not success:
print('unable to logout')
message += '\nunable to logout'
else:
print('succesfully logged out')
message += '\nsuccesfully logged out'
send_email(mailgun_domain,mailgun_api_key,diag_email_dest,('resiliant-trader log '+str(datetime.datetime.now())),message)
except Exception as e:
print("Unexpected error:", str(e))
send_email(mailgun_domain,mailgun_api_key,diag_email_dest,('resiliant-trader log '+str(datetime.datetime.now())),("Unexpected error: "+str(e)))
raise