improvement
This commit is contained in:
parent
761e595389
commit
360dc78833
@ -4,7 +4,7 @@ Handles all market data fetching and processing operations.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta, timezone
|
||||||
import logging
|
import logging
|
||||||
import alpaca_trade_api as tradeapi
|
import alpaca_trade_api as tradeapi
|
||||||
from config.trading_config import alpaca_config, trading_config
|
from config.trading_config import alpaca_config, trading_config
|
||||||
@ -55,6 +55,7 @@ class DataHandler:
|
|||||||
}
|
}
|
||||||
tf = tf_map.get(timeframe, tradeapi.TimeFrame.Hour)
|
tf = tf_map.get(timeframe, tradeapi.TimeFrame.Hour)
|
||||||
|
|
||||||
|
# Use simple date format for historical data
|
||||||
bars = self.api.get_crypto_bars(symbol, tf, start_date, end_date).df
|
bars = self.api.get_crypto_bars(symbol, tf, start_date, end_date).df
|
||||||
bars.index = pd.to_datetime(bars.index)
|
bars.index = pd.to_datetime(bars.index)
|
||||||
|
|
||||||
@ -129,12 +130,19 @@ class DataHandler:
|
|||||||
symbol = symbol or trading_config.symbol
|
symbol = symbol or trading_config.symbol
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Get latest bar
|
# Get latest bar using proper RFC3339 format
|
||||||
|
end_time = datetime.now()
|
||||||
|
start_time = end_time - timedelta(minutes=5)
|
||||||
|
|
||||||
|
# Convert to RFC3339 format with timezone offset
|
||||||
|
start_str = start_time.strftime('%Y-%m-%dT%H:%M:%S-00:00')
|
||||||
|
end_str = end_time.strftime('%Y-%m-%dT%H:%M:%S-00:00')
|
||||||
|
|
||||||
bars = self.api.get_crypto_bars(
|
bars = self.api.get_crypto_bars(
|
||||||
symbol,
|
symbol,
|
||||||
tradeapi.TimeFrame.Minute,
|
tradeapi.TimeFrame.Minute,
|
||||||
datetime.now() - timedelta(minutes=5),
|
start_str,
|
||||||
datetime.now()
|
end_str
|
||||||
).df
|
).df
|
||||||
|
|
||||||
if not bars.empty:
|
if not bars.empty:
|
||||||
|
|||||||
@ -59,10 +59,15 @@ class TradingEngine:
|
|||||||
'equity': float(account.equity),
|
'equity': float(account.equity),
|
||||||
'cash': float(account.cash),
|
'cash': float(account.cash),
|
||||||
'buying_power': float(account.buying_power),
|
'buying_power': float(account.buying_power),
|
||||||
'day_trade_count': int(account.day_trade_count),
|
'pattern_day_trader': getattr(account, 'pattern_day_trader', False)
|
||||||
'pattern_day_trader': account.pattern_day_trader
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Day trade count might not be available for crypto accounts
|
||||||
|
if hasattr(account, 'day_trade_count'):
|
||||||
|
account_info['day_trade_count'] = int(account.day_trade_count)
|
||||||
|
else:
|
||||||
|
account_info['day_trade_count'] = 0
|
||||||
|
|
||||||
# Update peak portfolio value
|
# Update peak portfolio value
|
||||||
if self.peak_portfolio_value is None or account_info['equity'] > self.peak_portfolio_value:
|
if self.peak_portfolio_value is None or account_info['equity'] > self.peak_portfolio_value:
|
||||||
self.peak_portfolio_value = account_info['equity']
|
self.peak_portfolio_value = account_info['equity']
|
||||||
@ -74,6 +79,7 @@ class TradingEngine:
|
|||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error getting account info: {e}")
|
logger.error(f"Error getting account info: {e}")
|
||||||
|
logger.error("Failed to get account information")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def get_current_position(self, symbol: str = None) -> dict:
|
def get_current_position(self, symbol: str = None) -> dict:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user