Strategy library

Gann HiLo Activator strategy in Pine Script

Flips between long and short when the close leaves the moving averages of the highs and the lows.

Gann HiLo · SMA · long and short · by starbolt · Mozilla Public License 2.0

What is its backtest worth?

A parameter sweep finds the best setting of the Gann HiLo Activator strategy. PineTester tells you if it holds up: whether the best result was only picked after the fact, what it makes with fees, what you could have known at the time (a walk-forward on data the pick did not see) and how deep it fell along the way. On the free start, your first own backtest gets the full evaluation; its trade list and result file come with the full report or a plan.

Creates a draft on Binance BTC/USDT 4h over about six and a half years with 0.1% commission per order and 2 ticks slippage; you start it yourself. A backtest uses credits; new accounts receive free credits.

The rules

  • Goes long when the close rises above the moving average of the highs (shifted by the offset), short when it falls below the moving average of the lows.
  • Has no separate exit: each flip turns the position around.
  • Uses 20% of equity per trade (set in the code).

What to watch for

  • Very short averages flip often; fees and slippage weigh heavily here.
  • Only two inputs: the full grid is small, so the result of one setting can be read against all its neighbours.

How PineTester judges a backtest, with its sources: how we judge results.

The Pine Script code

The author's code with its licence notice (Mozilla Public License 2.0), with one change marked in the code: the same test conditions for every library strategy in its strategy() line – the whole account per trade (split over the entries where it adds to a position), 0.1% commission per order and 2 ticks slippage; the trading rules are unchanged. Pine Script v5; PineTester converts it to v6 automatically, so results can differ slightly from TradingView.

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © starbolt
// Modified by PineTester (2026-11-14): the same test conditions for every library strategy in strategy(): 100% of equity per entry,
// 0.1% commission per order, 2 ticks slippage, initial capital 10,000. The trading rules are unchanged.
//@version=5
strategy('Gann HiLo Activator Strategy', overlay=true, pyramiding=0, process_orders_on_close=true, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_type=strategy.commission.percent, commission_value=0.1, slippage=2)
len = input.int(3, 'Length', step=1, minval=1)
displace = input.int(1, 'Offset', step=1, minval=0)
from_start = input(false, 'Begin from start?')
backtest_year = input(2017, 'From Year')
backtest_month = input.int(01, 'From Month', minval=1, maxval=12, step=1)
backtest_day = input.int(01, 'From Day', minval=1, maxval=31, step=1)
start_time = from_start ? 0 : timestamp(backtest_year, backtest_month, backtest_day, 00, 00)
float hilo = na
hi = ta.sma(high, len)
lo = ta.sma(low, len)
hilo := close > hi[displace] ? 1 : close < lo[displace] ? -1 : hilo[1]
ghla = hilo == -1 ? hi[displace] : lo[displace]
color = hilo == -1 ? color.red : color.green
buyCondition = hilo == 1 and hilo[1] == -1
sellCondition = hilo == -1 and hilo[1] == 1
if buyCondition and time >= start_time
    strategy.entry('Long', strategy.long)
if sellCondition and time >= start_time
    strategy.entry('Short', strategy.short)
plot(ghla, color=color, style=plot.style_cross)
  • Donchian Breakout + MA – A Donchian channel breakout in the direction of two moving averages, closed when the averages cross back.
  • Vortex Cross with Moving Average – A cross of the two Vortex lines, confirmed by the price against a smoothed moving average.
  • Kaufman's Efficiency Ratio – Buys while Kaufman's efficiency ratio is positive and protects the trade with an ATR trailing stop.
  • ALMA Cross – Two Arnaud Legoux moving averages cross while volume is rising; a fixed take profit and stop end the trade.

Open-source script by starbolt, shown under the Mozilla Public License 2.0. Not a recommendation; neither the author nor TradingView endorses PineTester. Backtests are hypothetical results on past data, not a forecast.