[PYTHON] Is it possible to eat stock price forecasts by machine learning [Implementation plan]

Introduction

This article is just an idea. I am neither a data scientist nor a highly conscious engineer, so there is no academic backing. It's just Poo Taro's idiot.

I know that stock prices are random walks, but I'm always wondering if I can predict stock prices by machine learning of past stock prices. For example, suppose that you can predict whether tomorrow's price range (difference between discount and close price) will be positive or negative with an accuracy of 60%. The hit rate when predicted by rolling the dice is 50%, so the hit rate of 60% does not seem to be so reckless.

A hit rate of 60% means that if you predict 10 times, you will win 2 times with 6 wins and 4 losses. If you invest the same amount each time, and if the profits you gained by hitting the mark and the losses you lost off the mark cancel each other out in the long run, you will accumulate the profits of two times every ten times.

If it is an individual brand, if it is not sold short, there is only a chance to purchase it when it is expected to rise, so the scheme will be such that it can be established only by buying in kind. That is, the Nikkei 225 is the forecast target.

If so, it will be possible to turn it just by buying in kind. Since I have a small amount of money on hand, I can make some leverage.

Nikkei 225 price range

So, first of all, let's find out how much the price range of the Nikkei 225 is. Drop the time series data of Nikkei 225 to 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
  ....

Write the following code to check. Depending on the market trend, the average price range at the time of rise and fall will be different, but it is regarded as a box market and the absolute value of the price range is averaged.

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

def calc_histgram(exchange):
    '''
 Calculation of standard deviation of price range distribution and creation of histogram
    '''    
 #Convert bid to list
    open_list = exchange['Open'].tolist()
 # Convert closing price to list
    close_list = exchange['Close'].tolist()
    diff_list = ((np.array(close_list) - np.array(open_list))/np.array(open_list)).tolist()
    stdev = statistics.stdev(np.array(diff_list))

 #Calculate the average price range
    abs_diff_list = np.abs(diff_list)
    abs_diff_mean = statistics.mean(np.array(abs_diff_list))
    print('abs_diff_mean={}'.format(abs_diff_mean))
    
 #Draw a histogram with a price range of 20
    plt.hist(diff_list, bins=80, rwidth=50)
    plt.title('n225 price range, sigma={}'.format(stdev))
    plt.show()

def main():
    nikkei_225 = pd.read_csv('NIKKEI225.csv').set_index('Date').sort_index()
    calc_histgram(nikkei_225)

if __name__ == '__main__':
    main()

fig1-1_n225_sigma.png

The average price range from 2014-10-14 to 2017-10-31 was 0.64%.

Forecast scheme

In this scheme, NF Nikkei Leverage and NF Nikkei Double Inverse are multiplied by double leverage, so the average price range Δ is twice that of N225, so Δ = 0.64% x 2 = 1.28%.

Assuming that the market opens 20 times a month, if you can predict that the hit rate is 60%, you will win 4 times a month, and the monthly profit will be the investment amount per day x 1.28% x 4. Trading fee (0.064% for round trip) is required for 20 times. In addition to this, an investment trust fee is also required, but this is a small amount, so ignore it. It is assumed that the profit and loss other than the winning four days was even.

Monthly profit = investment amount per day x (1.28% x 4-0.064 x 20) = Investment amount per day x 3.84% Will be.

If the monthly interest rate is 3.84%, the annual rate is 46% when multiplied by 12. This is delicious. Is it true? Assuming that the daily investment amount is 1 million yen, the expected value of monthly profit is 38,400 yen. It's a good pocket money. If you invest 10 million yen, you can eat it.

If something is wrong, what's wrong It says, "Predict the sign of the Nikkei 225 price range with 60% accuracy." .. ..

I would like to verify here.

Summary

It is a joke that if the sign of the price range of the Nikkei 225 can be predicted with 60% accuracy, an annual profit of 46% may be obtained.

Is it possible to eat with stock price forecast by machine learning [Machine Learning Part 1] ] is followed.

Recommended Posts

Is it possible to eat stock price forecasts by machine learning [Implementation plan]
Is it possible to eat by forecasting stock prices by machine learning [Machine learning part 1]
Stock price forecast by machine learning is so true Numerai Signals
Stock price forecast by machine learning Numerai Signals
Stock price forecast by machine learning Let's get started Numerai
Stock price forecast using machine learning (scikit-learn)
Stock price forecast using machine learning (regression)
Try to forecast power demand by machine learning
Reasonable price estimation of Mercari by machine learning
Python & Machine Learning Study Memo ⑦: Stock Price Forecast
Introduction to machine learning
What is machine learning?
[Introduction to Systre] Stock price forecast; Monday is weak m (__) m