Tags:

The core logic of this strategy is to detect whether the closing price of N consecutive candles keeps rising. If so, go long; otherwise, close position. This can capture the uptrend of stock price and make profit.

The core indicator of this strategy is nCounter. It compares the closing price and opening price of current candle to judge whether the price rises.

Specifically, if close[1]>=open[1], nCounter adds 1, indicating rise; if close[1]<open[1], nCounter resets to 0. Thus it can count the number of consecutive rising candles.

Then compare nCounter with parameter nLength. When nCounter>=nLength, output signal C1=1; otherwise C1=0. Here nLength is the number of consecutive rising candles we defined to generate signal.

After receiving C1=1 signal, if there is no current position, go long; if already in long position, keep holding.

In addition, this strategy sets stop loss and take profit conditions. If price falls below entry price by certain percentage, stop loss exits position; if rises above entry price by certain percentage, take profit.

This is a typical trend following strategy with below strengths:

- It can seize the uptrend opportunities of stock price, suitable as long strategy
- N consecutive rises as entry signal can effectively filter false breakout and reduce unnecessary trades
- Setting stop loss and take profit can limit downside risk and lock in profits
- The logic is simple and clear, easy to understand and modify
- Trading frequency can be controlled by adjusting nLength parameter

There are some risks of this strategy, mainly in below aspects:

- If uptrend reverses, failure to stop loss in time may lead to huge loss
- If nLength set too large, good entry opportunities may be missed
- It does not consider market environment. Holding long position when market crashes can lead to extra loss
- Using unified parameters without adjusting based on different stock characteristics may not apply to some stocks

To reduce these risks, we can set more strict stop loss, optimize nLength, add market condition rules, or test parameters separately for different stocks. Of course no strategy can completely avoid losses. It needs to match risk appetite of traders.

Considering above risks, we can optimize the strategy from below aspects:

- Add moving stop loss or trailing stop loss functions. They can adjust stop loss point accordingly based on price change to reduce loss risk
- Optimize nLength parameter. Respectively test for different types of stocks to find out more suitable parameter values
- Add market environment judgement. For example pause trading when market crashes to avoid extra losses in bear market
- Add other factors like volume as auxiliary conditions. For example require enlarged volume during uptrend to ensure breakout validity
- Set drawdown control like max allowed loss percentage, max consecutive loss times etc. to stop loss automatically controlling overall loss

This strategy captures uptrend by detecting N consecutive rising candles. It can effectively conduct trend following. The advantages are simple logic, flexible parameter tuning, filtering false breakout. But there are also some risks. It needs to add modules like stop loss, parameter optimization, environment judgement to improve. Overall speaking, this strategy provides a valuable basic model for quantitative trading. It can become a powerful trading tool after continuous improvement.

/*backtest start: 2023-01-01 00:00:00 end: 2023-12-07 00:00:00 period: 1d basePeriod: 1h exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}] */ //@version=4 //////////////////////////////////////////////////////////// // Copyright by HPotter v1.0 05/02/2020 // Evaluates for n number of consecutive higher 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 Up", shorttitle="NBU 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)) C1 = 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(C1== 1, close, nz(posprice[1], 0)) pos := iff(posprice > 0, 1, 0) if (pos == 0) strategy.close_all() if (pos == 1) strategy.entry("Long", strategy.long) posprice := iff(low <= posprice - input_stoploss and posprice > 0, 0 , nz(posprice, 0)) posprice := iff(high >= posprice + input_takeprofit and posprice > 0, 0 , nz(posprice, 0)) plot(C1, title='NBU', color=color.green)

- Moving Average Bollinger Bands Breakout Strategy
- Trend Following Grid Strategy
- Quantitative Trading Strategy Integrating Reversal and Future Lines of Demarcation
- Crossing Strategy Between Bollinger Bands and Hull Indicator
- Turtle Breakout Drawdown Adaptive Trading Strategy
- RSI Trend Following Strategy with Trailing Stop Loss
- Reversal Dynamic Pivot Points Exponential Moving Average Strategy
- kNN-based Trend Following Strategy
- Simple Momentum Strategy Based on SMA, EMA and Volume
- Donchian Channels Breakout Quantitative Trading Strategy
- Smart Quantitative Bottom Reversal Trading Strategy
- Bollinger + RSI Double Strategy (Long Only) v1.2
- CCI Zero Cross Trading Strategy
- Dual Moving Average Price Reversal Breakout Strategy
- Moving Average Pullback Trading Strategy
- Moving Average Aggregation Williams Commercial Bid-Ask Pressure Indicator Strategy
- Dual Moving Average Reversal Tracking Strategy
- Moving Average Aggregation MACD Strategy
- EMAskeletonstrategy
- Quantitative Trading Strategy Based on Random Numbers