Skip to content
This repository has been archived by the owner on Apr 16, 2021. It is now read-only.

Commit

Permalink
option order. fix vertical strategy generation.
Browse files Browse the repository at this point in the history
  • Loading branch information
westonplatter committed Mar 13, 2019
1 parent c25dace commit ed4e2d6
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 21 deletions.
8 changes: 6 additions & 2 deletions examples/option_order_place_iron_condor.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
#
# IC example not functional as of the 0.3.0 release
# pull requests welcome!
#

import configparser
from fast_arrow import (
Client,
IronCondor,
Option,
OptionChain,
OptionOrder,
Stock,
Stock
)


#
# get the authentication configs
#
Expand Down
2 changes: 0 additions & 2 deletions examples/option_order_place_single.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@
ed = oc['expiration_dates'][3]
ops = Option.in_chain(client, oc["id"], expiration_dates=[ed])

import ipdb; ipdb.set_trace()

#
# select the $SPY calls
#
Expand Down
14 changes: 9 additions & 5 deletions examples/option_order_place_vertical.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
stock = Stock.mergein_marketdata_list(client, [stock])[0]

oc = OptionChain.fetch(client, stock["id"], symbol)
ed = oc['expiration_dates'][3]
ed = oc['expiration_dates'][0]
ops = Option.in_chain(client, oc["id"], expiration_dates=[ed])

#
Expand All @@ -46,20 +46,23 @@
# genrate vertical spread table
#
width = 1
df = Vertical.gen_df(ops, width, "put", "sell")
spread_type = "put"
spread_kind = "sell"
df = Vertical.gen_df(ops, width, spread_type, spread_kind)

#
# select the 4th row (should be a deep OTM put, credit spread)
#
vertical = df.iloc[[4]]
index = int(len(ops) / 4)
vertical = df.iloc[[index]]

#
# create the order
#
direction = "credit"

legs = [
{ "side": "sell",
{ "side": spread_kind,
"option": vertical["instrument"].values[0],
"position_effect": "open",
"ratio_quantity": 1
Expand All @@ -85,10 +88,11 @@
trigger = "immediate"
order_type = "limit"

print("Selling a {} {}/{} Put Spread for {} (notional value = ${})".format(
print("Selling a {} {}/{} {} spread for {} (notation value = ${})".format(
symbol,
vertical["strike_price"].values[0],
vertical["strike_price_shifted"].values[0],
spread_type,
price,
my_bid_price_rounded)
)
Expand Down
9 changes: 6 additions & 3 deletions fast_arrow/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ def get(self, url=None, params=None, retry=True):
Execute HTTP GET
'''
headers = self._gen_headers(self.access_token, url)

attempts = 1
while attempts <= HTTP_ATTEMPTS_MAX:
try:
Expand All @@ -55,7 +54,9 @@ def get(self, url=None, params=None, retry=True):
return res.json()
except requests.exceptions.RequestException as e:
attempts += 1
if retry and res.status_code in [403]:
if res.status_code in [400]:
raise e
elif retry and res.status_code in [403]:
self.relogin_oauth2()


Expand All @@ -75,7 +76,9 @@ def post(self, url=None, payload=None, retry=True):
return res.json()
except requests.exceptions.RequestException as e:
attempts += 1
if retry and res.status_code in [403]:
if res.status_code in [400]:
raise e
elif retry and res.status_code in [403]:
self.relogin_oauth2()


Expand Down
2 changes: 2 additions & 0 deletions fast_arrow/option_strategies/iron_condor.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ def generate_by_deltas(cls, options,
- set delta for inner leg of the call credit spread (eg, 0.1)
"""

raise Exception("Not Implemented starting at the 0.3.0 release")

#
# put credit spread
#
Expand Down
23 changes: 14 additions & 9 deletions fast_arrow/option_strategies/vertical.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def gen_df(cls, options, width, spread_type="call", spread_kind="buy"):
# get CALLs or PUTs
options = list(filter(lambda x: x["type"] == spread_type, options))

coef = (1 if spread_kind == "sell" else -1)
shift = width * -1
coef = (1 if spread_type == "put" else -1)
shift = width * coef

df = pd.DataFrame.from_dict(options)
df['expiration_date'] = pd.to_datetime(df['expiration_date'], format="%Y-%m-%d")
Expand All @@ -34,17 +34,22 @@ def gen_df(cls, options, width, spread_type="call", spread_kind="buy"):
for k,v in df.groupby("expiration_date"):
sdf = v.shift(shift)

df.loc[v.index, "strike_price_shifted"] = sdf["strike_price"]
df.loc[v.index, "delta_shifted"] = sdf["delta"]
df.loc[v.index, "volume_shifted"] = sdf["volume"]
df.loc[v.index, "open_interest_shifted"] = sdf["open_interest"]
df.loc[v.index, "instrument_shifted"] = sdf["instrument"]
df.loc[v.index, "strike_price_shifted"] = sdf["strike_price"]
df.loc[v.index, "delta_shifted"] = sdf["delta"]
df.loc[v.index, "volume_shifted"] = sdf["volume"]
df.loc[v.index, "open_interest_shifted"] = sdf["open_interest"]
df.loc[v.index, "instrument_shifted"] = sdf["instrument"]
df.loc[v.index, "adjusted_mark_price_shift"] = sdf["adjusted_mark_price"]

if spread_kind == "sell":
df.loc[v.index, "margin"] = sdf["strike_price"] - v["strike_price"]
df.loc[v.index, "margin"] = abs(sdf["strike_price"] - v["strike_price"])
else:
df.loc[v.index, "margin"] = 0.0

df.loc[v.index, "premium_adjusted_mark_price"] = (sdf["adjusted_mark_price"] - v["adjusted_mark_price"]) * coef
if spread_kind == "buy":
df.loc[v.index, "premium_adjusted_mark_price"] = (v["adjusted_mark_price"] - sdf["adjusted_mark_price"])
elif spread_kind == "sell":
df.loc[v.index, "premium_adjusted_mark_price"] = (sdf["adjusted_mark_price"] - v["adjusted_mark_price"])


return df

0 comments on commit ed4e2d6

Please sign in to comment.