Momentum roc buy sell
Momentum flags: buy while the price is up over the lookback, sell while it is down.
Each side compares the close with the close the period bars earlier and keeps that rate of change only when it clears a threshold: buy when it is above the up threshold, sell when it is below minus the down threshold. Both thresholds default to zero, so with defaults the two sides cover almost every bar in turn: buy when the lookback rose, sell when it fell. The code has no position, stop or exit logic, so the names are the only trade meaning.
How to read it
- The buy line sits in its own pane below the price and draws only on bars where the lookback return clears the up threshold; a gap means buy is off, not zero.
- Its values are fractions: 0.00125 means the close is up 0.125% over the lookback, so read its height against the reference lines.
- Sell markers sit on candles where the lookback return is below minus the down threshold; at defaults they cover about half the candles, so dense markers mean a falling lookback, not a sell decision.
What it outputs
- Buy
- The rate of change over the period, drawn only on bars where it is above the up threshold (default 0). With confirm_days above 1 it becomes one flag on the bar a run reaches that length.
- Sell
- Marks each bar where the rate of change is below minus the down threshold (default 0, so any fall). With confirm_days above 1, marks only the bar a run reaches that length.
When it fires
Sell fires on each bar where the close is more than the down threshold below the close the period bars earlier, which at defaults means any lower close over that lookback. With confirm_days above 1, it fires once per run, on the bar the run reaches that length.
Settings
- period
- Bars back the close is compared with. Longer periods measure slower moves, so the buy line gets larger values and the sides flip less often; shorter periods flip on small wiggles and fire far more often.
- up
- Threshold the buy side must clear. At 0 buy is on for any rise. Raising it, such as 0.001, keeps only stronger rises and makes the buy line sparse; a negative value also counts small dips as buy, so both sides can be on at once.
- down
- Size of the fall the sell side must exceed. Only its size counts, so a negative value behaves like the positive one. At 0 any fall marks sell; raising it keeps only deeper falls and thins the sell markers.
- confirm_days
- Bars in a row a side must hold before it fires. At 1 every qualifying bar counts. Above 1, each side fires once per run, on the bar the run reaches that length, and the buy line turns into a single flag.
Pitfalls
- The names suggest a trade rule, but there is no position, stop or exit logic: buy and sell are the sign of the lookback return checked against two thresholds.
- At defaults the sides take turns on almost every bar, and sell starts about 75 separate runs per day in the SPY sample, so the chart fills with sell markers.
- The sides draw differently: buy is a pane line, sell is markers. Strategies read a side as on wherever its value is present, so the gaps in the buy line are its off bars.