Strategy · Pine Script v6 · Own pane

PineForge MTF Tutorial — HTF SMA Filter

by pineforge-4pass

This strategy calculates a chart-timeframe MACD oscillator alongside a higher-timeframe Simple Moving Average (SMA) trend filter using security requests.

View on GitHub

GitHub stars193on the repository
Pine versionv6current
Lines of code49
Settings5inputs
LicenceApache-2.0
Last updatetoday28 Oct 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

49 lines of Pine Script v6 from tutorial/mtf/strategy_htf.pine, published under Apache-2.0. Keep the author's credit and licence lines when you reuse it.

strategy_htf.pine
//@version=6
//
// PineForge tutorial — MTF demo (higher-timeframe filter via request.security).
//
// 15m chart cadence, MACD entries gated by an HTF SMA trend filter pulled
// in via `request.security`. The HTF and SMA length are inputs so the
// Python harness can sweep them without rebuilding the .so.
//
// Read alongside generated_htf.cpp to see how each Pine line maps onto
// the runtime API. The .pine here is reference only — the closed-source
// transpiler emits the sibling .cpp.
//
strategy("PineForge MTF tutorial — HTF SMA filter",
     overlay            = false,
     initial_capital    = 1000000,
     currency           = currency.USD,
     process_orders_on_close = false,
     pyramiding         = 1,
     commission_type    = strategy.commission.percent,
     commission_value   = 0,
     slippage           = 0,
     default_qty_type   = strategy.fixed,
     default_qty_value  = 1)

// === Inputs ===
fastLen   = input.int(12, "Fast Length",   minval=1)
slowLen   = input.int(26, "Slow Length",   minval=1)
signalLen = input.int(9,  "Signal Length", minval=1)
smaLen    = input.int(20, "SMA Length",    minval=1)
htf       = input.string("60", "HTF")

// === HTF trend filter via request.security ===
//
// Note: htfSma is computed *inside* the HTF context (the ta.sma call
// runs as part of the per-HTF-bar evaluator the engine schedules
// separately from the chart bar). htfClose is the HTF close.
htfSma   = request.security(syminfo.tickerid, htf, ta.sma(close, smaLen), lookahead=barmerge.lookahead_off)
htfClose = request.security(syminfo.tickerid, htf, close,                  lookahead=barmerge.lookahead_off)
trendUp  = htfClose > htfSma

// === MACD on chart TF ===
[macdLine, signalLine, _hist] = ta.macd(close, fastLen, slowLen, signalLen)
longCond  = ta.crossover(macdLine,  signalLine) and trendUp
shortCond = ta.crossunder(macdLine, signalLine) and not trendUp

if longCond
    strategy.entry("Long",  strategy.long)
if shortCond
    strategy.entry("Short", strategy.short)

Overview

PineForge MTF Tutorial — HTF SMA Filter triggers long signals when the MACD line crosses above its signal line while the higher-timeframe price is above its SMA. Short signals occur when the MACD crosses below its signal line while the higher-timeframe price sits below its SMA. User inputs adjust the MACD parameters, higher-timeframe resolution, and SMA period. It draws in a separate pane. Note that there are no stop loss or take profit exit rules.

Details

Published by

pineforge-4pass on GitHub

Type

Strategy, Pine Script v6

Draws

Separate pane below the chart

Category

Trend, Oscillators & momentum, Moving averages

Markets

Any symbol

Timeframes

M15-H1

Risk Notes

  • The strategy enters trades without a stop loss

Topics

algorithmic-tradingbacktestingc-apicppcpp17cryptodeterministicffifinancepine-script

Settings

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

SettingTypeDefaultVariable
Fast Lengthint12fastLen
Slow Lengthint26slowLen
Signal Lengthint9signalLen
SMA Lengthint20smaLen
HTFstring60htf
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

MACD + Divergences [Alorse] is a momentum strategy that calculates the MACD oscillator and identifies regular and hidden divergences using pivot high and low points. It filters trade signals with dual Exponential Moving Averages (EMA 150 and 600) to ensure tra

Strategy v4 Oscillators & momentumDivergence Repaints
Updated 2 months ago · None stated GitHub repository of MACD + Divergences [Alorse]

ADX Strategy

by hasnocool
106 stars

An ADX-based trend-following strategy that enters long positions when the positive directional indicator (DI+) crosses above the Average Directional Index (ADX) while ADX remains below a preset threshold and a fast EMA is above a slow EMA. It allows adding to

Strategy v4 TrendOscillators & momentum
Updated 2 years ago · GPL-3.0 GitHub repository of ADX Strategy

EMA Ribbon MACD

by iamrichardD
57 stars

This strategy combines an EMA ribbon with MACD confirmation and plots in a separate pane. It enters when ribbon alignment, optional MACD zero or signal crosses, volume, smart-money, and multi-timeframe filters agree, then exits with percentage stop loss and ta

Strategy v6 TrendMoving averages
Updated 1 year ago · Apache-2.0 GitHub repository of EMA Ribbon MACD

This strategy evaluates the dynamic relationship between price and a chosen moving average across six diagnostic dimensions: trend, respect, extension, compression, acceleration, and exhaustion. It plots a normalized relationship oscillator bounded between -10

Strategy v6 TrendMoving averages
Updated yesterday · MIT GitHub repository of Market–Average Relationship Engine [WavesUnchained]
7 stars

This strategy combines the Moving Average Convergence Divergence oscillator with an Exponential Moving Average trend filter. It enters long positions when the MACD histogram crosses above zero while price stays above the long-term EMA filter. Long trades close

Strategy v5 TrendOscillators & momentum
Updated 1 year ago · MPL-2.0 GitHub repository of MACD EMA Filter Strategy

MACD Signal Cross

by latentwill
4 stars

This strategy evaluates EMA momentum crossovers filtered by an RSI oscillator. It enters a long position when a fast EMA crosses above a slow EMA while RSI remains above 40. Conversely, it enters a short position when the fast EMA crosses below the slow EMA wh

Strategy v5 Moving averagesOscillators & momentum
Updated 6 days ago · None stated GitHub repository of MACD Signal Cross

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