7.7 KiB
This code simulates a trading strategy to assess the risk of ruin. It defines functions to simulate individual trades, an entire trading strategy, and calculate the risk of ruin over multiple simulations. The parameters include initial capital, win probability, average win/loss amounts, and the number of trades. The output is a plot showing how the probability of a winning trade affects the risk of ruin. This can help traders understand the robustness of their trading strategy.
import numpy as np import matplotlib.pyplot as plt
Simulate a single trade with given win probability and average win/loss amounts
def simulate_trade(win_prob, avg_win, avg_loss): """ Simulate a single trade with given win probability and average win/loss amounts. Parameters ---------- win_prob : float Probability of a winning trade avg_win : float Average amount won per winning trade avg_loss : float Average amount lost per losing trade Returns ------- float The result of the trade, positive for win and negative for loss """ # Determine the trade outcome based on win probability and return the result if np.random.rand() < win_prob: return avg_win else: return -avg_loss
Simulate the entire trading strategy over a given number of trades
def simulate_trading_strategy(initial_capital, trades, win_prob, avg_win, avg_loss): """ Simulate the entire trading strategy over a given number of trades. Parameters ---------- initial_capital : float Starting capital for the trading strategy trades : int Number of trades to simulate win_prob : float Probability of a winning trade avg_win : float Average amount won per winning trade avg_loss : float Average amount lost per losing trade Returns ------- list Capital history as a list of capital values after each trade """ # Initialize capital and history list capital = initial_capital capital_history = [capital] # Simulate each trade and update capital for _ in range(trades): capital += simulate_trade(win_prob, avg_win, avg_loss) capital_history.append(capital) return capital_history
Calculate the risk of ruin over a number of trading simulations
def calculate_risk_of_ruin(initial_capital, trades, win_prob, avg_win, avg_loss, simulations=100): """ Calculate the risk of ruin over a number of trading simulations. Parameters ---------- initial_capital : float Starting capital for the trading strategy trades : int Number of trades to simulate win_prob : float Probability of a winning trade avg_win : float Average amount won per winning trade avg_loss : float Average amount lost per losing trade simulations : int, optional Number of simulations to run (default is 100) Returns ------- float Proportion of simulations where the capital went to zero or below """ # Initialize ruin count ruin_count = 0 # Run the specified number of simulations for _ in range(simulations): capital_history = simulate_trading_strategy(initial_capital, trades, win_prob, avg_win, avg_loss) if min(capital_history) <= 0: ruin_count += 1 return ruin_count / simulations
Define initial parameters for the trading simulation
initial_capital = 10000 average_win = 110 average_loss = 100 trades = 1000
Calculate risk of ruin for varying win probabilities
risk_of_ruins = [] steps = range(30, 60) for step in steps: win_probability = step / 100 risk_of_ruin = calculate_risk_of_ruin(initial_capital, trades, win_probability, average_win, average_loss) risk_of_ruins.append(risk_of_ruin)
Plot the risk of ruin versus probability of a winning trade
plt.figure(figsize=(10, 6)) plt.plot(steps, risk_of_ruins, label='Risk of ruin') plt.xlabel('Probability of a winning trade') plt.ylabel('Risk of ruin') plt.title("Probability of ruin versus probability of a winning trade") plt.grid(True) plt.show()
PyQuant News is where finance practitioners level up with Python for quant finance, algorithmic trading, and market data analysis. Looking to get started? Check out the fastest growing, top-selling course to get started with Python for quant finance. For educational purposes. Not investment advise. Use at your own risk.
