Indicator · Pine Script v6 · On the chart

Taught to Trade - Cost Hurdle

by obekheet

This indicator calculates the total transaction cost hurdle comprising commission, slippage, and spread as a percentage, price value, and fraction of ATR.

View on GitHub

GitHub stars1on the repository
Pine versionv6current
Lines of code108
Settings10inputs
LicenceMPL-2.0
Last update12 days ago16 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 indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

108 lines of Pine Script v6 from scripts/cost-hurdle.pine, published under MPL-2.0. Keep the author's credit and licence lines when you reuse it.

cost-hurdle.pine
//@version=6
// Taught to Trade - Cost Hurdle
// Costs are not a fee taken off the end. They are a hurdle height you must clear
// before a trade is worth anything. This measures how tall that hurdle is here,
// and how often this instrument has actually produced a move that size.
// No buy/sell signals. Alerts are yours to configure.
indicator("Taught to Trade - Cost Hurdle", shorttitle = "TT HURDLE", overlay = true, max_bars_back = 1000)

grpC = "Your real costs"
commPct = input.float(0.04, "Commission per side (%)", minval = 0.0, step = 0.01, group = grpC)
slipPct = input.float(0.02, "Slippage per side (%)", minval = 0.0, step = 0.01, group = grpC)
spreadPct = input.float(0.0, "Spread paid once (%)", minval = 0.0, step = 0.01, group = grpC, tooltip = "Leave at zero if your commission figure already includes the spread.")

grpH = "Holding periods"
h1 = input.int(1, "Hold A (bars)", minval = 1, group = grpH)
h2 = input.int(5, "Hold B (bars)", minval = 1, group = grpH)
h3 = input.int(10, "Hold C (bars)", minval = 1, group = grpH)
h4 = input.int(20, "Hold D (bars)", minval = 1, group = grpH)
lookback = input.int(500, "Sample window (bars)", minval = 60, maxval = 1000, group = grpH)

grpT = "Table"
posIn = input.string("Top right", "Position", options = ["Top right", "Top left", "Bottom right", "Bottom left"], group = grpT)
szIn  = input.string("Small", "Text size", options = ["Tiny", "Small", "Normal"], group = grpT)

hurdlePct = (commPct + slipPct) * 2.0 + spreadPct
atr    = ta.atr(14)
atrPct = close > 0 ? atr / close * 100.0 : na
hurdleInAtr = nz(atrPct, 0) > 0 ? hurdlePct / atrPct : na
hurdleInPrice = close * hurdlePct / 100.0

f_move(int n) => close[n] > 0 ? math.abs(close - close[n]) / close[n] * 100.0 : na

m1 = f_move(h1)
m2 = f_move(h2)
m3 = f_move(h3)
m4 = f_move(h4)

c1 = math.sum(nz(m1, -1) > hurdlePct ? 1 : 0, lookback)
c2 = math.sum(nz(m2, -1) > hurdlePct ? 1 : 0, lookback)
c3 = math.sum(nz(m3, -1) > hurdlePct ? 1 : 0, lookback)
c4 = math.sum(nz(m4, -1) > hurdlePct ? 1 : 0, lookback)

v1 = math.sum(na(m1) ? 0 : 1, lookback)
v2 = math.sum(na(m2) ? 0 : 1, lookback)
v3 = math.sum(na(m3) ? 0 : 1, lookback)
v4 = math.sum(na(m4) ? 0 : 1, lookback)

med1 = ta.percentile_linear_interpolation(nz(m1, 0), lookback, 50)
med2 = ta.percentile_linear_interpolation(nz(m2, 0), lookback, 50)
med3 = ta.percentile_linear_interpolation(nz(m3, 0), lookback, 50)
med4 = ta.percentile_linear_interpolation(nz(m4, 0), lookback, 50)

f_safe(float num, float den) => den > 0 ? num / den : na
f_pc(float v)  => na(v) ? "-" : str.tostring(v, "#") + "%"
f_pc2(float v) => na(v) ? "-" : str.tostring(v, "#.###") + "%"

s1 = f_safe(c1 * 100.0, v1)
s2 = f_safe(c2 * 100.0, v2)
s3 = f_safe(c3 * 100.0, v3)
s4 = f_safe(c4 * 100.0, v4)

tblPos = posIn == "Top left" ? position.top_left : posIn == "Bottom right" ? position.bottom_right : posIn == "Bottom left" ? position.bottom_left : position.top_right
tblSz  = szIn == "Tiny" ? size.tiny : szIn == "Normal" ? size.normal : size.small

bgCol  = color.new(#0E1526, 10)
txtCol = color.new(#E6EAF2, 0)
hdrCol = color.new(#2F6FED, 0)
badCol = color.new(#B3261E, 25)
okCol  = color.new(#2E7D32, 25)

f_flag(float share) => na(share) ? bgCol : share < 50.0 ? badCol : okCol

var table t = table.new(tblPos, 4, 8, border_width = 1, border_color = color.new(#2F6FED, 70))

f_h(int c, string s) => table.cell(t, c, 0, s, text_color = color.white, text_size = tblSz, bgcolor = hdrCol)
f_r(int c, int r, string s, color bc) => table.cell(t, c, r, s, text_color = txtCol, text_size = tblSz, text_halign = text.align_right, bgcolor = bc)
f_l(int c, int r, string s, color bc) => table.cell(t, c, r, s, text_color = txtCol, text_size = tblSz, text_halign = text.align_left, bgcolor = bc)

if barstate.islast
    f_h(0, "HOLD")
    f_h(1, "Bars")
    f_h(2, "Cleared")
    f_h(3, "Median move")
    f_l(0, 1, str.tostring(h1) + " bars", bgCol)
    f_r(1, 1, str.tostring(v1), bgCol)
    f_r(2, 1, f_pc(s1), f_flag(s1))
    f_r(3, 1, f_pc2(med1), bgCol)
    f_l(0, 2, str.tostring(h2) + " bars", bgCol)
    f_r(1, 2, str.tostring(v2), bgCol)
    f_r(2, 2, f_pc(s2), f_flag(s2))
    f_r(3, 2, f_pc2(med2), bgCol)
    f_l(0, 3, str.tostring(h3) + " bars", bgCol)
    f_r(1, 3, str.tostring(v3), bgCol)
    f_r(2, 3, f_pc(s3), f_flag(s3))
    f_r(3, 3, f_pc2(med3), bgCol)
    f_l(0, 4, str.tostring(h4) + " bars", bgCol)
    f_r(1, 4, str.tostring(v4), bgCol)
    f_r(2, 4, f_pc(s4), f_flag(s4))
    f_r(3, 4, f_pc2(med4), bgCol)
    f_l(0, 5, "Hurdle", hdrCol)
    f_l(1, 5, str.tostring(hurdlePct, "#.###") + "% round trip", bgCol)
    f_l(0, 6, "In price", bgCol)
    f_l(1, 6, str.tostring(hurdleInPrice, format.mintick), bgCol)
    f_l(0, 7, "In ATR(14)", bgCol)
    f_l(1, 7, na(hurdleInAtr) ? "-" : str.tostring(hurdleInAtr, "#.##") + " x ATR", bgCol)

alertcondition(not na(hurdleInAtr) and hurdleInAtr > 1.0, title = "Round-trip cost now exceeds one ATR", message = "Cost Hurdle: a round trip on this chart now costs more than one ATR of movement.")
alertcondition(not na(hurdleInAtr) and hurdleInAtr > 0.5 and hurdleInAtr <= 1.0, title = "Round-trip cost now exceeds half an ATR", message = "Cost Hurdle: a round trip on this chart now costs more than half an ATR of movement.")

Overview

Taught to Trade - Cost Hurdle samples historical price moves across four user-defined holding periods over a configurable lookback window to determine how often moves clear the cost hurdle and computes median returns for each period. The results are displayed in an on-chart table to assess whether expected trade horizons produce enough movement to cover trading friction.

Details

Published by

obekheet on GitHub

Type

Indicator, Pine Script v6

Draws

On the price chart

Category

Screeners & dashboards, Volatility & bands

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.

Topics

pine-scriptpinescripttechnical-analysistrading-indicatorstradingviewtradingview-indicators

Settings

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

SettingTypeDefaultVariable
Commission per side (%)float0.04commPct
Slippage per side (%)float0.02slipPct
Spread paid once (%)float0.0spreadPct
Hold A (bars)int1h1
Hold B (bars)int5h2
Hold C (bars)int10h3
Hold D (bars)int20h4
Sample window (bars)int500lookback
PositionstringTop rightposIn
Text sizestringSmallszIn
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 overlay indicator plots a Supertrend from an ATR length and factor, four exponential moving averages, and a selectable VWAP or TWAP taken from a chosen timeframe on the price chart. It also shows MACD and stochastic with adjustable lengths, smoothing, ver

Indicator v5 Oscillators & momentumMoving averages
Updated 1 year ago · MPL-2.0 GitHub repository of Momentum Setup: Vijay Thakare Option Buying Scalping Setup
330 stars

This multi-symbol indicator evaluates a combined trading condition across up to 40 user-defined symbols. It processes MACD bullish crossovers, RSI thresholds, Bollinger Band boundaries, and DMI/ADX trend filters using security calls. When buy or sell criteria

Indicator v5 Oscillators & momentumVolatility & bands
Updated 2 months ago · None stated GitHub repository of Multi MACD + BB + RSI [Alorse]

Pricing Table

by namuan
318 stars

This indicator calculates theoretical options contract values and in-the-money probabilities using historical realized volatility across multiple standard deviations. It displays these metrics in a table overlay directly on the price chart. Users can adjust th

Indicator v4 Volatility & bandsScreeners & dashboards
Updated 2 months ago · MIT GitHub repository of Pricing Table
218 stars

This indicator maps market structure and supply and demand zones on the price chart. It marks validated and optional unvalidated swing highs and lows using a pivot length and extra confirmation bars. Consolidation and impulse moves are measured against ATR to

Indicator v6 SMC / ICT / price actionSupport & resistance Repaints
Updated 3 months ago · None stated GitHub repository of Market Structure + Supply/Demand
200 stars

Storm Gauge calculates annualized realized volatility and compares it against a historical percentile lookback window to classify market volatility regimes into calm, normal, or storm conditions. Based on a target volatility input, it calculates dynamic positi

Indicator v5 Volatility & bandsScreeners & dashboards
Updated 3 months ago · MIT GitHub repository of Storm Gauge — Vol Regime + Position Size

8zz反指標v2

by hansai-art
121 stars

The 8zz Contrarian Indicator plots reverse sentiment trading signals directly on the price chart based on tracked retail post activity. It generates bullish signals during retail panic and bearish signals during market euphoria. The indicator features adjustab

Indicator v6 Moving averagesOscillators & momentum
Updated 3 months ago · None stated GitHub repository of 8zz反指標v2

More indicators

Trade Your Charts With a Regulated Broker

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

Compare brokers