Strategy · Pine Script v5 · On the chart

Markov Strategy

by Rohanom

Markov Strategy is a mean-reversion quantitative strategy inspired by streak-based Markov chain analysis.

View on GitHub

GitHub stars9on the repository
Pine versionv5runs as is
Lines of code13
Settings0inputs
LicenceMIT
Last update1 year ago21 Jun 2025

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

13 lines of Pine Script v5 from Updated V5 Pinescript Code.txt, published under MIT. Keep the author's credit and licence lines when you reuse it.

Updated V5 Pinescript Code.txt
//@version=5
strategy("Markov Strategy", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100)

// Define a 6-day consecutive downtrend condition
condition = close < close[1] and close[1] < close[2] and close[2] < close[3] and close[3] < close[4] and close[4] < close[5] and close[5] < close[6]

// Entry: Go long if 6 red candles in a row
if condition
    strategy.entry("Long", strategy.long)

// Exit: Close long if today's close is higher than yesterday's close
if strategy.position_size > 0 and close > close[1]
    strategy.close("Long")

Overview

Applied directly to the price chart, it monitors price action for consecutive downward closes. The strategy enters a long position after detecting six consecutive lower closes and exits the trade as soon as a candle closes above the preceding bar. It operates without customizable inputs or a default stop loss.

Details

Published by

Rohanom on GitHub

Type

Strategy, Pine Script v5

Draws

On the price chart

Category

Mean reversion, Candlestick patterns

Markets

Stocks, Indices

Timeframes

H4 and above, M15-H1

Risk Notes

  • The strategy enters trades without a stop loss

Topics

alphamarkov-chainmarkov-modelpinescriptquantitative-financequantitative-tradingstock-price-predictionyfinance

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

330 stars

Exceeded Candle [Alorse] is a mean-reversion strategy that identifies candlestick engulfing patterns combined with Bollinger Bands filters. It enters a long position when a green candle surpasses the body of the previous red candle while price is below the mid

Strategy v4 Mean reversionCandlestick patterns
Updated 2 months ago · None stated GitHub repository of Exceeded candle [Alorse]
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

STRG One Bar Pursuit

by harryguiacorn
219 stars

The STRG One Bar Pursuit strategy identifies single-bar momentum reversals by monitoring shifts between bullish and bearish candlesticks starting from the year 2024. It enters a long position when a bullish candle follows a bearish candle and enters a short po

Strategy v5 Candlestick patternsMean reversion
Updated 1 year ago · None stated GitHub repository of STRG One Bar Pursuit

A Strategy for Leledec

by hasnocool
106 stars

This Pine Script strategy identifies major and minor Leledec exhaustion bars by tracking consecutive price bar comparisons against past closes and checking local extremes over set lookback lengths. It overlays visual markers directly on the price chart for det

Strategy v4 Mean reversionCandlestick patterns
Updated 2 years ago · GPL-3.0 GitHub repository of A Strategy for Leledec

BodyOutlier A003

by ruckard
13 stars

BodyOutlier A003 is a mean-reversion trading strategy designed for automated execution via Zignaly alerts. It identifies candlestick body outliers to place limit orders, buying at the low of unusually large bullish candles or selling at the high of large beari

Strategy v5 Candlestick patternsMean reversion
Updated 1 year ago · None stated GitHub repository of BodyOutlier A003

This strategy buys after a sharp down bar with a long lower wick. It opens one long when the lower wick exceeds the candle body by the pin-bar ratio and the one-bar close change falls past the decline threshold. Size defaults to the full account equity. While

Strategy v6 Candlestick patternsMean reversion Repaints
Updated 10 months ago · Apache-2.0 GitHub repository of youtube布欧量化策略-接针策略

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