Indicator · Pine Script v6 · On the chart

Candle Counter + MA + RSI + Mode

by Ahmed-GoCode· code credited to Ahmed - Quant-Edge Indicators

This indicator analyzes historical candle direction and price range over a configurable lookback window to gauge market trend.

View on GitHub

GitHub stars53on the repository
Pine versionv6current
Lines of code88
Settings9inputs
LicenceMPL-2.0
Last update2 months ago17 Aug 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

88 lines of Pine Script v6 from Quant-Edge-Indicators/Custom Indicators/Custom Indicator.pinescript, published under MPL-2.0. Keep the author's credit and licence lines when you reuse it.

Custom Indicator.pinescript
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Ahmed - Quant-Edge Indicators
//@version=6
indicator("Candle Counter + MA + RSI + Mode", overlay=true)

// Configurable Parameters
candle_count   = input.int(20, title="Number of Candles to Analyze")
trend_mode     = input.string(title="Trend Mode Definition", defval="Candles and Range", options=["Candles Only", "Range Only", "Candles and Range"])
use_ma         = input.bool(true, title="Use Moving Average Filter")
use_rsi        = input.bool(true, title="Use RSI Filter")
ma_len         = input.int(50, title="Moving Average Period")
rsi_len        = input.int(14, title="RSI Period")
overbought     = input.int(70, title="RSI Overbought Level")
oversold       = input.int(30, title="RSI Oversold Level")
panel_pos      = input.string(title="Panel Position", defval="Top Right", options=["Top Left", "Top Right", "Bottom Left", "Bottom Right"])

// Moving Average and RSI Calculation
ma        = ta.sma(close, ma_len)
rsi_val   = ta.rsi(close, rsi_len)

// Counter Initialization
var int bull_candles_count = 0
var int bear_candles_count = 0
var float bull_range_sum   = 0.0
var float bear_range_sum   = 0.0

if barstate.isconfirmed and bar_index >= candle_count
    bull_candles_count := 0
    bear_candles_count := 0
    bull_range_sum     := 0.0
    bear_range_sum     := 0.0
    for i = 1 to candle_count
        if not na(close[i])
            if close[i] > open[i]
                bull_candles_count += 1
                bull_range_sum     += high[i] - low[i]
            else if close[i] < open[i]
                bear_candles_count += 1
                bear_range_sum     += high[i] - low[i]

// Basic Trend Determination Based on Chosen Mode
bull_trend_candles = bull_candles_count > bear_candles_count
bear_trend_candles = bear_candles_count > bull_candles_count
bull_trend_range   = bull_range_sum > bear_range_sum
bear_trend_range   = bear_range_sum > bull_range_sum

bull_trend = (trend_mode == "Candles Only" and bull_trend_candles) or (trend_mode == "Range Only" and bull_trend_range) or (trend_mode == "Candles and Range" and bull_trend_candles and bull_trend_range)
bear_trend = (trend_mode == "Candles Only" and bear_trend_candles) or (trend_mode == "Range Only" and bear_trend_range) or (trend_mode == "Candles and Range" and bear_trend_candles and bear_trend_range)

// Additional Validations
ma_rising  = ma > ma[1]
ma_falling = ma < ma[1]
rsi_extreme = (rsi_val > overbought) or (rsi_val < oversold)

valid_ma_bull = use_ma ? (close > ma and ma_rising) : true
valid_ma_bear = use_ma ? (close < ma and ma_falling) : true
valid_rsi     = use_rsi ? not rsi_extreme : true

// Final Validated Trend Condition
bull_trend_valid = bull_trend and valid_ma_bull and valid_rsi
bear_trend_valid = bear_trend and valid_ma_bear and valid_rsi

// Chart Background Highlight
bgcolor(bull_trend_valid ? color.new(color.green, 85) : bear_trend_valid ? color.new(color.red, 85) : na)

// Plot Moving Average
plot(use_ma ? ma : na, title="Moving Average", color=color.orange)

// Display Signal Labels
plotshape(bull_trend_valid, title="Bullish Trend", style=shape.labelup, location=location.belowbar, color=color.green, size=size.small)
plotshape(bear_trend_valid, title="Bearish Trend", style=shape.labeldown, location=location.abovebar, color=color.red, size=size.small)

// Create Info Panel
table_pos = panel_pos == "Top Left" ? position.top_left : panel_pos == "Top Right" ? position.top_right : panel_pos == "Bottom Left" ? position.bottom_left : position.bottom_right
var table panel = table.new(position=table_pos, columns=1, rows=10, bgcolor=color.black, frame_color=color.gray, frame_width=1)

if barstate.islastconfirmedhistory
    table.clear(panel, start_column=0, start_row=0)
    table.cell(panel, 0, 0, text="Mode: " + trend_mode, text_color=color.white)
    table.cell(panel, 0, 1, text="MA Active: " + (use_ma ? "Yes" : "No"), text_color=color.white)
    table.cell(panel, 0, 2, text="RSI Active: " + (use_rsi ? "Yes" : "No"), text_color=color.white)
    table.cell(panel, 0, 3, text="Bullish Candles: " + str.tostring(bull_candles_count), text_color=color.lime)
    table.cell(panel, 0, 4, text="Bearish Candles: " + str.tostring(bear_candles_count), text_color=color.red)
    table.cell(panel, 0, 5, text="Bullish Range: " + str.tostring(bull_range_sum, "#.##"), text_color=color.lime)
    table.cell(panel, 0, 6, text="Bearish Range: " + str.tostring(bear_range_sum, "#.##"), text_color=color.red)
    table.cell(panel, 0, 7, text="RSI Value: " + str.tostring(rsi_val, "#.##"), text_color=color.yellow)
    table.cell(panel, 0, 8, text="Base Trend: " + (bull_trend ? "BULLISH" : (bear_trend ? "BEARISH" : "UNDEFINED")), text_color=color.white)
    table.cell(panel, 0, 9, text="Validated Trend: " + (bull_trend_valid ? "BULLISH" : (bear_trend_valid ? "BEARISH" : "NOT VALIDATED")), text_color=color.white, bgcolor=bull_trend_valid ? color.green : (bear_trend_valid ? color.red : color.gray))

Overview

Candle Counter + MA + RSI + Mode categorizes trend direction using candle count, price range sum, or both. Signals are filtered using a Simple Moving Average to ensure price aligns with trend direction and slope, alongside an RSI filter to avoid overbought or oversold extremes. The indicator plots a moving average line, displays trend labels, highlights the chart background, and renders an information dashboard panel.

Details

Published by

Ahmed-GoCode on GitHub

Code credit

Ahmed - Quant-Edge Indicators, as written in the script's header

Type

Indicator, Pine Script v6

Draws

On the price chart

Category

Trend, Oscillators & momentum, Moving averages, Screeners & dashboards

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Topics

fair-value-gapfibonaccimarket-structurepine-scriptrsi-strategysmart-money-conceptstechnical-analysistradingviewalgorithmic-tradingcrypto-trading

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Number of Candles to Analyzeint20candle_count
Trend Mode DefinitionstringCandles and Rangetrend_mode
Use Moving Average Filterbooltrueuse_ma
Use RSI Filterbooltrueuse_rsi
Moving Average Periodint50ma_len
RSI Periodint14rsi_len
RSI Overbought Levelint70overbought
RSI Oversold Levelint30oversold
Panel PositionstringTop Rightpanel_pos
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

218 stars

TradersPost Trading Checklist is an overlay indicator that evaluates a suite of popular technical conditions for long and short trade setups. It checks session-anchored VWAP, fast and slow EMA alignments and crosses, 200 SMA, RSI overbought or oversold conditi

Indicator v6 Screeners & dashboardsTrend
Updated 3 months ago · None stated GitHub repository of TradersPost Trading Checklist

Pocket Scout - SPY/QQQ

by chris-c-thomas
14 stars

Pocket Scout - SPY/QQQ is a mobile-optimized intraday bias indicator designed for major US equity ETFs and indices. It overlays a 9/21/50 EMA ribbon, session VWAP, prior day high/low/close levels, and intraday high/low lines directly on the main price chart. T

Indicator v6 TrendOscillators & momentum Repaints
Updated 6 months ago · MIT GitHub repository of Pocket Scout - SPY/QQQ

Portfolio Backtester

by f-cksociety
11 stars

The script is a Pine Script v5 indicator that emulates strategy backtesting directly on the price chart. It evaluates modular entry strategies, trade directions, entry stop-loss types such as ATR multiples or fixed percentages, and a wide variety of trade filt

Indicator v5 TrendOscillators & momentum
Updated 4 years ago · MPL-2.0 GitHub repository of Portfolio Backtester

KamaFama

by Mastaaa1987
141 stars

KamaFama is an indicator that translates a Freqtrade trading strategy into Pine Script for visualization. It combines Kaufman Adaptive Moving Average (KAMA), MAMA/FAMA calculations derived via Hilbert Transform, Williams %R, and correlation trend filters to ev

Indicator v6 Moving averagesOscillators & momentum
Updated 6 weeks ago · MIT GitHub repository of KamaFama

MTF WaveTrend Opportunity Hunter calculates multi-timeframe momentum and confluence scores using WaveTrend across custom Regime, Setup, Trigger, and Context timeframes. It plots a net confluence score histogram, multi-layer heat ribbons, an RRG-style Rotation

Indicator v6 TrendOscillators & momentum
Updated yesterday · MIT GitHub repository of MTF WaveTrend Opportunity Hunter [WavesUnchained]
948 stars

The Adaptive RSI Moving Average (ARSIMA) is an overlay indicator that dynamically adjusts its smoothing factor based on the Relative Strength Index (RSI). When the RSI moves toward extreme overbought or oversold conditions, the smoothing factor increases to ma

Indicator v4 Moving averagesOscillators & momentum
Updated 10 days ago · GPL-3.0 GitHub repository of Adaptive RSI Moving Average

More indicators

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers