System trading starting with Python3: long-term investment

The content of this article is a brief summary of the content of "system trading (pannrolling) starting with Python 3".

On November 3rd, "US Stock Online Study Group 3rd (Repeat 9/21)", "US Stock Online Study Group 4th (Repeat 9/21)" Repeat 9/21) ”.

Basics of long-term investment

In this article, you will learn the most basic data analysis methods with long-term investment in mind.

--Analyzing the characteristics of price movements with data available over the long term --Understanding the market size --Fake trends and random walks --To avoid bankruptcy

Analyzing the characteristics of price movements with long-term data

Do you think that it is better to use the latest data for proper analysis? I sometimes hear that the stock market is a living thing and its nature changes, so it should be analyzed with short data. But that's a mistake. With this kind of analysis method, the analysis result that you are expecting will come out immediately. The data used for the data has never been so long. It's never too long. Sometimes it is necessary to create pseudo data using the Monte Carlo method.

Understanding the market size

The basis of long-term investment is to know the other party well. It starts with comparing with others. Make sure you understand the exact size of the market when making an investment.



Did you get a sense of scale? At least the US financial markets are bigger than Japan. And while the Japanese government bond market is larger than the stock market, it is not the case in the United States. Given that GDP per capita is the same in both countries, which stock market is more likely to rise? I think that various images can be created with just this analysis.

Fake trends and random walks

Many economic time series and stock prices follow a random walk. The mean and variance of the time series that follow the random walk will change over time. In a random walk, the mean changes stochastically over time, and the variance increases in proportion to the square root over time. Therefore, the future of the stock price is unpredictable. The fact that you cannot predict is meaningless even if you plan the timing of buying and selling. You don't know where to go in the future, as the random walk is likened to a drunken staggered foot. And the width increases with the passage of time. In other words, it may rise significantly, but it will also fall.

Knowing whether stock data follows a random walk is important in two ways:

  1. Is the trend that occurred a fake trend, or is the trend happening?
  2. When the two time series show a correlation, are they a fake correlation or do they happen to occur? That is the point.

Let's create a random walk in Python. Next, the figure shows the downward movement made by random numbers. It is falling in almost one direction.

%matplotlib inline 
import matplotlib.pyplot as plt
import as web #Data download library
import numpy as np
import pandas as pd
def generate(y0,rho,sigma,nsample):
    e = np.random.normal(size=nsample)
    for i,u in enumerate(e[1:]):


Next, let's look at the actual stock price. The Dickey-Fuller test is a way to determine if a stock price is a random walk. For this test

--Random walk without drift (rw) --Random walk with drift (drw) --Random Shok with Time Trend Drift (tdrw) --Random walk with time trend drift with acceleration (qtdrw)

there is. If these results are less than or equal to 0.1, it is more likely that you are not a random walk.

Let's actually take a look at the minimum variance portfolio (usmv), NASDAQ 100 (qqq), S & P500 (spy), and Dow Jones Industrial Average (dia).

from statsmodels.tsa.stattools import adfuller


ror = web.DataReader("DGS1","fred","1980/1/4")#Download US Interest Rates from the US Federal Reserve Home Page
ror = ror/250/100#jpy

def LongtermInvest(PORT,ror,start):
    for asset in PORT:
        tsd = web.DataReader(asset,"yahoo",start).dropna()#Stock price download from yahoo finance us
        tmp=pd.concat([dtsd.loc[:,'Adj Close'],ror],axis=1).dropna()
        print(asset,adfuller((ts),regression='nc')[1:3],# ADF test
        if i==5:
    if i!=1:
usmv (0.9545542347136421, 23) (0.8638006294387173, 23) (0.0012513095084584341, 11) (0.005811135279764244, 11)
qqq (0.9917530792757916, 9) (0.9725398688611976, 9) (0.13498405344227182, 9) (0.09924300082908494, 9)
spy (0.8831775184401023, 9) (0.8368954755934965, 9) (0.00474390277885088, 9) (0.018407642576579095, 9)
dia (0.8438357944084672, 10) (0.7816330208711864, 10) (0.05496092342671481, 9) (0.08039004988307125, 9)


The results are, in order, a random walk without drift, a random walk with drift, a random show with time trend drift, and a random walk with time trend drift with acceleration. The results suggest that there may be a time trend (tdrw, qtdrw) in all cases.

To avoid bankruptcy

Which of the four basic equity trading strategies has the lowest probability of bankruptcy?

--Rebalancing: A strategy to restore the investment ratio if it deviates from the target value due to price movements. --Loss cut: When a loss occurs, close the position. --Option: Pay a premium like insurance to avoid risk. --Buy and Hold: Continue to hold a buy position.

Of these, the only strategies with a low probability of bankruptcy are rebalancing and buying.

--Options will go bankrupt if profits commensurate with the option fee are not obtained. --The loss cut will go bankrupt if the loss is larger than the profit.

Let's simulate using Apple stock and Intel stock. The following program is a simulation of the case where you do not move your position after first investing in half. In this case, the ratio of the value of the investment will be higher for the stock with the higher stock price.

aapl=web.DataReader("AAPL", "yahoo","1981/12/31","2020/12/31")['Adj Close']
intc=web.DataReader("INTC", "yahoo","1981/12/31","2020/12/31")['Adj Close']

aapl=aapl/aapl.iloc[0]#Indexing of stock prices
lnport.plot(label='no rebalance')
plt.legend(loc='upper left')


The following simulation is a program that rebalances daily and always adjusts the value of the investment to a ratio of 50% 50%.

def portfolio_rebalance(tsd1,tsd2):
    for i in range(1,len(port)):
        p1=port['p1'].iloc[i]#Apple stock price today
        p2=port['p2'].iloc[i]#Intel stock price today
        p1_0=port['p1'].iloc[i-1]#Apple stock price the day before
        p2_0=port['p2'].iloc[i-1]#Intel stock price the day before
        a1_0=port['a1'].iloc[i-1]#Number of shares held by Apple the day before
        a2_0=port['a2'].iloc[i-1]#Number of Intel holdings the day before
        v_0=port['v'].iloc[i-1]#Value of the previous day's rebalancing portfolio
        #v=a1_0*(p1-p1_0)+a2_0*(p2-p2_0)+v_0#Value of today's rebalancing portfolio
        v=a1_0*p1+a2_0*p2#Value of today's rebalancing portfolio
        port.iloc[i,4]=v#Rebalance Portfolio Value Update
        a1=float(v/2/p1)#Adjusted Apple Shares
        a2=float(v/2/p2)#Adjusted Intel Shares
        port.iloc[i,2]=a1#Apple Stock Update
        port.iloc[i,3]=a2#Intel share count update
    port['v2']=0.5*port.p1+0.5*port.p2#Value of portfolio without rebalancing
    return port
lnport.v.plot(label="port daily rebalance",linewidth=1.0)
plt.legend(loc="upper left")


I think you have an image of the investment efficiency of rebalancing.

Now, can this avoid bankruptcy? Even if you choose two stocks, the probability that two companies will go bankrupt is not zero. You can avoid that risk by investing in a stock index. This is why we encourage beginners to invest in stock indexes.

Is it possible to avoid bankruptcy with the dollar cost averaging method (funded investment)? Bankruptcy is avoided. Its investment efficiency is close to that of a "buy-and-hold" strategy. But what if the assets you invest in continue to fall for a long time? Don't you think it will put a lot of pressure on your heart? Consider the Nikkei average after the burst of the bubble, the gold market in the 1990s. Can you continue to invest in that asset if you lower it for more than 10 years in a row? If you can do that, the dollar cost averaging method is one of your strategies. However, I don't think the god of victory will smile at the investment method of this application, which is generally unwilling.

Results of analysis of this article and other than this article

--If you buy a stock, don't sell it until you make a profit. --No loss cut. ――You can sell it whenever you make a profit, but the longer you hold it, the higher the profit margin.


--Basically, invest in a diversified stock index.

For those unfamiliar with Python

Install Jupyter notebook I downloaded the stock price from Yahoo Finance US

Recommended Posts

System trading starting with Python3: long-term investment
System trading starting with Python 3: Investment and risk
"System trade starting with Python3" reading memo
Python starting with Windows 7
GRPC starting with Python
Reinforcement learning starting with Python
Presentation Support System with Python3
Python starting with Hello world!
I evaluated the strategy of stock system trading with Python.
System trade starting with Python3: Get the latest program code
Make a recommender system with python
Data analysis starting with python (data visualization 1)
Data analysis starting with python (data visualization 2)
FX automatic trading system made with python and genetic algorithm Part 1
Business efficiency starting from scratch with Python
Data analysis starting with python (data preprocessing-machine learning)
"First Elasticsearch" starting with a python client
Let's develop an investment algorithm with Python 1
FizzBuzz with Python3
Scraping with Python
Machine learning starting with Python Personal memorandum Part2
Investment quest: Make a system trade with pyhton (2)
Statistics with python
Scraping with Python
Python with Go
Machine learning starting with Python Personal memorandum Part1
Investment quest: Make a system trade with pyhton (1)
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
System trade starting with Python3: Bio-health stocks to look at in the new Corona
[Introduction to system trading] I drew a Stochastic Oscillator with python and played with it ♬
Discord Bot with recording function starting with Python: (3) Cooperation with Database
Discord Bot with recording function starting with Python: (1) Introduction
I tried using TradeWave (BitCoin system trading in Python)
Introduction to Mathematics Starting with Python Study Memo Vol.1
Execute python3 system with PHP exec () on AWS EC2
System Trade Beginning with Python 3: Hedge Funds and CTA
Articles that enable system development with Django (Python) _Introduction
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Try scraping with Python.
Learning Python with ChemTHEATER 03
Sequential search with Python
"Object-oriented" learning with python
Run Python with VBA