0% found this document useful (0 votes)
23 views2 pages

Projecter Py

This Python function simulates stock trades over a number of iterations by randomly generating wins and losses, while limiting consecutive wins or losses. It tracks the account balance and calculates the percentage return and win rate, printing a summary of results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views2 pages

Projecter Py

This Python function simulates stock trades over a number of iterations by randomly generating wins and losses, while limiting consecutive wins or losses. It tracks the account balance and calculates the percentage return and win rate, printing a summary of results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import random

def simulate_trades(starting_amount, num_trades, risk_per_trade,


risk_reward_ratio):
account_balance = starting_amount
win_count = 0
loss_count = 0
max_consecutive_wins_losses = 2 # Limit on consecutive wins or losses

consecutive_wins = 0
consecutive_losses = 0

for trade_num in range(1, num_trades + 1):


if consecutive_wins >= max_consecutive_wins_losses:
outcome = -1 # Force a loss if consecutive wins reach the limit
consecutive_wins = 0
consecutive_losses += 1
elif consecutive_losses >= max_consecutive_wins_losses:
outcome = 1 # Force a win if consecutive losses reach the limit
consecutive_losses = 0
consecutive_wins += 1
else:
outcome = random.choice([1, -1]) # Randomly choose win or loss

if outcome == 1:
# Win
win_count += 1
consecutive_wins += 1
amount_to_risk = account_balance * risk_per_trade
profit = amount_to_risk * risk_reward_ratio
account_balance += profit
print(f"Trade {trade_num}: Win, Amount Risked: ${amount_to_risk:,.2f},
Profit: ${profit:,.2f}, Account Balance: ${account_balance:,.2f}")
else:
# Loss
loss_count += 1
consecutive_losses += 1
amount_to_risk = account_balance * risk_per_trade
loss = amount_to_risk
account_balance -= loss
print(f"Trade {trade_num}: Loss, Amount Risked: ${amount_to_risk:,.2f},
Loss: ${loss:,.2f}, Account Balance: ${account_balance:,.2f}")

# Calculate percentage return and win rate


percentage_return = ((account_balance - starting_amount) / starting_amount) *
100
win_rate_percentage = (win_count / num_trades) * 100

# Print summary
print("\nSimulation Summary:")
print(f"Total Trades: {num_trades}")
print(f"Total Wins: {win_count}")
print(f"Total Losses: {loss_count}")
print(f"Final Account Balance: ${account_balance:,.2f}")
print(f"Percentage Return: {percentage_return:.2f}%")
print(f"Win Rate: {win_rate_percentage:.2f}%")

# Parameters
starting_amount = 1000 # Starting balance: $1000
num_trades = 60 # Changed to 60 trades
risk_per_trade = 0.20 # Risk per trade: 20% of account balance
risk_reward_ratio = 2 # Risk to reward ratio: 1 to 2

# Simulate trades
simulate_trades(starting_amount, num_trades, risk_per_trade, risk_reward_ratio)

You might also like