Compare nighttime and daytime returns on the Nikkei Stock Average with python

Introduction.

One month after the corona shock that sent off investors from all over the world, there are gradually increasing voices that the first wave has bottomed out. image.png By the way, what scares investors is the "night" when they are sleeping. Even if trading on the Tokyo Stock Exchange is closed, Nikkei average futures are actively traded on the Osaka Stock Exchange (so-called OSE) night session and the Chicago Mercantile Exchange during London time and New York time. It is not uncommon for the stock price to plummet at night without knowing it ... when you wake up in the morning ** "Ohagyaa!" **. I was wondering here, but for "medium- to long-term investors" who carry over positions at night, the current market may be a bottoming out and a great bargaining point, but day traders who do not carry over positions at night. The world in which you can see may be completely different. This time, I will download the hourly data of the Nikkei Stock Average using the ** MT5 ** python API, which is a favorite of day traders around the world, and analyze the relationship between the nighttime opening return and the daytime return of the Nikkei Stock Average. I did.

Nikkei Stock Average on March 27, 2020

As mentioned at the beginning, the time spans seen by medium- to long-term investors and day traders are completely different, so the image of the “rise” of the Nikkei Stock Average on March 27, 2020 is also very different. In particular, it is not uncommon for day traders to see the day as "GU (gap up) & close-up", even though it was a "rise" for medium- to long-term investors. There is no.

Nikkei Stock Average on March 27, 2020 as seen by medium- to long-term investors

From the closing price of 18,832.21 yen on the previous day to 19,389.43 yen on the day of the day ** A large increase of nearly 600 yen! ** ** image.png

Nikkei Stock Average on March 27, 2020 as seen by day traders

From the opening price of 19,021.97 yen on the day, the end of the front session was 18,901.46 yen, a drop of nearly ▲ 100 yen, and then the back market started from 19,045.80 yen, and at 15:00 when the day trader closed the position, it increased by about +100 yen to 19,176.30 yen. ** It was an almost flat day for day traders. ** ** image.png

Let's analyze with python!

Did you know what you want to analyze from the above example? Then, immediately download the hourly data (= 24 hours x 252 business days / year x 3 years) for the past 3 years from the broker via MT5, and ** close return (= close the day before → close to the day) ** and * * Let's examine the relationship between daytime returns (= close to the day → close on the day) **.

# set time index to raw bar data
def time_set(rate):
    rate_time = rate.dropna(how='all', axis='index').reset_index()
    rate_time['time'] = pd.to_datetime(rate_time['time'], unit='s')
    rate_time = rate_time.set_index('time')
    return rate_time

# specify symbol to research
symbol = 'JP225Cash'

# get bar data from MT5
mt5.initialize()
_rate = pd.DataFrame(mt5.copy_rates_from_pos(symbol, mt5.TIMEFRAME_H1, 0, 24 * 252 * 3)).set_index('time')
mt5.shutdown()

rate = time_set(_rate)
# XM: GMT+3, JP: GMT+9, diff = 6
rate.index = [x + relativedelta(hours=6) for x in rate.index]
rate_open = rate[rate.index.hour == 9]['open']
rate_close = rate[rate.index.hour == 15]['close']
rate_close.index = [x - relativedelta(hours=6) for x in rate_close.index]
rate_TK = pd.concat([rate_open, rate_close], axis='columns')
rate_TK.columns = ['open', 'close']
rate_TK['close to open'] = (rate_TK['open'] / rate_TK['close'].shift(1) - 1) * 100
rate_TK['open to close'] = (rate_TK['close'] / rate_TK['open'] - 1) * 100
rate_TK['close to close'] = (rate_TK['close'] / rate_TK['close'].shift(1) - 1) * 100
rate_TK = rate_TK.dropna()

sns.jointplot('close to open', 'open to close', rate_TK)
plt.show()

Analysis results (whole period)

The horizontal axis is the closer return, and the vertical axis is the closing return. image.png First of all, you can roughly see three trends.

  1. No tendency is seen in the range of ± 1σ (** random walk **).
  2. If the two ranges (± 1σ to ± 2σ) in the middle surrounded by the red frame have a large positive or negative return, the daytime return also fluctuates (** volatility clustering **).
  3. When the return is slumping in the leftmost range (-2σ or less) surrounded by the red frame and yellow marker, the daytime return is greatly positive (** BOJ gun? **) If you want to make a strategy, you should aim for the BOJ gun in 3. However, there are too few samples (only twice in the past 3 years), and the reproducibility remains uncertain. However, there is something in common with the content of the previous post (predicting the Shanghai Composite Index immediately after the crash with python) (there is more or less autonomous repulsion after lowering too much)!

By the way, after analyzing so far, it seems that the audience will hear such a voice. ** "Isn't there a big difference in the meaning of a crash (surge) in the medium term between the uptrend and the downtrend?" ** ** "Isn't the upside surge in the downtrend (the situation right now) a heavy upside due to profitable sales?" ** That may be true. Now, let's do the same analysis for the two cases where the 3-month average return is positive (uptrend) and negative (downtrend).

# calc 3M average return
rate_TK['3M Mean'] = rate_TK['close to close'].rolling(63).mean()
# split 2 cases: up trend / down trend
rate_TK_3MUP = rate_TK[rate_TK['3M Mean'] >= 0]
rate_TK_3MDN = rate_TK[rate_TK['3M Mean'] < 0]
# plot
sns.jointplot('close to open', 'open to close', rate_TK_3MUP)
plt.show()
sns.jointplot('close to open', 'open to close', rate_TK_3MDN)
plt.show()

Analysis result (upward trend period limited)

The scenery has changed a lot! I mean, there is no clear tendency. In other words, it is difficult to predict daytime returns by looking at the close returns. ** There are rumors that the stock price is falling in Tokyo time (= Nikkei average) and rising in NY time (= Dow), etc. If you analyze it, you will find that it is not so easy. ** ** And, in fact, in the "Tokyo time ⇔ NY time anomaly" strategy, at least in the uptrend market, it seems difficult to make a profit by trading only on the intraday of Tokyo time without carrying over the position at night. image.png

Analysis result (downtrend period limited)

The results seen over the entire period are shown here. After all, ** Isn't the day trader who earns a lot of money going hunting without missing the fierce repulsion at the fire place in the downtrend? ** ** image.png

In the first place, how much of the rise in the market has contributed to Tokyo time?

Let's consider two investment patterns here.

  1. Buy and hold the Nikkei Stock Average for 3 years (let's call this the ** World Economic Buying Strategy **).
  2. Repeat buying and holding the Nikkei Stock Average for Tokyo time only for 3 years (let's call this ** Abenomics buying and holding strategy **). It is a rather crude way of thinking that it reflects the Japanese economy because it is the movement of Tokyo time, but I do not intend to seriously measure the effect of Japan's economic measures here, but ** day traders are limited to Tokyo time. I would like to know how much you can enjoy the benefits of the rise in the overall market price when you buy and hold the Nikkei Stock Average. If the rise in stock prices is due to the recovery of the Japanese economy, I would like you to raise it in the Abenomics buying and holding strategy in 2.
# Global v.s. Abenomics
Buy_and_Hold = ((1 + rate_TK['close to close'] / 100).cumprod() - 1) * 100
Tokyo_time = ((1 + rate_TK['open to close'] / 100).cumprod() - 1) * 100

# plot
Buy_and_Hold.plot(legend='B&H')
Tokyo_time.plot(legend='TK time B&H')
(Tokyo_time - Buy_and_Hold).plot(kind='area', stacked=False)
plt.title('cumulative return')
plt.show()

Here is a plot of the cumulative return of each strategy calculated with the above code. image.png The blue close to close is the cumulative return of the global economic buying strategy, the orange open to close is the cumulative return of the Abenomics buying strategy, and the green area is the difference between the two strategic returns (Abenomics return-world economic return). If you look at it this way, you'll see why the Abenomics buying-and-hold strategy isn't something you've thrown away, or ** isn't it much better to buy it only during the day than to carry it over at night? Isn't it good to avoid risk? ** ** Also, for the past three years, most of the rise in the Nikkei Stock Average has been brought about at night, but in the last year the difference has gradually narrowed. ** In the corona shock, the Abenomics buying strategy is finally the global economy. You can see that the buying strategy has been greatly outperformed **! Mr. Abe, I'll do it (?).

Summary

Recommended Posts

Compare nighttime and daytime returns on the Nikkei Stock Average with python
Plot the Nikkei Stock Average with pandas
A memo with Python2.7 and Python3 on CentOS
Download files on the web with Python
Put Cabocha 0.68 on Windows and try to analyze the dependency with Python
Install OpenCV 4.0 and Python 3.7 on Windows 10 with Anaconda
Compare the speed of Python append and map
Compare xml parsing speeds with Python and Go
Introduction to Python with Atom (on the way)
Archive and compress the entire directory with python
Get the stock price of a Japanese company with Python and make a graph
[CGI] Run the Python program on the server with Vue.js + axios and get the output data
I tried to compare the processing speed with dplyr of R and pandas of Python
Notes on HDR and RAW image processing with Python
Install selenium on Mac and try it with python
Visualize the range of interpolation and extrapolation with python
Automatic follow on Twitter with python and selenium! (RPA)
Get comments on youtube Live with [python] and [pytchat]!
Information for controlling the motor with Python on RaspberryPi
Install the latest stable Python with pyenv (both 2 and 3)
Email hipchat with postfix, fluentd and python on Azure
Automate Chrome with Python and Selenium on your Chromebook
Install django on python + anaconda and start the server
Get the matched string with a regular expression and reuse it when replacing on Python3
The first artificial intelligence. Tensorflow on CentOS7.0. Built separately for python3.5 series and python2.7 series with virtualenv.
How to scrape stock prices of individual stocks from the Nikkei newspaper website with Python
Put Ubuntu in Raspi, put Docker on it, and control GPIO with python from the container
Try hitting the Twitter API quickly and easily with Python
Compare HTTP GET / POST with cURL (command) and Python (programming)
I evaluated the strategy of stock system trading with Python.
IP spoof using tor on macOS and check with python
Test Python with Miniconda on OS X and Linux with travis-ci
Access the host SQL Server with python27 / pyodbc on the container
Solve the spiral book (algorithm and data structure) with python!
Hash with the standard library hashlib and compare login passwords
Notes on deploying pyenv with Homebrew and managing Python versions
Save images on the web to Drive with Python (Colab)
Play with the password mechanism of GitHub Webhook and Python
[Stock price analysis] Learning pandas on the Nikkei average (005: Grouping by year / month-confirmation of statistical information)
Stock price plummeted with "new corona"? I tried to get the Nikkei Stock Average by web scraping
Programming with Python and Tkinter
Encryption and decryption with Python
Python and hardware-Using RS232C with Python-
Python on Ruby and angry Ruby on Python
Compare the Python array-like guys
scraping the Nikkei 225 with playwright-python
Call the API with python3.
python with pyenv and venv
Get stock price with Python
Works with Python and R
Life game with Python [I made it] (on the terminal & Tkinter)
I compared the speed of Hash with Topaz, Ruby and Python
How is the progress? Let's get on with the boom ?? in Python
Control the motor with a motor driver using python on Raspberry Pi 3!
Get started with the Python framework Django on Mac OS X
Create AtCoder Contest appointments on Google Calendar with Python and GAS
Build a 64-bit Python 2.7 environment with TDM-GCC and MinGW-w64 on Windows 7
Error and solution when installing python3 with homebrew on mac (catalina 10.15)
Linking Python and Arduino to display IME On / Off with LED
Quickly install OpenCV 2.4 (+ python) on OS X and try the sample
Create REST API that returns the current time with Python3 + Falcon