Strategy · Pine Script v5 · On the chart

HTF Condition Strategy

by g-moe· code credited to goodgains

HTF Condition Strategy is a multi-timeframe trading strategy that evaluates market structure conditions on both a higher timeframe and the current chart timeframe.

View on GitHub

GitHub stars3on the repository
Pine versionv5runs as is
Lines of code73
Settings16inputs
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

73 lines of Pine Script v5 from Tradingview/htf-condition-strategy.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

htf-condition-strategy.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("HTF Condition Strategy", overlay = true)

// @keywords: htf-strategy, higher-timeframe, long-conditions, short-conditions, and-or-logic, bullish-bearish, higher-low, lower-low, reversal-exit, take-profit, stop-loss, market-structure, kafa-dashboard, net-profit, profit-factor, win-rate, max-drawdown, dashboard-table, condition-builder, multi-timeframe

higher_tf = input.timeframe("D", title = 'Higher Timeframe', group = 'Timeframe Settings')




//long conditions
long_condition_1 = input.string(defval = 'Bullish on HTF', options = ['Bullish on HTF', 'Bullish on Chart TF', 'Higher Low on HTF', 'Higher Low on Chart TF', 'Lower Low on HTF', 'Lower Low on Chart TF']
     , title = 'Long Condition 1', group = 'Long Conditions', inline = '1')
long_and_or_1 = input.string(defval = 'AND', title =' ', options = ['AND', 'OR'], group = 'Long Conditions', inline = '1')

long_condition_2 = input.string(defval = 'Bullish on HTF', options = ['Bullish on HTF', 'Bullish on Chart TF', 'Higher Low on HTF', 'Higher Low on Chart TF', 'Lower Low on HTF', 'Lower Low on Chart TF']
     , title = 'Long Condition 2', group = 'Long Conditions', inline = '2')

long_exit = input.string(defval = 'Reversal on HTF', options = ['Reversal on HTF', 'Reversal on Chart TF', 'Higher High on HTF', 'Higher High on Chart TF', 'Higher Low on HTF', 'Higher Low on Chart TF']
     , title = 'Long Exit Condition', group = 'Long Conditions', inline = '3')


//short conditions
short_condition_1 = input.string(defval = 'Bullish on HTF', options = ['Bullish on HTF', 'Bullish on Chart TF', 'Higher Low on HTF', 'Higher Low on Chart TF', 'Lower Low on HTF', 'Lower Low on Chart TF']
     , title = 'Short Condition 1', group = 'Short Conditions', inline = '1')
short_and_or_1 = input.string(defval = 'AND', title =' ', options = ['AND', 'OR'], group = 'Short Conditions', inline = '1')

short_condition_2 = input.string(defval = 'Bullish on HTF', options = ['Bullish on HTF', 'Bullish on Chart TF', 'Higher Low on HTF', 'Higher Low on Chart TF', 'Lower Low on HTF', 'Lower Low on Chart TF']
     , title = 'Short Condition 2', group = 'Short Conditions', inline = '2')

short_exit = input.string(defval = 'Reversal on HTF', options = ['Reversal on HTF', 'Reversal on Chart TF', 'Higher High on HTF', 'Higher High on Chart TF', 'Higher Low on HTF', 'Higher Low on Chart TF']
     , title = 'Short Exit Condition', group = 'Short Conditions', inline = '3')


//tps and sls
tp_pct = input.float(2.00, title = 'TP %', group = 'TP / SL', inline = '1')
sl_pct = input.float(1.00, title = 'SL %', group = 'TP / SL', inline = '1')








//DASHBOARD
LabelSize = input.string(defval="Medium", options=["Small", "Medium", "Large"], group = "Dashboard Settings", title = "Dashboard Size", inline = '1')
dashboard_text_color = color.new(#000000, 0)
bull_color = input.color(color.new(#04fc04,0), "Bull Color", group = "Dashboard Settings", inline = '2')
bear_color = input.color(color.new(#fc0404,0), "Bear Color", group = "Dashboard Settings", inline = '2')
neutral_color = input.color(color.new(#b2b5be,0), "Neutral Color 1", group = "Dashboard Settings", inline = '3')
neutral_color2 = input.color(color.new(#8b8f9e,0), "Neutral Color 2", group = "Dashboard Settings", inline = '3')
label_size = LabelSize == "Small" ? size.small : LabelSize == "Medium" ? size.normal : LabelSize == "Large" ? size.large : size.small
var table t = table.new(position.bottom_right, 2, 11, border_width = 2, frame_width = 2, frame_color = #000000)

if (barstate.islastconfirmedhistory or barstate.islast)

    table.cell(t, 0, 0, text = "KaFa Market Structure", width = 0, bgcolor = color.new(#2962ff,0), text_size = label_size, text_color = dashboard_text_color)
    table.merge_cells(t,0,0,1,0)
    
    //column1
    table.cell(t, 0, 3, text = "Net Profit: ", width = 0, bgcolor = neutral_color, text_size = label_size, text_color = dashboard_text_color)
    table.cell(t, 0, 4, text = "Total Days: ", width = 0, bgcolor = neutral_color2, text_size = label_size, text_color = dashboard_text_color)
    table.cell(t, 0, 5, text = "Daily %", width = 0, bgcolor = neutral_color, text_size = label_size, text_color = dashboard_text_color)
    table.cell(t, 0, 6, text = "Max DD %: ", width = 0, bgcolor = neutral_color2, text_size = label_size, text_color = dashboard_text_color)
    table.cell(t, 0, 7, text = "Profit Factor: ", width = 0, bgcolor = neutral_color, text_size = label_size, text_color = dashboard_text_color)
    table.cell(t, 0, 8, text = "Avg. Bars in Trade: ", width = 0, bgcolor = neutral_color2, text_size = label_size, text_color = dashboard_text_color)
    table.cell(t, 0, 9, text = "Win %: ", width = 0, bgcolor = neutral_color, text_size = label_size, text_color = dashboard_text_color)
    table.cell(t, 0, 10, text = "Required Capital: ", width = 0, bgcolor = neutral_color2, text_size = label_size, text_color = dashboard_text_color)

Overview

Users can configure entry rules combining conditions such as bullish bias, higher lows, or lower lows using AND/OR logic. Exits are triggered by market structure reversals or configurable percentage Take Profit and Stop Loss levels. The strategy plots on the main price chart and displays an on-chart performance dashboard.

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

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.

Settings

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

SettingTypeDefaultVariable
Higher TimeframetimeframeDhigher_tf
Long Condition 1stringBullish on HTFlong_condition_1
stringANDlong_and_or_1
Long Condition 2stringBullish on HTFlong_condition_2
Long Exit ConditionstringReversal on HTFlong_exit
Short Condition 1stringBullish on HTFshort_condition_1
stringANDshort_and_or_1
Short Condition 2stringBullish on HTFshort_condition_2
Short Exit ConditionstringReversal on HTFshort_exit
TP %float2.00tp_pct
SL %float1.00sl_pct
Dashboard SizestringMediumLabelSize
Show all 16 settings
SettingTypeDefaultVariable
Bull Colorcolorcolor.new(#04fc04,0)bull_color
Bear Colorcolorcolor.new(#fc0404,0)bear_color
Neutral Color 1colorcolor.new(#b2b5be,0)neutral_color
Neutral Color 2colorcolor.new(#8b8f9e,0)neutral_color2
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

AC- MY SCRIPT1

by hasnocool
106 stars

This strategy plots an Optimized Trend Tracker and a selectable moving-average support line on the price chart. It builds the average as SMA, EMA, WMA, TMA, VAR, WWMA, ZLEMA, or TSF, then offsets long and short stops by a percent of that average. Optional mark

Strategy v5 TrendMoving averages
Updated 2 years ago · GPL-3.0 GitHub repository of AC- MY SCRIPT1
13 stars

This strategy automates the TradingLatino methodology by evaluating Squeeze Momentum combined with the Average Directional Index (ADX). It executes long or short positions based on momentum shifts and directional strength. Risk management options include const

Strategy v5 Oscillators & momentumTrend
Updated 1 year ago · None stated GitHub repository of Ruckard Lupown TradingLatino A002

Calculates a custom moving average (MaMA) by modifying the source price with price momentum, acceleration, and psychological win-rate probability before applying an EMA or SMA. Plots the adjusted moving average directly on the price chart alongside optional re

Strategy v5 Moving averagesTrend
Updated 4 years ago · None stated GitHub repository of Momentum Adjusted Moving Average by DGT

This long-only trend-following strategy combines Ichimoku Kinko Hyo components with higher timeframe moving average filters. It triggers long entries when price trades above a higher timeframe EMA and a bullish Tenkan-Sen cross above Kijun-Sen occurs under fav

Strategy v5 TrendOscillators & momentum
Updated 1 year ago · MPL-2.0 GitHub repository of Ichimoku Kinko Hyo Momentum Strategy
5 stars

RG Structure is a pivot-constrained break-and-retest strategy that calculates dynamic trendlines, market structure changes, and regime states on the price chart. It identifies support and resistance lines through pivot points and evaluates break-and-retest sig

Strategy v6 TrendSupport & resistance
Updated 8 weeks ago · None stated GitHub repository of RG Structure Visual Edition
4 stars

Momentum Rotation (Top 3) ranks ten large-cap US tech stocks by their six-month returns using daily closes. It rebalances monthly and holds the top three performing stocks long only on the price chart. A ranking table shows the current order. Main settings adj

Strategy v6 Oscillators & momentumTrend
Updated 5 months ago · None stated GitHub repository of Momentum Rotation (Top 3)

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