Strategy · Pine Script v6 · On the chart

While Loop Counter

by Snappni

This strategy sums the last len closing prices using a while loop then divides by len to obtain an average.

View on GitHub

GitHub stars1on the repository
Pine versionv6current
Lines of code26
Settings1inputs
LicenceMIT
Last update4 months ago6 Jun 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 strategy, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart. The trades and results appear in the Strategy Tester tab; adjust the settings with the gear icon.

Source Code

26 lines of Pine Script v6 from apps/server/src/pine-corpus/while_loop_counter.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.

while_loop_counter.pine
//@version=6
strategy("While Loop Counter", overlay=true)

len = input.int(4, "Len")

f_sum_last(_len) =>
    total = 0.0
    idx = 0
    while idx < _len
        total += nz(close[idx], 0)
        idx += 1
    total

score = f_sum_last(len)
avg = score / len

longCondition = close > avg
shortCondition = close < avg

if longCondition
    strategy.entry("Long", strategy.long)
else if shortCondition
    strategy.entry("Short", strategy.short)
else
    strategy.close("Long")
    strategy.close("Short")

Overview

While Loop Counter enters long when close exceeds the average and short when below. Positions close automatically on reversal. No stop loss is applied and entries occur on every bar.

Details

Published by

Snappni on GitHub

Type

Strategy, Pine Script v6

Draws

On the price chart

Category

Mean reversion, Trend, Oscillators & momentum

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

  • The strategy enters trades without a stop loss

Settings

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

SettingTypeDefaultVariable
Lenint4len
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

285 stars

Mean Reversion Strategy is an overlay strategy that identifies pullbacks using a long-term EMA, RSI, and candlestick patterns. It generates long signals when a bullish engulfing candle forms below the EMA while RSI is oversold, and short signals when a bearish

Strategy v4 Mean reversionCandlestick patterns
Updated 3 years ago · None stated GitHub repository of Mean Reversion Strategy

ADX + RSI Strat

by hasnocool
106 stars

This strategy combines a 7-period RSI with an ADX filter to trade market regimes on the price chart. When the ADX indicates ranging conditions below a set threshold, it triggers entries on RSI overbought or oversold extremes. By default, the entry logic is inv

Strategy v4 Oscillators & momentumTrend
Updated 2 years ago · GPL-3.0 GitHub repository of ADX + RSI Strat

ADX Trend Filter

by latentwill
4 stars

This strategy enters long when a series labeled ADX is above a threshold and RSI is below 40, and enters short when that series is above the threshold and RSI is above 60. Both series are calculated with the RSI function, so the trend filter is not a true ADX.

Strategy v5 TrendOscillators & momentum
Updated 6 days ago · None stated GitHub repository of ADX Trend Filter
4 stars

This strategy trades pullbacks in the direction of a moving-average trend and draws on the price chart. An uptrend requires the 50-period EMA above the 200-period EMA with price above the slower average, and a downtrend is the reverse. It enters long or short

Strategy v6 TrendOscillators & momentum

More strategys

Trade Your Charts With a Regulated Broker

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

Compare brokers