Skip to content

Commit

Permalink
getting the orderbook spread
Browse files Browse the repository at this point in the history
  • Loading branch information
poly-rodr committed May 14, 2024
1 parent 1c5eaaa commit 1e08acf
Show file tree
Hide file tree
Showing 16 changed files with 104 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ resp = client.create_and_post_order(OrderArgs(
price=0.50,
size=100.0,
side=BUY,
token_id="16678291189211314787145083999015737376658799626183230671758641503291735614088"
token_id="71321045679252212594626385532706912750332728571942532289631379312455583992563"
))

print(resp)
Expand Down
2 changes: 1 addition & 1 deletion examples/GTD_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def main():
price=0.50,
size=100.0,
side=BUY,
token_id="16678291189211314787145083999015737376658799626183230671758641503291735614088",
token_id="71321045679252212594626385532706912750332728571942532289631379312455583992563",
expiration="1000000000000",
)
signed_order = client.create_order(order_args)
Expand Down
4 changes: 2 additions & 2 deletions examples/get_balance_allowance.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ def main():
yes = client.get_balance_allowance(
params=BalanceAllowanceParams(
asset_type=AssetType.CONDITIONAL,
token_id="1343197538147866997676250008839231694243646439454152539053893078719042421992",
token_id="52114319501245915516055106046884209969926127482827954674443846427813813222426",
)
)
print(yes)

no = client.get_balance_allowance(
params=BalanceAllowanceParams(
asset_type=AssetType.CONDITIONAL,
token_id="16678291189211314787145083999015737376658799626183230671758641503291735614088",
token_id="71321045679252212594626385532706912750332728571942532289631379312455583992563",
)
)
print(no)
Expand Down
2 changes: 1 addition & 1 deletion examples/get_last_trade_price.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def main():
client = ClobClient(host, key=key, chain_id=chain_id)

resp = client.get_last_trade_price(
"16678291189211314787145083999015737376658799626183230671758641503291735614088"
"71321045679252212594626385532706912750332728571942532289631379312455583992563"
)
pprint(resp)
print("Done!")
Expand Down
4 changes: 2 additions & 2 deletions examples/get_last_trades_prices.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ def main():
resp = client.get_last_trades_prices(
params=[
BookParams(
token_id="16678291189211314787145083999015737376658799626183230671758641503291735614088"
token_id="71321045679252212594626385532706912750332728571942532289631379312455583992563"
),
BookParams(
token_id="1343197538147866997676250008839231694243646439454152539053893078719042421992"
token_id="52114319501245915516055106046884209969926127482827954674443846427813813222426"
),
]
)
Expand Down
2 changes: 1 addition & 1 deletion examples/get_mid_market_price.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def main():
client = ClobClient(host, key=key, chain_id=chain_id, creds=creds)

resp = client.get_midpoint(
"16678291189211314787145083999015737376658799626183230671758641503291735614088"
"71321045679252212594626385532706912750332728571942532289631379312455583992563"
)
# {'mid': '0.55'}
print(resp)
Expand Down
4 changes: 2 additions & 2 deletions examples/get_mid_markets_prices.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ def main():
resp = client.get_midpoints(
params=[
BookParams(
token_id="16678291189211314787145083999015737376658799626183230671758641503291735614088"
token_id="71321045679252212594626385532706912750332728571942532289631379312455583992563"
),
BookParams(
token_id="1343197538147866997676250008839231694243646439454152539053893078719042421992"
token_id="52114319501245915516055106046884209969926127482827954674443846427813813222426"
),
]
)
Expand Down
2 changes: 1 addition & 1 deletion examples/get_orderbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def main():
client = ClobClient(host)

orderbook = client.get_order_book(
"16678291189211314787145083999015737376658799626183230671758641503291735614088"
"71321045679252212594626385532706912750332728571942532289631379312455583992563"
)
print("orderbook", orderbook)

Expand Down
2 changes: 1 addition & 1 deletion examples/get_orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def main():

resp = client.get_orders(
OpenOrderParams(
market="16678291189211314787145083999015737376658799626183230671758641503291735614088",
market="71321045679252212594626385532706912750332728571942532289631379312455583992563",
)
)
print(resp)
Expand Down
30 changes: 30 additions & 0 deletions examples/get_spread.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import os

from py_clob_client.client import ClobClient
from py_clob_client.clob_types import ApiCreds
from dotenv import load_dotenv
from py_clob_client.constants import AMOY


load_dotenv()


def main():
host = "http://localhost:8080"
key = os.getenv("PK")
creds = ApiCreds(
api_key=os.getenv("CLOB_API_KEY"),
api_secret=os.getenv("CLOB_SECRET"),
api_passphrase=os.getenv("CLOB_PASS_PHRASE"),
)
chain_id = AMOY
client = ClobClient(host, key=key, chain_id=chain_id, creds=creds)

resp = client.get_spread(
"71321045679252212594626385532706912750332728571942532289631379312455583992563"
)
print(resp)
print("Done!")


main()
37 changes: 37 additions & 0 deletions examples/get_spreads.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import os

from py_clob_client.client import ClobClient
from py_clob_client.clob_types import ApiCreds, BookParams
from dotenv import load_dotenv
from py_clob_client.constants import AMOY


load_dotenv()


def main():
host = "http://localhost:8080"
key = os.getenv("PK")
creds = ApiCreds(
api_key=os.getenv("CLOB_API_KEY"),
api_secret=os.getenv("CLOB_SECRET"),
api_passphrase=os.getenv("CLOB_PASS_PHRASE"),
)
chain_id = AMOY
client = ClobClient(host, key=key, chain_id=chain_id, creds=creds)

resp = client.get_spreads(
params=[
BookParams(
token_id="71321045679252212594626385532706912750332728571942532289631379312455583992563"
),
BookParams(
token_id="52114319501245915516055106046884209969926127482827954674443846427813813222426"
),
]
)
print(resp)
print("Done!")


main()
2 changes: 1 addition & 1 deletion examples/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def main():
price=0.0005,
size=20,
side=BUY,
token_id="16678291189211314787145083999015737376658799626183230671758641503291735614088",
token_id="71321045679252212594626385532706912750332728571942532289631379312455583992563",
)
signed_order = client.create_order(order_args)
resp = client.post_order(signed_order)
Expand Down
23 changes: 19 additions & 4 deletions py_clob_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
MID_POINTS,
GET_ORDER_BOOKS,
GET_PRICES,
GET_SPREAD,
GET_SPREADS,
)
from .clob_types import (
ApiCreds,
Expand Down Expand Up @@ -249,7 +251,7 @@ def get_midpoints(self, params: list[BookParams]):
Get the mid market prices for a set of token ids
"""
body = [{"token_id": param.token_id} for param in params]
return get("{}{}".format(self.host, MID_POINTS), data=body)
return post("{}{}".format(self.host, MID_POINTS), data=body)

def get_price(self, token_id, side):
"""
Expand All @@ -262,7 +264,20 @@ def get_prices(self, params: list[BookParams]):
Get the market prices for a set
"""
body = [{"token_id": param.token_id, "side": param.side} for param in params]
return get("{}{}".format(self.host, GET_PRICES), data=body)
return post("{}{}".format(self.host, GET_PRICES), data=body)

def get_spread(self, token_id):
"""
Get the spread for the given market
"""
return get("{}{}?token_id={}".format(self.host, GET_SPREAD, token_id))

def get_spreads(self, params: list[BookParams]):
"""
Get the spreads for a set of token ids
"""
body = [{"token_id": param.token_id} for param in params]
return post("{}{}".format(self.host, GET_SPREADS), data=body)

def get_tick_size(self, token_id: str) -> TickSize:
if token_id in self.__tick_sizes:
Expand Down Expand Up @@ -432,7 +447,7 @@ def get_order_books(self, params: list[BookParams]) -> list[OrderBookSummary]:
Fetches the orderbook for a set of token ids
"""
body = [{"token_id": param.token_id} for param in params]
raw_obs = get("{}{}".format(self.host, GET_ORDER_BOOKS), data=body)
raw_obs = post("{}{}".format(self.host, GET_ORDER_BOOKS), data=body)
return [parse_raw_orderbook_summary(r) for r in raw_obs]

def get_order_book_hash(self, orderbook: OrderBookSummary) -> str:
Expand Down Expand Up @@ -484,7 +499,7 @@ def get_last_trades_prices(self, params: list[BookParams]):
Fetches the last trades prices for a set of token ids
"""
body = [{"token_id": param.token_id} for param in params]
return get("{}{}".format(self.host, GET_LAST_TRADES_PRICES), data=body)
return post("{}{}".format(self.host, GET_LAST_TRADES_PRICES), data=body)

def assert_level_1_auth(self):
"""
Expand Down
2 changes: 2 additions & 0 deletions py_clob_client/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
MID_POINTS = "/midpoints"
PRICE = "/price"
GET_PRICES = "/prices"
GET_SPREAD = "/spread"
GET_SPREADS = "/spreads"
GET_LAST_TRADE_PRICE = "/last-trade-price"
GET_LAST_TRADES_PRICES = "/last-trades-prices"
GET_NOTIFICATIONS = "/notifications"
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="py_clob_client",
version="0.16.0",
version="0.17.0",
author="Polymarket Engineering",
author_email="[email protected]",
maintainer="Polymarket Engineering",
Expand Down
4 changes: 2 additions & 2 deletions tests/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class TestUtilities(TestCase):
def test_parse_raw_orderbook_summary(self):
raw_obs = {
"market": "0xbd31dc8a20211944f6b70f31557f1001557b59905b7738480ca09bd4532f84af",
"asset_id": "1343197538147866997676250008839231694243646439454152539053893078719042421992",
"asset_id": "52114319501245915516055106046884209969926127482827954674443846427813813222426",
"bids": [
{"price": "0.15", "size": "100"},
{"price": "0.31", "size": "148.56"},
Expand All @@ -41,7 +41,7 @@ def test_parse_raw_orderbook_summary(self):
)
self.assertEqual(
orderbook_summary.asset_id,
"1343197538147866997676250008839231694243646439454152539053893078719042421992",
"52114319501245915516055106046884209969926127482827954674443846427813813222426",
)
self.assertEqual(
orderbook_summary.hash, "9d6d9e8831a150ac4cd878f99f7b2c6d419b875f"
Expand Down

0 comments on commit 1e08acf

Please sign in to comment.