diff --git a/main.py b/main.py index 7ab6ee1..feec364 100644 --- a/main.py +++ b/main.py @@ -7,6 +7,7 @@ from utils.tokens import fetch_tokens from utils.orderbook import OrderBook, SIGNALES from utils.clob_client import init_global_client, is_client_ready from utils.market_time import is_in_trading_window +from utils.trade_counter import reset_trades, get_trades_count, increment_trades from utils.clob_orders import ( place_anchor_and_hedge, cache_token_trading_infos, @@ -28,8 +29,6 @@ requests.options = session.options async def main(): - trades = 0 - logger = setup_logging() set_cpu_affinity() logger.info("Polymarket HFT Market Maker started") @@ -64,7 +63,7 @@ async def main(): logger.info("Trading session ended. Starting new session.") gc.collect() await asyncio.sleep(10) - trades = 0 + reset_trades() up_token, down_token, market_slug = await fetch_tokens() book = OrderBook(up_token, down_token, market_slug) asyncio.create_task(cache_token_trading_infos(book)) @@ -85,7 +84,7 @@ async def main(): up_trend = up_bid_price > down_bid_price - if trades < MAX_TRADES: + if get_trades_count() < MAX_TRADES: trading_side = book.last_signal if (trading_side == SIGNALES.UP) and not up_trend: @@ -96,9 +95,9 @@ async def main(): up_bid_price, size=5, ) - trades += 1 + current_trades = increment_trades() logger.info( - f"Placed UP anchor and hedge orders. Total trades: {trades}" + f"Placed UP anchor and hedge orders. Total trades: {current_trades}" ) elif (trading_side == SIGNALES.DOWN) and up_trend: @@ -109,9 +108,9 @@ async def main(): down_bid_price, size=5, ) - trades += 1 + current_trades = increment_trades() logger.info( - f"Placed DOWN anchor and hedge orders. Total trades: {trades}" + f"Placed DOWN anchor and hedge orders. Total trades: {current_trades}" ) await asyncio.sleep(0.01) diff --git a/utils/clob_orders.py b/utils/clob_orders.py index dcd23ab..0710571 100644 --- a/utils/clob_orders.py +++ b/utils/clob_orders.py @@ -5,6 +5,7 @@ from py_clob_client.clob_types import OrderArgs, OrderType from py_clob_client.order_builder.constants import BUY from config import PROFIT_MARGIN, PLACE_OPPOSITE_ORDER from utils.clob_client import get_client +from utils.trade_counter import decrement_trades from in_memory_db.utils import contains_item as in_memory_db_contains_item @@ -49,6 +50,9 @@ async def place_anchor_and_hedge( else: logger.info(f"Order price: {price}") + if anchor_order_id and not in_memory_db_contains_item(anchor_order_id): + decrement_trades() + async def place_limit_order(token_id: str, price: float, size: int, expire=False): client = get_client() diff --git a/utils/trade_counter.py b/utils/trade_counter.py new file mode 100644 index 0000000..1df1ab8 --- /dev/null +++ b/utils/trade_counter.py @@ -0,0 +1,23 @@ +# Global trades counter +_trades = 0 + + +def get_trades_count(): + return _trades + + +def increment_trades(): + global _trades + _trades += 1 + return _trades + + +def decrement_trades(): + global _trades + _trades -= 1 + return _trades + + +def reset_trades(): + global _trades + _trades = 0