This repository has been archived by the owner on Apr 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
option_order_replace.py
116 lines (96 loc) · 2.7 KB
/
option_order_replace.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
import configparser
import json
from fast_arrow import (
Client,
Option,
OptionChain,
OptionOrder,
Vertical,
Stock
)
import math
print("----- running {}".format(__file__))
#
# get auth_data (see https://github.com/westonplatter/fast_arrow_auth)
#
with open("fast_arrow_auth.json") as f:
auth_data = json.loads(f.read())
#
# initialize client with auth_data
#
client = Client(auth_data)
#
# fetch spy options
#
symbol = "SPY"
stock = Stock.fetch(client, symbol)
stock = Stock.mergein_marketdata_list(client, [stock])[0]
oc = OptionChain.fetch(client, stock["id"], symbol)
ed = oc['expiration_dates'][3]
ops = Option.in_chain(client, oc["id"], expiration_dates=[ed])
#
# enrich options with market data
#
ops = Option.mergein_marketdata_list(client, ops)
#
# genrate vertical spread table
#
width = 1
df = Vertical.gen_df(ops, width, "put", "sell")
#
# select the 4th row (should be a deep OTM put, credit spread)
#
vertical = df.iloc[[4]]
#
# create the order
#
direction = "credit"
legs = [
{ "side": "sell",
"option": vertical["instrument"].values[0],
"position_effect": "open",
"ratio_quantity": 1
},
{ "side": "buy",
"option": vertical["instrument_shifted"].values[0],
"position_effect": "open",
"ratio_quantity": 1
}
]
#
# for a Put Credit spread, set limit price at 1.0 less the full margin
#
my_bid_price = (vertical["margin"] * 100.0) - 1.00
my_bid_price_rounded = (math.floor(my_bid_price * 100.0))/100.0
x = my_bid_price_rounded / 100.0
my_bid_price_formatted = str(x)
price = my_bid_price_formatted
quantity = 1
time_in_force = "gfd"
trigger = "immediate"
order_type = "limit"
print("Selling a {} {}/{} Put Spread for {} (notional value = ${})".format(
symbol,
vertical["strike_price"].values[0],
vertical["strike_price_shifted"].values[0],
price,
my_bid_price_rounded)
)
oo = OptionOrder.submit(client, direction, legs, price, quantity, time_in_force, trigger, order_type)
print("Order submitted ... ref_id = {}".format(oo["ref_id"]))
my_bid_price = (vertical["margin"] * 100.0) - 1.00 - 1.00
my_bid_price_rounded = (math.floor(my_bid_price * 100.0))/100.0
x = my_bid_price_rounded / 100.0
my_bid_price_formatted = str(x)
new_price = my_bid_price_formatted
print("Replacing order ...")
print("... old price = {}".format(price))
print("... new price = {}".format(new_price))
oo_replaced = OptionOrder.replace(client, oo, new_price)
print("Order has been replaced ... ref_id = {}".format(oo_replaced["ref_id"]))
#
# cancel the order
#
print("Canceling replaced order = {}".format(oo_replaced["ref_id"]))
result = OptionOrder.cancel(client, oo_replaced['cancel_url'])
print("Order canceled result = {}".format(result))