Strategy · Pine Script v5 · On the chart

MicroQuant Trend Strat +TS

by g-moe· code credited to goodgains

This strategy executes trades based on a modified Supertrend indicator combining exponential moving averages, price channels, and ATR bands.

View on GitHub

GitHub stars3on the repository
Pine versionv5runs as is
Lines of code138
Settings14inputs
LicenceNone statedfile: MPL-2.0
Last update5 months ago27 May 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

138 lines of Pine Script v5 from Tradingview/microquant-trend-strat-ts.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

microquant-trend-strat-ts.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © goodgains


//@version=5
strategy("MicroQuant Trend Strat +TS", overlay = true, max_labels_count = 500)

// @keywords: supertrend, microquant-trend, strategy-entry, strategy-exit, atr-trail-stop, breakeven-stop, take-profit, stop-loss, trading-session, trend-flip, stepline-trend, entry-fill, position-management, trail-atr, eod-exit, long-short, supertrend-signal, risk-reward, position-lines, trend-direction

price = input.source(close, title = 'Price')
length = input.int(21, title = 'Length')
atrPeriod = input.int(21, "ATR Length")
factor = input.float(4.0, "Factor", step = 0.1)


// strategy settings
use_sess    = input.bool(true, title = 'Trading Session', group = 'Trade Settings', inline = 'sess')
i_sess      = input.session("0700-1700", title = '', group = 'Trade Settings', inline = 'sess')

use_tp      = input.bool(true, title = 'TP', group = 'Trade Settings', inline = 'tp')
tp_amount   = input.float(20.00, title = '', group = 'Trade Settings', inline = 'tp')

use_sl      = input.bool(true, title = 'SL', group = 'Trade Settings', inline = 'sl')
sl_amount   = input.float(10.00, title = '', group = 'Trade Settings', inline = 'sl')

use_ts      = input.bool(true, title = 'Trail Stop ATR %', group = 'Trail Stop Settings', inline = 'ts')
ts_amount   = input.float(300.00, minval = 1.00, maxval = 1000, step = 50, title = '', group = 'Trail Stop Settings', inline = 'ts')

use_be      = input.bool(true, title = 'Breakeven Stop starts at $', group = 'Trail Stop Settings', inline = 'be')
start_be     = input.float(500, title = '', group = 'Trail Stop Settings', inline = 'be')


// The same on Pine Script™
pine_supertrend() =>
    atr = ta.sma(ta.tr, atrPeriod)
    lowerBand = ta.highest(ta.ema(high, length), length) - atr * factor
    upperBand = ta.lowest(ta.ema(low, length), length) + atr * factor
    prevLowerBand = nz(lowerBand[1])
    prevUpperBand = nz(upperBand[1])

    lowerBand := lowerBand > prevLowerBand or price[1] < prevLowerBand ? lowerBand : prevLowerBand
    upperBand := upperBand < prevUpperBand or price[1] > prevUpperBand ? upperBand : prevUpperBand
    int direction = na
    float superTrend = na
    prevSuperTrend = superTrend[1]
    if na(atr[1])
        direction := 1
    else if prevSuperTrend == prevUpperBand
        direction := price > upperBand ? -1 : 1
    else
        direction := price < lowerBand ? 1 : -1
    superTrend := direction == -1 ? lowerBand : upperBand
    [superTrend, direction]

[pineSupertrend, pineDirection] = pine_supertrend()
plot(pineDirection < 0 ? pineSupertrend : na, "Up direction", color = color.green, style=plot.style_steplinebr)
plot(pineDirection > 0 ? pineSupertrend : na, "Down direction", color = color.red, style=plot.style_steplinebr)


var bool tradersPostBuy = na
var bool tradersPostSell = na
tradersPostBuy  := pineDirection < 0 and pineDirection[1] >= 0
tradersPostSell := pineDirection > 0 and pineDirection[1] <= 0



// STRATEGY SIGNALS
var float tp_lvl = na
var float sl_lvl = na

var float trail_lvl = na
var float be_lvl    = na

var float final_sl_lvl = na

// position management

tp_lvl := not use_tp ? na
     :strategy.position_size > 0 ? strategy.position_avg_price + tp_amount
     : strategy.position_size < 0 ? strategy.position_avg_price - tp_amount
     : na

sl_lvl := not use_sl ? na
     : strategy.position_size > 0 ? strategy.position_avg_price - sl_amount
     : strategy.position_size < 0 ? strategy.position_avg_price + sl_amount
     : na

atr = ta.atr(atrPeriod)
atr_percent = atr*(ts_amount/100)
trail_lvl := not use_ts ? na
     : strategy.position_size > 0 and strategy.position_size[1] <= 0 ? strategy.position_avg_price - atr_percent
     : strategy.position_size < 0 and strategy.position_size[1] >= 0 ? strategy.position_avg_price + atr_percent
     : strategy.position_size > 0 ? math.max(close - atr_percent, trail_lvl[1])
     : strategy.position_size < 0 ? math.min(close + atr_percent, trail_lvl[1])
     : na

be_lvl := not use_be ? na
     : strategy.opentrades.max_runup(strategy.opentrades - 1) >= start_be ? strategy.position_avg_price
     : na





final_sl_lvl := strategy.position_size > 0 ? math.max(sl_lvl, nz(trail_lvl, -999999999), nz(be_lvl, -999999999))
     : strategy.position_size < 0 ? math.min(sl_lvl, nz(trail_lvl, 999999999), nz(be_lvl ,999999999))
     : na


tradeTime = time == time(timeframe.period, i_sess, "America/New_York") or not use_sess
if tradeTime
    // entry signals
    if tradersPostBuy
        strategy.entry("LE", strategy.long)
    if tradersPostSell
        strategy.entry("SS_E", strategy.short)

    if strategy.position_size > 0
        strategy.exit('LX', from_entry = 'LE', limit = tp_lvl, stop = final_sl_lvl, comment_profit = 'L_TP', comment_loss = 'L_SL')
    if strategy.position_size < 0
        strategy.exit('SS_X', from_entry = 'SS_E', limit = tp_lvl, stop = final_sl_lvl, comment_profit = 'SS_TP', comment_loss = 'SS_SL')
        
else
    strategy.close_all("EOD Exit", "EOD Exit", immediately = true)




// STYLING
p_entry = plot(strategy.position_avg_price, title = 'Entry', color = color.blue, style = plot.style_circles)
p_tp = plot(tp_lvl, title = 'TP', color = color.green, style = plot.style_circles)
p_sl = plot(final_sl_lvl, title = 'SL', color = color.red, style = plot.style_circles)

// p_ts = plot(trail_lvl, style=plot.style_steplinebr, color = color.silver)


fill(p_entry, p_tp, color = color.new(color.green,90))
fill(p_entry, p_sl, color = color.new(color.red,90))

Overview

MicroQuant Trend Strat +TS enters long positions when the trend direction turns bullish and short positions when it turns bearish, filtered by an intraday trading session window. Position exits are controlled through customizable fixed take-profit and stop-loss levels, an ATR percentage trailing stop, and a monetary breakeven threshold. The indicator plots trailing steplines directly on the price chart.

Details

Published by

g-moe on GitHub

Code credit

goodgains, as written in the script's header

Draws

On the price chart

Markets

Any symbol

Timeframes

M15-H1

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.

Settings

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

SettingTypeDefaultVariable
Pricesourcecloseprice
Lengthint21length
ATR Lengthint21atrPeriod
Factorfloat4.0factor
Trading Sessionbooltrueuse_sess
i_sesssession0700-1700i_sess
TPbooltrueuse_tp
tp_amountfloat20.00tp_amount
SLbooltrueuse_sl
sl_amountfloat10.00sl_amount
Trail Stop ATR %booltrueuse_ts
ts_amountfloat300.00ts_amount
Show all 14 settings
SettingTypeDefaultVariable
Breakeven Stop starts at $booltrueuse_be
start_befloat500start_be
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

This strategy overlays Supertrend on the price chart and trades only inside a set session and backtest date range. It enters long when Supertrend turns up, or after a chosen candle delay while the trend is up, and enters short on the opposite conditions. It cl

Strategy v5 TrendVolatility & bands
Updated 2 years ago · GPL-3.0 GitHub repository of BankNifty 5min Supertrend Based Strategy

ORION: Hybrid Strategy

by eddiebelaval
7 stars

This strategy identifies volatility breakout opportunities by constructing dynamic ATR-based price boxes and detecting volatility squeezes. Trades are filtered using an exponential moving average trend condition and designated session hours. Entries trigger up

Strategy v5 Volatility & bandsBreakout
Updated 3 months ago · None stated GitHub repository of ORION: Hybrid Strategy
4 stars

This strategy trades directional momentum breakouts for crude oil on the price chart. It enters long or short positions when a fast exponential moving average crosses a slow exponential moving average, provided that current Average True Range exceeds its movin

Strategy v6 TrendMoving averages
Updated 2 months ago · MIT GitHub repository of CL Crude Oil Momentum [trugurpala]

Hedge Fund SMC Strategy - QuantAlgo

by thesenegalesehitch
4 stars

This overlay strategy combines EMA trend following with simple Smart Money Concepts filters. It reads H4 and H1 bias, then looks for a liquidity sweep plus a displacement candle in that direction, gated by ADX, ATR, and volume. Entries are allowed only in Lond

Strategy v6 SMC / ICT / price actionTrend Repaints
Updated 4 months ago · Custom licence GitHub repository of Hedge Fund SMC Strategy - QuantAlgo

3Commas Bot

by regalouisei
2 stars

This strategy draws two moving averages on the price chart and uses their bias to open long or short trades. It places the stop beyond a swing high or low using an ATR risk multiple, then sets a limit exit from the reward-to-risk ratio. An optional ATR trail c

Strategy v5 TrendMoving averages
Updated 8 months ago · None stated GitHub repository of 3Commas Bot

More Trend scripts

Related on ForexR

Trade Your Charts With a Regulated Broker

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

Compare brokers