|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Example demonstrating JoinBid and JoinAsk order types. |
| 4 | +
|
| 5 | +JoinBid and JoinAsk orders are passive liquidity-providing orders that automatically |
| 6 | +place limit orders at the current best bid or ask price. They're useful for: |
| 7 | +- Market making strategies |
| 8 | +- Providing liquidity |
| 9 | +- Minimizing market impact |
| 10 | +- Getting favorable queue position |
| 11 | +""" |
| 12 | + |
| 13 | +import asyncio |
| 14 | +import os |
| 15 | +import sys |
| 16 | +from pathlib import Path |
| 17 | + |
| 18 | +# Add src to Python path for development |
| 19 | +sys.path.insert(0, str(Path(__file__).parent.parent / "src")) |
| 20 | + |
| 21 | +from project_x_py import ProjectX, create_order_manager |
| 22 | + |
| 23 | + |
| 24 | +async def main(): |
| 25 | + """Demonstrate JoinBid and JoinAsk order placement.""" |
| 26 | + # Initialize client |
| 27 | + async with ProjectX.from_env() as client: |
| 28 | + await client.authenticate() |
| 29 | + |
| 30 | + # Create order manager |
| 31 | + order_manager = create_order_manager(client) |
| 32 | + |
| 33 | + # Contract to trade |
| 34 | + contract = "MNQ" |
| 35 | + |
| 36 | + print(f"=== JoinBid and JoinAsk Order Example for {contract} ===\n") |
| 37 | + |
| 38 | + # Get current market data to show context |
| 39 | + bars = await client.get_bars(contract, days=1, timeframe="1min") |
| 40 | + if bars and not bars.is_empty(): |
| 41 | + latest = bars.tail(1) |
| 42 | + print(f"Current market context:") |
| 43 | + print(f" Last price: ${latest['close'][0]:,.2f}") |
| 44 | + print(f" High: ${latest['high'][0]:,.2f}") |
| 45 | + print(f" Low: ${latest['low'][0]:,.2f}\n") |
| 46 | + |
| 47 | + try: |
| 48 | + # Example 1: Place a JoinBid order |
| 49 | + print("1. Placing JoinBid order (buy at best bid)...") |
| 50 | + join_bid_response = await order_manager.place_join_bid_order( |
| 51 | + contract_id=contract, size=1 |
| 52 | + ) |
| 53 | + |
| 54 | + if join_bid_response.success: |
| 55 | + print(f"✅ JoinBid order placed successfully!") |
| 56 | + print(f" Order ID: {join_bid_response.orderId}") |
| 57 | + print(f" This order will buy at the current best bid price\n") |
| 58 | + else: |
| 59 | + print(f"❌ JoinBid order failed: {join_bid_response.message}\n") |
| 60 | + |
| 61 | + # Wait a moment |
| 62 | + await asyncio.sleep(2) |
| 63 | + |
| 64 | + # Example 2: Place a JoinAsk order |
| 65 | + print("2. Placing JoinAsk order (sell at best ask)...") |
| 66 | + join_ask_response = await order_manager.place_join_ask_order( |
| 67 | + contract_id=contract, size=1 |
| 68 | + ) |
| 69 | + |
| 70 | + if join_ask_response.success: |
| 71 | + print(f"✅ JoinAsk order placed successfully!") |
| 72 | + print(f" Order ID: {join_ask_response.orderId}") |
| 73 | + print(f" This order will sell at the current best ask price\n") |
| 74 | + else: |
| 75 | + print(f"❌ JoinAsk order failed: {join_ask_response.message}\n") |
| 76 | + |
| 77 | + # Show order status |
| 78 | + print("3. Checking order status...") |
| 79 | + active_orders = await order_manager.get_active_orders() |
| 80 | + |
| 81 | + print(f"\nActive orders: {len(active_orders)}") |
| 82 | + for order in active_orders: |
| 83 | + if order.id in [join_bid_response.orderId, join_ask_response.orderId]: |
| 84 | + order_type = "JoinBid" if order.side == 0 else "JoinAsk" |
| 85 | + side = "Buy" if order.side == 0 else "Sell" |
| 86 | + print( |
| 87 | + f" - {order_type} Order {order.id}: {side} {order.size} @ ${order.price:,.2f}" |
| 88 | + ) |
| 89 | + |
| 90 | + # Cancel orders to clean up |
| 91 | + print("\n4. Cancelling orders...") |
| 92 | + if join_bid_response.success: |
| 93 | + cancel_result = await order_manager.cancel_order( |
| 94 | + join_bid_response.orderId |
| 95 | + ) |
| 96 | + if cancel_result.success: |
| 97 | + print(f"✅ JoinBid order {join_bid_response.orderId} cancelled") |
| 98 | + |
| 99 | + if join_ask_response.success: |
| 100 | + cancel_result = await order_manager.cancel_order( |
| 101 | + join_ask_response.orderId |
| 102 | + ) |
| 103 | + if cancel_result.success: |
| 104 | + print(f"✅ JoinAsk order {join_ask_response.orderId} cancelled") |
| 105 | + |
| 106 | + except Exception as e: |
| 107 | + print(f"❌ Error: {e}") |
| 108 | + |
| 109 | + print("\n=== JoinBid/JoinAsk Example Complete ===") |
| 110 | + print("\nKey Points:") |
| 111 | + print("- JoinBid places a limit buy order at the current best bid") |
| 112 | + print("- JoinAsk places a limit sell order at the current best ask") |
| 113 | + print("- These are passive orders that provide liquidity") |
| 114 | + print("- The actual fill price depends on market conditions") |
| 115 | + print("- Useful for market making and minimizing market impact") |
| 116 | + |
| 117 | + |
| 118 | +if __name__ == "__main__": |
| 119 | + # Check for required environment variables |
| 120 | + if not os.getenv("PROJECT_X_API_KEY") or not os.getenv("PROJECT_X_USERNAME"): |
| 121 | + print( |
| 122 | + "❌ Error: Please set PROJECT_X_API_KEY and PROJECT_X_USERNAME environment variables" |
| 123 | + ) |
| 124 | + print("Example:") |
| 125 | + print(' export PROJECT_X_API_KEY="your-api-key"') |
| 126 | + print(' export PROJECT_X_USERNAME="your-username"') |
| 127 | + sys.exit(1) |
| 128 | + |
| 129 | + asyncio.run(main()) |
0 commit comments