đŸ› ïžToolsShed

Stock Profit Calculator

Calculate profit, loss, and return on investment for stock trades.

Questions Fréquentes

Implémentation du Code

def stock_profit(
    shares: float,
    buy_price: float,
    sell_price: float,
    buy_fee: float = 0,
    sell_fee: float = 0
) -> dict:
    """Calculate stock trade profit/loss."""
    total_cost = shares * buy_price + buy_fee
    total_proceeds = shares * sell_price - sell_fee
    profit = total_proceeds - total_cost
    pct_return = (profit / total_cost) * 100 if total_cost > 0 else 0
    return {
        "total_cost": total_cost,
        "total_proceeds": total_proceeds,
        "profit": profit,
        "pct_return": pct_return,
        "fees_paid": buy_fee + sell_fee,
    }

# Example
result = stock_profit(
    shares=100,
    buy_price=50.00,
    sell_price=65.50,
    buy_fee=4.95,
    sell_fee=4.95
)
print(f"Total Cost:     ${result['total_cost']:,.2f}")
print(f"Total Proceeds: ${result['total_proceeds']:,.2f}")
print(f"Profit/Loss:    ${result['profit']:,.2f}")
print(f"Return:         {result['pct_return']:.2f}%")
print(f"Fees Paid:      ${result['fees_paid']:,.2f}")

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.