0bps

Strategy vault / Machine learning

N Bar Close Below Open Short Strategy

This strategy identifies short-term trend based on technical indictors and takes short position when detecting N consecutive bar closing below opening price. It is an intraday trading strategy.

Author
ChaoZhang
Complexity
Starter · 38 lines
Parameters
3
Backtest bar
30m · intraday
Tested on
BTC/USDT · futures

IMG

Overview

This strategy identifies short-term trend based on technical indictors and takes short position when detecting N consecutive bar closing below opening price. It is an intraday trading strategy.

Strategy Logic

The strategy uses a nCounter variable to count the number of consecutive bar with close below open. When close price is lower than open price, nCounter increments by 1. When close price is higher than open price, nCounter resets to 0. When nCounter reaches the input parameter nLength, it indicates N consecutive bars closing below opening price and the signal C2 becomes 1.

Upon signal, if there is no position, a short order will be sent. If already in short position, keep holding the position. After opening position, posprice records the entry price. Take profit and stop loss are set based on entry price: if price reaches take profit point (entry + input takeprofit), close position and reset; if price reaches stop loss point (entry - input stoploss), close position and reset.

Advantage Analysis

The main advantages of this strategy:

  1. Simple and clear logic, easy to understand and implement.
  2. Customizable parameters, flexible across different market conditions.
  3. Equipped with take profit and stop loss, effectively control risks.

Risk Analysis

The main risks of this strategy:

  1. N bar close below open cannot fully confirm trend reversal, false signal may occur. Fine tune N value or add other filters for verification.
  2. Improper stop loss or take profit setting may lead to premature exit or amplified losses. Reasonable parameters should be set according to market volatility.

Optimization Directions

The strategy can be improved from the following aspects:

  1. Add trend filter to avoid misjudging short-term corrections in sideways market. For example, combine with moving average to determine overall trend.

  2. Add volume confirmation. Surging volume can better confirm trend reversal.

  3. Optimize take profit and stop loss, such as using trailing stop loss, percentage stop loss to make more intelligent exits.

  4. Utilize machine learning models to dynamically adjust parameters like nLength according to real-time market changes.

Conclusion

This strategy identifies short-term trend simply based on the relationship between close price and open price. Trading signals are generated when detecting N consecutive bars closing below opening price. The strategy is intuitive, customizable and equipped with effective risk management. However, certain level of false signals exist. It is recommended to combine additional filters for optimization. With parameter tuning, risk management and model enhancement, this can be a very practical tool for short-term trading.

Parameters

NameDefaultWhat it controls
v_input_14nLength
v_input_220Take Profit pip
v_input_310Stop Loss pip

Source Pine Script

/*backtest
start: 2023-12-18 00:00:00
end: 2023-12-25 00:00:00
period: 30m
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=4
////////////////////////////////////////////////////////////
//  Copyright by HPotter v1.0 05/02/2020
// Evaluates for n number of consecutive lower closes. Returns a value 
// of 1 when the condition is true or 0 when false.
//
// WARNING:
// - For purpose educate only
// - This script to change bars colors.
////////////////////////////////////////////////////////////
strategy(title="N Bars Down", shorttitle="NBD Backtest", overlay = false) 
nLength = input(4, minval=1)
input_takeprofit = input(20, title="Take Profit pip", step=0.01)
input_stoploss = input(10, title="Stop Loss pip", step=0.01)
nCounter = 0
nCounter := iff(close[1] <= open[1], nz(nCounter[1],0)+1,
             iff(close[1] > open[1], 0, nCounter))
C2 = iff(nCounter >= nLength, 1, 0)
posprice = 0.0
pos = 0
barcolor(nz(pos[1], 0) == -1 ? color.red: nz(pos[1], 0) == 1 ? color.green : color.blue ) 
posprice := iff(C2== 1, close, nz(posprice[1], 0)) 
pos := iff(posprice > 0, -1, 0)
if (pos == 0) 
    strategy.close_all()
if (pos == -1)
    strategy.entry("Short", strategy.short)
posprice := iff(low <= posprice - input_takeprofit and posprice > 0, 0 ,  nz(posprice, 0))
posprice := iff(high >= posprice + input_stoploss and posprice > 0, 0 ,  nz(posprice, 0))
plot(C2, title='NBD', color=color.red)

Published by ChaoZhang on the FMZ strategy square · last modified 2023-12-26. 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/N-Bar-Close-Below-Open-Short-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