all changes done over the last 2 weeks
This commit is contained in:
@@ -276,6 +276,87 @@ class TradingDatabase:
|
||||
self.cursor.execute(query, values)
|
||||
self.conn.commit()
|
||||
|
||||
def close_trade(self, ticket: int, exit_price: float, exit_time: str,
|
||||
profit: float, status: str = 'closed', exit_reason: str = None,
|
||||
commission: float = 0, swap: float = 0):
|
||||
"""
|
||||
Simplified wrapper for closing a trade (used by Position Monitor)
|
||||
|
||||
Args:
|
||||
ticket: MT5 ticket number
|
||||
exit_price: Exit price
|
||||
exit_time: Exit datetime
|
||||
profit: Trade profit
|
||||
status: Trade status (default 'closed')
|
||||
exit_reason: Reason for exit ('tp', 'sl', 'manual', etc.)
|
||||
commission: Commission paid
|
||||
swap: Swap paid
|
||||
"""
|
||||
from datetime import datetime
|
||||
|
||||
# Calculate duration if we have entry_time
|
||||
duration_hours = None
|
||||
try:
|
||||
query = "SELECT entry_time FROM trades WHERE ticket = ?"
|
||||
self.cursor.execute(query, (ticket,))
|
||||
row = self.cursor.fetchone()
|
||||
if row:
|
||||
entry_time = datetime.fromisoformat(row[0])
|
||||
if isinstance(exit_time, str):
|
||||
exit_dt = datetime.fromisoformat(exit_time)
|
||||
else:
|
||||
exit_dt = exit_time
|
||||
duration_hours = (exit_dt - entry_time).total_seconds() / 3600
|
||||
except:
|
||||
pass
|
||||
|
||||
net_profit = profit - commission - swap
|
||||
|
||||
exit_data = {
|
||||
'exit_price': exit_price,
|
||||
'exit_time': exit_time if isinstance(exit_time, str) else exit_time.isoformat(),
|
||||
'duration_hours': duration_hours,
|
||||
'profit': profit,
|
||||
'commission': commission,
|
||||
'swap': swap,
|
||||
'net_profit': net_profit,
|
||||
'exit_reason': exit_reason,
|
||||
'profit_pct': None, # Would need entry data to calculate
|
||||
'rr_ratio': None # Would need entry data to calculate
|
||||
}
|
||||
|
||||
self.update_trade_exit(ticket, exit_data)
|
||||
|
||||
|
||||
def get_open_trades(self) -> List[Dict]:
|
||||
"""
|
||||
Get all currently open trades from database
|
||||
|
||||
Returns:
|
||||
List of open trades as dictionaries
|
||||
"""
|
||||
query = """
|
||||
SELECT
|
||||
ticket, position_id, symbol, strategy_name,
|
||||
type, volume, entry_price, sl_price, tp_price,
|
||||
entry_time, session, regime, quality,
|
||||
confidence, timeframe_alignment,
|
||||
risk_amount, risk_pct, status
|
||||
FROM trades
|
||||
WHERE status = 'open'
|
||||
ORDER BY entry_time DESC
|
||||
"""
|
||||
|
||||
self.cursor.execute(query)
|
||||
rows = self.cursor.fetchall()
|
||||
|
||||
# Convert to list of dictionaries
|
||||
trades = []
|
||||
for row in rows:
|
||||
trades.append(dict(row))
|
||||
|
||||
return trades
|
||||
|
||||
|
||||
def log_bot_status(self, status_data: Dict):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user