-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtrade_common.py
More file actions
543 lines (442 loc) · 22.1 KB
/
trade_common.py
File metadata and controls
543 lines (442 loc) · 22.1 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
# Common core of functions for trading SMOT trades.
# See StockMartetOptionsTrading.net for info on trading
# Code by Derek Jones
from genericpath import exists
from logging import makeLogRecord
from os import write
from tda import orders, utils
from tda.orders.options import bull_put_vertical_open, bull_put_vertical_close
from tda.orders.generic import OrderBuilder
from tda.orders.common import (
Duration,
OrderStrategyType,
OrderType,
Session,
ComplexOrderStrategyType,
OptionInstruction,
Destination
)
from tda.auth import easy_client
from tda.client import Client
from tda.utils import Utils
import json, time, httpx, sys
from datetime import datetime, timedelta
import bisect , os.path, csv
import pandas as pd
import pandas_ta as ta
import config
#Used for detail logging from TDA-API
#import logging
#logging.basicConfig(filename='trade_debug.log', level=logging.DEBUG)
#logging.getLogger('').addHandler(logging.StreamHandler())
##############################################################################################
def test_filter (filter_name, stock) :
print("Testing for", filter_name,"filter with", stock,".")
# flag to trade or not
make_trade = False
#Setup Client
c = easy_client(
api_key= config.API_KEY,
redirect_uri=config.REDIRECT_URI,
token_path=config.TOKEN_PATH)
# test for filter
#grab price data
resp = c.get_price_history( stock,
period_type=Client.PriceHistory.PeriodType.MONTH,
period=Client.PriceHistory.Period.TWO_MONTHS,
frequency_type=Client.PriceHistory.FrequencyType.DAILY,
frequency=Client.PriceHistory.Frequency.DAILY)
history=resp.json( )
df = pd.DataFrame(history["candles"])
df = df.sort_index()
#convert date colum to readable text
df["date"] = pd.to_datetime(df['datetime'], unit='ms')
# calculate emas for each
df["21ema"] = ta.ema(df["close"], length=21)
df["8ema"] = ta.ema(df["close"], length=8)
df["5ema"] = ta.ema(df["close"], length=5)
df["3ema"] = ta.ema(df["close"], length=3)
#clean up unused columns
del df["volume"]
del df["datetime"]
print("Pricing History:")
print ( df.tail())
print("")
if filter_name =='Alpha5' :
#Is last night's 5ema over day before?
print("Calculating Alpha 5 filter")
if df["5ema"].iloc[-1]> df["5ema"].iloc[-2] :
print ("Good to trade")
make_trade=True
elif filter_name =='CloseOver21' :
# Is last night's closing over 21ema?
print ("Calculating Close over 21ema")
if df["close"].iloc[-1] > df["21ema"].iloc[-1] :
print ("Good to trade")
make_trade=True
elif filter_name =='8over21' :
# Is 8ema over 21ema?
print ("Calculating 8ema over 21ema")
if df["8ema"].iloc[-1] > df["21ema"].iloc[-1] :
print ("Good to trade")
make_trade=True
elif filter_name =='3over8' :
# Is 3ema over 8ema
print ("Calculating 3ema over 8ema")
if df["3ema"].iloc[-1] > df["8ema"].iloc[-1] :
print ("Good to trade")
make_trade=True
elif filter_name =='Alpha3' :
#Is last night's 3ema over day before?
print("Calculating Alpha 3 filter")
if df["3ema"].iloc[-1]> df["3ema"].iloc[-2] :
print ("Good to trade")
make_trade=True
elif filter_name =='none' :
# no filter, proceed
print("No Filter used")
make_trade=True
else :
print("ERROR -- Filter not found!")
make_trade=False
return make_trade
##############################################################################
def nicklefy (org_price):
# Convert a price to the nearest nickle. SPX options are priced at 5 cent increments
new_price = org_price * 100 #bring up to whole
new_price = round( new_price/5, 0) *5 / 100 # convert to a 5 cent mark
new_price = round(new_price, 2)
return new_price
##############################################################################
def check_fulfillment (order, order_id, org_price, decrement, underlying):
# check to see if order is filled.
#Need existing order object, the TDA order_id,
# the original price, how much to subtract each loop, & what the underlying stock is
# (for 5 cent marking)
make_trade = True
time.sleep(60) # wait 60 seconds
#Setup Client
client = easy_client(
api_key= config.API_KEY,
redirect_uri=config.REDIRECT_URI,
token_path=config.TOKEN_PATH)
order_status = client.get_order(order_id, config.ACCOUNT_ID).json()
lower_price = org_price
#while order_status['status'] not in ['FILLED', 'REJECTED', 'CANCELED'] :
for loop_count in range(5):
order_status = client.get_order(order_id, config.ACCOUNT_ID).json()
print("Order status:", order_status['status'])
if order_status['status'] in ['FILLED', 'REJECTED', 'CANCELED'] :
break
#if order_status['status'] in ['QUEUED'] :
# print(" Order still in queue. Waiting 2 minutes.")
# time.sleep(120) # wait 120 seconds
# if order_status['status'] in ['FILLED', 'REJECTED', 'CANCELED'] :
# break
print(" Changing price by",decrement,"and reordering. ",loop_count)
#change price
lower_price = (lower_price - decrement ) #lower price
if underlying == '$SPX.X': #SPX needs to be nickled
print("Nickefing the price for SPX")
lower_price= nicklefy( lower_price ) # convert to a 5 cent mark
print("New price : {:.2f}".format(lower_price))
order = order.copy_price(str(lower_price))
r = client.replace_order(config.ACCOUNT_ID, order_id, order)
print("Order status code - " ,r.status_code)
if r.status_code < 400 : #http codes under 400 are success. usually 200 or 201
order_id = Utils(client, config.ACCOUNT_ID).extract_order_id(r)
print ("Order placed, order ID-", order_id )
else :
print("FAILED - placing the order failed.")
make_trade = False # stop the closing order
break
# wait 60 sec and loop
time.sleep(60) # wait 60 seconds
print("Final order status:", order_status['status'])
trade_logger(order_status)
return order_id, make_trade, lower_price
##############################################################################
def trade_logger(order_status ):
#record the detail about the order using pandas cvs
# if file not exist, create with headers
if not os.path.exists( config.TRADE_LOG) :
with open( config.TRADE_LOG, mode='w') as trade_log_file :
trade_writer = csv.writer(trade_log_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
trade_writer.writerow(["Date","Order ID","Leg 1","Leg 2",'Price','Quantity','Status' ])
#print (json.dumps(order_status, indent =4))
with open( config.TRADE_LOG, mode='a+') as trade_log_file :
trade_writer = csv.writer(trade_log_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
trade_writer.writerow([ order_status['enteredTime'],order_status['orderId'],
order_status['orderLegCollection'][0]['instrument']['symbol'],
order_status['orderLegCollection'][1]['instrument']['symbol'],
order_status['price'],order_status['quantity'],order_status['status']])
trade_log_file.close
return
##############################################################################
def trading_vertical(trade_strat, trade_date ):
#import trade strategy and trade date (expiration date)
#Setup Client
c = easy_client(
api_key= config.API_KEY,
redirect_uri=config.REDIRECT_URI,
token_path=config.TOKEN_PATH)
# Setup chain request
if trade_strat["type"] =='PUT' :
trade_type = c.Options.ContractType.PUT
elif trade_strat["type"] =='CALL' :
trade_type = c.Options.ContractType.CALL
# Get the options chain from TDA
results = c.get_option_chain( trade_strat["under"],
contract_type= trade_type,
strike_count= 100,
include_quotes=True,
from_date= trade_date,
to_date= trade_date )
# need to add multi account support
results = results.json() # pull out the json data
if results['status'] == 'FAILED':
print ("Getting the option chain failed for date", trade_date)
make_trade = False
sys.exit()
#grab last price
last_price_under = results["underlying"]["last"]
print ("Last price of underlying, ",trade_strat["under"], "- $", last_price_under)
print ("Option expiration date:", trade_date)
#what did we pull? TESTING
#print(json.dumps(results, indent=4))
chain = results["putExpDateMap"] # pull the options chain, enumerated by the strike date
expirations = list(chain.keys()) # expirations in the chain
# should only be one expiration date
chain = chain[expirations[0]] # only one, but pick it
strikes = list(map(float,chain.keys()))
### PUT or CALL? ITM or OTM?
# then pick two options that match strategy
# bisect_left and subtraction for OTM puts
atm_position = bisect.bisect_left(strikes,last_price_under)
atm_strike = strikes[ atm_position]
# need to pick the sell strike from distance or delta
#print(json.dumps(chain, indent =4))
if "delta" in trade_strat:
#Use Delta if provided
for strike in chain :
#find maching delta
#print(strike, chain[strike][0]['delta'])
strike_delta = abs(chain[strike][0]['delta'])*100
if strike_delta >= trade_strat['delta'] :
strike= float(strike)
print("Found Delta", strike, "{:.2f}".format(strike_delta) )
sell_position = strikes.index(strike)
buy_position = sell_position - trade_strat["width"]
break
else: # assume if no delta, then distance from ATM
dist = trade_strat['distance']
print("Found distance from ATM", dist)
sell_position = atm_position - dist
buy_position = sell_position - trade_strat["width"]
print ( "At the Money Strike = ", "{:.2f}".format(atm_strike))
print ( "Sell OTM Strike = ", "{:.2f}".format(strikes[ sell_position]))
print ( "Buy OTM Strike = ", "{:.2f}".format(strikes[ buy_position ]))
# Pull Option leg symbols
sell_leg = chain[str(strikes[ sell_position] )]
sell_leg = sell_leg[0]
print("Sell leg :" , sell_leg["symbol"] )
buy_leg = chain[str(strikes[ buy_position])]
buy_leg = buy_leg[0]
print("Buy leg :", buy_leg["symbol"])
#calculate prices
price_nat = round(sell_leg['bid'] - buy_leg['ask'],2)
price_high = round(sell_leg['mark'] - buy_leg['mark'],2)
sell_leg['mid'] =round((sell_leg['bid'] + sell_leg['ask'])/2,2)
buy_leg['mid'] = round((buy_leg['bid'] + buy_leg['ask'])/2,2)
price_mid = round( sell_leg['mid'] - buy_leg['mid'],2)
price_target = round( price_mid * trade_strat["target"] , 2) # adjust the target to get better fills
# XSP doesn't do nickle steps in pricing, need to add code to not use steps
if trade_strat['under'] == '$SPX.X': #SPX needs to be nickled
print("Nickefing the price for SPX")
price_target = nicklefy(price_target) # convert to a 5 cent mark
print("Price Bid = {:.2f}".format(sell_leg['bid'] )," - Price Ask = {:.2f}".format(buy_leg['ask']), " = Price Nat = ", "{:.2f}".format(price_nat))
print("Price High = {:.2f}".format(price_high))
print("Price Mid = {:.2f}".format(price_mid))
print("Price Target = {:.2f}".format(price_target), "(",trade_strat["target"],")" )
print(" ")
# Ready the order (PUT or CALL??)
#put_order = bull_put_vertical_open(buy_leg["symbol"],sell_leg["symbol"],trade_strat["quantity"], str(price_target))
put_order = bull_put_vertical_open(buy_leg["symbol"],sell_leg["symbol"],trade_strat["quantity"], price_target)
#place the order - support multi accts later
print("Making the trade...")
r = c.place_order(config.ACCOUNT_ID, put_order)
print("Order status code - " ,r.status_code)
if r.status_code < 400 : #http codes under 400 are success. usually 200 or 201
order_id = Utils(c, config.ACCOUNT_ID).extract_order_id(r)
print ("Order placed, order ID-", order_id )
else :
print("FAILED - placing the order failed.")
make_trade = False # stop the closing order
return
# wait 5 for order to be submitted & maybe filled
#moved wait to fulfilment code
#time.sleep(60) # wait 60 seconds
# Set price decrement?
decrement = .01
if trade_strat['under'] == '$SPX.X': #SPX needs to be nickled
decrement = .05
order_id, make_trade, lower_price = check_fulfillment(put_order, order_id,price_target, decrement, trade_strat['under'])
#place close order
if trade_strat["closing"] > 0 and make_trade :
close_price_target = round(lower_price * (1-trade_strat["closing"]), 2) #Set limit order at the inverse of the profit goal
if trade_strat['under'] == '$SPX.X': #SPX needs to be nickled
print("Nickefing the price for SPX")
close_price_target = nicklefy(close_price_target) # convert to a 5 cent mark
print(" Placing closing order at ${:.2f}".format(close_price_target), "(",trade_strat["closing"]*100,"%)" )
put_order = bull_put_vertical_close(buy_leg["symbol"],sell_leg["symbol"],trade_strat["quantity"], str(close_price_target))
put_order.set_duration(orders.common.Duration.GOOD_TILL_CANCEL)
r = c.place_order(config.ACCOUNT_ID, put_order)
print("Order status code - " ,r.status_code)
if r.status_code < 400 : #http codes under 400 are success. usually 200 or 201:
order_id = Utils(c, config.ACCOUNT_ID).extract_order_id(r)
print ("Sell to Close order placed, order ID-", order_id )
else :
print("FAILED - placing the order failed.")
return
##########################################################################################
def check_auth_token ():
# Read TDA token to note token expiration
with open(config.TOKEN_PATH) as file:
data = json.load(file)
#print(json.dumps(data, indent=4))
token_created = pd.to_datetime(data['creation_timestamp'], unit='s')
token_expires = token_created + timedelta( days=90)
print(" Authentication Token Created: ", str(token_created) , " Will Expire: ", str(token_expires) )
# add warning when nearing expiration
if (token_expires < datetime.now() - timedelta(days=7)) :
print(" --**-- Authorization token expiring soon. Run token_renew.py to renew.")
return
# code to send logs via email
# future
##########################################################################################
def trading_butterfly(trade_strat, trade_date ):
#import trade strategy and trade date (expiration date)
#Setup Client
c = easy_client(
api_key= config.API_KEY,
redirect_uri=config.REDIRECT_URI,
token_path=config.TOKEN_PATH)
# Setup chain request
if trade_strat["type"] =='PUT' :
trade_type = c.Options.ContractType.PUT
elif trade_strat["type"] =='CALL' :
trade_type = c.Options.ContractType.CALL
# Get the options chain from TDA
results = c.get_option_chain( trade_strat["under"],
contract_type= trade_type,
strike_count= 10,
include_quotes=True,
from_date= trade_date,
to_date= trade_date )
# need to add multi account support
results = results.json() # pull out the json data
if results['status'] == 'FAILED':
print ("Getting the option chain failed.")
make_trade = False
sys.exit()
#grab last price
last_price_under = results["underlying"]["last"]
print ("Last price of underlying, ",trade_strat["under"], "- $", last_price_under)
print ("Option expiration date:", trade_date)
chain = results["putExpDateMap"] # pull the options chain, enumerated by the strike date
expirations = list(chain.keys()) # expirations in the chain
# should only be one expiration date
# should only be one expiration date
chain = chain[expirations[0]] # only one, but pick it
#what did we pull? TESTING
#print(json.dumps(chain , indent=4))
# Find the Delta 55
for strike in chain :
data= chain[strike]
#print(data)
delta = abs(data[0]['delta'] ) *100
if delta >= trade_strat['delta'] :
print(" Delta over 55:",strike, " Delta:",delta )
body_strike = float(strike)
break
strikes = list(map(float,chain.keys()))
body_position = strikes.index(body_strike)
# Buy at 2 lower and 1 higher
print ( "Delta 55 Body = ", "{:.2f}".format(strikes[ body_position]))
print ( "Lower Strike = ", "{:.2f}".format(strikes[ body_position - 2]))
print ( "Higher Strike = ", "{:.2f}".format(strikes[ body_position + 1 ]))
# Pull Option leg symbols
sell_leg = chain[str(strikes[ body_position ] )]
sell_leg = sell_leg[0]
print("Sell (body) leg :" , sell_leg["symbol"] )
buy_lower_leg = chain[str(strikes[ body_position - 2 ])]
buy_lower_leg = buy_lower_leg[0]
print("Buy lower leg :", buy_lower_leg["symbol"])
buy_higher_leg = chain[str(strikes[ body_position + 1 ])]
buy_higher_leg = buy_higher_leg[0]
print("Buy higher leg :", buy_higher_leg["symbol"])
# Find the mid points of each leg
sell_leg['mid'] = (sell_leg['bid'] + sell_leg['ask'])/2
buy_lower_leg['mid'] = (buy_lower_leg['bid'] + buy_lower_leg['ask'])/2
buy_higher_leg['mid'] = (buy_higher_leg['bid'] + buy_higher_leg['ask'])/2
# print("Sell Leg ", sell_leg['bid'] , sell_leg['ask'], sell_leg['mid'],sell_leg['mark'])
# print("Buy Low ", buy_lower_leg['bid'] , buy_lower_leg['ask'], buy_lower_leg['mid'],buy_lower_leg['mark'])
# print("Buy High ", buy_higher_leg['bid'] , buy_higher_leg['ask'], buy_higher_leg['mid'],buy_higher_leg['mark'])
#print(sell_leg)
# Calculate prices
price_nat = round((sell_leg['bid'] *2) - (buy_lower_leg['ask'] + buy_higher_leg['ask']),2)
price_high = round((sell_leg['mark'] *2) - (buy_lower_leg['mark'] + buy_higher_leg['mark']) ,2)
price_mid = round((sell_leg['mid'] *2) - (buy_lower_leg['mid'] + buy_higher_leg['mid']) ,2)
price_target = round( price_mid * trade_strat["target"] , 2) # lower the target to get better fills
if trade_strat['under'] == '$SPX.X': #SPX needs to be nickled
print("Nickefing the price for SPX")
price_target = nicklefy(price_target) # convert to a 5 cent mark
print("Price Nat = ", "{:.2f}".format(price_nat))
print("Price High = ", "{:.2f}".format(price_high))
print("Price Mid = ", "{:.2f}".format(price_mid))
print("Price Target = ", "{:.2f}".format(price_target))
print(" ")
# Ready the order (PUT or CALL??)
order = OrderBuilder() \
.set_complex_order_strategy_type(ComplexOrderStrategyType.BUTTERFLY) \
.set_duration(Duration.DAY) \
.set_order_strategy_type(OrderStrategyType.SINGLE) \
.set_order_type(OrderType.NET_CREDIT) \
.copy_price(str(price_target)) \
.set_quantity(trade_strat["quantity"] ) \
.set_requested_destination(Destination.AUTO) \
.set_session(Session.NORMAL) \
.add_option_leg(OptionInstruction.BUY_TO_OPEN, buy_higher_leg["symbol"], trade_strat["quantity"] ) \
.add_option_leg(OptionInstruction.SELL_TO_OPEN, sell_leg["symbol"] , trade_strat["quantity"] *2) \
.add_option_leg(OptionInstruction.BUY_TO_OPEN, buy_lower_leg["symbol"] , trade_strat["quantity"] )
#place the order - support multi accts later
print("Making the trade...")
r = c.place_order(config.ACCOUNT_ID, order)
print("Order status code - " ,r.status_code)
if r.status_code < 400 : #http codes under 400 are success. usually 200 or 201
order_id = Utils(c, config.ACCOUNT_ID).extract_order_id(r)
print ("Order placed, order ID-", order_id )
else :
print("FAILED - placing the order failed.")
make_trade = False # stop the closing order
# wait 5 for order to be submitted & maybe filled
time.sleep(60) # wait 60 seconds
# check if order is filled , send starting price too
order_id, make_trade, lower_price = check_fulfillment(order, order_id,price_target, .05,trade_strat["under"])
#place close order
if trade_strat["closing"] > 0 and make_trade :
print(" Placing closing order.")
close_price_target = lower_price * (1-trade_strat["closing"])
if trade_strat['under'] == '$SPX.X': #SPX needs to be nickled
print("Nickefing the price for SPX")
close_price_target = nicklefy(close_price_target) # convert to a 5 cent mark
put_order = bull_put_vertical_close(buy_leg["symbol"],sell_leg["symbol"],trade_strat["quantity"], close_price_target)
put_order.set_duration(orders.common.Duration.GOOD_TILL_CANCEL)
###r = c.place_order(config.ACCOUNT_ID, put_order)
print("Order status code - " ,r.status_code)
if r.status_code < 400 : #http codes under 400 are success. usually 200 or 201:
order_id = Utils(c, config.ACCOUNT_ID).extract_order_id(r)
print ("Sell to Close order placed, order ID-", order_id )
else :
print("FAILED - placing the order failed.")
return