I evaluated the strategy of stock system trading with Python.

System trading validates strategies based on historical stock price data.

For example, the following verification results are the verification results of a certain order. 【分析結果】_名前なし__7順張り250日20日_20191118_pdf(2___12ページ).jpg

However, it is only an evaluation in one scenario.

Therefore, I used Python to evaluate the results of multiple simulations of the winning rate, profit rate, profit / loss rate, and amount of funds of the completed strategy.

#Introducing the library
import pandas as pd
import matplotlib.pyplot as plt
import random

#Function for evaluation
#asset initial capital
#What percentage of bet capital to invest
#probability Win rate
#win average profit margin
#loss average loss rate
#transaction number of transactions
def sim(asset, bet, probability, win, loss, transaction):
  result = []
  for i in range(transaction):

#Win if the random number is less than the winning percentage
    if random.random() < probability:

#Assets increase by the average profit margin multiplied by the invested assets.
#This time it is compound interest, but if you change this area, you can make it simple interest.
      asset = asset + (asset * bet * win)

#If you lose, the average loss rate multiplied by the invested assets will decrease.
    else:
      asset = asset - (asset * bet * loss)

#Store transaction results in a list.
    result.append(asset)    

  return result

#Function for evaluation 2
def mon(asset, bet, probability, win, loss, transaction=100, test=1):
  df = pd.DataFrame()

#Call the evaluation function for the number of trials and store it in the data frame.
  for i in range(test):
    df[i] = sim(asset, bet, probability, win, loss, transaction)

#graph display
#Settings for auxiliary lines
  xmin = -3
  xmax = transaction

#Bluff size setting
  plt.figure(figsize=(25, 15), dpi=50)
  plt.plot(df)

#Auxiliary line setting
  plt.hlines([asset], xmin, xmax, "blue")
  plt.show()

#Display of statistical elements
  print(df.iloc[transaction - 1].describe())

#Amount of funds 300, risk 20%, Win rate 61.54% average profit margin 5.83% average loss rate 4.63% Let's verify with 250 trades and 300 trials.
mon(300, 0.2, 0.6154, 0.0583, 0.0463, 250, 300)

Untitled11_ipynb_-_Colaboratory.jpg

If you look at this, you can see that there are cases where profits do not rise up to about 50 transactions.

In another strategy,

Untitled11_ipynb_-_Colaboratory.jpg

was.

This is more stable and the assets will increase.

This time, we are extracting values such as winning percentage and average profit margin from the completed strategy, so I don't know if it is statistically valid.

What do you think, guys?

Recommended Posts

I evaluated the strategy of stock system trading with Python.
I tried to find the entropy of the image with python
I wrote the basic grammar of Python with Jupyter Lab
I liked the tweet with python. ..
I compared the speed of Hash with Topaz, Ruby and Python
I tried scraping the ranking of Qiita Advent Calendar with Python
I want to output the beginning of the next month with Python
I tried to improve the efficiency of daily work with Python
System trading starting with Python3: long-term investment
Check the existence of the file with python
I didn't know the basics of Python
The Python project template I think of.
I replaced the numerical calculation of Python with Rust and compared the speed
I tried to get the authentication code of Qiita API with Python.
I tried to streamline the standard role of new employees with Python
I tried to get the movie information of TMDb API with Python
I measured the speed of list comprehension, for and while with python2.7.
I tried "smoothing" the image with Python + OpenCV
I tried hundreds of millions of SQLite with python
Prepare the execution environment of Python3 with Docker
2016 The University of Tokyo Mathematics Solved with Python
I tried "differentiating" the image with Python + OpenCV
[Note] Export the html of the site with python.
Calculate the total number of combinations with python
Check the date of the flag duty with Python
Try the free version of Progate [Python I]
I tried "binarizing" the image with Python + OpenCV
I touched some of the new features of Python 3.8 ①
Convert the character code of the file with Python3
[Python] Determine the type of iris with SVM
System trading starting with Python 3: Investment and risk
I installed Pygame with Python 3.5.1 in the environment of pyenv on OS X
I tried to easily visualize the tweets of JAWS DAYS 2017 with Python + ELK
Get the stock price of a Japanese company with Python and make a graph
I tried to automatically send the literature of the new coronavirus to LINE with Python
the zen of Python
[Trainer's Recipe] I touched the flame of the Python framework.
I tried using TradeWave (BitCoin system trading in Python)
I tried to touch the CSV file with Python
Destroy the intermediate expression of the sweep method with Python
[OpenCV / Python] I tried image analysis of cells with OpenCV
I tried to solve the soma cube with python
Calculate the regression coefficient of simple regression analysis with python
I checked out the versions of Blender and Python
I want to inherit to the back with python dataclass
I measured the performance of 1 million documents with mongoDB
A memo that I touched the Datastore with python
Summary of the basic flow of machine learning with Python
I tried to solve the problem with Python Vol.1
Get the operation status of JR West with Python
I investigated the reinforcement learning algorithm of algorithmic trading
Extract the band information of raster data with python
I moved the automatic summarization API "summpy" with python3.
I tried "morphology conversion" of images with Python + OpenCV
I tried hitting the API with echonest's python client
I tried to summarize the string operations of Python
I tried to put out the frequent word ranking of LINE talk with Python
I tried to automate the article update of Livedoor blog with Python and selenium.
[Introduction to system trading] I drew a Stochastic Oscillator with python and played with it ♬
[Python & SQLite] I analyzed the expected value of a race with horses with a win of 1x ②
I tried to compare the processing speed with dplyr of R and pandas of Python