12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- import sys
- import os
- from datetime import datetime
- # Adjust path if needed to import TradingStats and TradingEngine
- sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
- from src.stats.trading_stats import TradingStats
- from src.trading.trading_engine import TradingEngine
- def update_db_with_latest_mark_prices():
- engine = TradingEngine()
- stats = TradingStats()
- exchange_positions = engine.get_positions() or []
- print(f"Fetched {len(exchange_positions)} positions from exchange.")
- updated = 0
- for pos in exchange_positions:
- symbol = pos.get('symbol')
- mark_price = pos.get('markPrice') or pos.get('markPx')
- if symbol and mark_price is not None:
- # Find the open trade in DB
- open_positions = stats.get_open_positions()
- for db_pos in open_positions:
- if db_pos.get('symbol') == symbol:
- lifecycle_id = db_pos.get('trade_lifecycle_id')
- try:
- stats.trade_manager.update_trade_market_data(
- lifecycle_id,
- mark_price=float(mark_price)
- )
- updated += 1
- print(f"Updated {symbol} (Lifecycle: {lifecycle_id}) with mark_price={mark_price}")
- except Exception as e:
- print(f"Failed to update {symbol}: {e}")
- print(f"Updated {updated} positions in DB with latest mark prices.")
- def print_open_positions_mark_prices():
- stats = TradingStats()
- open_positions = stats.get_open_positions()
- print(f"\nDB now has {len(open_positions)} open positions:")
- for pos in open_positions:
- symbol = pos.get('symbol', 'N/A')
- entry_price = pos.get('entry_price', 'N/A')
- mark_price = pos.get('mark_price', 'N/A')
- print(f"Symbol: {symbol}, Entry Price: {entry_price}, Mark Price: {mark_price}")
- if __name__ == "__main__":
- update_db_with_latest_mark_prices()
- print_open_positions_mark_prices()
|