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_place_single.py
103 lines (80 loc) · 2.26 KB
/
option_order_place_single.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
import configparser
import json
from fast_arrow import (
Client,
Option,
OptionChain,
OptionOrder,
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])
#
# select the $SPY calls
#
desired_type = "call"
ops = list(filter(lambda x: x["type"] == desired_type, ops))
#
# select a single $SPY call option
#
strike_price = math.floor(float(stock["ask_price"]) + 3.0)
strike_price_str = "{:.4f}".format(strike_price)
option_to_buy = None
# for op in ops:
# if op["strike_price"] == strike_price_str:
# option_to_buy = op
option_to_buy = ops[25]
#
# enrich selected option with market data
#
option_to_buy = Option.mergein_marketdata_list(client, [option_to_buy])[0]
#
# send order
# NOTE!!! (at 10% of market bid prices so we don't actually get filled)
#
direction = "debit"
legs = [{ "side": "buy",
"option": option_to_buy["url"],
"position_effect": "open",
"ratio_quantity": 1 }]
if (float(option_to_buy["bid_price"]) * 0.1) > 0.01:
my_bid_price = (float(option_to_buy["bid_price"]) * 0.1)
else:
my_bid_price = 0.01
my_bid_price_rounded = (math.floor(my_bid_price * 100.0))/100.0
my_bid_price_formatted = str(my_bid_price_rounded)
price = my_bid_price_formatted
quantity = 1
time_in_force = "gfd"
trigger = "immediate"
order_type = "limit"
print("Buying the {} {} {} for {} (dollar cost = ${})".format(symbol,
strike_price, desired_type, my_bid_price_rounded,
my_bid_price_rounded*100.0)
)
oo = OptionOrder.submit(client, direction, legs, price, quantity, time_in_force, trigger, order_type)
print("Order submitted ... ref_id = {}".format(oo["ref_id"]))
#
# cancel the order
#
print("Canceling order = {}".format(oo["ref_id"]))
result = OptionOrder.cancel(client, oo['cancel_url'])
print("Order canceled result = {}".format(result))