How to simulate trades in Python. We always see, so called traders claim... | by Atilla Yurtseven | Towards Data Science

We always see, so called traders claim that they have a strategy with more than 70% win rate and their risk to reward ratio is above 1:2 (risk 1%, win 2%). Do you think it’s possible? Let’s find out!
I have a 70% win-rate with 1:2 risk-to-reward ratio
ROFL! Are you sure you didn’t miss the math class in high school (maybe primary?!)? Just keep in mind that, in 2019, hedge funds returns are average 6%. Here is the link of the source.
Now let’s start talking Python.
We only need two libraries
import numpy as npimport matplotlib.pyplot as plt
Now we need to write a function to simulate trades. Not actual ones of course.
def simulate(account_size, total_trades, risk_per_trade, win_rate, risk_reward):    account = account_size    accounts = [account]    profits = []    loss = []    wins = []    total_win = 0    max_con_l = 0    max_con_w = 0    con_l = 0    con_w = 0    pre = 0    rnd = list(np.round(np.random.uniform(1, 101, total_trades), 2))    for i in range(len(rnd)):        r = rnd[i]        win = r <= win_rate        risk = -np.round(account * risk_per_trade / 100, 2)        profit_per_trade = abs(risk) * risk_reward        profit = profit_per_trade if win else risk        profits.append(profit)        account += profit        accounts.append(account)        if profit > 0:            total_win += 1            wins.append(profit)            con_l = 0            if pre == 1:                con_w += 1                if con_w > max_con_w:                    max_con_w = con_w            pre = 1        else: # 0 is also a loss (spread + commissions)            loss.append(abs(profit))            con_w = 0            if pre == -1:                con_l += 1                if con_l > max_con_l:                    max_con_l = con_l            pre = -1    avg_win = np.mean(wins)    avg_loss = np.mean(loss)    max_win = np.max(wins)    max_loss = np.max(loss)    win_r = np.round(total_win / total_trades * 100, 2)    rrr = np.round(avg_win / avg_loss, 2)    profit_factor = np.round(np.sum(wins) / np.sum(loss), 2)    net_profits = np.cumsum(profits)    gain = np.round(accounts[-1] - account_size, 2)    growth_rate = np.round((accounts[-1] - account_size) / account_size * 100, 2)    print("--- Trading Results ---\n")    print("Total trades       : {}".format(total_trades))    print("Wins               : {} / {}%".format(total_win, win_r))    print("Average Win        : {}".format(np.round(avg_win, 2)))    print("Average Loss       : {}".format(np.round(avg_loss, 2)))    print("Max Win            : {}".format(np.round(max_win, 2)))    print("Max Loss           : {}".format(np.round(max_loss, 2)))    print("Max Cons. Wins     : {}".format(max_con_w))    print("Max Cons. Loss     : {}".format(max_con_l))    print("Risk Reward Ratio  : {}".format(rrr))    print("Profit Factor      : {}".format(profit_factor))    print("Risk per trade     : {}%".format(risk_per_trade))    print("---")    print("Initial Account    : {}".format(account_size))    print("Profit             : {} / {}%".format(gain, growth_rate))    print("Final Account      : {}".format(np.round(account, 2)))    print()    print("Results are compounded. Spread and commissions are not calculated.")    fig, ax = plt.subplots(2, 1, figsize=(16, 10))    ax[0].plot(net_profits)    ax[1].plot(accounts)    ax[1].axhline(account_size, color="#000000", ls="-.", linewidth=0.5)    ax[0].set_title("Equirty Curve")    ax[1].set_title("Account Growth")    plt.show()
Let’s define this guy’s claims
account_size = 10000total_trades = 200 #trades per year maybe?risk_per_trade = 2 #risk 2% of your account per tradewin_rate = 70 # in percentagerisk_reward = 2 # risk 1% profit 2%
OMG! This is going to be crazy. Let’s run if you are ready? :)
simulate(account_size, total_trades, risk_per_trade, win_rate, risk_reward)
And Oscar goes to this guy!!!!
He can make 8025% per year and still tries to sell you a product?! Come on!!!!
Ok; now let’s try to be more realistic. I am going to use the following params:
account_size = 10000total_trades = 200 #trades per year maybe?risk_per_trade = 2 #risk 2% of your account per tradewin_rate = 52risk_reward = 1.4
And the result
Even with 52% winrate with 1.4 risk to reward ratio makes 193% per year! However, this is not that easy.
Please just stop believing in all these so called traders out there. Most of social media traders are nothing but cons. They are only trying to sell you a dream and their so called holy-grail products. There’s no holy-grail and you don’t need a 70% win-rate.
Disclaimer
I’m not a professional financial advisor. This article and codes, shared for educational purposes only and not financial advice. You are responsible your own losses or wins.
The whole code of this article can be found on this repository:
github.com
Ah also; remember to follow me on the following social channels:
MediumTwitterYouTube!TradingView
Until next time; stay safe, trade safe!!!
Image from: https://hackernoon.com/algo-trading-for-dummies-implementing-an-actual-trading-strategy-part-4-acec5543052b
Atilla Yurtseven
Note from Towards Data Science’s editors: While we allow independent authors to publish articles in accordance with our rules and guidelines, we do not endorse each author’s contribution. You should not rely on an author’s works without seeking professional advice. See our Reader Terms for details.