Update trading logic and order placement in response to configuration changes

This commit is contained in:
Nawaz Haider
2026-01-03 13:11:51 +06:00
parent 868806f534
commit e158f553cf
3 changed files with 21 additions and 16 deletions
+2 -2
View File
@@ -9,5 +9,5 @@ PROFIT_MARGIN = 0.02
TRADING_BPS_THRESHOLD = 50 TRADING_BPS_THRESHOLD = 50
MARKET_SESSION_SECONDS = 900 MARKET_SESSION_SECONDS = 900
TIMEZONE = "US/Eastern" TIMEZONE = "US/Eastern"
MAX_TRADES = 1 MAX_TRADES = 2
PLACE_OPPOSITE_ORDER = False # Hedge orders PLACE_OPPOSITE_ORDER = True # Hedge orders
+4 -2
View File
@@ -80,10 +80,12 @@ async def main():
down_ask_price = 1 - up_bid_price down_ask_price = 1 - up_bid_price
down_bid_price = 1 - up_ask_price down_bid_price = 1 - up_ask_price
up_trend = up_bid_price > down_ask_price
if trades < MAX_TRADES: if trades < MAX_TRADES:
trading_side = book.last_signal trading_side = book.last_signal
if trading_side == SIGNALES.UP: if (trading_side == SIGNALES.UP) and up_trend:
await place_anchor_and_hedge( await place_anchor_and_hedge(
up_token, up_token,
down_token, down_token,
@@ -96,7 +98,7 @@ async def main():
f"Placed UP anchor and hedge orders. Total trades: {trades}" f"Placed UP anchor and hedge orders. Total trades: {trades}"
) )
elif trading_side == SIGNALES.DOWN: elif (trading_side == SIGNALES.DOWN) and not up_trend:
await place_anchor_and_hedge( await place_anchor_and_hedge(
up_token, up_token,
down_token, down_token,
+15 -12
View File
@@ -5,6 +5,7 @@ from py_clob_client.clob_types import OrderArgs
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
from in_memory_db.utils import contains_item as in_memory_db_contains_item
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -34,11 +35,15 @@ 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
asyncio.create_task(place_limit_order(anchor_token_id, price, size)) anchor_order_id = await place_limit_order(anchor_token_id, price, size)
if PLACE_OPPOSITE_ORDER: if PLACE_OPPOSITE_ORDER and anchor_token_id:
asyncio.create_task( for _ in range(900 * 100):
place_limit_order(hedge_token_id, 1 - price - PROFIT_MARGIN, size) if in_memory_db_contains_item(anchor_order_id):
) hedge_order_id = await place_limit_order(
hedge_token_id, 1 - price - PROFIT_MARGIN, size
)
break
await asyncio.sleep(0.01)
logger.info(f"Order prices: {price} and {1 - price - PROFIT_MARGIN}") logger.info(f"Order prices: {price} and {1 - price - PROFIT_MARGIN}")
else: else:
@@ -47,7 +52,6 @@ async def place_anchor_and_hedge(
async def place_limit_order(token_id: str, price: float, size: int): async def place_limit_order(token_id: str, price: float, size: int):
client = get_client() client = get_client()
start_time = asyncio.get_event_loop().time()
try: try:
response = client.create_and_post_order( response = client.create_and_post_order(
OrderArgs( OrderArgs(
@@ -57,11 +61,10 @@ async def place_limit_order(token_id: str, price: float, size: int):
side=BUY, side=BUY,
) )
) )
logger.info(
f"Placed limit order: Token ID={token_id}, Price={price}, Size={size}, ID={response['orderID']}"
)
return response["orderID"]
except Exception as e: except Exception as e:
logger.error(f"Error placing order for token {token_id}: {e}") logger.error(f"Error placing order for token {token_id}: {e}")
return return None
end_time = asyncio.get_event_loop().time()
logger.info(
f"Order placed! ID: {response['orderID']} in {end_time - start_time:.2f} sec"
)
return response["orderID"]