Skip to content

Commit

Permalink
neg risk flag resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
mshrieve committed Jul 19, 2024
1 parent 16e5a51 commit 2fed57d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
26 changes: 22 additions & 4 deletions py_clob_client/client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from typing import Optional

from .order_builder.builder import OrderBuilder
from .headers.headers import create_level_1_headers, create_level_2_headers
Expand Down Expand Up @@ -29,6 +30,7 @@
UPDATE_BALANCE_ALLOWANCE,
IS_ORDER_SCORING,
GET_TICK_SIZE,
GET_NEG_RISK,
ARE_ORDERS_SCORING,
GET_SIMPLIFIED_MARKETS,
GET_MARKETS,
Expand Down Expand Up @@ -290,6 +292,10 @@ def get_tick_size(self, token_id: str) -> TickSize:

return self.__tick_sizes[token_id]

def get_neg_risk(self, token_id: str) -> bool:
result = get("{}{}?token_id={}".format(self.host, GET_NEG_RISK, token_id))
return result["neg_risk"]

def __resolve_tick_size(
self, token_id: str, tick_size: TickSize = None
) -> TickSize:
Expand All @@ -307,7 +313,7 @@ def __resolve_tick_size(
return tick_size

def create_order(
self, order_args: OrderArgs, options: PartialCreateOrderOptions = None
self, order_args: OrderArgs, options: Optional[PartialCreateOrderOptions] = None
):
"""
Creates and signs an order
Expand All @@ -320,7 +326,6 @@ def create_order(
order_args.token_id,
options.tick_size if options else None,
)
neg_risk = options.neg_risk if options else False

if not price_valid(order_args.price, tick_size):
raise Exception(
Expand All @@ -332,6 +337,12 @@ def create_order(
+ str(1 - float(tick_size))
)

neg_risk = (
options.neg_risk
if options and options.neg_risk
else self.get_neg_risk(order_args.token_id)
)

return self.builder.create_order(
order_args,
CreateOrderOptions(
Expand All @@ -341,7 +352,9 @@ def create_order(
)

def create_market_order(
self, order_args: MarketOrderArgs, options: PartialCreateOrderOptions = None
self,
order_args: MarketOrderArgs,
options: Optional[PartialCreateOrderOptions] = None,
):
"""
Creates and signs an order
Expand All @@ -354,7 +367,6 @@ def create_market_order(
order_args.token_id,
options.tick_size if options else None,
)
neg_risk = options.neg_risk if options else False

if order_args.price is None or order_args.price <= 0:
order_args.price = self.calculate_market_price(
Expand All @@ -371,6 +383,12 @@ def create_market_order(
+ str(1 - float(tick_size))
)

neg_risk = (
options.neg_risk
if options and options.neg_risk
else self.get_neg_risk(order_args.token_id)
)

return self.builder.create_market_order(
order_args,
CreateOrderOptions(
Expand Down
5 changes: 2 additions & 3 deletions py_clob_client/clob_types.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from dataclasses import dataclass
from typing import Any
from dataclasses import dataclass, asdict
from json import dumps
Expand Down Expand Up @@ -190,13 +189,13 @@ class OrdersScoringParams:
@dataclass
class CreateOrderOptions:
tick_size: TickSize
neg_risk: bool = False
neg_risk: bool


@dataclass
class PartialCreateOrderOptions:
tick_size: Optional[TickSize] = None
neg_risk: bool = False
neg_risk: Optional[bool] = None


@dataclass
Expand Down
1 change: 1 addition & 0 deletions py_clob_client/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
IS_ORDER_SCORING = "/order-scoring"
ARE_ORDERS_SCORING = "/orders-scoring"
GET_TICK_SIZE = "/tick-size"
GET_NEG_RISK = "/neg-risk"
GET_SAMPLING_SIMPLIFIED_MARKETS = "/sampling-simplified-markets"
GET_SAMPLING_MARKETS = "/sampling-markets"
GET_SIMPLIFIED_MARKETS = "/simplified-markets"
Expand Down

0 comments on commit 2fed57d

Please sign in to comment.