IC Markets

Level Up Your Trend Trading: The T3 Moving Average (Tillson) + MT4/MT5 Code

Share, develop, and backtest custom MQL4/MQL5 Expert Advisors, Python data-scraping scripts, trading bots, and automated market alert systems.
PTScalper
Site Admin
Posts: 1114
Joined: Mon Jul 20, 2026 1:28 pm

Re: Level Up Your Trend Trading: The T3 Moving Average (Tillson) + MT4/MT5 Code

Post by PTScalper »

Key Pine Script Paradigms Leveraged:

Built-in ta.ema(): Instead of manually calculating alpha and tracking previous states like (1 - alpha) * e1[1], we just pass the data through Pine's built-in ta.ema(). TradingView's backend calculates this natively in C++, making it much more performant than a custom math loop.

Declarative execution: Notice there is no Calculate(index) or OnCalculate() function. In Pine Script, every single line of code implicitly runs on every historical and real-time bar from left to right.

Variable Chaining: Because everything is evaluated procedurally on the current bar, you can simply feed e1 directly into the ta.ema() for e2. You don't need to define empty arrays or buffers beforehand.

Native UI Features: input.source(close) inherently gives the user a drop-down menu in the indicator settings to apply the math to hl2, hlc3, ohlc4, etc., replicating the MT4/MT5/cTrader feature natively.
Preserve your own money. Scale with the market's money. Exponential growth is the ultimate key.
PTScalper
Site Admin
Posts: 1114
Joined: Mon Jul 20, 2026 1:28 pm

Re: Level Up Your Trend Trading: The T3 Moving Average (Tillson) + MT4/MT5 Code

Post by PTScalper »

To elevate a Pine Script indicator to a truly "Professional" standard, we must go beyond just making the math work. Professional TradingView scripts prioritize modular architecture, UI/UX design in the settings menu, dynamic visuals (like trend coloring), and alert integrations.

Here is the "Pro" version leveraging Pine Script v5's newest features, including the method syntax, input groupings, trend detection, and alerts.

Professional Tillson T3 [Advanced UI/UX]

Code: Select all

//@version=5
indicator("Tillson T3 [Pro]", shorttitle="T3 Pro", overlay=true, timeframe="", timeframe_gaps=true)

// =========================================================================
// 1. INPUTS & UI/UX SETTINGS
// =========================================================================
var string GRP_CALC = "Calculation Settings"
var string GRP_VIS  = "Visual & Alert Settings"

// Calculation Inputs
int   length  = input.int(14, title="T3 Period", minval=1, group=GRP_CALC, tooltip="The lookback period for the EMAs.")
float vFactor = input.float(0.7, title="Volume Factor (v)", minval=0.0, maxval=1.0, step=0.1, group=GRP_CALC, tooltip="Controls responsiveness vs smoothness. 0.7 is the standard default.")
float src     = input.source(close, title="Source Data", group=GRP_CALC)

// Visual Inputs
color colorUp   = input.color(#00E676, title="Uptrend Color", group=GRP_VIS, inline="Colors")
color colorDown = input.color(#FF5252, title="Downtrend Color", group=GRP_VIS, inline="Colors")
bool  showFill  = input.bool(false, title="Show Trend Ribbon?", group=GRP_VIS, tooltip="Fills the space between the T3 line and the price to highlight trend strength.")

// =========================================================================
// 2. CUSTOM METHODS (Modularity)
// =========================================================================
// By defining this as a 'method', we can call it on any series data 
// using object-oriented syntax (e.g., src.calc_t3(len, v))
method calc_t3(series float source, int len, float v) =>
    // Pre-calculate constants
    float c1 = -(v * v * v)
    float c2 = (3 * v * v) + (3 * v * v * v)
    float c3 = -(6 * v * v) - (3 * v) - (3 * v * v * v)
    float c4 = 1 + (3 * v) + (v * v * v) + (3 * v * v)
    
    // Chained EMAs
    float e1 = ta.ema(source, len)
    float e2 = ta.ema(e1, len)
    float e3 = ta.ema(e2, len)
    float e4 = ta.ema(e3, len)
    float e5 = ta.ema(e4, len)
    float e6 = ta.ema(e5, len)
    
    // Blend and return
    float result = (c1 * e6) + (c2 * e5) + (c3 * e4) + (c4 * e3)
    result

// =========================================================================
// 3. CORE EXECUTION & STATE DETECTION
// =========================================================================
// Execute the math using method syntax
float t3 = src.calc_t3(length, vFactor)

// Detect Trend Direction (Rising vs Falling)
bool isUp   = t3 > t3[1]
bool isDown = t3 < t3[1]

// Detect Trend Reversals (For Alerts)
bool trendTurnedUp   = isUp and isDown[1]
bool trendTurnedDown = isDown and isUp[1]

// Determine Dynamic Color
color t3Color = isUp ? colorUp : colorDown

// =========================================================================
// 4. RENDERING (PLOTS & FILLS)
// =========================================================================
// Main T3 Plot
p_t3 = plot(t3, title="Tillson T3", color=t3Color, linewidth=2, style=plot.style_line)

// Optional Trend Ribbon Fill
// We plot the source invisibly, then fill the space between the source and T3
p_src = plot(showFill ? src : na, title="Source Baseline", color=na, display=display.none)
fill(p_t3, p_src, color=showFill ? color.new(t3Color, 85) : na, title="Trend Ribbon")

// =========================================================================
// 5. ALERTS
// =========================================================================
alertcondition(trendTurnedUp, title="T3 Trend: UP", message="Tillson T3 has shifted to an uptrend.")
alertcondition(trendTurnedDown, title="T3 Trend: DOWN", message="Tillson T3 has shifted to a downtrend.")
Preserve your own money. Scale with the market's money. Exponential growth is the ultimate key.
PTScalper
Site Admin
Posts: 1114
Joined: Mon Jul 20, 2026 1:28 pm

Re: Level Up Your Trend Trading: The T3 Moving Average (Tillson) + MT4/MT5 Code

Post by PTScalper »

What Makes This "Much More Professional"?

1.) Custom method Syntax: By defining method calc_t3(), we upgrade the code to Pine Script's newer object-oriented style. It allows you to call the math as src.calc_t3(length, vFactor). This makes the code highly modular—if you ever wanted to calculate a secondary T3 on the RSI, you could just type ta.rsi(14).calc_t3(length, vFactor) without rewriting any math.

2.) Clean Settings UI: Using group=, inline=, and tooltip=, the indicator settings menu is now cleanly divided into "Calculation Settings" and "Visual & Alert Settings". The colors sit neatly on a single line, and users get hover-over explanations for the inputs.

3.) Dynamic State Visualization: Professional indicators rarely use static colors. The script now compares the current T3 to the previous bar (t3 > t3[1]) to dynamically color the line Green/Red based on the slope direction.

4.) Trend Ribbons (Fills): Added an optional, toggleable fill() function that shades the area between the price action and the T3 line, providing a modern "trend ribbon" visual without cluttering the chart unless the user wants it.

5.)Integrated Alert Engine: Built-in alertcondition() functions allow users to easily set up webhook or app notifications when the T3 slope changes direction.
Preserve your own money. Scale with the market's money. Exponential growth is the ultimate key.
Post Reply