Flying Dragon Trend strategy in Pine Script
The price against a Hull moving average shifted a few bars to the right decides the side of the trade.
Hull moving average · long and short · by MarkoP010 · Mozilla Public License 2.0
What is its backtest worth?
A parameter sweep finds the best setting of the Flying Dragon Trend 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
- At the default risk level (Medium) it is long while the close is above the Hull average of a few bars ago, short while it is below.
- Can add up to three entries in the same direction (pyramiding 3); the stop loss is off by default.
- The other moving average and offsets belong to other risk levels and do not change the trades here.
What to watch for
- Shifting an average to the right makes it lag more – the offset is the input to look at in the heatmap.
- Always in the market and adding to positions: the largest drop matters more than the net profit.
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/
// © MarkoP010 2023
// Modified by PineTester (2026-11-14): the same test conditions for every library strategy in strategy(): 33% of equity per entry (pyramiding 3),
// 0.1% commission per order, 2 ticks slippage, initial capital 10,000. The trading rules are unchanged.
//@version=5
//The basic idea of the strategy is to select the best set of MAs, types, lenghts and offsets, which draws red trend bands for downtrend (and green for uptrend).
//Strategy executes by selected risk level either when there is MA crossover with price (Leading MA Offset1 on Highest risk level, Lagging MA on Low risk level) or three bands with the same color at the same time (on Lowest risk level, Leading MA Offset1 crossover with Lagging MA).
//Strategy plots user selectable Moving Average lines, a colored trend band between the MA lines and pivot indicators. The trend bands can be turned off individually if required. Also pivot indicators can be turned off if required.
//The Offset option shifts the selected MA with the set number of steps to the right. That is where the Magic happens and the Dragon roars!
//Strategy version history:
//Version 1 - Initial script
//Version 2 - Bug fix for Lowest risk level to show the indicator
//Version 3 - Updated graph picture
//Version 4 - Alert function and pivot indicators added, alerts are enabled when indicators are on. Inlining the code with the corresponding indicator.
strategy("Flying Dragon Trend Strategy", shorttitle="Flying Dragon Trend Strategy", overlay=true, pyramiding=3, calc_on_order_fills=false, process_orders_on_close=true, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=33, commission_type=strategy.commission.percent, commission_value=0.1, slippage=2)
//Moving Averages function
MA(source, length, type) =>
type == "EMA" ? ta.ema(source, length) :
type == "HMA" ? ta.hma(source, length) :
type == "RMA" ? ta.rma(source, length) :
type == "SMA" ? ta.sma(source, length) :
type == "SWMA" ? ta.swma(source) :
type == "VWMA" ? ta.vwma(source, length) :
type == "WMA" ? ta.wma(source, length) :
na
//Strategy options
strDirection = input.string(defval="Both", title="Strategy Direction", options=["Both", "Long", "Short"], group="Strategy Options") //Strategy direction selector by DashTrader
strSelection = strDirection == "Long" ? strategy.direction.long : strDirection == "Short" ? strategy.direction.short : strategy.direction.all //Strategy direction selector by DashTrader
strategy.risk.allow_entry_in(strSelection)
riskLevel = input.string(defval="Medium", title="Risk Level", options=["Highest", "High", "Medium", "Low", "Lowest"], inline="Options", group="Strategy Options")
indicatorsOn = input(defval=true, title="Indicators", inline="Options", tooltip="Turn on or off pivot indicators. Strategy execution criteria. When risk level Highest then Leading MA Offset1 crossover with price triggers the indicator and executes the strategy, when Low then Lagging MA crossover with price, when Lowest then all the Bands are the same color (Leading MA Offset1 crossover with Lagging MA). Alerts are enabled when indicators are on. Create Alert on selected equity and timeframe, select on Condition this strategy and alert criteria (for example: Alert () function calls only), Create.", group="Strategy Options")
useStop = input(defval=false, title="Use Stop Loss", inline="SL", group="Strategy Options")
stopPrct = input.int(defval=10, title=" %", minval=0, maxval=100, step=1, inline="SL", group="Strategy Options") / 100
//Inputs
ma1Type = input.string(defval="HMA", title="", inline="MA1", options=["EMA", "HMA", "RMA", "SMA","SWMA", "VWMA", "WMA"], group="Leading Moving Average")
ma1Length = input.int(defval=35, title="",minval=1, inline="MA1", group="Leading Moving Average")
ma1Source = input(defval=close, title="", tooltip="For short timeframes, minutes to hours, instead of Default values try Lowest risk level and HMA75 with Offsets 0,1,4 and SMA12 with Offset 6.", inline="MA1", group="Leading Moving Average")
ma1Color = input(defval=color.purple, title="", inline="MA-1", group="Leading Moving Average")
ma1Offset = input.int(defval=0, title="Offset1 Steps", minval=0, maxval=10, step=1, tooltip="The Magic happens here! The offset to move the line to the right.", inline="MA-1", group="Leading Moving Average")
ma1 = MA(ma1Source, ma1Length, ma1Type)[ma1Offset]
ma2Color = input(defval=color.lime, title="", inline="MA-2", group="Leading Moving Average")
ma2Offset = input.int(defval=4, title="Offset2 Steps", minval=0, maxval=10, step=1, tooltip="The Magic happens here! The offset to move the line to the right.", inline="MA-2", group="Leading Moving Average")
ma2 = ma1[ma2Offset]
ma3Color = input(defval=color.aqua, title="", inline="MA-3", group="Leading Moving Average")
ma3Offset = input.int(defval=6, title="Offset3 Steps", minval=0, maxval=10, step=1, tooltip="The Magic happens here! The offset to move the line to the right.", inline="MA-3", group="Leading Moving Average")
ma3 = ma1[ma3Offset]
ma4Type = input.string(defval="SMA", title="", inline="MA4", options=["EMA", "HMA", "RMA", "SMA","SWMA", "VWMA", "WMA"], group="Lagging Moving Average")
ma4Length = input.int(defval=22, title="",minval=1, inline="MA4", group="Lagging Moving Average")
ma4Source = input(defval=close, title="", inline="MA4", group="Lagging Moving Average")
ma4Color = input(defval=color.yellow, title="", inline="MA-4", group="Lagging Moving Average")
ma4Offset = input.int(defval=2, title="Offset Steps", minval=0, maxval=10, step=1, tooltip="The Magic happens here! The offset to move the line to the right.", inline="MA-4", group="Lagging Moving Average")
ma4 = MA(ma4Source, ma4Length, ma4Type)[ma4Offset]
bandTransp = input.int(defval=60, title="Band Transparency", minval=20, maxval=80, step=10, group="Banding")
useBand1 = input(defval=true, title="Band 1", inline="Band", group="Banding")
band1Transp = useBand1 ? bandTransp : 100
band1clr = ma1 > ma2 ? color.new(#00ff00, transp=band1Transp) : color.new(#ff0000, transp=band1Transp)
useBand2 = input(defval=true, title="Band 2", inline="Band", group="Banding")
band2Transp = useBand2 ? bandTransp : 100
band2clr = ma1 > ma3 ? color.new(#00ff00, transp=band2Transp) : color.new(#ff0000, transp=band2Transp)
useBand3 = input(defval=true, title="Band 3", tooltip="Up trend green, down trend red. Colors get reversed if MA1 lenght is greater than MA2 lenght, or they are different type and MA2 quicker. In that case, just reverse your selections for MA1 and MA2, or let it be as is.", inline="Band", group="Banding")
band3Transp = useBand3 ? bandTransp : 100
band3clr = ma1 > ma4 ? color.new(#00ff00, transp=band3Transp) : color.new(#ff0000, transp=band3Transp)
//Graphs
piirto1 = plot(ma1, color = ma1Color, title="MA1")
piirto2 = plot(ma2, color = ma2Color, title="MA2")
piirto3 = plot(ma3, color = ma3Color, title="MA3")
piirto4 = plot(ma4, color = ma4Color, title="MA4")
fill(piirto1, piirto2, color=band1clr)
fill(piirto1, piirto3, color=band2clr)
fill(piirto1, piirto4, color=band3clr)
//Strategy entry and stop conditions, indicator plot conditions
longCondition = riskLevel == "Highest" ? ma1Source > ma1 : riskLevel == "High" ? ma1Source > ma2 : riskLevel == "Medium" ? ma1Source > ma3 : riskLevel == "Low" ? ma4Source > ma4 : riskLevel == "Lowest" ? ma1 > ma4 : na
shortCondition = riskLevel == "Highest" ? ma1Source < ma1 : riskLevel == "High" ? ma1Source < ma2 : riskLevel == "Medium" ? ma1Source < ma3 : riskLevel == "Low" ? ma4Source < ma4 : riskLevel == "Lowest" ? ma1 < ma4 : na
pivotUp = longCondition and shortCondition[1] and indicatorsOn ? true : na
pivotDown = shortCondition and longCondition[1] and indicatorsOn ? true : na
plotshape(pivotUp, title="Up", style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small)
plotshape(pivotDown, title="Down", style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small)
stopLprice = useStop == true ? strategy.position_avg_price * (1-stopPrct) : na
stopSprice = useStop == true ? strategy.position_avg_price * (1+stopPrct) : na
if longCondition
strategy.entry("Long",strategy.long)
strategy.exit("Long Stop", "Long", stop=stopLprice)
if shortCondition
strategy.entry("Short",strategy.short)
strategy.exit("Short Stop", "Short", stop=stopSprice)
//Alerts
if pivotUp
alertMessage = syminfo.ticker + ' - ' + timeframe.period + ": Pivot Up!"
alert(alertMessage, alert.freq_once_per_bar_close)
if pivotDown
alertMessage = syminfo.ticker + ' - ' + timeframe.period + ": Pivot Down!"
alert(alertMessage, alert.freq_once_per_bar_close)
//End
Related strategies
- Hull Kaufman SuperTrend Cloud – A cloud of three lines – Kaufman adaptive average, its Hull average and a Supertrend – that the price has to break.
- Double Inside Bar & Trend – After two inside bars, a stop order on the side of the Hull average trend enters the breakout.
- ALMA Cross – Two Arnaud Legoux moving averages cross while volume is rising; a fixed take profit and stop end the trade.
- Vortex Cross with Moving Average – A cross of the two Vortex lines, confirmed by the price against a smoothed moving average.
Open-source script by MarkoP010, 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.