[PYTHON] Is this a system trade?

Synopsis up to the last time

I try to complete the story by myself in this article, but if you read the following my article, the flow of the story will become clearer.

Introduction

This time, I will try to predict whether the price range (price-close price) of the next day of NIKKEI 225 is positive or negative by a method different from machine learning. The point to pay attention to is that when you look at the chart with candlesticks, the positive line continues for a while and the negative line continues quite often. "Three days in a row" or "four days in a row".

If today is a positive line and the probability of becoming a positive line tomorrow is different from the probability of becoming a negative line, if the larger probability is used as the predicted value, the possibility of hitting the prediction will increase.

Yin Yang Bigram

Immediately, let's check the ratio of NIKKEI 225 to continuously add positive or negative lines.

Drop the stock price data of NIKKEI225 with csv,

Date,Open,High,Low,Close,Volume
2017-10-31,21896.38,22020.38,21840.07,22011.61,1055801728.0
2017-10-30,22047.95,22086.88,21921.24,22011.67,1397960064.0
2017-10-27,21903.27,22016.5,21815.72,22008.45,1241389952.0
2017-10-26,21698.95,21793.62,21688.56,21739.78,851784320.0
2017-10-25,21900.13,21921.36,21648.35,21707.62,1258339712.0
    ...

Use the code below to find out the percentage of yang, yin, yang, yang, and yin.

import pandas as pd
import numpy as np

nikkei_225 = pd.read_csv('NIKKEI225.csv').set_index('Date').sort_index()
open_list = nikkei_225['Open'].tolist()
close_list = nikkei_225['Close'].tolist()
diff_list = np.array(close_list) - np.array(open_list)

posiposi = 0
posinega = 0
negaposi = 0
neganega = 0
for i in range(len(diff_list)-1):
    if diff_list[i]>=0 and diff_list[i+1]>=0:
        posiposi = posiposi + 1
    elif diff_list[i]>=0 and diff_list[i+1]<0:
        posinega = posinega + 1
    elif diff_list[i]<0 and diff_list[i+1]>=0:
        negaposi = negaposi + 1
    else:
        neganega = neganega + 1
print(posiposi, posinega, negaposi, neganega)        

2014-10-30_2017-10-31 With 735 stock prices Yang Yang = 170 cases Yin = 195 Yin Yang = 195 cases Shade = 175 cases Met.

Than this,

The result was that. I thought there were a lot of yang and yin, but I removed it. But it's asymmetric, so I'll try it.

Trading scheme

Repeat this. In short, if it's a positive line today, it's a negative line tomorrow, and if it's a negative line today, it's a positive line tomorrow. Is this a system trade?

Profit and loss simulation (back test)

From 2017-11-01 to 2020-07-28, I tried to simulate the case of repeating buying and selling by 10,000,000 yen each time with the above trading scheme. The trading fee was set at 2026 yen for a round trip. The code is below.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

nikkei225 = pd.read_csv('NIKKEI225.csv').set_index('Date').sort_index()
n1357 = pd.read_csv('1357.csv').set_index('Date').sort_index()
n1358 = pd.read_csv('1358.csv').set_index('Date').sort_index()

nikkei225_open_list = nikkei225['Open'].tolist()
nikkei225_close_list = nikkei225['Close'].tolist()
nikkei225_diff_list = np.array(nikkei225_close_list) - np.array(nikkei225_open_list)

n1357_open_list = n1357['Open'].tolist()
n1357_close_list = n1357['Close'].tolist()
n1357_diff_list = np.array(n1357_close_list) - np.array(n1357_open_list)

n1358_open_list = n1358['Open'].tolist()
n1358_close_list = n1358['Close'].tolist()
n1358_diff_list = np.array(n1358_close_list) - np.array(n1358_open_list)

x = []
y = []
total_gain = 0
for i in range(len(nikkei225_diff_list)-1):
 if nikkei225_diff_list [i]> = 0: # Today's Nikkei225 is a positive line
 Buy # 1357 for 10 million yen and sell it at the close
        n = int(10000000/n1357_close_list[i])
        daily_gain = n1357_diff_list[i+1]*n - 2026
 else: # Today's Nikkei 225 is hidden
 Buy # 1358 for 10 million yen and sell it at the close
        n = int(10000000/n1358_close_list[i])
        daily_gain = n1358_diff_list[i+1]*n - 2026
    total_gain += daily_gain
    x.append(i)
    y.append(total_gain)

# plot
plt.plot(x, y, label="total_gain")
plt.show()
print('total_gain=', total_gain)

Profit and loss simulation results

Isn't this good!

result.png

total_gain= 5633247

In the 33 months from 2017-11-01 to 2020-07-28, the original 10,000,000 yen became 15,633,247 yen, so the annual rate is calculated to be 20.48%. Is it true? I would like to have a follow-up exam for everyone.

The cumulative profit and loss graph shows a gradual increase and decrease, but it is rising in the long run, and its shape is not bad.

Consideration of results

So, I'm going to separate the study period and the evaluation period. Since it is not a great learning, it is not necessary to separate the learning period and the evaluation period separately, so let's plot the cumulative profit and loss graph for the entire period from 2014-10-30 to 2020-07-28.

result2.png

total_gain= 969052

Cumulative profit / loss is positive for the entire period, but the graph shows that the loss expanded in the first half of 700 days and recovered in the second half of 700 days. How do you see this? The latter 700 days should be seen as a mere coincidence.

Summary

Continued (maybe)

Recommended Posts

Is this a system trade?
Is this string a decimal?
What is a system call
This is a webiopi question
About February 02, 2020 * This is a Python article.
Investment quest: Make a system trade with pyhton (2)
Investment quest: Make a system trade with pyhton (1)
What is a distribution?
What is a terminal?
What is a hacker?
What is a pointer?
This is a sample of function application in dataframe.
About Go error handling * This is a rough article.
What is a decision tree?
Python list is not a list
What is a Context Switch?
What is a super user?
[Definition] What is a framework?
What is a callback function?
The image is a slug
What is a python map?
Is go-template Turing complete? I made a brainf * ck processing system
[Python] What is a zip function?
[Python] What is a with statement?
Is it a character string operation?
Is there a special in scipy? ??
A note when gcloud is broken
What is a lexical scope / dynamic scope?
What is a Convolutional Neural Network?
What kind of Kernel is this Kernel?
What is the X Window System?
Make a recommender system with python
Is "destructive method" a Ruby term?