Untitled
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) async def get_order_book_imbalance(exchange, symbol, duration=60): end_time = exchange.milliseconds() + duration * 1000 imbalances = [] while exchange.milliseconds() < end_time: order_book = await exchange.watch_order_book(symbol) bids = np.array(order_book['bids']) asks = np.array(order_book['asks']) bid_volume = np.sum(bids[:, 1]) ask_volume = np.sum(asks[:, 1]) imbalance = bid_volume / (bid_volume + ask_volume) imbalances.append(imbalance) await asyncio.sleep(1) average_imbalance = np.mean(imbalances) return average_imbalance async def get_trade_buy_sell_ratio(exchange, symbol, duration=60): end_time = exchange.milliseconds() + duration * 1000 buy_volume = 0 total_volume = 0 seen_trades = set() while exchange.milliseconds() < end_time: trades = await exchange.watch_trades(symbol) for trade in trades: trade_id = trade.get('id') if trade_id not in seen_trades: seen_trades.add(trade_id) if trade['side'] == 'buy': buy_volume += trade['amount'] total_volume += trade['amount'] await asyncio.sleep(1) buy_sell_ratio = buy_volume / total_volume if total_volume > 0 else 0 return buy_sell_ratio async def get_market_direction(): exchange = ccxt.pro.binanceusdm() await exchange.load_markets() symbols = ['BNB/USDT:USDT', 'BTC/USDT:USDT'] # Run both data collectors concurrently for both symbols tasks = [ get_order_book_imbalance(exchange, symbol) for symbol in symbols ] + [ get_trade_buy_sell_ratio(exchange, symbol) for symbol in symbols ] results = await asyncio.gather(*tasks) bnb_order_book_imbalance, btc_order_book_imbalance, bnb_buy_sell_ratio, btc_buy_sell_ratio = results # Determine strategy (Using percentage change for trend-following logic) strategy = 'Mean Reversion' direction = 0 if all([bnb_order_book_imbalance > 0.51, btc_order_book_imbalance > 0.51]): direction = 1 elif all([bnb_order_book_imbalance < 0.49, btc_order_book_imbalance < 0.49]): direction = -1 # Print overview overview = { 'BNB Order Book Imbalance': bnb_order_book_imbalance, 'BTC Order Book Imbalance': btc_order_book_imbalance, 'BNB Buy/Sell Ratio': bnb_buy_sell_ratio, 'BTC Buy/Sell Ratio': btc_buy_sell_ratio, 'Selected Strategy': strategy, 'Predicted Direction': direction } print("\n--- Market Overview ---") for k, v in overview.items(): print(f"{k}: {v}") await exchange.close() return direction decision = asyncio.run(get_market_direction()) print(f"\nPredicted Market Direction (Next 5 mins): {decision}") new_fixed_amount = 0.1 value = w3.to_wei(new_fixed_amount, 'ether') database.loc[db_index, 'bet_size'] = new_fixed_amount
Leave a Comment