0bps

Strategy vault / Trend following

Consecutive Up Down N Days Strategy

Consecutive Up/Down N Days Strategy This article will introduce in detail the trading logic, pros, potential risks and summary of the Consecutive Up/Down N Days strategy. This is a long-only strategy that determines…

Pine ScriptTrend following
Author
ChaoZhang
Complexity
Starter · 45 lines
Parameters
6
Backtest bar
12h · swing
Tested on
BTC/USDT · futures

Consecutive Up/Down N Days Strategy

This article will introduce in detail the trading logic, pros, potential risks and summary of the Consecutive Up/Down N Days strategy.

This is a long-only strategy that determines entries and exits based on user-defined consecutive up days and consecutive down days. The trading logic is very straightforward.

Strategy Logic

Firstly, we need to set two parameters:

consecutiveBarsUp: consecutive up days consecutiveBarsDown: consecutive down days

Then we record two variables:

ups: current consecutive up days dns: current consecutive down days

Each day we compare the close price with previous close to determine if it's an up day or down day. If up, ups + 1, if down, dns + 1.

When ups reaches consecutiveBarsUp, we go long. When dns reaches consecutiveBarsDown, we exit positions.

That's the simple logic for a consecutive up/down strategy. We only go long after consecutive up days from bottom. And exit after consecutive down days. This avoids frequent trading in range-bound markets.

Pros

  1. Simple logic, easy to understand and implement

  2. Filtering short-term fluctuations by the consecutive days setting

  3. Long only, less trades, lower transaction costs and slippage impact

  4. Easy to set stop loss, effectively control single trade loss

Potential Risks

  1. Unable to short tops, missing shorting opportunities

  2. Need consecutive up days to enter, possibly missing best entry point

  3. Time lag, not capturing turns in real time

  4. Large single trade loss without stop loss

Summary

The consecutive up/down days strategy is widely popular for its simplicity and low frequency trading. With proper parameter tuning, it can filter out whipsaws effectively. But it also has limitations like time lag and inability to short. Investors need to consider carefully before adopting. Overall it suits investors seeking stable returns when tracking medium-long term trends.

Parameters

NameDefaultWhat it controls
v_input_1trueconsecutiveBarsUp
v_input_2trueconsecutiveBarsDown
v_input_3timestamp(2021-01-01T00:00:00)startDate
v_input_4timestamp(2021-12-31T00:00:00)finishDate
v_input_5{{strategy.order.alert_message}}Buy message
v_input_6{{strategy.order.alert_message}}Sell message

Source Pine Script

/*backtest
start: 2023-08-12 00:00:00
end: 2023-09-11 00:00:00
period: 12h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=4

// Strategy
// strategy("Up/Down Long Strategy", overlay=true, initial_capital = 10000, default_qty_value = 10000, default_qty_type = strategy.cash)

// There will be no short entries, only exits from long.
// strategy.risk.allow_entry_in(strategy.direction.long)

consecutiveBarsUp = input(1)
consecutiveBarsDown = input(1)

price = close

ups = 0.0
ups := price > price[1] ? nz(ups[1]) + 1 : 0

dns = 0.0
dns := price < price[1] ? nz(dns[1]) + 1 : 0

// Strategy Backesting
startDate  = input(timestamp("2021-01-01T00:00:00"), type = input.time)
finishDate = input(timestamp("2021-12-31T00:00:00"), type = input.time)

time_cond  = true

// Messages for buy and sell
message_buy  = input("{{strategy.order.alert_message}}", title="Buy message")
message_sell = input("{{strategy.order.alert_message}}", title="Sell message")

// Strategy Execution

if (ups >= consecutiveBarsUp) and time_cond
    strategy.entry("Long", strategy.long, stop = high + syminfo.mintick, alert_message = message_buy)
    
if (dns >= consecutiveBarsDown) and time_cond
    strategy.entry("Short", strategy.short, stop = low + syminfo.mintick, alert_message = message_sell)

Published by ChaoZhang on the FMZ strategy square · last modified 2023-09-12. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.

Build this with AgenKit

A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.

in Claude Code, Cursor, Codex & 4 more
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/Consecutive-Up-Down-N-Days-Strategy.md
  1. npx agenkit install engineering-kit in your project
  2. Paste the command into your agent and approve each phase
  3. Get tests, backtest, risk controls and a paper-trading switch

Understand it first

Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.

Related specs

esc

Try , , or .

↑↓ move↵ open