Pine Script v5 · Strategy
Heikin Ashi Candle Startegy for Long Position
A Pine Script v5 strategy with no technical-analysis calculation. It has 2 order rules, 1 entry among them, and trades long only with the default settings. Exits use a stop loss and a profit target. Each trade is one contract and pays no commission. 6 settings; defaults include Validation Period 3, Qty 1.0 and Maximum Active Open Position 1.0.
Generated from the script's code by a template: every clause is a fact read from the source. Not written by a person or a model, and not taken from the author's description.
- Does Heikin Ashi Candle Startegy for Long Position repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 20 of 20 trades.
- How did Heikin Ashi Candle Startegy for Long Position do on BTCUSDT 1D with default settings?
- −0.20%
- From 26 Jan 2024 to 21 Oct 2026, over 62 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Does Heikin Ashi Candle Startegy for Long Position run in Pine Script v6?
- No
- The v6 version runs in Zeal Terminal's engine, but its results differ from the v5 version: Trades differ: 62 before, 52 after on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. It is shown for reference and is not offered as a download.
- Does Heikin Ashi Candle Startegy for Long Position use a stop loss?
- Yes
- strategy.exit sets a stop on line 103.
- Does Heikin Ashi Candle Startegy for Long Position run in Zeal Terminal?
- Yes
- It compiles in 16 ms and runs 1,000 bars in 51 ms, in the browser.
BTCUSDT · 1D · default settings
- Limit
- Trailing
- Stop
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- −0.20%
- −$1,975.25
- Buy and hold
- +102.5%
- same period
- Closed trades
- 62
- 58 won · 4 lost
- Profitable
- 93.5%
- of closed trades
- Profit factor
- 0.98
- gross profit ÷ gross loss
- Max drawdown
- 4.11%
- −$41,849.00
One run with the author's default settings, $1,000,000 starting capital, commission as the script sets it. A single backtest says little about future results and is not a recommendation. Backtests are simulations on historical data and are not investment advice.
Review
What a careful reader of this script would point out, found by checking the source and the backtest.
-
Check
1 setting does nothing
“Trailing Stop (%)” is declared but never read.
-
Check
No commission
The backtest assumes free trading. Results on any real exchange will be lower.
-
Note
No slippage
Fills are assumed at the exact open price. Add slippage in the properties for a more realistic test.
-
Note
Uses an argument v6 removed
when = on line 96. The upgraded version below moves each call inside an if block.
Settings
Every input the script declares, with its default and what it feeds. Bounds are the ones the script enforces.
| Setting | Default | Type and range | Feeds |
|---|---|---|---|
| Candle | |||
| Validation Period | 3 | int | isLong line 63 |
| Order | |||
| Qty | 1.0 | float | entry Long |
| Maximum Active Open Position | 1.0 | float | line 94 |
| Long | |||
| Take Profit (%) | 1 | float · min 0.0 · step 0.1 | limitLong |
| Stop Loss (%) | 25 | float · min 0.0 · step 0.1 | stopLong |
| Trailing Stop (%) | 0.2 | float · min 0.0 · step 0.1 | unused |
What it calculates
No technical-analysis calculation: the script works on prices and settings directly.
Signal flow
How price data and settings reach the orders. Each box is a name in the script, or several computed the same way; each arrow is a value one of them reads. Hover a box for its members.
Order rules
| Order | Fires when | By default | Line |
|---|---|---|---|
| Open Long long | isLongisGreen and haClose[validationPeriod] < haOpen[validationPeriod] | On | 96 |
| Exit Long Exit stop and limit | (strategy.position_size > 0) | On | 103 |
Each order is guarded by the setting shown. “Off” orders never fire unless that setting is switched on.
Does it repaint?
No
No repainting patterns in the source, and the live replay matched history.
- ClearHigher-timeframe data2 request.security call(s), none with lookahead on.
- ClearFuture-bar referencesNo negative history offsets such as close[-1].
- ClearIntrabar recalculationNo varip, calc_on_every_tick or realtime-only state. Orders are decided once per closed bar.
- ClearOrder timingOrders fill at the next bar's open, after the signal bar has closed.
The checks read the source; the replay runs it. Together they catch the common causes, not every possible one.
Other markets
The same script with default settings on 8 Binance markets. Each row is a separate backtest over the most recent 1,000 bars.
| Market | Net | Buy and hold | Trades | Won | Profit factor | Max drawdown | Period |
|---|---|---|---|---|---|---|---|
| BTCUSDT 1D | −0.2% | +102.5% | 62 | 94% | 0.98 | 4.1% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +0.2% | +5.5% | 32 | 97% | 1.11 | 2.3% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −0.4% | +9.8% | 52 | 85% | 0.33 | 0.6% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +0.0% | +8.4% | 37 | 97% | 1.59 | 0.1% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +0.0% | +16.7% | 64 | 92% | 1.06 | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.0% | +21.5% | 31 | 97% | 1.38 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +0.1% | +148.3% | 56 | 100% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +0.0% | +17.1% | 30 | 100% | — | 0.0% | 8 May 2026 – 21 Oct 2026 |
Higher-timeframe data for request.security(): heikinashi:SYMBOL D (Binance BTCUSDT 1d, as Heikin-Ashi by the engine), heikinashi:BINANCE:BTCUSDT D (Binance BTCUSDT 1d, as Heikin-Ashi by the engine), fetched from Binance's public market data for each market.
Backtests are simulations on historical data and are not investment advice.
The trades
By market direction
| At entry, last 90 bars | Trades | Won | P&L |
|---|---|---|---|
| Rising market | 20 | 90% | −$15,381.60 |
| Sideways market | 30 | 93% | $604.60 |
| Falling market | 12 | 100% | $12,801.80 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
6.8%
The 95th percentile of the worst drawdown when the same 62 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 3.0% to 6.8%; the tick is what actually happened, 3.6%. Order luck alone moves it this much.
All 62 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 9 Feb 2024 45,288.70 | 10 Feb 2024 47,132.80 | 1 | $1,844.12 |
| long | 28 Feb 2024 57,037.30 | 29 Feb 2024 62,432.10 | 1 | $5,394.76 |
| long | 27 Mar 2024 69,988.00 | 28 Mar 2024 70,687.90 | 1 | $699.88 |
| long | 8 Apr 2024 69,360.40 | 9 Apr 2024 71,620.00 | 1 | $2,259.62 |
| long | 22 Apr 2024 64,941.20 | 23 Apr 2024 66,819.30 | 1 | $1,878.17 |
| long | 6 May 2024 64,012.00 | 15 May 2024 64,652.10 | 9 | $640.12 |
| long | 16 May 2024 66,206.50 | 17 May 2024 66,868.60 | 1 | $662.07 |
| long | 28 May 2024 69,436.40 | 3 Jun 2024 70,130.80 | 6 | $694.37 |
| long | 6 Jun 2024 71,108.00 | 7 Jun 2024 71,819.10 | 1 | $711.08 |
| long | 3 Jul 2024 62,135.50 | 15 Jul 2024 62,756.80 | 12 | $621.36 |
| long | 16 Jul 2024 64,724.10 | 17 Jul 2024 65,371.30 | 1 | $647.25 |
| long | 29 Jul 2024 68,249.90 | 5 Aug 2024 51,187.40 | 7 | −$17,062.50 |
| long | 11 Aug 2024 60,923.50 | 13 Aug 2024 61,532.80 | 2 | $609.24 |
| long | 23 Aug 2024 60,375.80 | 24 Aug 2024 64,037.20 | 1 | $3,661.41 |
| long | 12 Sep 2024 57,338.00 | 13 Sep 2024 58,132.30 | 1 | $794.31 |
| long | 20 Sep 2024 62,948.00 | 22 Sep 2024 63,577.50 | 2 | $629.48 |
| long | 8 Oct 2024 62,224.00 | 11 Oct 2024 62,846.30 | 3 | $622.25 |
| long | 14 Oct 2024 62,870.00 | 15 Oct 2024 66,084.00 | 1 | $3,213.98 |
| long | 30 Oct 2024 72,736.40 | 6 Nov 2024 73,463.80 | 7 | $727.37 |
| long | 8 Nov 2024 75,857.90 | 9 Nov 2024 76,616.50 | 1 | $758.58 |
| long | 1 Dec 2024 96,408.00 | 2 Dec 2024 97,372.10 | 1 | $964.08 |
| long | 7 Dec 2024 99,740.80 | 8 Dec 2024 100,738.00 | 1 | $997.41 |
| long | 14 Dec 2024 101,424.00 | 15 Dec 2024 102,438.00 | 1 | $1,014.25 |
| long | 27 Dec 2024 95,791.60 | 2 Jan 2025 96,749.50 | 6 | $957.92 |
| long | 4 Jan 2025 98,174.20 | 6 Jan 2025 99,155.90 | 2 | $981.75 |
| long | 17 Jan 2025 99,987.30 | 18 Jan 2025 104,077.00 | 1 | $4,090.17 |
| long | 1 Feb 2025 102,430.00 | 11 Mar 2025 76,822.20 | 38 | −$25,607.40 |
| long | 17 Mar 2025 82,574.50 | 18 Mar 2025 84,010.00 | 1 | $1,435.50 |
| long | 26 Mar 2025 87,392.90 | 2 Apr 2025 88,266.80 | 7 | $873.93 |
| long | 12 Apr 2025 83,423.80 | 13 Apr 2025 85,276.90 | 1 | $1,853.08 |
| long | 20 Apr 2025 85,077.00 | 21 Apr 2025 85,927.80 | 1 | $850.77 |
| long | 4 May 2025 95,856.40 | 6 May 2025 96,815.00 | 2 | $958.57 |
| long | 9 May 2025 103,262.00 | 10 May 2025 104,294.00 | 1 | $1,032.62 |
| long | 21 May 2025 106,850.00 | 22 May 2025 109,644.00 | 1 | $2,793.99 |
| long | 10 Jun 2025 110,263.00 | 9 Jul 2025 111,366.00 | 29 | $1,102.64 |
| long | 12 Jul 2025 117,528.00 | 13 Jul 2025 118,703.00 | 1 | $1,175.28 |
| long | 9 Aug 2025 116,675.00 | 10 Aug 2025 117,841.00 | 1 | $1,166.75 |
| long | 5 Sep 2025 110,731.00 | 8 Sep 2025 111,838.00 | 3 | $1,107.31 |
| long | 10 Sep 2025 111,546.00 | 11 Sep 2025 113,960.00 | 1 | $2,413.62 |
| long | 1 Oct 2025 114,049.00 | 2 Oct 2025 118,595.00 | 1 | $4,546.05 |
| long | 26 Oct 2025 111,646.00 | 27 Oct 2025 114,559.00 | 1 | $2,913.14 |
| long | 12 Nov 2025 103,059.00 | 31 Jan 2026 77,294.30 | 80 | −$25,764.80 |
| long | 10 Feb 2026 70,138.00 | 15 Feb 2026 70,839.40 | 5 | $701.38 |
| long | 23 Feb 2026 67,643.40 | 25 Feb 2026 68,319.80 | 2 | $676.44 |
| long | 4 Mar 2026 68,338.00 | 5 Mar 2026 72,666.80 | 1 | $4,328.76 |
| long | 13 Mar 2026 70,541.30 | 14 Mar 2026 71,246.80 | 1 | $705.42 |
| long | 26 Mar 2026 71,336.50 | 7 Apr 2026 72,049.90 | 12 | $713.37 |
| long | 8 Apr 2026 71,924.20 | 9 Apr 2026 72,643.50 | 1 | $719.25 |
| long | 16 Apr 2026 74,810.00 | 17 Apr 2026 75,558.10 | 1 | $748.10 |
| long | 24 Apr 2026 78,257.50 | 27 Apr 2026 79,040.10 | 3 | $782.58 |
| long | 4 May 2026 78,568.60 | 5 May 2026 79,861.00 | 1 | $1,292.43 |
| long | 12 May 2026 81,745.70 | 5 Jun 2026 61,309.20 | 24 | −$20,436.40 |
| long | 14 Jun 2026 64,458.00 | 15 Jun 2026 65,746.40 | 1 | $1,288.44 |
| long | 23 Jun 2026 64,020.00 | 6 Jul 2026 64,660.20 | 13 | $640.21 |
| long | 13 Jul 2026 63,780.00 | 14 Jul 2026 64,417.80 | 1 | $637.80 |
| long | 17 Jul 2026 63,830.20 | 18 Jul 2026 64,468.50 | 1 | $638.31 |
| long | 21 Jul 2026 65,255.50 | 22 Jul 2026 66,556.10 | 1 | $1,300.64 |
| long | 6 Aug 2026 64,665.20 | 7 Aug 2026 65,311.90 | 1 | $646.66 |
| long | 20 Aug 2026 69,334.80 | 21 Aug 2026 73,027.00 | 1 | $3,692.24 |
| long | 6 Sep 2026 79,831.80 | 18 Sep 2026 80,630.10 | 12 | $798.32 |
| long | 21 Sep 2026 81,178.00 | 22 Sep 2026 86,620.00 | 1 | $5,442.00 |
| long | 3 Oct 2026 84,518.00 | 4 Oct 2026 85,363.20 | 1 | $845.18 |
Similar scripts
No other indicator or strategy here shares enough of its calculations.
Upgrade and export
Written in Pine Script v5. The clean-v5 step types the settings and moves when = into if blocks, which v6 requires; it is run before and after like any upgrade.
- 1 strategy call uses the when = argument (line 96). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
- No v6-only features. v6 is not needed to run this script.
Same 62 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 96: when = isLong → if block
⋯strategy.risk.allow_entry_in(strategy.direction.long)-strategy.entry(- id = "Long", - direction = strategy.long, - qty = qtyOrder, - when = isLong, - alert_message = "LN")+if isLong+ strategy.entry(+ id = "Long", + direction = strategy.long, + qty = qtyOrder, + alert_message = "LN")if (strategy.position_size > 0)⋯Does it run in Pine Script v6?
No
The v6 version runs in Zeal Terminal's engine, but its results differ from the v5 version: Trades differ: 62 before, 52 after on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. It is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.
⋯// © YohanNaftali-//@version=5+//@version=6///////////////////////////////////////////////////////////////////////////////⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 16 ms | With a workaround 1,000 bars in 51 ms | Not measured |
| PineTS | Not tested | Not tested | Not measured |
Zeal Terminal's engine currently reads an order side written as direction = strategy.long as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (line 96) so the engine trades the way the script says. The engine's host API cannot yet declare a chart's symbol and timeframe, so this page sets them on the engine directly for each market. Data the page supplies at the chart's own timeframe (another symbol, or Heikin-Ashi) reaches the script one bar later in Zeal Terminal's engine than on TradingView.
“Matches TradingView” needs this script's own output exported from TradingView and compared bar by bar, which has not been done for this script. The engine is built on OpenPineScript, whose conformance results are in its conformance report.
Source
Copied verbatim from the author's published script, with its licence header intact. The author may have updated the original since this snapshot.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/// © YohanNaftali//@version=5///////////////////////////////////////////////////////////////////////////////// Heikin Ashi Candle Startegy// ver 2021.12.29// © YohanNaftali// This script composed by Yohan Naftali for educational purpose only // Reader who will use this signal must do own research///////////////////////////////////////////////////////////////////////////////strategy( title = 'Heikin Ashi Candle Startegy Long', shorttitle = 'HA Strategy Long', format = format.price, precision = 6, overlay = true)// InputvalidationPeriod = input.int( defval = 3, title = 'Validation Period', group = 'Candle')qtyOrder = input.float( defval = 1.0, title = 'Qty', group = 'Order')maxActive = input.float( defval = 1.0, title = 'Maximum Active Open Position', group = 'Order')// Long StrategytpLong = input.float( defval = 1, title = "Take Profit (%)", minval = 0.0, step = 0.1, group = "Long") * 0.01slLong = input.float( defval = 25, title = "Stop Loss (%)", minval=0.0, step=0.1, group="Long") * 0.01trailingStopLong = input.float( defval = 0.2, title = "Trailing Stop (%)", minval = 0.0, step = 0.1, group = 'Long') * 0.01// CalculationhaTicker = ticker.heikinashi(syminfo.tickerid)haClose = request.security(haTicker, timeframe.period, close)haOpen = request.security(haTicker, timeframe.period, open)// LonglimitLong = tpLong > 0.0 ? strategy.position_avg_price * (1 + tpLong) : nastopLong = slLong > 0.0 ? strategy.position_avg_price * (1 - slLong) : nafloat trailLong = 0.0trailLong := if strategy.position_size > 0 trailClose = close * (1 - trailLong) math.max(trailClose, trailLong[1])else 0isGreen = truefor i = 0 to validationPeriod-1 isGreen := isGreen and haClose[i] > haOpen[i] isLong = isGreen and haClose[validationPeriod] < haOpen[validationPeriod]plot( limitLong, title = 'Limit', color = color.rgb(0, 0, 255, 0), style = plot.style_stepline, linewidth = 1)plot( trailLong, title = 'Trailing', color = color.rgb(255, 255, 0, 0), style = plot.style_stepline, linewidth = 1)plot( stopLong, title = 'Stop', style = plot.style_stepline, color = color.rgb(255, 0, 0, 0), linewidth = 1)// plotshape(// isLong, // title = 'Entry', // style = shape.arrowup, // location = location.belowbar, // offset = 1, // color = color.new(color.green, 0), // text = 'Long Entry',// size = size.small)// Strategystrategy.risk.max_position_size(maxActive)strategy.risk.allow_entry_in(strategy.direction.long)strategy.entry( id = "Long", direction = strategy.long, qty = qtyOrder, when = isLong, alert_message = "LN")if (strategy.position_size > 0) strategy.exit( id = "Long Exit", from_entry = "Long", limit = limitLong, stop = stopLong, trail_price = trailLong, alert_message = "LX")