{"id":1927,"date":"2023-07-11T14:00:00","date_gmt":"2023-07-11T14:00:00","guid":{"rendered":"https:\/\/www.tejwin.com\/en\/insights\/what-is-the-look-ahead-bias\/"},"modified":"2026-09-23T18:15:43","modified_gmt":"2026-09-23T18:15:43","slug":"what-is-the-look-ahead-bias","status":"publish","type":"insight","link":"https:\/\/www.tejwin.com\/en\/insights\/what-is-the-look-ahead-bias\/","title":{"rendered":"What is the Look-ahead Bias ?"},"content":{"rendered":"\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" alt=\"What is the Look-ahead Bias ? \" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/0CqSGzwHrv6bS_Uwu.jpg\"\/><figcaption class=\"wp-element-caption\">Photo by <a href=\"https:\/\/unsplash.com\/@emilegt?utm_source=medium&amp;utm_medium=referral\" rel=\"noreferrer noopener\" target=\"_blank\">Emile Guillemot<\/a> on\u00a0<a href=\"https:\/\/unsplash.com?utm_source=medium&amp;utm_medium=referral\" rel=\"noreferrer noopener\" target=\"_blank\">Unsplash<\/a><\/figcaption><\/figure>\n<h2 class=\"wp-block-heading\">Highlight<\/h2>\n<ul class=\"wp-block-list\">\n<li>Difficulty\uff1a\u2605\u2605\u2605\u2606\u2606<\/li>\n<li>Automated trading via Bollinger Band<\/li>\n<li>Demonstrate how the look-ahead bias affects the trading result<\/li>\n<\/ul>\n<h2 class=\"wp-block-heading\">Preface<\/h2>\n<p class=\"wp-block-paragraph\"><mark class=\"has-inline-color has-vivid-green-cyan-color\" style=\"background-color:rgba(0, 0, 0, 0)\"><strong>Look-ahead bias <\/strong><\/mark>is the phenomenon that unconsciously uses unavailable or unrevealed data in analyzing or simulating historical events.<br\/>It exists in the processes of making decisions or evaluations which use information or data that was unknown at that time.\u00a0<br\/>Look-ahead bias may cause distortion and misleadingness of the result because it violates the principle of using only information available during analysis. It could emerge in any field, such as finance, economy, and data analysis, and influence investment strategy, backtesting of the trading system, and performance grading.<\/p>\n<p class=\"wp-block-paragraph\">Today\u2019s practice will demonstrate a typical scenario of Look-ahead bias \u2501 using historical data, which contains the info of future variety in testing trading strategy. However, the info is unrevealed during the period of testing. This phenomenon may cause man-made overestimated performance and unrealistic expectations of the strategy\u2019s profitability.<\/p>\n<h2 class=\"wp-block-heading\">Programming environment and Module\u00a0required<\/h2>\n<p class=\"wp-block-paragraph\">MacOS and Jupyter Notebook is used as editor<\/p>\n<pre class=\"wp-block-preformatted\"><code>import pandas as pd \nimport re\nimport numpy as np \nimport tejapi\nfrom functools import reduce\nimport matplotlib.pyplot as plt\nfrom collections import defaultdict, OrderedDict\nfrom tqdm import trange, tqdm\nimport plotly.express as px\nimport plotly.graph_objects as go\n\ntejapi.ApiConfig.api_key = \"Your api key\"\ntejapi.ApiConfig.ignoretz = True<\/code><\/pre>\n<h2 class=\"wp-block-heading\">Database<\/h2>\n<ul class=\"wp-block-list\">\n<li>Listed (OTC) adjusted stock price (day)\u200a\u2014\u200aaverage price<\/li>\n<li>unadjusted (day) technical indicator<\/li>\n<\/ul>\n<h2 class=\"wp-block-heading\">Import data<\/h2>\n<p class=\"wp-block-paragraph\">For the period from 2021\u201306\u201301 to 2022\u201312\u201331, we take YangMing Marine Transport Corporation(2609) as an example, we will use unadjusted closed price\u3001BB-Upper(20)\u3001BB-Lower(20) to construct the Bollinger Band, and then we will compare the return with Market Return Index(Y9997)<\/p>\n<p class=\"wp-block-paragraph\">After acquiring the stock price and technical indicators data, as in the previous article, we use <em>plotly.express <\/em>to visualize our Bollinger Band. In the diagram, bbu20 will be the upper track \u3001bbl20 will be the lower track, and close_d will be the closed price.<\/p>\n<figure class=\"wp-block-image aligncenter is-resized caption-align-center\"><img decoding=\"async\" alt=\"YangMing Marine Transport Corporation(2609) Bollinger Band\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/1KfJZPn-vBA4he9HoZPwUTQ.png\" style=\"width:840px;height:447px\"\/><figcaption class=\"wp-element-caption\">YangMing Marine Transport Corporation(2609) Bollinger Band<\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\">Next, we will implement two Bollinger Band trading strategies and compare their differences.<\/p>\n<ol class=\"wp-block-list\">\n<li>Same as the previous article, when the closed price touches the upper track, we will sell our holding position at<em><strong> tomorrow\u2019s open price<\/strong><\/em>; when the closed price touch the lower track, we will buy 1 unit at <em><strong>tomorrow\u2019s opened price<\/strong><\/em>; when the conditions above are already satisfied, we remain adequate principal, both the holding position and the closed price are lower than last time buying price, we will buy one more unit.<\/li>\n<li>when the closed price touches the upper track, we will sell our holding position at<strong> <em>today\u2019s closed price<\/em><\/strong>; when the closed price touch the lower track, we will buy 1 unit at <em><strong>today\u2019s closed price<\/strong><\/em>; when the conditions above are already satisfied, we remain adequate principal, both the holding position and the closed price are lower than last time buying price, we will buy one more unit.<\/li>\n<\/ol>\n<p class=\"wp-block-paragraph\">In light of the only difference in strategies is the transaction\u2019s unit price, we modify our strategy code in the previous article. We define our strategy in <em>def<\/em> <em>bollingeband_strategy<\/em>, add an<em> if <\/em>condition, and set a parameter\u200a\u2014\u200a<em>mode<\/em> to control which strategy we want to execute. When <em>mode <\/em>is<em> True, <\/em>execute strategy 1; when <em>mode <\/em>is <em>False, <\/em>run strategy 2.<\/p>\n<p class=\"wp-block-paragraph\">Now we build an automated trading strategy. The return of the <em>def<\/em> <em>bollingeband_strategy<\/em> is a transaction table that could let us understand each transaction\u2019s details. Further, we define a <em>def simplify <\/em>to summarize all info for better readability.<\/p>\n<p class=\"wp-block-paragraph\">The final step is calculating both strategies\u2019 performance. Basically, the code in this part is similar to the previous code. However, because of the upgrade of the pandas version, the latest version no longer supports the function <em>append<\/em>; we made a slight modification to make the code can keep working and wrap it in <em>def<\/em> <em>back_test.<\/em><\/p>\n<div style=\"border: 1px black; border-style: solid none; text-align: center; border-color: #296580; padding: 24px; margin-top: 24px; margin-bottom: 24px;\">\n<p style=\"margin: 0px; font-size: 24px; font-weight: bold; line-height: 1.5;\">Get Started with TEJ Comprehensive Historical Data Now!<br\/>Make a Better Trading Decision Today.<\/p>\n<div style=\"margin-top: 32px;\"><strong><a href=\"\/en\/databank-solution\/market-data\/\" style=\"border: none; border-radius: 4px; background-color: #296580; color: white; font-size: 20px; width: fit-content; text-decoration: none; padding: 12px 30px 12px 30px;\">TEJ Databank Solutions<\/a><\/strong><\/div>\n<\/div>\n<p class=\"wp-block-paragraph\">So far, we already finished the whole coding process, then we can compare both strategies\u2019 performance with real data.<\/p>\n<h3 class=\"wp-block-heading\">Trade at the open price of tomorrow<\/h3>\n<pre class=\"wp-block-preformatted\"><code>principal = 500000\ncash = principal\nposition = 0\norder_unit = 0\n\ntrade_book = bollingeband_strategy(data, principal, cash, position, order_unit, True)\ntrade_book_ = simplify(trade_book)\nback_test(principal, trade_book_, data, market)<\/code><\/pre>\n<figure class=\"wp-block-image aligncenter caption-align-center\"><img decoding=\"async\" alt=\"Trade at the open price of tomorrow\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/1vz3y6RzsQ5NufmL12lICsg.png\"\/><figcaption class=\"wp-element-caption\">Trade at the open price of tomorrow<\/figcaption><\/figure>\n<h3 class=\"wp-block-heading\">\u00a0Trade at the closed price of today<\/h3>\n<pre class=\"wp-block-preformatted\"><code>principal = 500000\ncash = principal\nposition = 0\norder_unit = 0\n\ntrade_book_cu_close = bollingeband_strategy(data, principal, cash, position, order_unit, False)\ntrade_book_cu_close_ = simplify(trade_book_cu_close)\nback_test(principal, trade_book_cu_close_, data, market)<\/code><\/pre>\n<figure class=\"wp-block-image aligncenter caption-align-center\"><img decoding=\"async\" alt=\"Trade at the closed price of today\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/1xTQOaCyhYThlBMZUdTBv2g.png\"\/><figcaption class=\"wp-element-caption\">Trade at the closed price of today<\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\">By observing the results of two trading strategies, it can be noticed that trading based on the day\u2019s closing price yields better overall performance. However, novice stock market participants may mistakenly use the historical backtesting data and assume the closing price as the trading price, disregarding the fact that it is impossible to know the closing price in advance in the actual market. <mark class=\"has-inline-color has-vivid-green-cyan-color\" style=\"background-color:rgba(0, 0, 0, 0)\"><strong>Using information that is not known when trading for backtesting constitutes a \u201clook-ahead bias,\u201d<\/strong><\/mark> resulting in discrepancies in the backtesting results. Therefore, it is advisable to<strong><span class=\"has-inline-background\" style=\"background-color:#f1db9d\"> use the next day\u2019s opening price as the trading price to reflect the most accurate trading conditions.<\/span><\/strong><\/p>\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n<p class=\"wp-block-paragraph\">Through this implementation of simple trading backtest, we have demonstrated the presence of the look-ahead bias in backtesting, which is not limited to trading alone but is a common occurrence in the field of finance. In order to avoid the look-ahead bias,<mark class=\"has-inline-color has-vivid-green-cyan-color\" style=\"background-color:rgba(0, 0, 0, 0)\"><strong> it is crucial to ensure that historical analysis or decision-making processes are based solely on the information available at that time<\/strong><\/mark>. This requires using historical data in a manner consistent with what was known in the past, excluding any subsequent information that was not available at the time. Being aware of the look-ahead bias and handling the data with caution is essential for maintaining the integrity and accuracy of statistical analysis and decision-making processes.<\/p>\n<div aria-hidden=\"true\" class=\"wp-block-spacer\" style=\"height:16px\"><\/div>\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button has-custom-width wp-block-button__width-100 is-style-fill\"><a class=\"wp-block-button__link has-background has-custom-font-size wp-element-button\" href=\"\/en\/databank-solution\/market-data\/\" style=\"border-radius:14px;background:linear-gradient(135deg,rgb(137,219,231) 0%,rgb(25,56,194) 100%);font-size:21px\"><strong>Avoid the Look-ahead Bias and Make a Better Trading Decision. <br\/>Get Started with TEJ Comprehensive Historical Data Now!<\/strong><\/a><\/div>\n<\/div>\n<div aria-hidden=\"true\" class=\"wp-block-spacer\" style=\"height:16px\"><\/div>\n<p class=\"wp-block-paragraph\">Last but not least, please note that the \u201c<strong>Stocks this article mentions are just for discussion. Please do not consider it to be any recommendations or suggestions for investment or products.\u201d<\/strong>\u00a0Hence, if you are interested in issues like Creating Trading Strategy, Performance Backtesting, and Evidence-based research, you are welcome to purchase the plans offered in\u00a0<a aria-label=\"TEJ E-Shop (opens in a new tab)\" class=\"ek-link\" href=\"\/en\/solution\/quantitative-finance-solution\/\" rel=\"noreferrer noopener\" target=\"_blank\">TEJ Quantitative Solutio<\/a><a aria-label=\"TEJ E-Shop (opens in a new tab)\" class=\"ek-link\" href=\"\/en\/solution\/quantitative-finance-solution\/\" rel=\"noreferrer noopener\" target=\"_blank\">n<\/a>\u00a0and use the well-complete database to create your optimal trading strategy.<\/p>\n<div aria-hidden=\"true\" class=\"wp-block-spacer\" style=\"height:20px\"><\/div>\n<h2 class=\"wp-block-heading\">Source Code<\/h2>\n<ul class=\"wp-block-list\">\n<li><a class=\"ek-link\" href=\"https:\/\/gist.github.com\/tej87681088\/db30a28736462434914d96452ef4b4a7\" rel=\"noopener\" target=\"_blank\">Click here to go Github<\/a><\/li>\n<\/ul>\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-tej-en wp-block-embed-tej-en\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"Oa4ENoiyaa\"><a href=\"https:\/\/www.tejwin.com\/en\/insights\/options-pricing-with-monte-carlo-simulation\/\">Options Pricing with Monte Carlo Simulation<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; visibility: hidden;\" title=\"\u201cOptions Pricing with Monte Carlo Simulation\u201d \u2014 TEJ\" src=\"https:\/\/www.tejwin.com\/en\/insights\/options-pricing-with-monte-carlo-simulation\/embed\/#?secret=3dL7BKBVfY#?secret=Oa4ENoiyaa\" data-secret=\"Oa4ENoiyaa\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-tej-en wp-block-embed-tej-en\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"msHTjZKlL5\"><a href=\"https:\/\/www.tejwin.com\/en\/insights\/survivorship-bias\/\">Survivorship Bias<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; visibility: hidden;\" title=\"\u201cSurvivorship Bias\u201d \u2014 TEJ\" src=\"https:\/\/www.tejwin.com\/en\/insights\/survivorship-bias\/embed\/#?secret=ZkBD0uyjN4#?secret=msHTjZKlL5\" data-secret=\"msHTjZKlL5\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n<h2 class=\"wp-block-heading\">Extended Reading<\/h2>\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<\/div>\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Look-ahead bias is the phenomenon that unconsciously uses unavailable or unrevealed data in analyzing or simulating historical events.<br \/>\nIt exists in the processes of making decisions or evaluations which use information or data that was unknown at that time.\u00a0<br \/>\nLook-ahead bias may cause distortion and misleadingness of the result because it violates the principle of using only information available during analysis. It could emerge in any field, such as finance, economy, and data analysis, and influence investment strategy, backtesting of the trading system, and performance grading.<\/p>\n","protected":false},"featured_media":1926,"template":"","tags":[64,91],"insight_category":[88],"class_list":["post-1927","insight","type-insight","status-publish","has-post-thumbnail","hentry","tag-point-in-time","tag-beginners-guide","insight_category-market-knowledge-data-guides"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight\/1927","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight"}],"about":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/types\/insight"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/media\/1926"}],"wp:attachment":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/media?parent=1927"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/tags?post=1927"},{"taxonomy":"insight_category","embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight_category?post=1927"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}