Skip to content

Learn

Stock Strategy Development

A long-only US stock residual reversal research strategy

A long-only residual reversal strategy buys a stock after weakness that remains after a declared market model. It does not short the market or create a market-neutral spread. The regression supplies an explanatory signal; the account still holds cash or positive stock inventory and remains exposed to losses. A negative residual is a model observation, not proof that the price must recover.

This guide implements a lagged twenty-observation regression, a five-residual entry condition, recovery and maximum-holding exits, and complete synthetic accounts. CoTrading’s ability to run this exact regression and execution contract remains planned or unverified. The supplied returns and opening prices are controlled fixtures, not a recovered historical stock and benchmark dataset.

Specify aligned return inputs

At close i, supply a stock return and a market return for the same completed interval. Use consistent units, corporate-action adjustments and session alignment. The market is an explanatory input only. The example never places a market order, sells a stock short or subtracts a benchmark portfolio from account equity. Calling the result market-neutral would therefore misdescribe its holdings.

For real work, record the stock identifier, benchmark construction, return definition, adjustment vintage and data timestamp. A missing market observation cannot be replaced with zero merely because the stock traded. A delisted or suspended stock needs an explicit policy rather than disappearance from the panel. These input decisions can change residuals as much as the regression formula.

The synthetic opening-price fixture is separately supplied for execution and cash arithmetic. It is not computed by compounding the signal-return array. That separation lets the tests control signal formation and fill prices independently; it must not be presented as an actual reconstructed price history. Historical research would require both inputs to reconcile to a coherent market dataset.

Fit using only the preceding twenty observations

For observation i, fit stock return y against market return x using indices i minus twenty through i minus one, with an intercept. Compute the means, the sum of squared deviations of x, beta as covariance numerator divided by that variance numerator, and alpha as mean y minus beta times mean x. Then calculate the current residual as current y minus alpha minus beta times current x.

The current return helps form the residual but does not enter its own coefficient estimate. This distinction prevents a large current surprise from immediately changing the model used to measure that surprise. The Python statistics documentation provides the linear-regression reference; this script implements the short formula directly so its window endpoints remain visible.

If the preceding market values have zero variance, beta is unidentified. The output records unidentified_market_variance, leaves the residual unknown and does not fabricate beta zero. Non-finite or missing inputs similarly leave an invalid observation. The first twenty rows are warmup. A five-residual sum exists only when five consecutive current residuals are known; unknown is distinct from a calculated zero.

The fixture uses exact rational values through Python Fraction. This avoids a floating approximation changing a strict threshold in the hand-check. It does not solve historical measurement errors or promise that financial regressions generally need rational arithmetic. Production implementations may use numerical arrays with documented tolerances and matched endpoint tests.

Separate entry, recovery and expiry

Enter only while flat when the arithmetic sum of the latest five residuals is strictly less than negative 0.06. The decision is made after the current close and creates a request for the next provided opening. Buy forty shares if the cash can cover their price and one unit of commission. The fixture raises an error for insufficient cash instead of silently borrowing or shrinking the quantity.

While holding, a five-residual sum greater than or equal to zero requests an exit at the next opening. Independently, ten opening-to-opening intervals after entry force a maximum-holding exit. Recovery is an observed close condition; expiry is a schedule known from the entry. The account processes a pending exit at the opening before examining that day’s completed return.

The strategy has one position and no pyramiding. Positive residuals do not request short sales. A missing residual while holding does not invent recovery; the maximum-holding rule still supplies a risk boundary in this fixture. Real blocked openings would require an additional pending-exit and valuation contract. The demonstration supplies executable positive finite prices and rejects invalid ones.

Reproduce the recovery trade

The market alternates negative and positive 0.01 returns for forty observations. Stock returns initially equal 1.5 times the market. Observations twenty through twenty-four add negative 0.02, and observations twenty-five through twenty-nine add positive 0.03. Every model window remains lagged; later fitted coefficients therefore change as the earlier disturbances enter their training windows.

At observation twenty the fitted alpha is zero, beta is three halves and the residual is negative one fiftieth. At observation twenty-four, the five-residual sum is negative twenty-three over 250, or negative 0.092. It crosses the strict negative 0.06 entry boundary. At opening twenty-five, forty shares at 10 plus one fee cost 401, leaving 599 cash from the initial 1,000.

At close twenty-six the five-residual sum is nine over 500, or 0.018, so recovery requests an exit. Opening twenty-seven sells forty at 10.5, receiving 419 after one fee. Final cash and equity are 1,018, inventory zero and total fees two. The cash bridge is 1,000 minus 401 plus 419, and the price gain of twenty is reduced by two commissions.

The output asserts exact coefficient, residual, signal, buy and sell rows. A statement that the stock eventually rose would be insufficient: shifting either order by a session could change the mechanism even if the final account happened to match. Printed row indices make the relationship between the close decision and later opening observable.

Test different causes, not just a second profitable path

In the common-market control, the market falls for five observations and the stock always equals 1.5 times that market return. Residuals remain zero, no order appears and cash stays 1,000. This demonstrates why a raw price decline is not automatically a residual-reversal entry. The distinction comes from the explanatory model, not from using a different profit target.

In a persistent negative-disturbance control, entry still occurs at opening twenty-five but recovery does not release the position in time. The maximum-holding exit sells at opening thirty-five for 9.5. The forty-share account ends at 978 after two fees. This losing control tests the independent clock rather than merely changing the successful trade’s exit price.

A constant-market control produces unidentified variance and no valid five-residual entry. Running only the first twenty-six observations reproduces exactly the corresponding prefix of the full ledger. Changing stock return thirty to an extreme future value leaves all indicators through twenty-five unchanged. These tests target model identifiability and future-data boundaries separately from cash accounting.

Run the complete fixture

Download the complete example and run python R592.py using Python 3.12 or newer. It uses only the standard library, exact synthetic return fractions and synthetic execution openings. Output includes the first fit, trigger, entry, recovery exit, maximum-holding control and final accounts. No network, historical-price service or broker is involved.

Before using real observations, compare independently calculated coefficients for selected dates, audit window membership and confirm next-opening alignment. Freeze any changes to the market benchmark or return adjustments as new experiments. A passing synthetic account validates the specified arithmetic and sequencing; it does not establish tradable historical alpha, risk neutrality or verified CoTrading support.

Export model and account decisions separately

Keep the coefficient window, current residual, five-residual sum, pending action and eventual order on separate columns. At twenty-four the decision exists but stock inventory is still zero; at twenty-five it becomes forty shares. Recovery at twenty-six likewise precedes its sale at twenty-seven. This trace distinguishes a changed model from an order-alignment error. When a real-data version differs, compare the first unequal return and fitted window before changing the threshold to recover a preferred trade.

FAQ

Is the benchmark sold short?

No. It appears only in the regression. The account trades positive stock inventory and cash, so its economic exposure is long-only.

Why can the maximum-holding control lose money?

Residual weakness can persist. The ten-interval exit limits this fixture’s holding duration but does not guarantee a favorable price or a positive net return.