Last active
March 14, 2024 21:27
-
-
Save Karlheinzniebuhr/967ed03b30de0937d25a2a2ce3d039b0 to your computer and use it in GitHub Desktop.
Simulation of individual vs Shared portfolio
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import matplotlib.pyplot as plt | |
# Adjusted simulation settings | |
trading_days = 30 # Half a year | |
individual_portfolios = [1000, 1000, 1000, 1000] | |
shared_portfolio = 1000 | |
win_rate = 0.75 # 50% win rate | |
# New risk-reward settings | |
loss_amount = -0.0014 # -0.14% | |
win_amount = 0.0113 # 1.13% | |
individual_returns = [[portfolio] for portfolio in individual_portfolios] | |
shared_returns = [shared_portfolio] | |
for _ in range(trading_days): | |
for i in range(4): | |
outcome = random.choices([win_amount, loss_amount], weights=[win_rate, 1 - win_rate])[0] | |
individual_portfolios[i] *= (1 + outcome) | |
individual_returns[i].append(individual_portfolios[i]) | |
for _ in range(4): | |
outcome = random.choices([win_amount, loss_amount], weights=[win_rate, 1 - win_rate])[0] | |
shared_portfolio *= (1 + outcome) | |
shared_returns.append(shared_portfolio) | |
# Preparing the RR and win rate details for the legend | |
info_text = f"RR: {win_amount*100}% / {round(loss_amount*100, 4)}%, Win Rate: {win_rate*100}%" | |
# Plotting and adding the RR and win rate details to the legend | |
plt.figure(figsize=(14, 8)) | |
for i, returns in enumerate(individual_returns): | |
plt.plot(returns, linestyle='--', label=f'Bot {i+1}') | |
plt.plot(shared_returns, color='black', linewidth=2, label='Shared Portfolio') | |
# Including the info text in the legend by adding it to an invisible line | |
plt.plot([], [], ' ', label=info_text) | |
plt.xlabel('Trading Days') | |
plt.ylabel('Portfolio Value ($)') | |
plt.title('Portfolio Growth Over Half a Year with Adjusted Risk-Reward') | |
plt.legend(loc='upper left') | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment