Spaces:
Running
Running
saadrizvi09 commited on
Commit ·
f647b21
1
Parent(s): 6960cb6
Remove duplicate function definitions in live_trading.py
Browse files- live_trading.py +0 -69
live_trading.py
CHANGED
|
@@ -444,75 +444,6 @@ def _train_hmm_model(ticker: str, n_states: int = 3, train_years: int = 2):
|
|
| 444 |
return None, None
|
| 445 |
|
| 446 |
|
| 447 |
-
def get_user_sessions_from_db(user_email: str) -> List[dict]:
|
| 448 |
-
"""Get all sessions for a user from database"""
|
| 449 |
-
try:
|
| 450 |
-
with Session(engine) as db_session:
|
| 451 |
-
statement = select(TradingSession).where(
|
| 452 |
-
TradingSession.user_email == user_email
|
| 453 |
-
).order_by(TradingSession.start_time.desc()).limit(20)
|
| 454 |
-
sessions = db_session.exec(statement).all()
|
| 455 |
-
|
| 456 |
-
result = []
|
| 457 |
-
for s in sessions:
|
| 458 |
-
if s.session_id in active_sessions:
|
| 459 |
-
runner = active_sessions[s.session_id]
|
| 460 |
-
result.append(runner.get_status())
|
| 461 |
-
else:
|
| 462 |
-
elapsed = (datetime.now() - s.start_time).total_seconds() / 60
|
| 463 |
-
remaining = max(0, s.duration_minutes - elapsed)
|
| 464 |
-
if s.is_running and remaining <= 0:
|
| 465 |
-
s.is_running = False
|
| 466 |
-
s.end_time = datetime.now()
|
| 467 |
-
db_session.add(s)
|
| 468 |
-
db_session.commit()
|
| 469 |
-
|
| 470 |
-
result.append({
|
| 471 |
-
'session_id': s.session_id,
|
| 472 |
-
'strategy': s.strategy,
|
| 473 |
-
'symbol': s.symbol,
|
| 474 |
-
'trade_amount': s.trade_amount,
|
| 475 |
-
'is_running': s.is_running,
|
| 476 |
-
'position': 'FLAT',
|
| 477 |
-
'trades_count': s.trades_count,
|
| 478 |
-
'pnl': s.total_pnl,
|
| 479 |
-
'elapsed_minutes': round(elapsed, 1),
|
| 480 |
-
'remaining_minutes': round(remaining, 1),
|
| 481 |
-
'trades': []
|
| 482 |
-
})
|
| 483 |
-
return result
|
| 484 |
-
except Exception as e:
|
| 485 |
-
print(f"Error getting sessions: {e}")
|
| 486 |
-
return []
|
| 487 |
-
|
| 488 |
-
def _train_hmm_model(ticker: str, n_states: int = 3, train_years: int = 2):
|
| 489 |
-
"""Trains a GaussianHMM model based on historical data."""
|
| 490 |
-
try:
|
| 491 |
-
end_date = datetime.now()
|
| 492 |
-
start_date = end_date - timedelta(days=train_years * 365)
|
| 493 |
-
|
| 494 |
-
df = yf.download(ticker, start=start_date, end=end_date, progress=False)
|
| 495 |
-
if df.empty or len(df) < 100:
|
| 496 |
-
print("Not enough historical data to train HMM model.")
|
| 497 |
-
return None, None
|
| 498 |
-
|
| 499 |
-
df['Log_Returns'] = np.log(df['Close'] / df['Close'].shift(1))
|
| 500 |
-
df['Volatility'] = df['Log_Returns'].rolling(window=10).std()
|
| 501 |
-
df = df.dropna()
|
| 502 |
-
|
| 503 |
-
X_train = df[['Log_Returns', 'Volatility']].values * 100
|
| 504 |
-
hmm_model = GaussianHMM(n_components=n_states, covariance_type="full", n_iter=100, random_state=42)
|
| 505 |
-
hmm_model.fit(X_train)
|
| 506 |
-
|
| 507 |
-
state_vars = [hmm_model.means_[i][1] for i in range(n_states)]
|
| 508 |
-
high_vol_state = np.argmax(state_vars)
|
| 509 |
-
|
| 510 |
-
print(f"HMM model trained for {ticker}. High volatility state identified as: {high_vol_state}")
|
| 511 |
-
return hmm_model, high_vol_state
|
| 512 |
-
except Exception as e:
|
| 513 |
-
print(f"Error training HMM model: {e}")
|
| 514 |
-
return None, None
|
| 515 |
-
|
| 516 |
class BaseTradingSessionRunner:
|
| 517 |
"""Base class for managing a live trading session."""
|
| 518 |
|
|
|
|
| 444 |
return None, None
|
| 445 |
|
| 446 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 447 |
class BaseTradingSessionRunner:
|
| 448 |
"""Base class for managing a live trading session."""
|
| 449 |
|