refactor: Update trade entry handling for sell orders with exit price and date
This commit is contained in:
parent
9493f5d7ac
commit
e22a0ad67e
@ -168,6 +168,15 @@ class TradeEntry:
|
||||
notes: Optional[str] = None
|
||||
position_id: Optional[str] = None
|
||||
|
||||
def __post_init__(self):
|
||||
# For sell orders, move entry_price to exit_price and entry_date to exit_date
|
||||
if self.direction.lower() == 'sell':
|
||||
self.exit_price = self.entry_price
|
||||
self.exit_date = self.entry_date
|
||||
# Clear entry price since this is a sell
|
||||
self.entry_price = None
|
||||
self.entry_date = None
|
||||
|
||||
@property
|
||||
def expected_profit_loss(self) -> Optional[float]:
|
||||
if self.direction == 'buy' and self.target_price:
|
||||
@ -348,6 +357,7 @@ def get_order_type() -> Optional[str]:
|
||||
def add_trade(trade: TradeEntry):
|
||||
"""Add a new trade to the database"""
|
||||
with create_client() as client:
|
||||
# For sell orders, use exit_price and exit_date instead of entry_price and entry_date
|
||||
query = f"""
|
||||
INSERT INTO stock_db.trades (
|
||||
id, position_id, ticker, entry_date, shares, entry_price, target_price,
|
||||
@ -357,18 +367,18 @@ def add_trade(trade: TradeEntry):
|
||||
{generate_id()},
|
||||
'{trade.position_id}',
|
||||
'{trade.ticker}',
|
||||
'{trade.entry_date.strftime('%Y-%m-%d %H:%M:%S')}',
|
||||
{f"'{trade.entry_date.strftime('%Y-%m-%d %H:%M:%S')}'" if trade.entry_date else 'NULL'},
|
||||
{trade.shares},
|
||||
{trade.entry_price},
|
||||
{trade.entry_price if trade.entry_price else 'NULL'},
|
||||
{trade.target_price if trade.target_price else 'NULL'},
|
||||
{trade.stop_loss if trade.stop_loss else 'NULL'},
|
||||
{f"'{trade.strategy}'" if trade.strategy else 'NULL'},
|
||||
'{trade.order_type}',
|
||||
'{trade.direction.lower()}', # Ensure direction is stored in lowercase
|
||||
'{trade.direction.lower()}',
|
||||
{1 if trade.followed_rules else 0},
|
||||
{f"'{trade.entry_reason}'" if trade.entry_reason else 'NULL'},
|
||||
NULL,
|
||||
NULL,
|
||||
{trade.exit_price if trade.exit_price else 'NULL'},
|
||||
{f"'{trade.exit_date.strftime('%Y-%m-%d %H:%M:%S')}'" if trade.exit_date else 'NULL'},
|
||||
{f"'{trade.exit_reason}'" if trade.exit_reason else 'NULL'},
|
||||
{f"'{trade.notes}'" if trade.notes else 'NULL'}
|
||||
)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user