|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Example: Simplified Data Access with v3.0.0 |
| 4 | +
|
| 5 | +This example demonstrates the new convenience methods for accessing market data |
| 6 | +in the ProjectX SDK v3.0.0. These methods provide a cleaner, more intuitive API |
| 7 | +for common data access patterns. |
| 8 | +
|
| 9 | +Key improvements: |
| 10 | +- get_latest_bars() - Get recent bars without verbose parameters |
| 11 | +- get_latest_price() - Clear method name for current price |
| 12 | +- get_ohlc() - Get OHLC values as a simple dictionary |
| 13 | +- get_price_range() - Calculate price statistics easily |
| 14 | +- get_volume_stats() - Quick volume analysis |
| 15 | +- is_data_ready() - Check if enough data is loaded |
| 16 | +- get_bars_since() - Get data since a specific time |
| 17 | +
|
| 18 | +Author: SDK v3.0.0 Examples |
| 19 | +""" |
| 20 | + |
| 21 | +import asyncio |
| 22 | +from datetime import datetime, timedelta |
| 23 | + |
| 24 | +from project_x_py import EventType, TradingSuite |
| 25 | + |
| 26 | + |
| 27 | +async def demonstrate_simplified_access(): |
| 28 | + """Show the new simplified data access methods.""" |
| 29 | + |
| 30 | + # Create trading suite with 3 timeframes |
| 31 | + async with await TradingSuite.create( |
| 32 | + "MNQ", timeframes=["1min", "5min", "15min"], initial_days=2 |
| 33 | + ) as suite: |
| 34 | + print("=== Simplified Data Access Demo ===\n") |
| 35 | + |
| 36 | + # 1. Check if data is ready |
| 37 | + if await suite.data.is_data_ready(min_bars=50): |
| 38 | + print("✅ Sufficient data loaded for all timeframes") |
| 39 | + else: |
| 40 | + print("⏳ Waiting for more data...") |
| 41 | + await asyncio.sleep(5) |
| 42 | + |
| 43 | + # 2. Get latest price - much cleaner than get_current_price() |
| 44 | + price = await suite.data.get_latest_price() |
| 45 | + if price: |
| 46 | + print(f"\n📊 Current Price: ${price:,.2f}") |
| 47 | + |
| 48 | + # 3. Get OHLC as a simple dictionary |
| 49 | + ohlc = await suite.data.get_ohlc("5min") |
| 50 | + if ohlc: |
| 51 | + print(f"\n📈 Latest 5min Bar:") |
| 52 | + print(f" Open: ${ohlc['open']:,.2f}") |
| 53 | + print(f" High: ${ohlc['high']:,.2f}") |
| 54 | + print(f" Low: ${ohlc['low']:,.2f}") |
| 55 | + print(f" Close: ${ohlc['close']:,.2f}") |
| 56 | + print(f" Volume: {ohlc['volume']:,.0f}") |
| 57 | + |
| 58 | + # 4. Get latest few bars - cleaner syntax |
| 59 | + recent_bars = await suite.data.get_latest_bars(count=5, timeframe="1min") |
| 60 | + if recent_bars is not None: |
| 61 | + print(f"\n📊 Last 5 1-minute bars:") |
| 62 | + for i in range(len(recent_bars)): |
| 63 | + bar = recent_bars.row(i, named=True) |
| 64 | + print( |
| 65 | + f" {bar['timestamp']}: ${bar['close']:,.2f} (vol: {bar['volume']:,.0f})" |
| 66 | + ) |
| 67 | + |
| 68 | + # 5. Get price range statistics |
| 69 | + range_stats = await suite.data.get_price_range(bars=20, timeframe="5min") |
| 70 | + if range_stats: |
| 71 | + print(f"\n📊 20-bar Price Range (5min):") |
| 72 | + print(f" High: ${range_stats['high']:,.2f}") |
| 73 | + print(f" Low: ${range_stats['low']:,.2f}") |
| 74 | + print(f" Range: ${range_stats['range']:,.2f}") |
| 75 | + print(f" Avg Range per Bar: ${range_stats['avg_range']:,.2f}") |
| 76 | + |
| 77 | + # 6. Get volume statistics |
| 78 | + vol_stats = await suite.data.get_volume_stats(bars=20, timeframe="5min") |
| 79 | + if vol_stats: |
| 80 | + print(f"\n📊 20-bar Volume Stats (5min):") |
| 81 | + print(f" Current Volume: {vol_stats['current']:,.0f}") |
| 82 | + print(f" Average Volume: {vol_stats['average']:,.0f}") |
| 83 | + print(f" Relative Volume: {vol_stats['relative']:.1%}") |
| 84 | + |
| 85 | + if vol_stats["relative"] > 1.5: |
| 86 | + print(" ⚡ HIGH VOLUME ALERT!") |
| 87 | + |
| 88 | + # 7. Get bars since a specific time |
| 89 | + one_hour_ago = datetime.now() - timedelta(hours=1) |
| 90 | + recent_activity = await suite.data.get_bars_since(one_hour_ago, "1min") |
| 91 | + if recent_activity is not None: |
| 92 | + print(f"\n📊 Bars in last hour: {len(recent_activity)}") |
| 93 | + |
| 94 | + # Calculate price movement |
| 95 | + if len(recent_activity) > 0: |
| 96 | + first_price = float(recent_activity["open"][0]) |
| 97 | + last_price = float(recent_activity["close"][-1]) |
| 98 | + change = last_price - first_price |
| 99 | + change_pct = (change / first_price) * 100 |
| 100 | + |
| 101 | + print(f" Price Change: ${change:+,.2f} ({change_pct:+.2f}%)") |
| 102 | + |
| 103 | + # 8. Multi-timeframe quick access |
| 104 | + print("\n📊 Multi-Timeframe Summary:") |
| 105 | + for tf in ["1min", "5min", "15min"]: |
| 106 | + bars = await suite.data.get_latest_bars(count=1, timeframe=tf) |
| 107 | + if bars is not None and not bars.is_empty(): |
| 108 | + close = float(bars["close"][0]) |
| 109 | + volume = float(bars["volume"][0]) |
| 110 | + print(f" {tf}: ${close:,.2f} (vol: {volume:,.0f})") |
| 111 | + |
| 112 | + |
| 113 | +async def demonstrate_trading_usage(): |
| 114 | + """Show how simplified access improves trading logic.""" |
| 115 | + |
| 116 | + async with await TradingSuite.create("MNQ") as suite: |
| 117 | + print("\n=== Trading Logic with Simplified Access ===\n") |
| 118 | + |
| 119 | + # Wait for enough data |
| 120 | + while not await suite.data.is_data_ready(min_bars=50): |
| 121 | + print("Waiting for data...") |
| 122 | + await asyncio.sleep(1) |
| 123 | + |
| 124 | + # Simple trading logic using new methods |
| 125 | + price = await suite.data.get_latest_price() |
| 126 | + range_stats = await suite.data.get_price_range(bars=20) |
| 127 | + vol_stats = await suite.data.get_volume_stats(bars=20) |
| 128 | + |
| 129 | + if price and range_stats and vol_stats: |
| 130 | + # Example strategy logic |
| 131 | + print(f"Current Price: ${price:,.2f}") |
| 132 | + print(f"20-bar Range: ${range_stats['range']:,.2f}") |
| 133 | + print(f"Volume Ratio: {vol_stats['relative']:.1%}") |
| 134 | + |
| 135 | + # Simple breakout detection |
| 136 | + if price > range_stats["high"]: |
| 137 | + print("🚀 Price breaking above 20-bar high!") |
| 138 | + if vol_stats["relative"] > 1.2: |
| 139 | + print(" ✅ With above-average volume - Strong signal!") |
| 140 | + else: |
| 141 | + print(" ⚠️ But volume is weak - Be cautious") |
| 142 | + |
| 143 | + elif price < range_stats["low"]: |
| 144 | + print("📉 Price breaking below 20-bar low!") |
| 145 | + if vol_stats["relative"] > 1.2: |
| 146 | + print(" ✅ With above-average volume - Strong signal!") |
| 147 | + else: |
| 148 | + print(" ⚠️ But volume is weak - Be cautious") |
| 149 | + |
| 150 | + else: |
| 151 | + range_position = (price - range_stats["low"]) / range_stats["range"] |
| 152 | + print(f"Price is {range_position:.1%} within the 20-bar range") |
| 153 | + |
| 154 | + |
| 155 | +async def main(): |
| 156 | + """Run all demonstrations.""" |
| 157 | + try: |
| 158 | + # Show simplified data access |
| 159 | + await demonstrate_simplified_access() |
| 160 | + |
| 161 | + # Show trading usage |
| 162 | + await demonstrate_trading_usage() |
| 163 | + |
| 164 | + except KeyboardInterrupt: |
| 165 | + print("\n\nDemo interrupted by user") |
| 166 | + except Exception as e: |
| 167 | + print(f"\n❌ Error: {e}") |
| 168 | + import traceback |
| 169 | + |
| 170 | + traceback.print_exc() |
| 171 | + |
| 172 | + |
| 173 | +if __name__ == "__main__": |
| 174 | + print("ProjectX SDK v3.0.0 - Simplified Data Access") |
| 175 | + print("=" * 50) |
| 176 | + asyncio.run(main()) |
0 commit comments