Trading journal

 avatar
unknown
plain_text
a month ago
2.1 kB
2
Indexable
import pandas as pd

# Define the structure of the trading journal
data = {
    'Date': [],
    'Ticker': [],
    'Entry Price': [],
    'Exit Price': [],
    'Stop Loss': [],
    'Target Price': [],
    'Shares': [],
    'Risk': [],
    'Reward': [],
    'R/R Ratio': [],
    'P/L': [],
    'Win/Loss': []
}

# Create a DataFrame
journal = pd.DataFrame(data)

# Function to add a trade to the journal
def add_trade(date, ticker, entry_price, exit_price, stop_loss, target_price, shares):
    risk = abs(entry_price - stop_loss) * shares
    reward = abs(target_price - entry_price) * shares
    rr_ratio = reward / risk if risk != 0 else None
    pl = (exit_price - entry_price) * shares
    win_loss = 'Win' if pl > 0 else 'Loss'

    # Append trade details to the DataFrame
    journal.loc[len(journal)] = {
        'Date': date,
        'Ticker': ticker,
        'Entry Price': entry_price,
        'Exit Price': exit_price,
        'Stop Loss': stop_loss,
        'Target Price': target_price,
        'Shares': shares,
        'Risk': risk,
        'Reward': reward,
        'R/R Ratio': rr_ratio,
        'P/L': pl,
        'Win/Loss': win_loss
    }

# Function to display summary statistics
def summarize_journal():
    total_trades = len(journal)
    total_wins = len(journal[journal['Win/Loss'] == 'Win'])
    total_losses = len(journal[journal['Win/Loss'] == 'Loss'])
    win_rate = (total_wins / total_trades) * 100 if total_trades > 0 else 0
    total_profit = journal['P/L'].sum()
    avg_rr_ratio = journal['R/R Ratio'].mean() if total_trades > 0 else 0

    summary = {
        'Total Trades': total_trades,
        'Total Wins': total_wins,
        'Total Losses': total_losses,
        'Win Rate (%)': round(win_rate, 2),
        'Total Profit/Loss': round(total_profit, 2),
        'Average R/R Ratio': round(avg_rr_ratio, 2) if avg_rr_ratio else 'N/A'
    }

    return summary

# Example Usage
add_trade('2025-01-10', 'AAPL', 150, 155, 145, 160, 100)
add_trade('2025-01-11', 'TSLA', 700, 690, 680, 750, 50)

# Display the journal and summary
print(journal)
print(summarize_journal())
Leave a Comment