Moving average line calculation method based on incremental updating mechanism

Through the moving average calculation method of the incremental update mechanism, the problems of low computing efficiency, waste of memory and insufficient real-time performance in the traditional method are solved, and efficient moving average calculation is achieved, which is suitable for real-time market analysis of securities, futures and foreign exchange markets.

CN120336695APending Publication Date: 2025-07-18刘金贵
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510489098.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-04-18
Publication Date
2025-07-18

AI Technical Summary

Technical Problem

The traditional moving average calculation method has shortcomings in terms of computing efficiency, memory resources and real-time performance, and cannot meet the needs of high-frequency trading.

Method used

The incremental update mechanism is adopted to update the mean by replacing the difference between the oldest value of the window and the new added value, and only the oldest value of the window is cached to avoid repeated summing.

Benefits of technology

Significantly reduce the amount of calculation and storage, improve the calculation speed, and meet the real-time needs of high-frequency trading.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure BDA0005365021350000011
    Figure BDA0005365021350000011
  • Figure BDA0005365021350000021
    Figure BDA0005365021350000021
  • Figure BDA0005365021350000022
    Figure BDA0005365021350000022
Patent Text Reader

Abstract

The invention relates to the technical field of financial time series data processing, and particularly discloses a moving average line calculation method based on an incremental updating mechanism, which comprises the following steps: inputting a price sequence Prices [0... n-1] and a window period M; initializing an incremental moving average sequence IMA [0... M-2] into NaN; calculating a first effective value by using a traditional averaging method, and assigning the first effective value to IMA [M-1]; and through an increment replacement mechanism, circularly calculating subsequent values from i = M to n-1, and completing calculation of an increment moving average sequence IMA [0... n-1]. Compared with a traditional moving average line calculation method, by means of an increment updating mechanism, repeated summation of data in the whole window is avoided when the moving average is calculated each time. When large-scale data is processed, the calculation amount can be remarkably reduced, and the calculation time is greatly shortened. According to the method, only the oldest value of the window is cached, so that the data storage amount is greatly reduced. In a long-term data accumulation process, the advantage is particularly obvious.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of financial time series data processing, and particularly relates to an efficient moving average calculation method applicable to real-time market analysis in markets such as securities, futures, and foreign exchange, and is optimized for high-frequency trading and big data real-time processing scenarios. Background Art

[0002] Limitations of the traditional simple moving average (SMA)

[0003] 1. Low calculation efficiency:

[0004] Each calculation requires traversing all data within the window, and the time complexity is O(n×M) (n is the total amount of data, and M is the window period).

[0005] Example: Calculating the MA250 of 1 million data requires 250 million addition operations.

[0006] 2. Waste of memory resources:

[0007] It is necessary to repeatedly store window data, and the peak memory occupancy is high.

[0008] 3. Lack of real-time performance:

[0009] It cannot meet the microsecond-level response requirements in high-frequency trading scenarios.

[0010] Attempts at improving the prior art:

[0011] Recursive moving average method: It relies on a recursive formula but does not solve the window sliding problem and still requires full-scale calculation.

[0012] Exponential moving average (EMA): It is a non-fixed window method and is not suitable for strict periodic analysis.

[0013] Hardware acceleration solutions: They rely on GPU / FPGA parallel computing, which is costly and complex to deploy.

[0014] Therefore, a moving average calculation method based on an incremental update mechanism has become an urgent problem to be solved. Summary of the Invention

[0015] The technical problem to be solved by the present invention is to provide a moving average calculation method based on an incremental update mechanism to improve the calculation efficiency of the moving average and reduce the amount of calculation.

[0016] To solve the above technical problem, the technical solution provided by the present invention is: A moving average calculation method based on an incremental update mechanism, including the following steps:

[0017] Step S1, input the price sequence Prices[0...n - 1] and the window period M;

[0018] Step S2: Initialize the incremental moving average sequence IMA[0...M - 2] as NaN;

[0019] Step S3: Calculate the first valid value using the traditional mean method and assign it to IMA[M - 1];

[0020] Step S4: Through the incremental replacement mechanism, calculate the subsequent values in a loop from i = M to n - 1 to complete the calculation of the incremental moving average sequence IMA[0...n - 1].

[0021] In the above steps, the mean is updated by replacing the difference between the oldest value in the window and the new value, and the calculation formula is as follows:

[0022]

[0023] Among them, IMA M [i] represents the M - period incremental moving average value of the i - th data point, and Price[i] represents the price of the i - th data point.

[0024] Furthermore, the first valid value is calculated by the traditional mean method, that is:

[0025]

[0026] Each subsequent data point updates the mean by replacing the removed value with the new value to avoid repeated summation.

[0027] Furthermore, only the oldest value in the window is cached during the calculation process.

[0028] The advantages of the present invention compared with the prior art are as follows: Compared with the traditional moving average calculation method, the present invention uses an incremental update mechanism to avoid repeated summation of data within the entire window when calculating the moving average value each time. When dealing with large - scale data, it can significantly reduce the amount of calculation and greatly shorten the calculation time. The way of only caching the oldest value in the window in the present invention greatly reduces the data storage volume. This advantage is particularly obvious during the long - term data accumulation process. Detailed implementation manners

[0029] The following will describe various exemplary embodiments of the present invention in detail. It should be noted that: Unless otherwise specifically stated, the relative arrangements, numerical expressions, and numerical values of the components and steps described in these embodiments do not limit the scope of the present invention.

[0030] The description of at least one exemplary embodiment below is actually only illustrative and in no way limits the present invention and its application or use.

[0031] Techniques, methods, and equipment known to those of ordinary skill in the relevant art may not be discussed in detail, but where appropriate, such techniques, methods, and equipment should be regarded as part of the specification.

[0032] In all examples shown and discussed herein, any specific values should be construed as merely exemplary and not as a limitation. Thus, other examples of the exemplary embodiments may have different values.

[0033] The following further details a moving average calculation method based on an incremental update mechanism according to the present invention.

[0034] A moving average calculation method based on an incremental update mechanism is as follows:

[0035] Step S1: Input the price sequence Prices[0...n - 1] and the window period M. Here, the price sequence can be a stock price sequence, a commodity price sequence, etc. in the financial market, and the window period M determines the size of the data window considered for the moving average calculation.

[0036] Step S2: Initialize the incremental moving average sequence IMA[0...M - 2] as NaN. This is because when the window period has not reached M, an effective moving average value cannot be calculated.

[0037] Step S3: Calculate the first valid value using the traditional mean method and assign it to IMA[M - 1]. The formula for the first valid value is:

[0038]

[0039] Taking the stock price as an example, if the window period M is 5, then the first valid value is the average of the first 5 stock prices.

[0040] Step S4: Through the incremental replacement mechanism, loop from i = M to n - 1 to calculate the subsequent values and complete the calculation of the incremental moving average sequence IMA[0...n - 1]. Update the mean by replacing the difference between the oldest value in the window and the new value, and the calculation formula is as follows:

[0041]

[0042] Where IMA M [i] represents the M - period incremental moving average value of the i - th data point, and Price[i] represents the price of the i - th data point. For example, when calculating the moving average value of the 6th data point, use the moving average value of the 5th data point, plus one - fifth of the difference between the price of the 6th data point and the price of the 1st data point (assuming M = 5) to efficiently obtain the result.

[0043] Only cache the oldest value of the window during the calculation process. Doing so can greatly reduce the data storage volume and further improve the calculation efficiency. In the scenario of financial data processing, new price data is generated every day. Without adopting this efficient caching method, as time goes by, the storage cost will increase significantly, and the time for retrieving data during calculation will also become longer. By only caching the oldest value of the window, it is possible to avoid storing a large amount of historical data and only need to calculate based on the newly added data and the cached oldest value.

[0044] The present invention is applicable to the real-time data processing module of securities, futures, and foreign exchange market systems.

[0045] The specific implementation process of a moving average calculation method based on an incremental update mechanism of the present invention is as follows:

[0046] Input: Price sequence Prices[0...n - 1], window period M;

[0047] Output: Incremental moving average sequence IMA[0...n - 1];

[0048]

[0049] Performance comparison (test with 1 million data):

[0050] Index Traditional SMA IMA Algorithm Optimization Multiple Computing Time (ms) 4200 58 72.4x Memory Occupation (MB) 95.3 6.1 15.6x Real-time Latency (μs) 2500 34 73.5x

[0051] The application scenarios of the present invention are as follows:

[0052] High-frequency trading system: Support real-time moving average calculation for millions of orders per second.

[0053] Quantitative analysis platform: Improve the batch processing speed of minute-level K-line data.

[0054] Embedded terminal: Achieve efficient operation in low-power devices (such as intelligent investment advisor hardware).

[0055] The above describes the present invention and its implementation manners. This description is not restrictive. If those of ordinary skill in the art are inspired by it and, without departing from the gist of the present invention, design similar structural manners and embodiments without creative efforts, they shall fall within the protection scope of the present invention.

Claims

1. A moving average calculation method based on an incremental update mechanism, characterized in that, It includes the following steps: Step S1: Input the price sequence Prices[0...n-1] and the window period M; Step S2: Initialize the incremental moving average sequence IMA[0...M-2] as NaN; Step S3: Calculate the first valid value using the traditional mean method and assign it to IMA[M-1]; Step S4: Through the incremental replacement mechanism, loop from i = M to n-1 to calculate the subsequent values and complete the calculation of the incremental moving average sequence IMA[0...n-1]; In the above steps, the mean is updated by replacing the difference between the oldest value in the window and the new value, and the calculation formula is as follows: Among them, IMA M [i] represents the M-period incremental moving average of the i-th data point, and Price[i] represents the price of the i-th data point.

2. The moving average calculation method based on an incremental update mechanism according to claim 1, wherein: The first valid value is calculated by the traditional mean method, that is: Each subsequent data point updates the mean by replacing the removed value with the new value to avoid repeated summation.

3. The moving average calculation method based on an incremental update mechanism according to claim 2, characterized in that: Only the oldest value in the window is cached during the calculation process.