You are welcome to subscribe to Python In Action: Building a Stock-based Quantitative Trading System, which will be followed by additional columns that will expand on the topics covered. This column is a supplement to the brochure content!!
preface
In the introductory section of the booklet: NumPy Simulation of random Walk Theory, we used an illustration:
As early as 1900, Louis Bachelier (1887-1946), a doctoral student in Paris, followed the ups and downs of the Paris stock market, hoping to use mathematical tools to describe the process of share price movements. In his paper, “The Theory of Speculation”, he argues that daily movements in stock prices are fundamentally unpredictable, like Brownian motion, a random walk without any rules to follow. A person who buys a stock and sells it immediately is equally likely to win or lose.
The wonderful thing about mathematics is that we can turn the unpredictability of stocks into predictability. The classic example is the simulated random walk of a drunk: suppose a drunk starts walking aimlessly under a street lamp. Each step may be forward, backward or turn. So after a certain amount of time, what is the position of the drunk?
Here we use the Numpy and Matplotlib libraries in Python to describe how to plot random walks and how to predict the stock price of random walks statistically.
Generates a random walk array
We use numpy.random.randint(low, high=None, size=None, dtype= ‘L’) to generate random numbers.
- Returns a random integer in the range of [low,high], including low and excluding high
- Parameters: low is the minimum value, high is the maximum value, size is the size of the array dimension, dtype is the data type, the default data type is Np.int
- High If not specified, the default range for generating random numbers is [0, low].
print("Np. Random. Randint: \ n {}".format(np.random.randint(1,size=5)))# returns an integer between [0,1), so only 0
"" np.random. Randint: [0 0 0 0 0] """
print("Np. Random. Randint: \ n {}".format(np.random.randint(1.5)))# return 1 random integer of [1,5] time
"" np.random. Randint: 2 """
print("Np. Random. Randint: \ n {}".format(np.random.randint(-5.5,size=(2.2))))
"" np.random. Randint: [[-5 -3] [2-3]] """
Copy the code
Visualize random walk paths
For the sake of understanding, we reduce the movement of a drunk to a one-dimensional motion, stipulating that he can only move randomly forward or backward in a straight line. The code to calculate the drunk’s random walk trajectory is shown as follows:
draws = np.random.randint(0.2, size=nsteps)
steps = np.where(draws > 0.1, -1)
walk = steps.cumsum()
Copy the code
We use the matplotlib.pyplot.plot() function to plot the simulated track graph of the drunk’s random walk of 2000 steps starting from axis 0, as shown below:
Since the drunkard’s every move is completely random, his exact final position is impossible to calculate, just as the daily movements of stock prices are unpredictable. From a statistical point of view, however, the probability distribution of the drunk’s final position can be calculated. Next, we use 1000 random walks to see the results. We encapsulate the calculation of the random walk trajectory into the function random_walk(), as shown below:
_ = [plt.plot(np.arange(2000), random_walk(nsteps=2000), c='b', alpha=0.05) for _ in np.arange(0.1000)]
Copy the code
The simulated track graph of 1000 random walks of 2000 steps of the drunk starting from axis 0 is shown as follows:
Each light blue line is a simulation. The horizontal axis is the number of steps taken, and the vertical axis is the position left from the starting point. The darker the blue is, the higher the probability that the drunk will appear in this position after he has taken the corresponding number of steps. It can be seen that the range of possible positions that the drunk may appear is increasing, but the further away from the starting point, the lower the probability.
conclusion
The true probability distribution is precisely calculated using mathematical formulas, which is the essence of quantitative trading. Edward Thorpe, the ancestor of quantitative trading, uses the idea of this random walk model to calculate the probability distribution of stock prices corresponding to warrants on the day when the contract is realized, so as to calculate whether the current price of warrants is too high or too low, and then use Kelly formula to buy and sell.
Subscribe for more quantitative trading contentSmall volumesRead!!!!! You are also welcome to follow my wechat official account to learn more about Python quantitative trading