feat: Add functions to get and update trading plans
This commit is contained in:
parent
ca4ceabc40
commit
8440171a08
@ -1,6 +1,6 @@
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from typing import Optional, Dict, Any
|
from typing import Optional, Dict, Any, List
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from db.db_connection import create_client
|
from db.db_connection import create_client
|
||||||
|
|
||||||
@ -207,3 +207,160 @@ def save_trading_plan(plan: TradingPlan) -> int:
|
|||||||
|
|
||||||
client.execute(query, params)
|
client.execute(query, params)
|
||||||
return plan.id
|
return plan.id
|
||||||
|
|
||||||
|
def get_trading_plan(plan_id: int) -> Optional[TradingPlan]:
|
||||||
|
"""Get a trading plan by ID"""
|
||||||
|
with create_client() as client:
|
||||||
|
result = client.execute("""
|
||||||
|
SELECT * FROM trading_plans WHERE id = %(id)s
|
||||||
|
""", {'id': plan_id})
|
||||||
|
|
||||||
|
if not result:
|
||||||
|
return None
|
||||||
|
|
||||||
|
plan = result[0]
|
||||||
|
return TradingPlan(
|
||||||
|
id=plan[0],
|
||||||
|
plan_name=plan[1],
|
||||||
|
status=PlanStatus(plan[2]),
|
||||||
|
created_at=plan[3],
|
||||||
|
updated_at=plan[4],
|
||||||
|
timeframe=Timeframe(plan[5]),
|
||||||
|
market_focus=MarketFocus(plan[6]),
|
||||||
|
entry_criteria=plan[7],
|
||||||
|
exit_criteria=plan[8],
|
||||||
|
stop_loss=plan[9],
|
||||||
|
profit_target=plan[10],
|
||||||
|
risk_reward_ratio=plan[11],
|
||||||
|
trade_frequency=TradeFrequency(plan[12]),
|
||||||
|
market_conditions=plan[13],
|
||||||
|
indicators_used=plan[14],
|
||||||
|
entry_confirmation=plan[15],
|
||||||
|
position_sizing=plan[16],
|
||||||
|
maximum_drawdown=plan[17],
|
||||||
|
max_trades_per_day=plan[18],
|
||||||
|
max_trades_per_week=plan[19],
|
||||||
|
total_risk_per_trade=plan[20],
|
||||||
|
max_portfolio_risk=plan[21],
|
||||||
|
adjustments_for_drawdown=plan[22],
|
||||||
|
risk_controls=plan[23],
|
||||||
|
win_rate=plan[24],
|
||||||
|
average_return_per_trade=plan[25],
|
||||||
|
profit_factor=plan[26],
|
||||||
|
historical_backtest_results=plan[27],
|
||||||
|
real_trade_performance=plan[28],
|
||||||
|
improvements_needed=plan[29],
|
||||||
|
strategy_version=plan[30],
|
||||||
|
plan_author=plan[31],
|
||||||
|
trade_review_notes=plan[32],
|
||||||
|
future_testing_ideas=plan[33],
|
||||||
|
sector_focus=plan[34],
|
||||||
|
fundamental_criteria=plan[35],
|
||||||
|
options_strategy_details=plan[36]
|
||||||
|
)
|
||||||
|
|
||||||
|
def get_all_trading_plans(status: Optional[PlanStatus] = None) -> List[TradingPlan]:
|
||||||
|
"""Get all trading plans, optionally filtered by status"""
|
||||||
|
with create_client() as client:
|
||||||
|
query = "SELECT * FROM trading_plans"
|
||||||
|
params = {}
|
||||||
|
|
||||||
|
if status:
|
||||||
|
query += " WHERE status = %(status)s"
|
||||||
|
params['status'] = status.value
|
||||||
|
|
||||||
|
query += " ORDER BY updated_at DESC"
|
||||||
|
|
||||||
|
results = client.execute(query, params)
|
||||||
|
return [get_trading_plan(result[0]) for result in results]
|
||||||
|
|
||||||
|
def update_trading_plan(plan: TradingPlan) -> bool:
|
||||||
|
"""Update an existing trading plan"""
|
||||||
|
if not plan.id:
|
||||||
|
raise ValueError("Cannot update plan without ID")
|
||||||
|
|
||||||
|
with create_client() as client:
|
||||||
|
plan.updated_at = datetime.now()
|
||||||
|
|
||||||
|
query = """
|
||||||
|
ALTER TABLE trading_plans
|
||||||
|
UPDATE
|
||||||
|
plan_name = %(plan_name)s,
|
||||||
|
status = %(status)s,
|
||||||
|
updated_at = %(updated_at)s,
|
||||||
|
timeframe = %(timeframe)s,
|
||||||
|
market_focus = %(market_focus)s,
|
||||||
|
entry_criteria = %(entry_criteria)s,
|
||||||
|
exit_criteria = %(exit_criteria)s,
|
||||||
|
stop_loss = %(stop_loss)s,
|
||||||
|
profit_target = %(profit_target)s,
|
||||||
|
risk_reward_ratio = %(risk_reward_ratio)s,
|
||||||
|
trade_frequency = %(trade_frequency)s,
|
||||||
|
market_conditions = %(market_conditions)s,
|
||||||
|
indicators_used = %(indicators_used)s,
|
||||||
|
entry_confirmation = %(entry_confirmation)s,
|
||||||
|
position_sizing = %(position_sizing)s,
|
||||||
|
maximum_drawdown = %(maximum_drawdown)s,
|
||||||
|
max_trades_per_day = %(max_trades_per_day)s,
|
||||||
|
max_trades_per_week = %(max_trades_per_week)s,
|
||||||
|
total_risk_per_trade = %(total_risk_per_trade)s,
|
||||||
|
max_portfolio_risk = %(max_portfolio_risk)s,
|
||||||
|
adjustments_for_drawdown = %(adjustments_for_drawdown)s,
|
||||||
|
risk_controls = %(risk_controls)s,
|
||||||
|
win_rate = %(win_rate)s,
|
||||||
|
average_return_per_trade = %(average_return_per_trade)s,
|
||||||
|
profit_factor = %(profit_factor)s,
|
||||||
|
historical_backtest_results = %(historical_backtest_results)s,
|
||||||
|
real_trade_performance = %(real_trade_performance)s,
|
||||||
|
improvements_needed = %(improvements_needed)s,
|
||||||
|
strategy_version = %(strategy_version)s,
|
||||||
|
plan_author = %(plan_author)s,
|
||||||
|
trade_review_notes = %(trade_review_notes)s,
|
||||||
|
future_testing_ideas = %(future_testing_ideas)s,
|
||||||
|
sector_focus = %(sector_focus)s,
|
||||||
|
fundamental_criteria = %(fundamental_criteria)s,
|
||||||
|
options_strategy_details = %(options_strategy_details)s
|
||||||
|
WHERE id = %(id)s
|
||||||
|
"""
|
||||||
|
|
||||||
|
params = {
|
||||||
|
'id': plan.id,
|
||||||
|
'plan_name': plan.plan_name,
|
||||||
|
'status': plan.status.value,
|
||||||
|
'updated_at': plan.updated_at,
|
||||||
|
'timeframe': plan.timeframe.value,
|
||||||
|
'market_focus': plan.market_focus.value,
|
||||||
|
'entry_criteria': plan.entry_criteria,
|
||||||
|
'exit_criteria': plan.exit_criteria,
|
||||||
|
'stop_loss': plan.stop_loss,
|
||||||
|
'profit_target': plan.profit_target,
|
||||||
|
'risk_reward_ratio': plan.risk_reward_ratio,
|
||||||
|
'trade_frequency': plan.trade_frequency.value,
|
||||||
|
'market_conditions': plan.market_conditions,
|
||||||
|
'indicators_used': plan.indicators_used,
|
||||||
|
'entry_confirmation': plan.entry_confirmation,
|
||||||
|
'position_sizing': plan.position_sizing,
|
||||||
|
'maximum_drawdown': plan.maximum_drawdown,
|
||||||
|
'max_trades_per_day': plan.max_trades_per_day,
|
||||||
|
'max_trades_per_week': plan.max_trades_per_week,
|
||||||
|
'total_risk_per_trade': plan.total_risk_per_trade,
|
||||||
|
'max_portfolio_risk': plan.max_portfolio_risk,
|
||||||
|
'adjustments_for_drawdown': plan.adjustments_for_drawdown,
|
||||||
|
'risk_controls': plan.risk_controls,
|
||||||
|
'win_rate': plan.win_rate,
|
||||||
|
'average_return_per_trade': plan.average_return_per_trade,
|
||||||
|
'profit_factor': plan.profit_factor,
|
||||||
|
'historical_backtest_results': plan.historical_backtest_results,
|
||||||
|
'real_trade_performance': plan.real_trade_performance,
|
||||||
|
'improvements_needed': plan.improvements_needed,
|
||||||
|
'strategy_version': plan.strategy_version,
|
||||||
|
'plan_author': plan.plan_author,
|
||||||
|
'trade_review_notes': plan.trade_review_notes,
|
||||||
|
'future_testing_ideas': plan.future_testing_ideas,
|
||||||
|
'sector_focus': plan.sector_focus,
|
||||||
|
'fundamental_criteria': plan.fundamental_criteria,
|
||||||
|
'options_strategy_details': plan.options_strategy_details
|
||||||
|
}
|
||||||
|
|
||||||
|
client.execute(query, params)
|
||||||
|
return True
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user