TradingView
PineCoders
Nov 11, 2019 1:13 PM

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

Bitcoin / U.S. dollarBitstamp

Description

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.



Look first. Then leap.

Release Notes

Updated comments.

Release Notes

Updated comments.

Release Notes

Fixed a bug in the code determining the label type. Thx allanster!
Comments
cryptovesta
@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!
JMProTrader
I'm hardly dissapointed - I went down to the 1 second timeframe to avoid this! But I am looking such clean charts, without wicks, and in comparisson to japanesse candles I've seen some difference. Also 1 second is not enough to capture brick formation, but most importantly is the section b) in this article: tradingview.com/support/solutions/43000480330-the-renko-chart-shows-incorrect-values-and-or-is-constantly-being-recalculated/

This is an absolutely terrible measure! Number one they state as a matter of fact real tick data can't be provided... What type of mafia cartel is this?
Number two and even worse... In order to fill historical data when there is not... Then they just make it up with historical data from higher time frames!
This is such a disservice and blatant missleading absurdity! You know all the assumptions I've made because of not knowing this? I had to lead myself into assuming something must be wrong... This goes so against the purpose of historical data in order to reflect backtesting result to live testing results. Otherwise what is the point for it to exist? Mislead people into false expectations and failure? I'm totally dishearted and confused about this. This makes no sense at all.

It was enough figuring out time frame applies to renko bricks, but then I go down to the very 1 second to get the clearest picture, and turns out they make up historical bricks recalculated with the data of higher time frames. This should be flagged even as a scam! It truly is a trap of confusion and how would you know if you rely on TradingView as a data representation provider that they would go out of their way to reflect into a time frame data from another one to make up bricks?!

This should be raised up and most importantly a solution be found, and TV shouldn't at all keep this falsy policy since the goal of historical data is to reflect what has happened not something made up that has no informational value but leading people into a fallacy... & Lastly, now I am left in doubt wether all my strategy plan will even work after so much high hopes because of this!!!
PineCoders
@josea, As the article mentions, the use of higher TFs occurs "when no more historical data is available at the current chart's timeframe". The Renko brick building logic is clearly explained in the article. While it may not be to your liking, we don't see how that constitutes a scam. As to backtesting on Renko charts, the warning you see when doing so should be heeded: don't.
JMProTrader
@PineCoders, It is not that it is not of my liking. It is, that is completely wrong! I do understand that backtesting results can be wrong in timeframes from the 1 minute up, but on the 1 second chart, with a few exceptions, you should simply get the brick formation filled after 1 second, and be left with that rendered brick as is. Also, when are you suppossed to know that "no more historical data is available"? Charts on the 1 second renko arrive up to March of this year with my box size. The issue here is, that wathever renko bars are traced back and shown, should be the ones that were formed live under the specific time frame that you might be using. What can't happen, if that without notice, tracing an undefined amount of time, would show bricks that relate no to the time frame you are using a traditional box size with. This is totally confusing and I am totally surprised you want to argue is a matter of likeability and not a matter of logic. Just as an example, then why to bother about using a traditional box size? I understand time frame can have its sense okay... But don't tell me that after a period of time, if I look on the charts, the bricks that are shown are not the ones that were formed with a given brick size. It should simply render what is formed on the time frame for a given brick size up until there is data available and that's it. Also, to deepen historical data they do have their packages. So you are telling me all brick formation is correct until it points out this is the last brick available?
CryptoZeeKay
@JMProTrader, I totally agree with you.
@PineCoders Renko in TV is misleading not inline with Renko as Renko. TV have their own definition of Renko which is confusing and misleading.
JMProTrader
@PineCoders, Also, if you want to trade with renko charts, you are going to want to backtest your strategy. So your proposal of not backtesting is incorrect, it should be to simply: find another platform that is able to render a box size when price fills the level and stay away from this mess with recalculations on higher time frames etc. So if they don't have the data, they make it up. Well I can build my platform as that as well. I might just fill the bricks with the sun's magnetic field variations "when there is no more historic data left". I'd appreciate you'd provide value and solutions and not simply tell me to give up on backtesting renko which by default is discharding renko in the first place given that any sensible trader will want to back test at some point before live testing.
JMProTrader
@PineCoders, Lastly: how is it possible Trading View states as a matter of fact it is not possible to find a real tick data provider? This doesn't make sense at all. In fact, that is the reason why they suppossedly offer data packages, which I thought to be of value. Any recalculation must be erased from renko, and only if the user decides to follow thru' the choice should be clear to the end user. As a default, any available historic renko bricks should simply render the closing box size in the choosen time frame and that's it.
PineCoders
@josea, It is up to you to choose whatever platform you feel meets your requirements. The full quote from our Help Center page is this, which says something completely different than what you mention. It doesn't say tick data doesn't exist; it says we can't provide the tick data required to build Renko bricks with maximal precision:
"TradingView and data suppliers cannot make available the complete historical tick data required to calculate Renko bricks with maximal precision readily available, therefore, compromises must be made, as is common on other trading platforms."

As to placing orders using synthetic prices, it is of course also up to you to choose if you want to do that. We don't see why any rigorous trader would want to work this way, so we will continue to recommend to our users that they shouldn't.
JMProTrader
@PineCoders, This all only shows that you don't understand renko and you think that the faulty decisions made in Trading View have to do with Renko. Price shouldn't ever fill on synthetic prices only when price reaches in the form of a pending order the price determined by the closing renko box for example. This is how renkos are suppossed to work. The choosing of the brik size is suppossed to match with the pip movement on real price data and that's it. There shouldn't be anything synthetic at all. Renko is about price movement, so when a given set of price moves in a given amount of time a box should be formed and that's it. I believe as you state any serious trader should understand that which you don't. Synthetic data is not on Renko, is on Trading View! Also you are unable to answer what does that shady comment "when no more historical data is available" the recalculations are performed. This is not renko therefore, since Renko's definition doesn't imply any recalculation except for with the ATR value! Traditional box size should never imply any time of recalculation since this is violating the utility of having access to a platform that renders price data. No one has talked about placing orders on synthetic prices at all. The product is not working as expected for a traditional box size on a given time frame for renko and what you are talking is defending a bad service in the platform for doing so since you are not defending the interests of a rigurous system for renko in trading view given that you completely ignore what renko is about. There is no synthetic prices in renko, in renko there is only price movement. I would have never expected I'd found a moderator so interested in defending his own ignorance on a topic you are suppossed to help with. Also I supposse you are completely fine with not knowing when the price recalculations are going to be implemented "when no more historical data is available". It could be 2 weeks ago, 1 month ago. Also I'd appreciate other than throwing wrong assumptions about myself in a passive aggressive manner you inform yourself on how renko works and the difference between faulty decisions made on it in TradingView and what Renko really is about because you have it all backwards.
PineCoders
@josea, This publication is about warning traders against the use of non-standard charts when backtesting. We explain why we don't recommend it and provide a strategy script that unequivocally demonstrates how it's a bad idea. Disagree all you want with our recommendations; we stand by them and our code proves why we should. You are of course free to pursue your alternate views on reality.

You chose to comment here about another subject, namely Renko brick-building. We tried to help you clear some of your misconceptions about what the Help Center text says, and explain why your allegation to the effect that there is a scam going on here is not factual, as TradingView's Renko brick-building logic is clearly and honestly documented in the Help Center.

We are not the designers of the Renko brick-building algorithm. We are TradingView users who are traders and Pine programmers, and we help the Pine coding community. If you want more details on Renko brick-building or wish to argue design decisions, contact support.
More