PineCoders

Backtesting on Non-Standard Charts: Caution! - PineCoders FAQ

Much confusion exists in the TradingView community about backtesting on non-standard charts. This script tries to shed some light on the subject in the hope that traders make better use of those chart types.
Non-standard charts are:
  • Heikin Ashi (HA)
  • Renko
  • Kagi
  • Point & Figure
  • Range

These chart types are called non-standard because they all transform market prices into synthetic views of price action. Some focus on price movement and disregard time. Others like HA use the same division of bars into fixed time intervals but calculate artificial open, high, low and close (OHLC) values.

Non-standard chart types can provide traders with alternative ways of interpreting price action, but they are not designed to test strategies or run automated traded systems where results depend on the ability to enter and exit trades at precise price levels at specific times, whether orders are issued manually or algorithmically. Ironically, the same characteristics that make non-standard chart types interesting from an analytical point of view also make them ill-suited to trade execution. Why? Because of the dislocation that a synthetic view of price action creates between its non-standard chart prices and real market prices at any given point in time. Switching from a non-standard chart price point into the market always entails a translation of time/price dimensions that results in uncertainty—and uncertainty concerning the level or the time at which orders are executed is detrimental to all strategies.

The delta between the chart’s price when an order is issued (which is assumed to be the expected price) and the price at which that order is filled is called slippage. When working from normal chart types, slippage can be caused by one or more of the following conditions:
• Time delay between order submission and execution. During this delay the market may move normally or be subject to large orders from other traders that will cause large moves of the bid/ask levels.
• Lack of bids for a market sell or lack of asks for a market buy at the current price level.
• Spread taken by middlemen in the order execution process.
• Any other event that changes the expected fill price.


When a market order is submitted, matching engines attempt to fill at the best possible price at the exchange. TradingView strategies usually fill market orders at the opening price of the next candle. A non-standard chart type can produce misleading results because the open of the next candle may or may not correspond to the real market price at that time. This creates artificial and often beneficial slippage that would not exist on standard charts.

Consider an HA chart. The open for each candle is the average of the previous HA bar’s open and close prices. The open of the HA candle is a synthetic value, but the real market open at the time the new HA candle begins on the chart is the unrelated, regular open at the chart interval. The HA open will often be lower on long entries and higher on short entries, resulting in unrealistically advantageous fills.

Another example is a Renko chart. A Renko chart is a type of chart that only measures price movement. The purpose of a Renko chart is to cluster price action into regular intervals, which consequently removes the time element. Because Trading View does not provide tick data as a price source, it relies on chart interval close values to construct Renko bricks. As a consequence, a new brick is constructed only when the interval close penetrates one or more brick thresholds. When a new brick starts on the chart, it is because the previous interval’s close was above or below the next brick threshold. The open price of the next brick will likely not represent the current price at the time this new brick begins, so correctly simulating an order is impossible.

Some traders have argued with us that backtesting and trading off HA charts and other non-standard charts is useful, and so we have written this script to show traders what happens when order fills from backtesting on non-standard charts are compared to real-world fills at market prices.

Let’s review how TV backtesting works. TV backtesting uses a broker emulator to execute orders. When an order is executed by the broker emulator on historical bars, the price used for the fill is either the close of the order’s submission bar or, more often, the open of the next. The broker emulator only has access to the chart’s prices, and so it uses those prices to fill orders. When backtesting is run on a non-standard chart type, orders are filled at non-standard prices, and so backtesting results are non-standard—i.e., as unrealistic as the prices appearing on non-standard charts. This is not a bug; where else is the broker emulator going to fetch prices than from the chart?

This script is a strategy that you can run on either standard or non-standard chart types. It is meant to help traders understand the differences between backtests run on both types of charts. For every backtest, a label at the end of the chart shows two global net profit results for the strategy:
• The net profits (in currency) calculated by TV backtesting with orders filled at the chart’s prices.
• The net profits (in currency) calculated from the same orders, but filled at market prices (fetched through security() calls from the underlying real market prices) instead of the chart’s prices.

If you run the script on a non-standard chart, the top result in the label will be the result you would normally get from the TV backtesting results window. The bottom result will show you a more realistic result because it is calculated from real market fills.

If you run the script on a normal chart type (bars, candles, hollow candles, line, area or baseline) you will see the same result for both net profit numbers since both are run on the same real market prices. You will sometimes see slight discrepancies due to occasional differences between chart prices and the corresponding information fetched through security() calls.

Features
• Results shown in the Data Window (third icon from the top right of your chart) are:
— Cumulative results
— For each order execution bar on the chart, the chart and market previous and current fills, and the trade results calculated from both chart and market fills.
• You can choose between 2 different strategies, both elementary.
• You can use HA prices for the calculations determining entry/exit conditions. You can use this to see how a strategy calculated from HA values can run on a normal chart. You will notice that such strategies will not produce the same results as the real market results generated from HA charts. This is due to the different environment backtesting is running on where for example, position sizes for entries on the same bar will be calculated differently because HA and standard chart close prices differ.
• You can choose repainting/non-repainting signals.
• You can show MAs, entry/exit markers and market fill levels.
• You can show candles built from the underlying market prices.
• You can color the background for occurrences where an order is filled at a different real market price than the chart’s price.

Notes
• On some non-standard chart types you will not obtain any results. This is sometimes due to how certain types of non-standard types work, and sometimes because the script will not emit orders if no underlying market information is detected.
• The script illustrates how those who want to use HA values to calculate conditions can do so from a standard chart. They will then be getting orders emitted on HA conditions but filled at more realistic prices because their strategy can run on a standard chart.
• On some non-standard chart types you will see market results surpass chart results. While this may seem interesting, our way of looking at it is that it points to how unreliable non-standard chart backtesting is, and why it should be avoided.
• In order not to extend an already long description, we do not discuss the particulars of executing orders on the realtime bar when using non-standard charts. Unless you understand the minute details of what’s going on in the realtime bar on a particular non-standard chart type, we recommend staying away from this.
• Some traders ask us: Why does TradingView allow backtesting on non-standard chart types if it produces unrealistic results? That’s somewhat like asking a hammer manufacturer why it makes hammers if hammers can hurt you. We believe it’s a trader’s responsibility to understand the tools he is using.

Takeaways
• Non-standard charts are not bad per se, but they can be badly used.
• TV backtesting on non-standard charts is not broken and doesn’t require fixing. Traders asking for a fix are in dire need of learning more about trading. We recommend they stop trading until they understand why.
• Stay away from—even better, report—any vendor presenting you with strategies running on non-standard charts and implying they are showing reliable results.
• If you don’t understand everything we discussed, don’t use non-standard charts at all.
• Study carefully how non-standard charts are built and the inevitable compromises used in calculating them so you can understand their limitations.

Thanks to @allanster and @mortdiggiddy for their help in editing this description.
Release Notes: Updated comments.
Release Notes: Updated comments.
Release Notes: Fixed a bug in the code determining the label type. Thx allanster!
Remove from Favorite Scripts Add to Favorite Scripts
Tools and ideas for all Pine coders: http://www.pinecoders.com
Pine Wizards: https://www.tradingview.com/pine-wizards/
Our Pine FAQ & Code: http://www.pinecoders.com/faq_and_code/
Get Pine news and tips on Telegram: https://t.me/PineCodersSquawkBox

Comments

@PineCoders, Excellent article and code illustration. When I started trading RENKO, I was taken aback by the negativity associated with RENKO on TV and it took time and manual effort to figure out why folks were complaining.
I wish I knew about this article back then. Great job guys!
+5 Reply
Backtest Rookies also has a good write-up on why Heikin Ashi prices shouldn't be used to place entries/exits:
https://backtest-rookies.com/2018/01/26/backtrader-working-heikin-ashi/
+2 Reply
Very interesting. Thank you.
You know, I would say: some steps must be done every time. If so- you can provide an API for this. For example: open, high, low, close - based only on standard candlesticks. open_ha, low_ha...- based on heikin ashi candlesticks. So, can reuce all complexity by reviewing new scripts and make development easier )
+1 Reply
PineCoders DrLotto
@DrLotto, The `heikinashi()` function can help with that. Eventually, new built-in variable names could also be created for HA values, as you suggest. In the meantime, @allanster has useful scripts to help with HA calcs:
+1 Reply
@PineCoders & @LucF, Thank you for yet another wonderful explanation and coding example!
From my understanding from your article, it is possible to use the candle-chart (standard for real prices) combined with an HA-chart (with MA's), to produce possible buy/sell signals?
Thanks in advance!
+1 Reply
PineCoders TradingTom94
@TradingTom94, If you are thinking of strategies, then because it uses chart's prices it needs to be on a standard chart, but the calculations can be done using any source you want, including HA. If you are talking about a discretionary trading setup where you would be using two different charts, sure, it's possible. An alternative could be to use `ohlc4` or `hlc3` as the MA source on a normal chart, which compared to the usual `close` will produce smoother lines. Whichever method is used, the important thing is to avoid using the non-standard chart prices to determine the expected fill prices for your orders.
+2 Reply
TradingTom94 PineCoders
@PineCoders,
Indeed, my strategy uses two different charts. The main chart is the standard candle chart, to get the correct price. The second, non-overlay, chart is the HA with TEMA's calculated based on the HA-chart. I'll definitely test the alternative using both 'ohlc4' and 'hlc3' settings for the MA's, to compare my results.
Thank you for taking the time to make it crystal clear!
+1 Reply
Can someone tell me how to use this script ie display market price and chart price with my strategy?
Reply
PineCoders Food_Plus
@Food_Plus, It's all in there in the code. `realOpen` is the real market open and `chartOpen` is the chart open. Be very careful in how you use `real*` variables. The `security()` call they use is custom-designed to work on non-standard charts, as lookahead needs to be used on that type of chart. If you use that technique on a normal chart, your script will be using future data on historical bars, which is never good for prices other than `open`.
Reply
Does the traditional renko chart redraw the bricks?
Reply
Home Stock Screener Forex Screener Crypto Screener Economic Calendar How It Works Chart Features Pricing Refer a friend House Rules Help Center Website & Broker Solutions Widgets Charting Solutions Lightweight Charting Library Blog & News Twitter
Profile Profile Settings Account and Billing Referred friends Coins My Support Tickets Help Center Ideas Published Followers Following Private Messages Chat Sign Out