A stock portfolio backtesting method, system and platform

CN122736768APending Publication Date: 2026-09-11BEIJING LINGXI JINSUAN TECHNOLOGY CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202610859111.0
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-06-15
Publication Date
2026-09-11

AI Technical Summary

Technical Problem

[0004]然而,现有技术仍存在明显不足

Benefits of technology

(1)本发明以交易日序列为主线,在每一交易日获取对应的股池候选列表,并结合候选股票及已有持仓的行情数据执行组合回测,使股池获取、行情加载、买卖执行和权益计算能够在同一流程中连续完成;相比仅依据日线价格或单只股票逻辑进行模拟的方式,本发明更适合复现动态股池策略在多个交易日中的组合交易过程。

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122736768A_ABST
    Figure CN122736768A_ABST
Patent Text Reader

Abstract

The application belongs to the technical field of financial data processing, and discloses a stock portfolio backtesting method, system and platform. The stock portfolio backtesting method comprises the following steps: initializing a backtesting account based on backtesting parameters, determining a trading day sequence according to the backtesting parameters, and obtaining a corresponding stock pool candidate list in each trading day in the trading day sequence; loading the market data of candidate stocks and stocks held according to the stock pool candidate list and the existing holdings of the backtesting account; adjusting the holdings that hit the events of rights and interests before transaction execution, and executing the buy and sell processes in the preset order of buying and selling time; after the end of transaction execution in each trading day, calculating the total equity of the account according to the market data of the remaining holdings at the end of the day, and generating the backtesting result based on the total equity of the account. The application can more realistically simulate the sub-portfolio management and capital occupation in portfolio trading, and avoid the problems of excessive buying, distorted positions or unreasonable allocation of funds in backtesting.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of financial data processing technology, and in particular to a stock portfolio backtesting method, system, and platform. Background Technology

[0002] Backtesting of stock portfolios is an important means of verifying the effectiveness of stock pool strategies. It is typically used to reproduce the processes of candidate stock generation, capital allocation, buying and selling, and profit statistics under historical market conditions. With the increasing application of quantitative trading and stock pool strategies, strategy verification is no longer limited to buying and selling decisions for single stocks. Instead, it requires portfolio-level simulations around a daily changing set of candidate stocks to determine the overall performance of the strategy under different trading days, different holding states, and different funding conditions.

[0003] Existing stock pool backtesting schemes typically read the candidate stocks in the stock pool for each trading day, then buy the stocks they do not hold according to a preset order, and sell them after holding them for a certain number of days or when selling conditions are met. Finally, they calculate the account profit based on the closing price at the end of the day. Some schemes also primarily use daily chart data for backtesting, or simulate the buying and selling process of only a single stock. These schemes are relatively simple to implement and can reflect the general profit of the strategy, but they focus more on buy signals, sell signals, and the final profit result, and give less consideration to the capital occupation, changes in holdings, and post-trade analysis in portfolio trading.

[0004] However, existing technologies still have significant shortcomings. First, when backtesting based on daily prices, it is difficult to reflect the impact of specific transaction times on buy and sell prices in real trading, and the backtesting results are prone to deviation from actual trading. Second, existing solutions do not adequately consider the allocation budget, maximum holding quantity, and utilization of remaining cash in multi-stock portfolio trading, making it difficult to accurately reproduce the fund allocation process under the dynamic stock pool strategy. Third, factors such as T+1 restrictions, whole-lot trading, price limits, transaction fees, and ex-rights / ex-dividends in A-share trading are often not uniformly incorporated into the backtesting process, resulting in inaccurate statistics on account cash, holding costs, and returns. Third, existing backtesting results are usually rather coarse, making it difficult to form a complete result chain including daily equity, transaction details, closing results, monthly returns, and maximum drawdown, which is not conducive to subsequent review and auditing.

[0005] Therefore, how to improve the authenticity, reusability, and auditability of dynamic stock pool portfolios while verifying historical transactions has become an urgent problem to be solved. Summary of the Invention

[0006] This invention provides a stock portfolio backtesting method, system, and platform to address the aforementioned problems in the prior art.

[0007] According to a first aspect of the present invention, a stock portfolio backtesting method is provided.

[0008] In one embodiment, the stock portfolio backtesting method includes: initializing a backtesting account based on backtesting parameters, determining a trading day sequence according to the backtesting parameters, and obtaining a corresponding stock pool candidate list for each trading day in the trading day sequence; loading market data of candidate stocks and held stocks based on the stock pool candidate list and the existing holdings of the backtesting account; adjusting holdings that have hit ex-rights and ex-dividend events before execution of the trade, and executing buy and sell processing according to a preset buy and sell time sequence; wherein, the buy processing includes determining a single position budget based on the remaining cash and available holding slots of the backtesting account, and determining whether the buy can be executed based on the intraday trading volume of the target trading minute; the sell processing includes executing a sell based on the trading constraint that the stocks established on the day are restricted from being sold before the next trading day, and calculating transaction fees and realized profits and losses; after the execution of each trading day, calculating the total equity of the account based on the market data of the remaining holdings at the end of the day, and generating backtesting results based on the total equity of the account.

[0009] According to a second aspect of the present invention, a stock portfolio backtesting system is provided.

[0010] In one embodiment, the stock portfolio backtesting system includes: a parameter initialization module for initializing a backtesting account based on backtesting parameters; a trading calendar module for determining a trading day sequence according to the backtesting parameters; a stock pool acquisition module for acquiring a corresponding stock pool candidate list for each trading day in the trading day sequence; a minute-by-minute market data module for loading market data of candidate stocks and held stocks based on the stock pool candidate list and the existing holdings of the backtesting account; an ex-rights and ex-dividend processing module for adjusting holdings that have triggered ex-rights and ex-dividend events before trade execution; and a trade execution module for executing trades according to preset buy / sell orders. The buy and sell processes are executed sequentially. The buy process includes determining a single-position budget based on the remaining cash and available holding slots in the backtesting account, and determining whether the buy order can be executed based on the intraday trading volume of the target trading minute. The sell process includes executing a sell order based on the trading constraint that stocks established on the current day cannot be sold before the next trading day, and calculating transaction fees and realized profits and losses. An equity valuation module is used to calculate the total account equity based on the market data of the remaining holdings at the end of each trading day after the trading execution is completed. A result output module is used to generate backtesting results based on the total account equity.

[0011] According to a third aspect of the present invention, a computer platform is provided.

[0012] In some embodiments, the computer platform includes a memory and a processor, the memory storing a computer program, and the processor executing the computer program to implement the steps of the method described above.

[0013] The technical solutions provided by the embodiments of the present invention may include the following beneficial effects: (1) This invention takes the trading day sequence as the main line, obtains the corresponding stock pool candidate list on each trading day, and performs combination backtesting by combining the market data of candidate stocks and existing holdings, so that stock pool acquisition, market loading, buying and selling execution and equity calculation can be completed continuously in the same process. Compared with the method of simulating only based on daily price or single stock logic, this invention is more suitable for reproducing the combination trading process of dynamic stock pool strategy in multiple trading days.

[0014] (2) In the buy-in process, this invention determines the single-position budget based on the remaining cash and available holding slots in the backtesting account, and determines the number of shares to be bought according to the buy price and whole-lot trading rules, so that each buy-in is constrained by the account funds, the maximum holding slot, and the minimum trading quantity. This can more realistically simulate the sub-position management and capital occupation in portfolio trading, and avoid problems such as over-buying, position distortion, or unreasonable capital allocation in backtesting.

[0015] (3) In the process of transaction execution, this invention uniformly handles the transaction constraints, non-trading status, transaction fees, realized profits and losses and ex-rights and ex-dividend adjustments of stocks that are established on the same day and restricted from being sold before the next trading day. Before buying, it determines whether the purchase can be executed based on the intraday transaction volume of the target trading minute. By incorporating the above transaction rules into the same backtesting account status, it can reduce backtesting deviations caused by ignoring A-share trading rules, liquidity restrictions or equity adjustments.

[0016] (4) After the execution of each trading day, the present invention calculates the total equity of the account based on the market data of the remaining positions at the end of the day, and generates backtesting results based on the total equity of the account. This allows the backtesting process to not only provide the final return, but also to generate daily equity, changes in positions, transaction flow, closing results, profit statistics, monthly statistics and maximum drawdown data. This facilitates the review of the trading process, capital changes and risk performance of the stock pool strategy, and improves the completeness and auditability of the backtesting results.

[0017] It should be understood that the above general description and the following detailed description are exemplary and explanatory only, and are not intended to limit the invention. Attached Figure Description

[0018] The accompanying drawings, which are incorporated in and form part of this specification, illustrate embodiments consistent with the invention and, together with the description, serve to explain the principles of the invention.

[0019] Figure 1 This is a flowchart illustrating a stock portfolio backtesting method according to an exemplary embodiment; Figure 2This is a schematic diagram of a single-day transaction simulation process according to an exemplary embodiment; Figure 3 This is a flowchart illustrating a buy execution process according to an exemplary embodiment; Figure 4 This is a flowchart illustrating a sell execution process according to an exemplary embodiment; Figure 5 This is a structural block diagram of a stock portfolio backtesting system according to an exemplary embodiment; Figure 6 This is a detailed implementation diagram illustrating a stock portfolio backtesting method according to an exemplary embodiment; Figure 7 This is a schematic diagram of the structure of a computer platform according to an exemplary embodiment. Detailed Implementation

[0020] The following description and accompanying drawings fully illustrate specific embodiments described herein to enable those skilled in the art to practice them. Some portions and features of certain embodiments may be included in or replace portions and features of other embodiments. The scope of the embodiments herein includes the entire scope of the claims and all available equivalents thereof. The various embodiments described herein are presented in a progressive manner, with each embodiment focusing on its differences from other embodiments; similar or identical parts between embodiments can be referred to interchangeably.

[0021] The modules in the apparatus or system of this application can be implemented entirely or partially through software, hardware, or a combination thereof. These modules can be embedded in or independent of the processor in a computer platform in hardware form, or stored in the memory of the computer platform in software form, so that the processor can call and execute the operations corresponding to each module.

[0022] Where there is no conflict, the embodiments and features in the embodiments of the present invention can be combined with each other.

[0023] Figure 6 An embodiment of a stock portfolio backtesting method of the present invention is shown.

[0024] In this optional embodiment, the stock portfolio backtesting method includes: Step S101, initializing the backtesting account based on backtesting parameters, determining the trading day sequence according to the backtesting parameters, and obtaining the corresponding stock pool candidate list for each trading day in the trading day sequence; Step S102, loading the market data of candidate stocks and held stocks according to the stock pool candidate list and the existing holdings of the backtesting account; adjusting the holdings that hit the ex-rights and ex-dividend event before the transaction is executed, and executing the buy and sell processing according to the preset buy and sell time sequence; wherein, the buy processing includes determining the single position budget based on the remaining cash and empty holding slots of the backtesting account, and judging whether the buy can be executed based on the intraday trading volume of the target trading minute; the sell processing includes executing the sell based on the trading constraint that the stocks established on the day are restricted from being sold before the next trading day, and calculating the transaction fees and realized profits and losses; Step S103, after the transaction execution of each trading day, calculating the total equity of the account based on the market data of the remaining holdings at the end of the day, and generating the backtesting result based on the total equity of the account.

[0025] In this optional embodiment, the backtesting parameters include the backtesting start date, backtesting end date, initial capital, maximum holding slot, buy time, sell time, and transaction fee rate; such as Figure 1 As shown, in step S101, the backtesting account is initialized based on the backtesting parameters, and the trading day sequence is determined according to the backtesting parameters. This includes: generating the initial cash of the backtesting account based on the initial funds, and configuring the maximum holding slot to be idle; determining the initial account equity based on the initial funds, and initializing statistical variables used to record daily equity, changes in holdings, transaction flow, closing results, and profit statistics; and selecting the trading day sequence from the trading calendar based on the backtesting start date and backtesting end date. The trading day sequence includes several trading days arranged in chronological order.

[0026] In this optional embodiment, step S101, obtaining the corresponding stock pool candidate list for each trading day in the trading day sequence, includes: generating candidate stocks using a color label system based on the stock market characteristics of the current trading day and several previous trading days; generating the stock pool candidate list for the current trading day according to the stock code, label type, and stock selection time of the candidate stocks; using the stock pool candidate list as the source of candidate targets for the current trading day to execute buy processing; performing the above stock pool candidate list generation process on each trading day in the trading day sequence to obtain the stock pool candidate list corresponding to each trading day; wherein, the color label system includes a first color label generated based on intraday price strength and a second color label generated based on limit-up status and historical limit-up interval.

[0027] Specifically, in this invention, a K-line refers to a price trend representation unit formed by market data such as opening price, closing price, highest price, and lowest price within a preset trading period. In this embodiment, the first color label is a purple K-line label, and the second color label is a yellow K-line label; the yellow K-line label and the purple K-line label are color labels generated based on the K-line according to rules such as limit-up status, historical limit-up intervals, or intraday price strength.

[0028] It should be noted that stock market characteristics include at least one of the following: opening price, highest price, lowest price, closing price, previous trading day's closing price, trading volume, transaction price, and whether the stock is at its daily limit up or down for the current trading day and several trading days prior. Based on these stock market characteristics, it is possible to calculate the highest price increase for the current trading day, determine whether the stock has reached its daily limit up for the current trading day, and determine whether the stock has reached its daily limit up for several trading days prior to the current trading day. This provides a data foundation for the generation of purple K-line and yellow K-line labels.

[0029] Specifically, when generating the stock pool candidate list for the current trading day, the stock code, corresponding tag type, and stock selection time of each candidate stock are linked and recorded. The tag type indicates that the candidate stock is generated based on a purple K-tag, a yellow K-tag, or both, while the stock selection time indicates the time when the candidate stock was identified as a candidate. The resulting stock pool candidate list is used to sequentially read candidate stocks during the buy processing of the current trading day and serves as the data source for subsequent market data loading and buy decision-making.

[0030] In this optional embodiment, candidate stocks are generated using a color labeling system, including: when the highest price of a target stock on the current trading day increases relative to the closing price of the previous trading day by a first preset threshold, the K-line corresponding to the current trading day is marked with a first color, and a first color label is generated; in this embodiment, the K-line corresponding to the current trading day is marked with purple, and a purple K-line label is generated; when a target stock reaches its daily limit up on the current trading day, but did not reach its daily limit up on the previous trading day, and has not reached its daily limit up in the preceding trading days, the K-line corresponding to the current trading day is marked with a second color, and a second color label is generated; in this embodiment, the K-line corresponding to the current trading day is marked with yellow, and a yellow K-line label is generated; stocks that generate the first color label and / or the second color label are determined as candidate stocks.

[0031] In this optional embodiment, step S102 involves loading market data for candidate stocks and held stocks based on the candidate stock pool list and the existing holdings of the backtesting account. This includes: merging the candidate stock pool list and the existing holdings of the backtesting account for the current trading day to obtain a set of stocks to be loaded; loading market data for the current trading day for each stock in the set of stocks to be loaded; wherein the market data includes minute time, minute transaction price, minute transaction volume, limit up status, and limit down status; and determining the target transaction minute from the market data based on the purchase and sale times.

[0032] In this optional embodiment, step S102, adjusting the holdings that have triggered the ex-rights and ex-dividend event before the transaction is executed, includes: before the transaction is executed on the current trading day, querying whether the existing holdings of the backtesting account have triggered the ex-rights and ex-dividend event; wherein, when the stock code of the existing holding is consistent with the stock code in the ex-rights and ex-dividend event record, and the current trading day is the ex-rights and ex-dividend date corresponding to the ex-rights and ex-dividend event record, it is determined that the existing holding has triggered the ex-rights and ex-dividend event; if the ex-rights and ex-dividend event has been triggered, the ex-rights and ex-dividend information of the corresponding stock is obtained; the number of shares held, available cash, and holding cost of the corresponding holding are adjusted according to the ex-rights and ex-dividend information; the backtesting account is updated based on the adjusted number of shares held, available cash, and holding cost, so that subsequent buy processing, sell processing, and account equity calculation are executed based on the adjusted account status.

[0033] Specifically, the ex-rights and ex-dividend information includes at least one of the following: stock code, ex-rights and ex-dividend date, cash dividend amount, bonus share ratio, stock split ratio, rights issue ratio, and rights issue price; wherein, the cash dividend amount is used to determine the amount of cash to be increased, the bonus share ratio, stock split ratio, and rights issue ratio are used to determine the number of shares to be adjusted, and the rights issue price is used to determine the cash deduction amount and the holding cost adjustment amount resulting from the rights issue.

[0034] In this alternative embodiment, such as Figure 1 As shown, in step S102, the buy processing is executed according to the preset buy and sell time sequence, including: determining the stock to be bought according to the stock pool candidate list; determining whether the stock to be bought already exists in the existing holdings of the backtesting account, and if it already exists, skipping the stock to be bought; determining whether there are any empty holding slots in the backtesting account, and if not, stopping the buy processing; determining the single-position budget based on the remaining cash and empty holding slots; determining the buy price from the market data of the stock to be bought according to the fixed time point pricing method or the first touch price pricing method; determining the number of shares to be bought based on the single-position budget and the buy price, and correcting the number of shares to be bought according to the whole lot trading rules; if the corrected number of shares to be bought meets the minimum trading quantity requirement, and the stock to be bought is not in an untradeable state at the target trading minute, then the buy can be traded judgment is entered; among which, the untradeable state includes the untradeable state at the upper limit and the untradeable state at the lower limit.

[0035] It should also be noted that when determining the single-position budget, the remaining cash in the backtesting account before the current trading day's buy-in processing is first determined, along with the number of unused holding slots in the maximum holding slots. When the number of unused holding slots is greater than zero, the remaining cash is allocated according to the number of unused holding slots to obtain the single-position budget corresponding to the current stock to be bought. This method ensures that the amount of each purchase is constrained by both the remaining cash and the maximum holding quantity, preventing a single candidate stock from consuming too much cash and affecting the purchase processing of subsequent candidate stocks.

[0036] Specifically, the fixed-time pricing method in this invention is as follows: based on the pre-set purchase time in the backtesting parameters, the minute market data corresponding to the purchase time is searched in the current trading day's market data of the stock to be purchased, and the transaction price in the minute market data is used as the purchase price; if the minute market data corresponding to the purchase time does not exist, the stock to be purchased can be skipped, or a minute market data adjacent to the purchase time and with a valid transaction price can be selected as the pricing basis according to the preset rules.

[0037] Specifically, the initial price selection method in this invention is as follows: based on the pre-set target price condition, the minute-level market data of the stock to be bought on the current trading day is traversed in chronological order. When a minute market data record with a transaction price that reaches or exceeds the target price condition appears for the first time, the minute market data record is determined as the target transaction minute, and the transaction price in the minute market data record is used as the purchase price; if no minute market data record that meets the target price condition appears on the current trading day, the purchase process for the stock to be bought is abandoned.

[0038] It should be added that when determining the number of shares to be purchased, the theoretical number of shares that can be purchased is calculated based on the single-position budget and the purchase price. That is, the single-position budget is divided by the purchase price to obtain the corresponding number of shares. In the calculation process, if transaction fees need to be included, the available amount after deducting the estimated transaction fees is used as the basis for calculation. The theoretical number of shares that can be purchased represents the maximum number of shares that can be purchased under the current single-position budget and purchase price. This theoretical number of shares that can be purchased will be adjusted later according to the whole lot trading rules.

[0039] Specifically, the whole lot trading rule is as follows: the theoretically purchasable number of shares is adjusted downwards according to the trading unit of one lot, which corresponds to one hundred shares, to obtain the adjusted number of shares to be purchased, ensuring that the adjusted number of shares to be purchased is an integer multiple of one hundred shares. If the adjusted number of shares to be purchased is less than one lot, i.e., less than one hundred shares, it is determined that the stock to be purchased does not meet the minimum trading quantity requirement, and the purchase process is abandoned; if the adjusted number of shares to be purchased is not less than one lot, the judgment of untradeable status and purchase tradeable status will continue.

[0040] In this optional embodiment, the determination of whether a buy order can be executed includes: calculating the intraday trading volume of the target trading minute based on the intraday trading volume and intraday trading price of the target trading minute; calculating the proposed purchase amount based on the corrected number of shares to be purchased and the purchase price; determining whether the proposed purchase amount is less than the product of the intraday trading volume and the preset trading volume ratio; if the proposed purchase amount is less than the product of the intraday trading volume and the preset trading volume ratio, it is determined that the stock to be purchased can be traded in the target trading minute, and the backtesting account is updated based on the proposed purchase amount, transaction fees, and the corrected number of shares to be purchased; if the proposed purchase amount is greater than or equal to the product of the intraday trading volume and the preset trading volume ratio, it is determined that the liquidity of the target trading minute is insufficient, and the purchase of the stock to be purchased is abandoned or postponed to a subsequent candidate stock in the stock pool candidate list.

[0041] It should be noted that when calculating the intraday trading volume for the target trading minute, the intraday trading volume and price of the stock to be bought at the target trading minute are obtained. The intraday trading volume and price are then multiplied to obtain the intraday trading volume for that target trading minute. The intraday trading volume is used to represent the actual trading scale of the stock within the target trading minute and serves as a basis for judging whether the proposed purchase amount can be absorbed by the market trading volume, thereby improving the authenticity of the buy order judgment in backtesting.

[0042] Specifically, the proposed purchase amount is calculated based on the revised number of shares to be purchased and the purchase price. This is achieved by multiplying the revised number of shares to be purchased by the purchase price. The proposed purchase amount represents the expected transaction amount for this purchase and is compared with the intraday transaction volume of the target trading minute and a preset transaction volume ratio. When the proposed purchase amount is less than the product of the intraday transaction volume and the preset transaction volume ratio, the purchase is deemed feasible for execution in that target trading minute.

[0043] In this optional embodiment, step S102 involves performing a sell order according to a preset buy / sell time sequence, including: obtaining the existing holdings of the backtesting account on the current trading day; determining whether the stocks in the existing holdings are stocks established on the current trading day; if they are stocks established on the current trading day, prohibiting their sale on the current trading day based on the trading constraint that stocks established on the current day are restricted from being sold before the next trading day; if they are not stocks established on the current trading day, determining the selling price based on a fixed-time pricing method or a custom selling logic; when the corresponding stock is not in an untradeable state, calculating the selling amount, transaction fees, and realized profit / loss based on the selling price, the number of shares sold, and the transaction fee rate; updating the cash in the backtesting account based on the selling amount and transaction fees, and updating the holding status of the corresponding stock based on the number of shares sold.

[0044] It should also be noted that when determining the selling price based on a fixed-time pricing method, the system searches for the minute-level market data corresponding to the selling time in the current trading day's market data, based on the pre-set selling time in the backtesting parameters, and uses the transaction price in that minute-level market data record as the selling price. When determining the selling price based on a custom selling logic, the system judges the market data of the current trading day and the preset selling conditions, determines the target selling minute at the minute-level market data record that meets the selling conditions, and uses the transaction price corresponding to that target selling minute as the selling price.

[0045] Specifically, when calculating the selling amount, transaction fees, and realized profit / loss, the selling amount is calculated based on the selling price and the number of shares sold, and at least one of the following transaction fees—selling commission or stamp duty—is calculated based on the selling amount and the transaction fee rate. Realized profit / loss is determined by the difference between the selling amount after deducting transaction fees and the corresponding holding cost; the corresponding holding cost can be calculated based on the stock's holding cost per unit and the number of shares sold. After the calculations are completed, the selling amount after deducting transaction fees is added to the backtesting account cash, and the number of shares held and the holding status of the stock are updated simultaneously.

[0046] In this alternative embodiment, such as Figure 1 As shown, in step S103, after the execution of each trading day, the total equity of the account is calculated based on the market data of the remaining open positions at the end of the day, and backtesting results are generated based on the total equity of the account. This includes: obtaining the remaining open positions of the backtesting account at the end of the day after the buy and sell processing of the current trading day; determining the end-of-day valuation price based on the market data corresponding to the remaining open positions at the end of the day; calculating the market value of the remaining open positions at the end of the day based on the market value of the open positions and the cash of the backtesting account; calculating the daily return rate based on the total equity of the account on the current trading day and the total equity of the account on the previous trading day, and calculating the cumulative return rate based on the total equity of the account on the current trading day and the initial equity of the account; generating backtesting results based on the total equity of the account, the daily return rate, the cumulative return rate, the open position status, and the transaction flow for each trading day; the backtesting results include daily records, open position history, transaction details, monthly returns, and maximum drawdown.

[0047] To facilitate understanding of the above technical solution of the present invention, the following is a detailed explanation using a backtesting project of a dynamic stock pool portfolio of A-shares as an example: In this backtesting project, the user set the backtesting period to a number of consecutive trading days, the initial capital to 1 million yuan, the maximum number of holding slots to 5, the buy time to 9:31, and the sell time to 14:50. Before the market opens each day, the system generates a candidate stock pool list based on the color tag system of purple K and yellow K labels, and records the candidate stock codes, tag types, and stock selection times. After entering a trading day, the system first establishes a daily minute-by-minute market data cache, loading the minute-by-minute market data of the candidate stocks and the account's existing holdings together to avoid repeatedly reading market data during the execution of trades.

[0048] like Figure 2 As shown, during the single-day trading simulation, the system first processes existing holdings for ex-rights and ex-dividend adjustments, and then determines the execution order based on the order of purchase and sale times. For example, when the purchase time is earlier than the sale time, the system first executes purchases according to the daily stock pool candidate list, and then sells existing holdings; when the sale time is earlier than the purchase time, the system first releases the funds from holdings that meet the selling conditions, and then uses the returned cash to participate in subsequent purchases. After the transaction is completed, the system values ​​the remaining holdings at minute-by-minute prices and generates daily equity, daily profit, cumulative profit, holding snapshot, and daily records.

[0049] like Figure 3 As shown, during the buy execution process, the system reads stocks one by one according to the candidate order, first determining whether the account has any available slots, whether the cash balance is greater than zero, and whether the stock is already held. For stocks that are not held but can be bought, the system reads the target minute's market data, determines the buy price and buy time, calculates the single-position budget based on the remaining cash and available slots, and then adjusts the number of shares to be bought according to the 100-share lot rule. If the adjusted number of shares is less than one lot, or the target minute's market data does not exist, the stock will not be bought, and the system continues to read the next candidate stock.

[0050] To make backtesting results more closely resemble actual trading activity, the system calculates the intraday trading volume for the target minute before making a purchase and compares the proposed purchase amount with a preset percentage of that minute's trading volume. For example, it checks if the proposed purchase amount is less than one-tenth of that minute's trading volume. Only when this condition is met is the purchase considered tradable for that minute, and cash and holdings are updated; otherwise, the system considers the liquidity insufficient for that minute, abandons the candidate stock, and moves on to the next candidate stock. This avoids situations where a purchase might appear tradable on paper but be difficult to buy in the actual market when backtesting solely based on price signals.

[0051] like Figure 4As shown, during the sell execution process, the system iterates through the current holdings, first determining whether a holding still exists, and then determining whether the holding is a stock established on the same day. For stocks established on the same day, the system prohibits selling according to the T+1 trading constraint; for stocks not established on the same day, the system reads the minute-by-minute market data for selling and determines the selling price. If the selling conditions are met and the price is available, the system calculates the selling amount, commission, stamp duty, and realized profit or loss. After the sell is completed, the net proceeds flow back to cash, the system records the closing ledger and updates or clears the corresponding holdings, thereby ensuring that subsequent equity calculations are based on the actual account status. This embodiment illustrates that the present invention can improve the matching degree between stock portfolio backtesting results and actual trading scenarios, and provide more complete and traceable data for subsequent strategy review, risk analysis, and parameter optimization.

[0052] Figure 5 An embodiment of a stock portfolio backtesting system of the present invention is shown.

[0053] In this optional embodiment, the stock portfolio backtesting system includes: a parameter initialization module for initializing a backtesting account based on backtesting parameters; a trading calendar module for determining a trading day sequence according to the backtesting parameters; a stock pool acquisition module for acquiring a corresponding stock pool candidate list for each trading day in the trading day sequence; a minute-by-minute market data module for loading market data of candidate stocks and held stocks based on the stock pool candidate list and the existing holdings of the backtesting account; an ex-rights and ex-dividend processing module for adjusting holdings that have triggered ex-rights and ex-dividend events before trade execution; and a trade execution module for executing trades according to preset parameters. The buy and sell processes are executed sequentially. The buy process includes determining the single-position budget based on the remaining cash and available holding slots in the backtesting account, and determining whether the buy order can be executed based on the intraday trading volume of the target trading minute. The sell process includes executing a sell order based on the trading constraint that stocks established on the current day cannot be sold before the next trading day, and calculating transaction fees and realized profits and losses. The equity valuation module is used to calculate the total account equity based on the market data of the remaining holdings at the end of each trading day after the execution of the trades. The result output module is used to generate backtesting results based on the total account equity.

[0054] In addition, the present invention also provides a computer platform, including a memory and a processor, wherein the memory stores a computer program, and the processor executes the computer program to implement the steps in the above method embodiments.

[0055] In one embodiment, the computer platform may be a server, and its internal structure diagram may be as follows: Figure 7As shown, the computer platform includes a processor, memory, and a network interface connected via a system bus. The processor provides computing and control capabilities. The memory includes non-volatile storage media and internal memory. The non-volatile storage media stores the operating system, computer programs, and a database. The internal memory provides an environment for the operation of the operating system and computer programs in the non-volatile storage media. The database stores static and dynamic information data. The network interface communicates with external terminals via a network connection. When the computer program is executed by the processor, it implements the steps in the above method embodiments.

[0056] Those skilled in the art will understand that Figure 7 The structure shown is merely a block diagram of a portion of the structure related to the present invention and does not constitute a limitation on the computer platform on which the present invention is applied. A specific computer platform may include more or fewer components than those shown in the figure, or combine certain components, or have different component arrangements.

[0057] In addition, the present invention also provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the steps in the above method embodiments.

[0058] Those skilled in the art will understand that all or part of the processes in the methods of the above embodiments can be implemented by a computer program instructing related hardware. The computer program can be stored in a non-volatile computer-readable storage medium, and when executed, it can include the processes of the embodiments of the methods described above. Any references to memory, storage, databases, or other media used in the embodiments provided by this invention can include at least one of non-volatile and volatile memory. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, or optical storage, etc. Volatile memory can include random access memory (RAM) or external cache memory. By way of illustration and not limitation, RAM can be in various forms, such as static random access memory (SRAM) or dynamic random access memory (DRAM), etc.

[0059] This invention is not limited to the structures described above and shown in the accompanying drawings, and various modifications and changes can be made without departing from its scope. The scope of this invention is limited only by the appended claims.

Claims

1. A stock portfolio backtesting method, characterized in that, The backtesting method for this stock portfolio includes: The backtesting account is initialized based on the backtesting parameters, and the trading day sequence is determined according to the backtesting parameters. For each trading day in the trading day sequence, the corresponding stock pool candidate list is obtained. Based on the candidate stock pool list and the existing holdings of the backtesting account, the market data of the candidate stocks and the held stocks are loaded; before the transaction is executed, the holdings that hit the ex-rights and ex-dividend event are adjusted, and the buy and sell processes are executed according to the preset buy and sell time sequence; wherein, the buy process includes determining the single position budget based on the remaining cash and empty holding slots of the backtesting account, and determining whether the buy can be executed based on the intraday trading volume of the target trading minute; the sell process includes executing the sell based on the trading constraint that the stocks established on the same day are restricted from being sold before the next trading day, and calculating the transaction fees and realized profits and losses; After the execution of each trading day, the total equity of the account is calculated based on the market data of the remaining open positions at the end of the day, and backtesting results are generated based on the total equity of the account.

2. The stock portfolio backtesting method according to claim 1, characterized in that, The backtesting parameters include the backtesting start date, backtesting end date, initial capital, maximum holding slots, purchase time, sale time, and transaction fee rate. The process of initializing the backtesting account based on backtesting parameters and determining the trading day sequence according to the backtesting parameters includes: The initial cash for the backtesting account is generated based on the initial funds, and the maximum holding slot is configured to be idle. The initial account equity is determined based on the initial capital, and statistical variables are initialized to record daily equity, changes in holdings, transaction flow, closing results, and profit statistics. The sequence of trading days is obtained by filtering from the trading calendar based on the backtest start date and backtest end date; The trading day sequence includes several trading days arranged chronologically.

3. The stock portfolio backtesting method according to claim 1, characterized in that, The step of obtaining the corresponding stock pool candidate list for each trading day in the trading day sequence includes: Based on the stock market characteristics of the current trading day and several previous trading days, candidate stocks are generated using a color label system; A stock pool candidate list for the current trading day is generated based on the stock codes, tag types, and stock selection time of the candidate stocks. The stock pool candidate list will be used as the source of candidate targets for the current trading day's buy order processing; The above stock pool candidate list generation process is performed on each trading day in the trading day sequence to obtain the stock pool candidate list corresponding to each trading day. The color label system includes a first color label generated based on intraday price strength and a second color label generated based on limit-up status and historical limit-up intervals.

4. The stock portfolio backtesting method according to claim 3, characterized in that, The method of generating candidate stocks using a color-coded label system includes: When the price increase of the highest price of the target stock on the current trading day relative to the closing price of the previous trading day reaches a first preset threshold, the K-line corresponding to the current trading day is marked with a first color, and a first color label is generated. When the target stock reaches the daily limit up on the current trading day, and did not reach the daily limit up on the previous trading day, and did not reach the daily limit up in the previous several trading days, the K-line corresponding to the current trading day is marked with the second color, and a second color label is generated; Stocks that generate a first color label and / or a second color label are identified as candidate stocks.

5. The stock portfolio backtesting method according to claim 1, characterized in that, The adjustment of positions that coincide with ex-rights and ex-dividend events before the execution of the transaction includes: Before the execution of the current trading day, check whether the existing holdings of the backtesting account have been affected by the ex-rights and ex-dividend event; wherein, if the stock code of the existing holding is consistent with the stock code in the ex-rights and ex-dividend event record, and the current trading day is the ex-rights and ex-dividend date corresponding to the ex-rights and ex-dividend event record, it is determined that the existing holding has been affected by the ex-rights and ex-dividend event. If the aforementioned ex-rights and ex-dividend event is triggered, the ex-rights and ex-dividend information for the corresponding stock will be obtained. Adjust the number of shares held, available cash, and holding cost of the corresponding positions based on the ex-rights and ex-dividend information; The backtesting account is updated based on the adjusted number of shares held, available cash, and holding costs, so that subsequent buy processing, sell processing, and account equity calculation are performed based on the adjusted account status.

6. The stock portfolio backtesting method according to claim 1, characterized in that, The process of executing buy orders according to a preset buy / sell time sequence includes: Determine the stocks to be bought based on the aforementioned stock pool candidate list; Determine whether the stock to be bought already exists in the existing holdings of the backtesting account. If it already exists, skip the stock to be bought. Determine if the backtesting account has any available holding slots; if not, stop the buying process. The single-warehouse budget is determined based on the remaining cash and the available storage slots. The purchase price is determined from the market data of the stock to be purchased, based on either a fixed-time pricing method or an initial price-touching method. The number of shares to be purchased is determined based on the single-position budget and the purchase price, and then adjusted according to the whole lot trading rules. If the revised number of shares to be purchased meets the minimum transaction quantity requirement, and the shares to be purchased are not in an untradeable state within the target transaction minute, then the purchase can be executed.

7. The stock portfolio backtesting method according to claim 6, characterized in that, The determination of whether a buy order can be executed includes: Calculate the intraday transaction amount for the target transaction minute based on the minute-by-minute transaction volume and minute-by-minute transaction price; Calculate the intended purchase amount based on the revised number of shares to be purchased and the purchase price; Determine whether the intended purchase amount is less than the product of the intraday trading volume and the preset trading volume ratio; If the proposed purchase amount is less than the product of the time-sharing transaction amount and the preset transaction amount ratio, it is determined that the stock to be purchased can be traded in the target transaction minute, and the backtesting account is updated according to the proposed purchase amount, transaction fees and the corrected number of shares to be purchased. If the intended purchase amount is greater than or equal to the product of the intraday trading volume and the preset trading volume ratio, it is determined that the liquidity of the target trading minute is insufficient, and the purchase of the stock to be purchased is abandoned or postponed to a subsequent candidate stock in the stock pool candidate list.

8. The stock portfolio backtesting method according to claim 1, characterized in that, The step of executing the sell process according to the preset buy and sell time sequence includes: Obtain the existing holdings of the backtesting account on the current trading day; Determine whether the stocks in the existing holdings are stocks that were bought on the current trading day; If the stock was purchased on the current trading day, it is prohibited from being sold on the current trading day due to the trading constraint that stocks purchased on the same day are restricted from being sold before the next trading day. If the stock is not purchased on the current trading day, the selling price will be determined based on a fixed-time pricing method or a custom selling logic. When the corresponding stock is not in an untradeable state, calculate the selling amount, transaction fees, and realized profit or loss based on the selling price, number of shares sold, and transaction fee rate. The cash balance of the backtesting account is updated based on the sales amount and transaction fees, and the holding status of the corresponding stock is updated based on the number of shares sold.

9. A stock portfolio backtesting system, characterized in that, The stock portfolio backtesting system includes: The parameter initialization module is used to initialize the backtesting account based on the backtesting parameters; The trading calendar module is used to determine the sequence of trading days based on backtesting parameters; The stock pool acquisition module is used to obtain the corresponding stock pool candidate list for each trading day in the trading day sequence; The minute-by-minute market data module is used to load market data of candidate stocks and held stocks based on the candidate stock pool list and the existing holdings of the backtesting account. The ex-rights and ex-dividend processing module is used to adjust the positions that have triggered ex-rights and ex-dividend events before the transaction is executed; The transaction execution module is used to execute buy and sell processing according to a preset buy and sell time sequence. The buy processing includes determining the single position budget based on the remaining cash and available position slots in the backtesting account, and determining whether the buy can be executed based on the intraday trading volume of the target trading minute. The sell processing includes executing the sell based on the trading constraint that the stocks established on the same day are restricted from being sold before the next trading day, and calculating the transaction fees and realized profits and losses. The equity valuation module is used to calculate the total equity of the account based on the market data of the remaining positions at the end of each trading day after the execution of the trades. The results output module is used to generate backtesting results based on the total equity of the account.

10. A computer platform comprising a memory and a processor, wherein the memory stores a computer program, characterized in that, When the processor executes the computer program, it implements the steps of the method according to any one of claims 1 to 8.