Using Image Recognition to Predict Trend Reversals

Using Image Recognition to Predict Trend Reversals

The WeChat public account on quantitative investment and machine learning is a mainstream self-media in the industry focusing onquantitative investment, hedge funds,Fintech, artificial intelligence, big data and other fields.The public account has over300,000+ followers from industries such aspublic funds, private equity, securities, futures, banks, insurance, and universities, and it won the 2021 AMMA Excellent Brand Strength and Excellent Insight Awards, being selected as the “Best Author of the Year” by Tencent Cloud + Community for two consecutive years.

Authors: CHERN-BIN JU, AN-PIN CHEN

Introduction

In recent years, deep learning algorithms have performed exceptionally well in the field of computer vision. We often wonder if we can use image recognition technology to predict stock prices in the field of quantitative investment. To solve this problem, we must first answer the following two questions:

  • How to convert stock price sequences into computer images? (X)

  • How to define the prediction target? (Y)

The essence of these two questions is how to define training samples and training targets. This is a problem encountered in every machine learning task. The intuitive answer for many is: if it is an image recognition model, can we input the stock price candlestick chart directly and predict the rise and fall for a future period? This end-to-end approach is unlikely to yield very good results.

This article shared today comes from IEEE. It addresses the above two questions from the following aspects: first, it converts the stock price sequence into a grayscale image through Market Profile, and then defines the prediction target as trend reversal. Finally, it uses a CNN model to predict whether a trend reversal will occur in the future.

Market Profile

The Market Profile indicator, also known as the four-dimensional space indicator in China, can intuitively display the current market price distribution compared to traditional candlestick charts. Suppose we divide a day into five time periods, represented by the letters A, B, C, D, and E. Each time period has high, open, low, and close prices. We only use the highest and lowest prices, and then label the corresponding time period (represented by letters) on the price range corresponding to that time period. For example, in Day 1, if the highest price during period A is 3130 and the lowest price is 3100, we mark A at the position from 3130 to 3100; if the highest price during period B is 3120 and the lowest price is 3090, we mark B at the position from 3120 to 3090; and so on.

Using Image Recognition to Predict Trend Reversals

Market Profile to Grayscale Image

The Market Profile obtained from the above conversion cannot be directly used as input for CNN; it must be converted into an image. In the above example, intraday market data was used (dividing a day into five time periods). However, in the empirical model, the author only used daily candlestick price data. The author used price data from the past 25 days, and there are various methods to convert the price data for these 25 days into Market Profile. The author adopted the following methods for comparison:

  • Convert all 25 days of candlestick data into Market Profile

  • Divide the 25 days into 5 segments of 5 days each, converting each 5-day period into a Market Profile

Then, different colors can be used to represent different dates, resulting in several ways to define sample data (see figures 9 and 10 for specifics):

  • EGA, based on all 25 days generating Market Profile, with each date represented in a different color.

  • EGB, based on 5 days generating 5 Market Profiles, all dates represented in the same color.

  • EGC, based on 5 days generating 5 Market Profiles, with the same color for each time segment, but different colors between time segments.

  • EGC, based on 5 days generating 5 Market Profiles, with each time segment’s N-th day represented in one color, for example, all first days in each time segment being one color.

Using Image Recognition to Predict Trend Reversals

The author used the S&P 500 mini futures data from the past 20 years and applied a 1-day window, as shown in the figure below, rolling to convert candlestick data into image data.

Using Image Recognition to Predict Trend Reversals

Data Annotation

The above steps show how to convert candlesticks into images, solving the first problem. For the prediction target, which is trend reversal, the author used the following definition, where c represents the closing price. If the closing price 5 days ago is greater than the closing price 10 days ago, the closing price today is greater than the closing price 5 days ago, and the closing price 5 days later is less than today’s closing price, it indicates an upward trend reversal; if the closing price 5 days ago is less than the closing price 10 days ago, today’s closing price is less than the closing price 5 days ago, and the closing price 5 days later is greater than today’s closing price, it indicates a downward trend reversal.

Using Image Recognition to Predict Trend Reversals

Model Structure

The article employs a CNN model for training and predicting the input images, with the specific model structure shown below:

Using Image Recognition to Predict Trend Reversals

Empirical Results

The article first presents the model results, as shown in tables 2 and 3. It also provides test results applied to specific trading strategies, as shown in tables 4 and 5. Table 4 shows results with a 2% stop loss, while table 5 shows results with a 5% stop loss. The specific trading strategy logic is as follows:

  • On day t, when the model predicts a downward trend reversal (prediction 0), buy and plan to sell 5 days later:
    • If a stop loss is triggered during this period, sell;
    • If the next day is still prediction 0, sell 6 days later;
    • If the next day predicts 1, sell 5 days later;
    • If the next day predicts 2, sell.
  • On day t, when the model predicts an upward trend reversal (prediction 1), sell and plan to buy 5 days later:
    • If a stop loss is triggered during this period, buy;
    • If the next day is still prediction 1, buy 6 days later;
    • If the next day predicts 0, buy 5 days later;
    • If the next day predicts 2, buy.

As shown in figure 14, among all comparisons,EGC achieved the highest average single profit, with an annualized return of 50.5% under a 2% stop loss, and a profit-loss ratio of 1.74.

Using Image Recognition to Predict Trend Reversals

Using Image Recognition to Predict Trend Reversals

Conclusion

The greatest innovation of this article is the use of Market Profile to transform the original time series prediction problem into an image recognition problem.This way, CNN can be used for predicting trend reversals. For other details, please refer to the original text:

https://ieeexplore.ieee.org/stamp/stamp.jsp?arnumber=9693504

Leave a Comment