INNER CODE UNIT · Python
get_ohlcv_history
flukelaster/ai-trading-agent · mt5_bridge/main.py:432
async def get_ohlcv_history(symbol: str, timeframe: str = "M15", from_date: str = "", to_date: str = ""):
"""Get historical OHLCV data by date range. Returns up to 50000 bars."""
if not ensure_connected():
return mt5_response(False, error="MT5 not connected")
tf = TIMEFRAMES.get(timeframe.upper())
if tf is None:
return mt5_response(False, error=f"Invalid timeframe: {timeframe}")
try:
dt_from = datetime.fromisoformat(from_date)
dt_to = datetime.fromisoformat(to_date)
except (ValueError, TypeError):
return mt5_response(False, error="Invalid date format. Use ISO format: YYYY-MM-DD")
rates = mt5.copy_rates_range(symbol, tf, dt_from, dt_to)
if rates is None or len(rates) == 0:
return mt5_response(False, error=f"No OHLCV data for {symbol} in range")
data = []
for r in rates: