Fix typo in cache_token_trading_infos function and update order placement logic for bid prices

This commit is contained in:
Nawaz Haider
2026-01-03 15:37:11 +06:00
parent 1f2b679e0c
commit b549edd757
2 changed files with 28 additions and 19 deletions
+5 -5
View File
@@ -9,7 +9,7 @@ from utils.clob_client import init_global_client
from utils.market_time import is_in_trading_window from utils.market_time import is_in_trading_window
from utils.clob_orders import ( from utils.clob_orders import (
place_anchor_and_hedge, place_anchor_and_hedge,
cache_tocken_trading_infos, cache_token_trading_infos,
) )
from utils.cpu_affinity import set_cpu_affinity from utils.cpu_affinity import set_cpu_affinity
from config import MAX_TRADES from config import MAX_TRADES
@@ -37,7 +37,7 @@ async def main():
await asyncio.sleep(2) await asyncio.sleep(2)
up_token, down_token, market_slug = await fetch_tokens() up_token, down_token, market_slug = await fetch_tokens()
book = OrderBook(up_token, down_token, market_slug) book = OrderBook(up_token, down_token, market_slug)
await asyncio.create_task(cache_tocken_trading_infos(book)) await asyncio.create_task(cache_token_trading_infos(book))
book.start() book.start()
await asyncio.sleep(5) # Allow some time for initial order book data await asyncio.sleep(5) # Allow some time for initial order book data
@@ -64,7 +64,7 @@ async def main():
trades = 0 trades = 0
up_token, down_token, market_slug = await fetch_tokens() up_token, down_token, market_slug = await fetch_tokens()
book = OrderBook(up_token, down_token, market_slug) book = OrderBook(up_token, down_token, market_slug)
asyncio.create_task(cache_tocken_trading_infos(book)) asyncio.create_task(cache_token_trading_infos(book))
book.start() book.start()
market_data = book.get_current_market_data() market_data = book.get_current_market_data()
@@ -90,7 +90,7 @@ async def main():
up_token, up_token,
down_token, down_token,
"UP", "UP",
up_ask_price, up_bid_price,
size=5, size=5,
) )
trades += 1 trades += 1
@@ -103,7 +103,7 @@ async def main():
up_token, up_token,
down_token, down_token,
"DOWN", "DOWN",
down_ask_price, down_bid_price,
size=5, size=5,
) )
trades += 1 trades += 1
+23 -14
View File
@@ -1,7 +1,7 @@
import asyncio import asyncio
import logging import logging
from py_clob_client.client import ClobClient import time
from py_clob_client.clob_types import OrderArgs from py_clob_client.clob_types import OrderArgs, OrderType
from py_clob_client.order_builder.constants import BUY from py_clob_client.order_builder.constants import BUY
from config import PROFIT_MARGIN, PLACE_OPPOSITE_ORDER from config import PROFIT_MARGIN, PLACE_OPPOSITE_ORDER
from utils.clob_client import get_client from utils.clob_client import get_client
@@ -11,7 +11,7 @@ from in_memory_db.utils import contains_item as in_memory_db_contains_item
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
async def cache_tocken_trading_infos( async def cache_token_trading_infos(
order_book, order_book,
) -> None: ) -> None:
client = get_client() client = get_client()
@@ -35,9 +35,9 @@ async def place_anchor_and_hedge(
anchor_token_id = down_token_id anchor_token_id = down_token_id
hedge_token_id = up_token_id hedge_token_id = up_token_id
anchor_order_id = await place_limit_order(anchor_token_id, price, size) anchor_order_id = await place_limit_order(anchor_token_id, price, size, expire=True)
if PLACE_OPPOSITE_ORDER and anchor_token_id: if PLACE_OPPOSITE_ORDER and anchor_order_id:
for _ in range(900 * 100): for _ in range(10 * 100):
if in_memory_db_contains_item(anchor_order_id): if in_memory_db_contains_item(anchor_order_id):
hedge_order_id = await place_limit_order( hedge_order_id = await place_limit_order(
hedge_token_id, 1 - price - PROFIT_MARGIN, size hedge_token_id, 1 - price - PROFIT_MARGIN, size
@@ -50,16 +50,25 @@ async def place_anchor_and_hedge(
logger.info(f"Order price: {price}") logger.info(f"Order price: {price}")
async def place_limit_order(token_id: str, price: float, size: int): async def place_limit_order(token_id: str, price: float, size: int, expire=False):
client = get_client() client = get_client()
expiration = 0
if expire:
one_minute = 60
desired_seconds = 5
expiration = int(time.time()) + one_minute + desired_seconds
try: try:
response = client.create_and_post_order(
OrderArgs( order_args = OrderArgs(
token_id=token_id, token_id=token_id,
price=price, price=price,
size=size, size=size,
side=BUY, side=BUY,
) expiration=expiration,
)
signed_order = client.create_order(order_args)
response = client.post_order(
signed_order, OrderType.GTD if expire else OrderType.GTC
) )
logger.info( logger.info(
f"Placed limit order: Token ID={token_id}, Price={price}, Size={size}, ID={response['orderID']}" f"Placed limit order: Token ID={token_id}, Price={price}, Size={size}, ID={response['orderID']}"