Refactor trading logic and enhance ClobClient integration for improved market operations

This commit is contained in:
Nawaz Haider
2026-01-01 21:59:16 +06:00
parent 73f08a0c49
commit a8a4fb2a0e
3 changed files with 93 additions and 17 deletions
+80 -5
View File
@@ -1,31 +1,106 @@
import time
import asyncio
import requests
from utils.logger import setup_logging
from utils.tokens import fetch_tokens
from utils.clob_client import init_clob_client
from utils.orderbook import OrderBook
from utils.market_time import is_in_trading_window
from utils.clob_client_and_order import init_clob_client
from utils.orderbook import OrderBook, SIGNALES
from utils.market_time import is_in_trading_window, get_period_elapsed_seconds
from utils.clob_client_and_order import (
place_anchor_and_hedge,
cache_tocken_trading_infos,
)
session = requests.Session()
requests.get = session.get
requests.post = session.post
requests.put = session.put
requests.patch = session.patch
requests.delete = session.delete
requests.head = session.head
requests.options = session.options
async def main():
max_trades = 1
trades = 0
logger = setup_logging()
logger.info("Polymarket HFT Market Maker started")
up_token, down_token, market_slug = await fetch_tokens()
client = await init_clob_client()
client = init_clob_client()
book = OrderBook(up_token, down_token, market_slug)
await asyncio.create_task(cache_tocken_trading_infos(client, book))
book.start()
await asyncio.sleep(5) # Allow some time for initial order book data
market_data = book.get_current_market_data()
up_bid_price = market_data["best_bid_price"]
up_ask_price = market_data["best_ask_price"]
down_ask_price = 1 - up_bid_price
down_bid_price = 1 - up_ask_price
print(
f"Initial Prices - UP: {up_bid_price:.2f}/{up_ask_price:.2f} | DOWN: {down_bid_price:.2f}/{down_ask_price:.2f}",
flush=True,
)
while True:
if not is_in_trading_window():
book.stop()
logger.info("Trading session ended. Starting new session.")
await asyncio.sleep(10)
up_token, down_token, market_slug = await fetch_tokens()
book = OrderBook(up_token, down_token, market_slug)
asyncio.create_task(cache_tocken_trading_infos(client, book))
book.start()
market_data = book.get_current_market_data()
if not market_data:
continue
up_bid_price = market_data["best_bid_price"]
up_ask_price = market_data["best_ask_price"]
down_ask_price = 1 - up_bid_price
down_bid_price = 1 - up_ask_price
if trades < max_trades:
trading_side = book.last_signal
if trading_side == SIGNALES.UP:
await place_anchor_and_hedge(
client,
up_token,
down_token,
"UP",
up_bid_price,
size=5,
)
trades += 1
logger.info(
f"Placed UP anchor and hedge orders. Total trades: {trades}"
)
elif trading_side == SIGNALES.DOWN:
await place_anchor_and_hedge(
client,
up_token,
down_token,
"DOWN",
down_bid_price,
size=5,
)
trades += 1
logger.info(
f"Placed DOWN anchor and hedge orders. Total trades: {trades}"
)
await asyncio.sleep(1)
@@ -16,13 +16,13 @@ SIGNATURE_TYPE = os.getenv("SIGNATURE_TYPE")
logger = logging.getLogger(__name__)
async def init_clob_client() -> ClobClient:
def init_clob_client() -> ClobClient:
try:
client = ClobClient(
POLYMARKET_HOST,
key=PRIVATE_KEY,
chain_id=CHAIN_ID,
signature_type=SIGNATURE_TYPE,
signature_type=int(SIGNATURE_TYPE),
funder=POLYMARKET_PROXY_ADDRESS,
)
client.set_api_creds(client.create_or_derive_api_creds())
@@ -34,16 +34,17 @@ async def init_clob_client() -> ClobClient:
async def cache_tocken_trading_infos(
client: ClobClient, up_token_id: str, down_token_id: str
client: ClobClient,
order_book,
) -> None:
while True:
client.get_tick_size(up_token_id)
client.get_tick_size(down_token_id)
client.get_neg_risk(up_token_id)
client.get_neg_risk(down_token_id)
client.get_fee_rate_bps(up_token_id)
client.get_fee_rate_bps(down_token_id)
await asyncio.sleep(10)
up_token_id, down_token_id = order_book.up_token_id, order_book.down_token_id
client.get_tick_size(up_token_id)
client.get_tick_size(down_token_id)
client.get_neg_risk(up_token_id)
client.get_neg_risk(down_token_id)
client.get_fee_rate_bps(up_token_id)
client.get_fee_rate_bps(down_token_id)
async def place_anchor_and_hedge(
+1 -1
View File
@@ -10,4 +10,4 @@ def get_period_elapsed_seconds():
def is_in_trading_window():
elapsed_seconds = get_period_elapsed_seconds()
return elapsed_seconds > MARKET_SESSION_SECONDS
return elapsed_seconds < (MARKET_SESSION_SECONDS - 5)