Refactor trading logic and enhance ClobClient integration for improved market operations
This commit is contained in:
@@ -1,31 +1,106 @@
|
|||||||
import time
|
import time
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import requests
|
||||||
from utils.logger import setup_logging
|
from utils.logger import setup_logging
|
||||||
from utils.tokens import fetch_tokens
|
from utils.tokens import fetch_tokens
|
||||||
from utils.clob_client import init_clob_client
|
from utils.clob_client_and_order import init_clob_client
|
||||||
from utils.orderbook import OrderBook
|
from utils.orderbook import OrderBook, SIGNALES
|
||||||
from utils.market_time import is_in_trading_window
|
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():
|
async def main():
|
||||||
|
max_trades = 1
|
||||||
|
trades = 0
|
||||||
|
|
||||||
logger = setup_logging()
|
logger = setup_logging()
|
||||||
logger.info("Polymarket HFT Market Maker started")
|
logger.info("Polymarket HFT Market Maker started")
|
||||||
|
|
||||||
up_token, down_token, market_slug = await fetch_tokens()
|
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)
|
book = OrderBook(up_token, down_token, market_slug)
|
||||||
|
await asyncio.create_task(cache_tocken_trading_infos(client, book))
|
||||||
book.start()
|
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:
|
while True:
|
||||||
if not is_in_trading_window():
|
if not is_in_trading_window():
|
||||||
|
|
||||||
book.stop()
|
book.stop()
|
||||||
logger.info("Trading session ended. Starting new session.")
|
logger.info("Trading session ended. Starting new session.")
|
||||||
|
await asyncio.sleep(10)
|
||||||
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(client, book))
|
||||||
book.start()
|
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)
|
await asyncio.sleep(1)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -16,13 +16,13 @@ SIGNATURE_TYPE = os.getenv("SIGNATURE_TYPE")
|
|||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
async def init_clob_client() -> ClobClient:
|
def init_clob_client() -> ClobClient:
|
||||||
try:
|
try:
|
||||||
client = ClobClient(
|
client = ClobClient(
|
||||||
POLYMARKET_HOST,
|
POLYMARKET_HOST,
|
||||||
key=PRIVATE_KEY,
|
key=PRIVATE_KEY,
|
||||||
chain_id=CHAIN_ID,
|
chain_id=CHAIN_ID,
|
||||||
signature_type=SIGNATURE_TYPE,
|
signature_type=int(SIGNATURE_TYPE),
|
||||||
funder=POLYMARKET_PROXY_ADDRESS,
|
funder=POLYMARKET_PROXY_ADDRESS,
|
||||||
)
|
)
|
||||||
client.set_api_creds(client.create_or_derive_api_creds())
|
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(
|
async def cache_tocken_trading_infos(
|
||||||
client: ClobClient, up_token_id: str, down_token_id: str
|
client: ClobClient,
|
||||||
|
order_book,
|
||||||
) -> None:
|
) -> None:
|
||||||
while True:
|
|
||||||
client.get_tick_size(up_token_id)
|
up_token_id, down_token_id = order_book.up_token_id, order_book.down_token_id
|
||||||
client.get_tick_size(down_token_id)
|
client.get_tick_size(up_token_id)
|
||||||
client.get_neg_risk(up_token_id)
|
client.get_tick_size(down_token_id)
|
||||||
client.get_neg_risk(down_token_id)
|
client.get_neg_risk(up_token_id)
|
||||||
client.get_fee_rate_bps(up_token_id)
|
client.get_neg_risk(down_token_id)
|
||||||
client.get_fee_rate_bps(down_token_id)
|
client.get_fee_rate_bps(up_token_id)
|
||||||
await asyncio.sleep(10)
|
client.get_fee_rate_bps(down_token_id)
|
||||||
|
|
||||||
|
|
||||||
async def place_anchor_and_hedge(
|
async def place_anchor_and_hedge(
|
||||||
@@ -10,4 +10,4 @@ def get_period_elapsed_seconds():
|
|||||||
|
|
||||||
def is_in_trading_window():
|
def is_in_trading_window():
|
||||||
elapsed_seconds = get_period_elapsed_seconds()
|
elapsed_seconds = get_period_elapsed_seconds()
|
||||||
return elapsed_seconds > MARKET_SESSION_SECONDS
|
return elapsed_seconds < (MARKET_SESSION_SECONDS - 5)
|
||||||
|
|||||||
Reference in New Issue
Block a user