167 lines
6.9 KiB
Python
167 lines
6.9 KiB
Python
from datetime import datetime
|
|
from trading.position_calculator import PositionCalculator
|
|
from trading.portfolio import Portfolio, Position
|
|
from trading.journal import (TradeEntry, add_trade, get_open_trades,
|
|
create_portfolio_table, update_portfolio_value,
|
|
get_latest_portfolio_value, get_datetime_input,
|
|
get_order_type)
|
|
|
|
def get_float_input(prompt: str) -> float:
|
|
while True:
|
|
try:
|
|
value = input(prompt)
|
|
if value.lower() in ['q', 'quit', 'exit']:
|
|
return None
|
|
return float(value)
|
|
except ValueError:
|
|
print("Please enter a valid number")
|
|
|
|
def main():
|
|
# Initialize tables
|
|
create_portfolio_table()
|
|
|
|
# Get latest portfolio value or ask for initial value
|
|
portfolio_data = get_latest_portfolio_value()
|
|
if portfolio_data:
|
|
portfolio_value = portfolio_data['total_value']
|
|
cash_balance = portfolio_data['cash_balance']
|
|
print(f"\nCurrent Portfolio Value: ${portfolio_value:.2f}")
|
|
print(f"Cash Balance: ${cash_balance:.2f}")
|
|
print(f"Last Updated: {portfolio_data['date']}")
|
|
|
|
if input("\nUpdate portfolio value? (y/n): ").lower() == 'y':
|
|
new_value = get_float_input("Enter new portfolio value: $")
|
|
new_cash = get_float_input("Enter new cash balance: $")
|
|
notes = input("Notes (optional): ")
|
|
if new_value and new_cash:
|
|
portfolio_value = new_value
|
|
cash_balance = new_cash
|
|
update_portfolio_value(portfolio_value, cash_balance, notes)
|
|
else:
|
|
portfolio_value = get_float_input("Enter your initial portfolio value: $")
|
|
cash_balance = get_float_input("Enter your cash balance: $")
|
|
if portfolio_value and cash_balance:
|
|
update_portfolio_value(portfolio_value, cash_balance, "Initial portfolio setup")
|
|
|
|
if not portfolio_value or not cash_balance:
|
|
return
|
|
|
|
# Initialize calculator with current portfolio value
|
|
calculator = PositionCalculator(account_size=portfolio_value)
|
|
|
|
# Initialize portfolio
|
|
portfolio = Portfolio()
|
|
|
|
# Load existing open trades into portfolio
|
|
open_trades = get_open_trades()
|
|
for trade in open_trades:
|
|
position = Position(
|
|
symbol=trade['ticker'],
|
|
entry_date=trade['entry_date'],
|
|
entry_price=trade['entry_price'],
|
|
shares=trade['shares'],
|
|
stop_loss=trade['stop_loss'],
|
|
target_price=trade['target_price']
|
|
)
|
|
portfolio.add_position(position)
|
|
|
|
while True:
|
|
print("\nTrading Management System")
|
|
print("1. Calculate Position Size")
|
|
print("2. Add Position")
|
|
print("3. View Portfolio")
|
|
print("4. Remove Position")
|
|
print("5. Update Portfolio Value")
|
|
print("6. Exit")
|
|
|
|
choice = input("\nSelect an option (1-6): ")
|
|
|
|
if choice == "1":
|
|
try:
|
|
entry_price = get_float_input("Enter entry price: $")
|
|
target_price = get_float_input("Enter target price: $")
|
|
|
|
position = calculator.calculate_position_size(entry_price, target_price)
|
|
|
|
print("\nPosition Details:")
|
|
print(f"Shares: {position['shares']}")
|
|
print(f"Position Value: ${position['position_value']:.2f}")
|
|
print(f"Entry Price: ${entry_price:.2f}")
|
|
print(f"Stop Loss Price: ${position['stop_loss']:.2f}")
|
|
print(f"Target Price: ${position['target_price']:.2f}")
|
|
print(f"\nRisk Analysis:")
|
|
print(f"Risk Amount (1%): ${position['risk_amount']:.2f}")
|
|
print(f"Potential Loss: ${position['potential_loss']:.2f}")
|
|
print(f"Potential Profit: ${position['potential_profit']:.2f}")
|
|
print(f"Risk/Reward Ratio: {position['risk_reward_ratio']:.2f}")
|
|
|
|
except ValueError as e:
|
|
print(f"Error: {e}")
|
|
|
|
elif choice == "2":
|
|
try:
|
|
symbol = input("Enter symbol: ")
|
|
entry_price = float(input("Enter entry price: $"))
|
|
shares = int(input("Enter number of shares: "))
|
|
stop_loss = entry_price * 0.94 # Automatic 6% stop loss
|
|
target_price = float(input("Enter target price: $"))
|
|
|
|
position = Position(
|
|
symbol=symbol,
|
|
entry_date=datetime.now(),
|
|
entry_price=entry_price,
|
|
shares=shares,
|
|
stop_loss=stop_loss,
|
|
target_price=target_price
|
|
)
|
|
|
|
portfolio.add_position(position)
|
|
print(f"\nAdded position: {symbol}")
|
|
|
|
except ValueError:
|
|
print("Invalid input. Please try again.")
|
|
|
|
elif choice == "3":
|
|
positions = portfolio.get_position_summary()
|
|
if not positions:
|
|
print("\nNo positions in portfolio")
|
|
else:
|
|
print("\nCurrent Portfolio:")
|
|
for pos in positions:
|
|
print(f"\nSymbol: {pos['symbol']}")
|
|
print(f"Entry Date: {pos['entry_date']}")
|
|
print(f"Entry Price: ${pos['entry_price']:.2f}")
|
|
print(f"Shares: {pos['shares']}")
|
|
print(f"Current Value: ${pos['current_value']:.2f}")
|
|
print(f"Stop Loss: ${pos['stop_loss']:.2f}")
|
|
print(f"Target: ${pos['target_price']:.2f}")
|
|
print(f"Potential Profit: ${pos['potential_profit']:.2f}")
|
|
print(f"Potential Loss: ${pos['potential_loss']:.2f}")
|
|
print(f"Risk/Reward Ratio: {pos['risk_reward_ratio']:.2f}")
|
|
|
|
elif choice == "4":
|
|
symbol = input("Enter symbol to remove: ")
|
|
portfolio.remove_position(symbol)
|
|
print(f"\nRemoved position: {symbol}")
|
|
|
|
elif choice == "5":
|
|
new_value = get_float_input("Enter new portfolio value: $")
|
|
new_cash = get_float_input("Enter new cash balance: $")
|
|
notes = input("Notes (optional): ")
|
|
if new_value and new_cash:
|
|
portfolio_value = new_value
|
|
cash_balance = new_cash
|
|
update_portfolio_value(portfolio_value, cash_balance, notes)
|
|
print(f"\nPortfolio value updated: ${portfolio_value:.2f}")
|
|
print(f"Cash balance updated: ${cash_balance:.2f}")
|
|
|
|
elif choice == "6":
|
|
print("\nExiting Trading Management System")
|
|
break
|
|
|
|
else:
|
|
print("\nInvalid choice. Please try again.")
|
|
|
|
if __name__ == "__main__":
|
|
main()
|