Skip to content

Commit

Permalink
Merge pull request #19 from 8ball030/feat/option_orders
Browse files Browse the repository at this point in the history
feat ensured example for option trading
  • Loading branch information
8ball030 authored Jan 10, 2024
2 parents 1e93ce5 + e21fbde commit adacd63
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lyra/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
"DOMAIN_SEPARATOR": '0x9bcf4dc06df5d8bf23af818d5716491b995020f377d3b7b64c29ed14e3dd1105',
"ETH_PERP_ADDRESS": '0x010e26422790C6Cb3872330980FAa7628FD20294',
"BTC_PERP_ADDRESS": '0xAFB6Bb95cd70D5367e2C39e9dbEb422B9815339D',
"ETH_OPTION_ADDRESS": '0xBcB494059969DAaB460E0B5d4f5c2366aab79aa1',
"BTC_OPTION_ADDRESS": '0xAeB81cbe6b19CeEB0dBE0d230CFFE35Bb40a13a7',
"TRADE_MODULE_ADDRESS": '0x87F2863866D85E3192a35A73b388BD625D83f2be',
},
Environment.PROD: {
Expand All @@ -23,6 +25,8 @@
"DOMAIN_SEPARATOR": '0xd96e5f90797da7ec8dc4e276260c7f3f87fedf68775fbe1ef116e996fc60441b',
"ETH_PERP_ADDRESS": '0xAf65752C4643E25C02F693f9D4FE19cF23a095E3',
"BTC_PERP_ADDRESS": '0xDBa83C0C654DB1cd914FA2710bA743e925B53086',
"ETH_OPTION_ADDRESS": '0x4BB4C3CDc7562f08e9910A0C7D8bB7e108861eB4',
"BTC_OPTION_ADDRESS": '0xd0711b9eBE84b778483709CDe62BacFDBAE13623',
"TRADE_MODULE_ADDRESS": '0xB8D20c2B7a1Ad2EE33Bc50eF10876eD3035b5e7b',
},
}
10 changes: 9 additions & 1 deletion lyra/lyra.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ def create_order(
instruments = {i['instrument_name']: i for i in instruments}
base_asset_sub_id = instruments[instrument_name]['base_asset_sub_id']
instrument_type = InstrumentType.PERP
else:
instruments = self.fetch_instruments(instrument_type=InstrumentType.OPTION, currency=_currency)
instruments = {i['instrument_name']: i for i in instruments}
base_asset_sub_id = instruments[instrument_name]['base_asset_sub_id']
instrument_type = InstrumentType.OPTION

signed_order = self._sign_order(order, base_asset_sub_id, instrument_type, _currency)
response = self.submit_order(signed_order)
Expand Down Expand Up @@ -187,7 +192,10 @@ def submit_order(self, order):
while True:
message = json.loads(self.ws.recv())
if message['id'] == id:
return message['result']['order']
try:
return message['result']['order']
except KeyError as error:
raise Exception(f"Unable to submit order {message}") from error

def sign_authentication_header(self):
timestamp = str(int(time.time() * 1000))
Expand Down
28 changes: 28 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,31 @@ def test_get_tickers(lyra_client):
"""Test get tickers."""
tickers = lyra_client.fetch_tickers()
assert isinstance(tickers, dict)


@pytest.mark.parametrize(
"currency, side",
[
(UnderlyingCurrency.ETH, OrderSide.BUY),
(UnderlyingCurrency.ETH, OrderSide.SELL),
(UnderlyingCurrency.BTC, OrderSide.BUY),
(UnderlyingCurrency.BTC, OrderSide.SELL),
],
)
def test_can_create_option_order(lyra_client, currency, side):
"""Test can create option order."""
tickers = lyra_client.fetch_tickers(
instrument_type=InstrumentType.OPTION,
currency=currency,
)
symbol, ticker = tickers.popitem()
index_price = float(ticker['mark_price'])
order_price = index_price * 1.1 if side == OrderSide.SELL else index_price * 0.9
order = lyra_client.create_order(
price=int(order_price),
amount=1,
instrument_name=symbol,
side=side,
order_type=OrderType.LIMIT,
)
assert order

0 comments on commit adacd63

Please sign in to comment.