{"id":201588,"date":"2024-01-29T11:23:20","date_gmt":"2024-01-29T16:23:20","guid":{"rendered":"https:\/\/ibkrcampus.com\/?p=201588"},"modified":"2024-01-29T11:23:28","modified_gmt":"2024-01-29T16:23:28","slug":"backtesting-py-an-introductory-guide-to-backtesting-with-python","status":"publish","type":"post","link":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/backtesting-py-an-introductory-guide-to-backtesting-with-python\/","title":{"rendered":"Backtesting.py &#8211; An Introductory Guide to Backtesting with Python"},"content":{"rendered":"\n<p><em>The article &#8220;<a href=\"https:\/\/algotrading101.com\/learn\/backtesting-py-guide\/\">Backtesting.py \u2013 An Introductory Guide to Backtesting with Python<\/a>&#8221; first appeared on AlgoTrading101 blog.<\/em><\/p>\n\n\n\n<p><em>Excerpt<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-what-is-backtesting-py\">What is Backtesting.py?<\/h2>\n\n\n\n<p>Backtesting.py is an open-source backtesting Python library that allows users to test their trading strategies via code.<\/p>\n\n\n\n<p><a>Link:&nbsp;<\/a><a href=\"https:\/\/github.com\/kernc\/backtesting.py\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/github.com\/kernc\/backtesting.py<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-what-is-backtesting-py-used-for\">What is Backtesting.py used for?<\/h2>\n\n\n\n<p>Algorithmic traders often use Backtesting.py to backtest, optimize, research, and improve different trading strategies. To learn more about backtesting and its benefits please read the following article:<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-quantitative-trading-ideas-and-guides-algotrading-101-blog wp-block-embed-quantitative-trading-ideas-and-guides-algotrading-101-blog\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"9yOLwJ9PG5\"><a href=\"https:\/\/algotrading101.com\/learn\/backtesting-guide\/\">What is Backtesting? 3 Aims of Backtesting<\/a><\/blockquote><iframe class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;What is Backtesting? 3 Aims of Backtesting&#8221; &#8212; Quantitative Trading Ideas and Guides - AlgoTrading101 Blog\" src=\"https:\/\/algotrading101.com\/learn\/backtesting-guide\/embed\/#?secret=ZGbuz0AREv#?secret=9yOLwJ9PG5\" data-secret=\"9yOLwJ9PG5\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Why should I use Backtesting.py?<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Backtesting.py is easy to use.<\/li>\n\n\n\n<li>Backtesting.py is open-sourced.<\/li>\n\n\n\n<li>Is compatible with forex, crypto, stocks, futures, and more.<\/li>\n\n\n\n<li>Offers interactive charts.<\/li>\n\n\n\n<li>Allows for vectorized or event-based backtesting.<\/li>\n\n\n\n<li>Has a built-in optimizer.<\/li>\n\n\n\n<li>Is actively maintained.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Why shouldn\u2019t I use Backtesting.py?<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Backtesting.py could use more features.<\/li>\n\n\n\n<li>Doesn\u2019t support the use of multiple assets at the same time.<\/li>\n\n\n\n<li>Data that the strategy needs is constrained (OHLCV).<\/li>\n\n\n\n<li>Is heavily indicator based.<\/li>\n\n\n\n<li>Complex strategies either can\u2019t work or require hacking to work.<\/li>\n\n\n\n<li>The documentation could be better.<\/li>\n\n\n\n<li>The charting should be more customizable.<\/li>\n\n\n\n<li>Can be easily replaced by its alternatives.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Is Backtesting.py free?<\/h2>\n\n\n\n<p>Yes, Backtesting.py is completely free and open-sourced.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are some Backtesting.py alternatives?<\/h2>\n\n\n\n<p>Some Backtesting.py alternatives are the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>VectorBT<\/li>\n\n\n\n<li><a><\/a><a href=\"https:\/\/algotrading101.com\/learn\/backtrader-for-backtesting\/\">Backtrader<\/a><\/li>\n\n\n\n<li>Zipline<\/li>\n\n\n\n<li>Bt<\/li>\n\n\n\n<li><a href=\"https:\/\/algotrading101.com\/learn\/quantconnect-guide\/\">QuantConnect<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/algotrading101.com\/learn\/pine-script-tradingview-guide\/\">TradinView\u2019s PineScript<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">How to get started with Backtesting.py?<\/h2>\n\n\n\n<p><a>To get started with&nbsp;<\/a><a href=\"https:\/\/kernc.github.io\/backtesting.py\/\">Backtesting.py<\/a>, you will need to install it via pip with the following command:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">pip3 install backtesting<\/pre>\n\n\n\n<p>To start creating a trading strategy, you will import the Strategy object which is used within your trading algorithm classes:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from backtesting import Strategy\n\nclass Algorithm(Strategy):\n      ...<\/pre>\n\n\n\n<p>For this article, I\u2019ll be using Google Colab. Feel free to use any IDE of your choice. All code will be found on our&nbsp;<a href=\"https:\/\/github.com\/AlgoTrading101\">GitHub<\/a>&nbsp;and also at the bottom of the article. In the following sections, we\u2019ll test out the main features that backtesting.py has to offer.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to get data with Backtesting.py?<\/h2>\n\n\n\n<p>To import data from Backtesting.py, we will access the&nbsp;<code>test<\/code>&nbsp;module and obtain a specific asset by passing its symbol. It returns the data as a Pandas data frame.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from backtesting.test import GOOG\n\nGOOG.head()<\/pre>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><\/td><td>Open<\/td><td>High<\/td><td>Low<\/td><td>Close<\/td><td>Volume<\/td><\/tr><tr><td>2004-08-19 <\/td><td>100.00 <\/td><td>104.06 <\/td><td>95.96 <\/td><td>100.34 <\/td><td>22351900<\/td><\/tr><tr><td>2004-08-20 <\/td><td>101.01 <\/td><td>109.08 <\/td><td>100.50 <\/td><td>108.31 <\/td><td>11428600<\/td><\/tr><tr><td>2004-08-23 <\/td><td>110.75 <\/td><td>113.48 <\/td><td>109.05 <\/td><td>109.40 <\/td><td>9137200<\/td><\/tr><tr><td>2004-08-24 <\/td><td>111.24 <\/td><td>111.60<\/td><td> 103.57 <\/td><td>104.87 <\/td><td>7631300<\/td><\/tr><tr><td>2004-08-25 <\/td><td>104.96 <\/td><td>108.00 <\/td><td>103.88 <\/td><td>106.00 <\/td><td>4598900<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><a>Keep in mind that Backtesting only has GOOG and EURUSD for test data. Thus, you should use alternative data providers such as&nbsp;<\/a><a href=\"https:\/\/algotrading101.com\/learn\/yahoo-finance-api-guide\/\">Yahoo Finance<\/a>&nbsp;or&nbsp;<a href=\"https:\/\/algotrading101.com\/learn\/quandl-guide\/\">Quandl<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to use technical indicators with Backtesting.py?<\/h2>\n\n\n\n<p>To use technical indicators with Backtesting.py, you will need to import them from the&nbsp;<code>test<\/code>&nbsp;module by passing their function name. For example, if you want to obtain the Simple Moving Average indicator, you would write:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from backtesting.test import SMA\n\nclass SmaCross(Strategy):\n    n1 = 20 # period of the first SMA\n    n2 = 50 # period of the second SMA\n\n    def init(self):\n        close = self.data.Close # close price data\n        self.sma1 = self.I(SMA, close, self.n1)\n        self.sma2 = self.I(SMA, close, self.n2)<\/pre>\n\n\n\n<p><a>Have in mind that Backtesting.py only offers the SMA as an example, it isn\u2019t an indicators library which means that you should build your own indicators or use a library such as&nbsp;<\/a><a href=\"https:\/\/mrjbq7.github.io\/ta-lib\/\">TA-Lib<\/a>&nbsp;or&nbsp;<a href=\"https:\/\/tulipindicators.org\/\">Tulip<\/a>. Backtesting.py integrated well with both proposed libraries.<\/p>\n\n\n\n<p>Each technical indicator can be combined with an event such as the cross and crossover.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from backtesting.lib import crossover\n\ndef next(self):\n      if crossover(self.sma1, self.sma2):\n          self.buy()\n      elif crossover(self.sma2, self.sma1):\n          self.sell()<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How to define entries and exits with Backtesting.py?<\/h2>\n\n\n\n<p>Entries and exits can be defined with Backtesting.py by using conditions that can trigger a buy or sell order. An example of buy and sell order parameters are the following:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">def buy(self, *, size=.9999, limit=None, stop=None, sl=None, tp=None)\n\ndef sell(self, *, size=.9999, limit=None, stop=None, sl=None, tp=None)<\/pre>\n\n\n\n<p><a>When an entry or exit is executed, it results in a trade. We can query existing orders through&nbsp;<code><\/code><\/a><code><a href=\"https:\/\/kernc.github.io\/backtesting.py\/doc\/backtesting\/backtesting.html#backtesting.backtesting.Strategy.orders\">Strategy.orders<\/a><\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What strategy types does Backtesting.py offer?<\/h2>\n\n\n\n<p>Backtesting.py has three main types of strategies which are the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a><strong>Strategy&nbsp;<\/strong>\u2013 A trading strategy base class. Extend this class and override methods&nbsp;<code><\/code><\/a><code><a href=\"https:\/\/kernc.github.io\/backtesting.py\/doc\/backtesting\/backtesting.html#backtesting.backtesting.Strategy.init\">Strategy.init()<\/a><\/code>&nbsp;and&nbsp;<code><a href=\"https:\/\/kernc.github.io\/backtesting.py\/doc\/backtesting\/backtesting.html#backtesting.backtesting.Strategy.next\">Strategy.next()<\/a><\/code>&nbsp;to define your own strategy.<\/li>\n\n\n\n<li><strong>Trailing Strategy&nbsp;<\/strong>\u2013 A strategy with automatic trailing stop-loss, trailing the current price at a distance of some multiple of the average true range (ATR).<\/li>\n\n\n\n<li><strong>Signal Strategy<\/strong>&nbsp;\u2013 A simple helper strategy that operates on position entry\/exit signals. This makes the backtest of the strategy simulate a&nbsp;vectorized backtest.<\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class ExampleStrategy(SignalStrategy):\n    def init(self):\n        super().init()\n        self.set_signal(sma1 &gt; sma2, sma1 &lt; sma2)<\/pre>\n\n\n\n<p>Knowing when to use which strategy type will help you. Keep in mind that the&nbsp;<code>Strategy<\/code>&nbsp;module is an ancestor of the Trailing and Signal strategies. Now, let\u2019s code a pairs trade strategy as an example and backtest it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to code a mean-reversion strategy with Backtesting.py?<\/h2>\n\n\n\n<p>To code a mean-reversion strategy with Backtesting.py, we will first need to obtain the data of the asset we plan to trade. Then, we will lay out our strategy logic to make all the steps clear. After that, we will code it out and run the backtest.<\/p>\n\n\n\n<p>The goal of this strategy will be to sell\/short the asset if it is trading more than 3 standard deviations above the rolling mean and to buy\/long the asset if it is trading more than 3 standard deviations below the rolling mean.<\/p>\n\n\n\n<p>We will use 15-minute candles and a rolling mean of 50. The take profit will be the value of our simple moving average.<\/p>\n\n\n\n<p>Now, let us obtain the data for the HE asset. We will do a date closer to the time of writing due to the yfinance constraints on the 15-minute data.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Obtain OHLV data for HE\n# Obtain OHLV data for HE\nhe = yf.download(\"HE\", start=\"2023-01-15\", interval=\"15m\")[\n    [\"Open\", \"High\", \"Low\", \"Close\", \"Volume\"]\n]\nhe.head()<\/pre>\n\n\n\n<p>Now, let\u2019s set up the trading strategy and the initialization logic.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">rom backtesting.test import SMA\n\ndef std_3(arr, n):\n    return pd.Series(arr).rolling(n).std() * 3\n\nclass MeanReversion(Strategy):\n    roll = 50\n\n    def init(self):\n        self.he = self.data.Close\n\n        self.he_mean = self.I(SMA, self.he, self.roll)\n        self.he_std = self.I(std_3, self.he, self.roll)\n        self.he_upper = self.he_mean + self.he_std\n        self.he_lower = self.he_mean - self.he_std\n\n        self.he_close = self.I(SMA, self.he, 1)<\/pre>\n\n\n\n<p>Above, I imported the built-in SMA indicator and coded the 3 STD indicator by hand. You can also use libraries for more complicated indicators. Then, we specified the rolling window and initialized the algorithm.<\/p>\n\n\n\n<p>You can also see the first \u201chack\u201d that needed to be introduced to adequately compare the indicator to the candle closing price. Essentially, it needed to be transformed into an indicator. There might be a better way of doing this but accessing the prices from&nbsp;<code>self.he<\/code>&nbsp;was broken at the time of writing.<\/p>\n\n\n\n<p>Now, we will code the trading logic.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">def next(self):\n\n        if self.he_close &lt; self.he_lower:\n            self.buy(\n                tp = self.he_mean,\n            )\n\n        if self.he_close &gt; self.he_upper:\n            self.sell(\n                tp = self.he_mean,\n            )<\/pre>\n\n\n\n<p>Now, we will wrap everything up and plot the strategy and print out its stats.<\/p>\n\n\n\n<p><em>Visit <a href=\"https:\/\/algotrading101.com\/learn\/backtesting-py-guide\/\">AlgoTrading101<\/a> for the full tutorial.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Backtesting.py is an open-source backtesting Python library that allows users to test their trading strategies via code.<\/p>\n","protected":false},"author":815,"featured_media":193877,"comment_status":"open","ping_status":"closed","sticky":true,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[343,349,338,341],"tags":[851,4873,16603,16606,1224,595,16605,16607,1545,16604],"contributors-categories":[13746],"class_list":{"0":"post-201588","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-programing-languages","8":"category-python-development","9":"category-ibkr-quant-news","10":"category-quant-development","11":"tag-algo-trading","12":"tag-backtesting","13":"tag-backtesting-py","14":"tag-mean-reversion-strategy","15":"tag-pandas","16":"tag-python","17":"tag-signal-strategy","18":"tag-sma-indicator","19":"tag-technical-indicators","20":"tag-trailing-strategy","21":"contributors-categories-algotrading101"},"pp_statuses_selecting_workflow":false,"pp_workflow_action":"current","pp_status_selection":"publish","acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.9 (Yoast SEO v27.5) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Backtesting.py &#8211; An Introductory Guide to Backtesting with Python<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.interactivebrokers.com\/campus\/wp-json\/wp\/v2\/posts\/201588\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Backtesting.py - An Introductory Guide to Backtesting with Python\" \/>\n<meta property=\"og:description\" content=\"Backtesting.py is an open-source backtesting Python library that allows users to test their trading strategies via code.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/backtesting-py-an-introductory-guide-to-backtesting-with-python\/\" \/>\n<meta property=\"og:site_name\" content=\"IBKR Campus US\" \/>\n<meta property=\"article:published_time\" content=\"2024-01-29T16:23:20+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-01-29T16:23:28+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/07\/python-blue-keyboard.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1000\" \/>\n\t<meta property=\"og:image:height\" content=\"563\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Igor Radovanovic\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Igor Radovanovic\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\n\t    \"@context\": \"https:\\\/\\\/schema.org\",\n\t    \"@graph\": [\n\t        {\n\t            \"@type\": \"NewsArticle\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/backtesting-py-an-introductory-guide-to-backtesting-with-python\\\/#article\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/backtesting-py-an-introductory-guide-to-backtesting-with-python\\\/\"\n\t            },\n\t            \"author\": {\n\t                \"name\": \"Igor Radovanovic\",\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/person\\\/b43b33f424bad38d84a7b78eb0193592\"\n\t            },\n\t            \"headline\": \"Backtesting.py &#8211; An Introductory Guide to Backtesting with Python\",\n\t            \"datePublished\": \"2024-01-29T16:23:20+00:00\",\n\t            \"dateModified\": \"2024-01-29T16:23:28+00:00\",\n\t            \"mainEntityOfPage\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/backtesting-py-an-introductory-guide-to-backtesting-with-python\\\/\"\n\t            },\n\t            \"wordCount\": 997,\n\t            \"commentCount\": 3,\n\t            \"publisher\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/backtesting-py-an-introductory-guide-to-backtesting-with-python\\\/#primaryimage\"\n\t            },\n\t            \"thumbnailUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2023\\\/07\\\/python-blue-keyboard.jpg\",\n\t            \"keywords\": [\n\t                \"Algo Trading\",\n\t                \"backtesting\",\n\t                \"Backtesting.py\",\n\t                \"Mean-Reversion Strategy\",\n\t                \"Pandas\",\n\t                \"Python\",\n\t                \"Signal Strategy\",\n\t                \"SMA Indicator\",\n\t                \"technical indicators\",\n\t                \"Trailing Strategy\"\n\t            ],\n\t            \"articleSection\": [\n\t                \"Programming Languages\",\n\t                \"Python Development\",\n\t                \"Quant\",\n\t                \"Quant Development\"\n\t            ],\n\t            \"inLanguage\": \"en-US\",\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"CommentAction\",\n\t                    \"name\": \"Comment\",\n\t                    \"target\": [\n\t                        \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/backtesting-py-an-introductory-guide-to-backtesting-with-python\\\/#respond\"\n\t                    ]\n\t                }\n\t            ]\n\t        },\n\t        {\n\t            \"@type\": \"WebPage\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/backtesting-py-an-introductory-guide-to-backtesting-with-python\\\/\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/backtesting-py-an-introductory-guide-to-backtesting-with-python\\\/\",\n\t            \"name\": \"Backtesting.py - An Introductory Guide to Backtesting with Python | IBKR Campus US\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#website\"\n\t            },\n\t            \"primaryImageOfPage\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/backtesting-py-an-introductory-guide-to-backtesting-with-python\\\/#primaryimage\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/backtesting-py-an-introductory-guide-to-backtesting-with-python\\\/#primaryimage\"\n\t            },\n\t            \"thumbnailUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2023\\\/07\\\/python-blue-keyboard.jpg\",\n\t            \"datePublished\": \"2024-01-29T16:23:20+00:00\",\n\t            \"dateModified\": \"2024-01-29T16:23:28+00:00\",\n\t            \"inLanguage\": \"en-US\",\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"ReadAction\",\n\t                    \"target\": [\n\t                        \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/backtesting-py-an-introductory-guide-to-backtesting-with-python\\\/\"\n\t                    ]\n\t                }\n\t            ]\n\t        },\n\t        {\n\t            \"@type\": \"ImageObject\",\n\t            \"inLanguage\": \"en-US\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/backtesting-py-an-introductory-guide-to-backtesting-with-python\\\/#primaryimage\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2023\\\/07\\\/python-blue-keyboard.jpg\",\n\t            \"contentUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2023\\\/07\\\/python-blue-keyboard.jpg\",\n\t            \"width\": 1000,\n\t            \"height\": 563,\n\t            \"caption\": \"Python\"\n\t        },\n\t        {\n\t            \"@type\": \"WebSite\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#website\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/\",\n\t            \"name\": \"IBKR Campus US\",\n\t            \"description\": \"Financial Education from Interactive Brokers\",\n\t            \"publisher\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\"\n\t            },\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"SearchAction\",\n\t                    \"target\": {\n\t                        \"@type\": \"EntryPoint\",\n\t                        \"urlTemplate\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/?s={search_term_string}\"\n\t                    },\n\t                    \"query-input\": {\n\t                        \"@type\": \"PropertyValueSpecification\",\n\t                        \"valueRequired\": true,\n\t                        \"valueName\": \"search_term_string\"\n\t                    }\n\t                }\n\t            ],\n\t            \"inLanguage\": \"en-US\"\n\t        },\n\t        {\n\t            \"@type\": \"Organization\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\",\n\t            \"name\": \"Interactive Brokers\",\n\t            \"alternateName\": \"IBKR\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/\",\n\t            \"logo\": {\n\t                \"@type\": \"ImageObject\",\n\t                \"inLanguage\": \"en-US\",\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/logo\\\/image\\\/\",\n\t                \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/05\\\/ibkr-campus-logo.jpg\",\n\t                \"contentUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/05\\\/ibkr-campus-logo.jpg\",\n\t                \"width\": 669,\n\t                \"height\": 669,\n\t                \"caption\": \"Interactive Brokers\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/logo\\\/image\\\/\"\n\t            },\n\t            \"publishingPrinciples\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/about-ibkr-campus\\\/\",\n\t            \"ethicsPolicy\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/cyber-security-notice\\\/\"\n\t        },\n\t        {\n\t            \"@type\": \"Person\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/person\\\/b43b33f424bad38d84a7b78eb0193592\",\n\t            \"name\": \"Igor Radovanovic\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/author\\\/igor-radovanovic\\\/\"\n\t        }\n\t    ]\n\t}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Backtesting.py &#8211; An Introductory Guide to Backtesting with Python","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.interactivebrokers.com\/campus\/wp-json\/wp\/v2\/posts\/201588\/","og_locale":"en_US","og_type":"article","og_title":"Backtesting.py - An Introductory Guide to Backtesting with Python","og_description":"Backtesting.py is an open-source backtesting Python library that allows users to test their trading strategies via code.","og_url":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/backtesting-py-an-introductory-guide-to-backtesting-with-python\/","og_site_name":"IBKR Campus US","article_published_time":"2024-01-29T16:23:20+00:00","article_modified_time":"2024-01-29T16:23:28+00:00","og_image":[{"width":1000,"height":563,"url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/07\/python-blue-keyboard.jpg","type":"image\/jpeg"}],"author":"Igor Radovanovic","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Igor Radovanovic","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"NewsArticle","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/backtesting-py-an-introductory-guide-to-backtesting-with-python\/#article","isPartOf":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/backtesting-py-an-introductory-guide-to-backtesting-with-python\/"},"author":{"name":"Igor Radovanovic","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/person\/b43b33f424bad38d84a7b78eb0193592"},"headline":"Backtesting.py &#8211; An Introductory Guide to Backtesting with Python","datePublished":"2024-01-29T16:23:20+00:00","dateModified":"2024-01-29T16:23:28+00:00","mainEntityOfPage":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/backtesting-py-an-introductory-guide-to-backtesting-with-python\/"},"wordCount":997,"commentCount":3,"publisher":{"@id":"https:\/\/ibkrcampus.com\/campus\/#organization"},"image":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/backtesting-py-an-introductory-guide-to-backtesting-with-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/07\/python-blue-keyboard.jpg","keywords":["Algo Trading","backtesting","Backtesting.py","Mean-Reversion Strategy","Pandas","Python","Signal Strategy","SMA Indicator","technical indicators","Trailing Strategy"],"articleSection":["Programming Languages","Python Development","Quant","Quant Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/backtesting-py-an-introductory-guide-to-backtesting-with-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/backtesting-py-an-introductory-guide-to-backtesting-with-python\/","url":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/backtesting-py-an-introductory-guide-to-backtesting-with-python\/","name":"Backtesting.py - An Introductory Guide to Backtesting with Python | IBKR Campus US","isPartOf":{"@id":"https:\/\/ibkrcampus.com\/campus\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/backtesting-py-an-introductory-guide-to-backtesting-with-python\/#primaryimage"},"image":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/backtesting-py-an-introductory-guide-to-backtesting-with-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/07\/python-blue-keyboard.jpg","datePublished":"2024-01-29T16:23:20+00:00","dateModified":"2024-01-29T16:23:28+00:00","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/backtesting-py-an-introductory-guide-to-backtesting-with-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/backtesting-py-an-introductory-guide-to-backtesting-with-python\/#primaryimage","url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/07\/python-blue-keyboard.jpg","contentUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/07\/python-blue-keyboard.jpg","width":1000,"height":563,"caption":"Python"},{"@type":"WebSite","@id":"https:\/\/ibkrcampus.com\/campus\/#website","url":"https:\/\/ibkrcampus.com\/campus\/","name":"IBKR Campus US","description":"Financial Education from Interactive Brokers","publisher":{"@id":"https:\/\/ibkrcampus.com\/campus\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ibkrcampus.com\/campus\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/ibkrcampus.com\/campus\/#organization","name":"Interactive Brokers","alternateName":"IBKR","url":"https:\/\/ibkrcampus.com\/campus\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/logo\/image\/","url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/05\/ibkr-campus-logo.jpg","contentUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/05\/ibkr-campus-logo.jpg","width":669,"height":669,"caption":"Interactive Brokers"},"image":{"@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/logo\/image\/"},"publishingPrinciples":"https:\/\/www.interactivebrokers.com\/campus\/about-ibkr-campus\/","ethicsPolicy":"https:\/\/www.interactivebrokers.com\/campus\/cyber-security-notice\/"},{"@type":"Person","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/person\/b43b33f424bad38d84a7b78eb0193592","name":"Igor Radovanovic","url":"https:\/\/www.interactivebrokers.com\/campus\/author\/igor-radovanovic\/"}]}},"jetpack_featured_media_url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/07\/python-blue-keyboard.jpg","_links":{"self":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/201588","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/users\/815"}],"replies":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/comments?post=201588"}],"version-history":[{"count":0,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/201588\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media\/193877"}],"wp:attachment":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media?parent=201588"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/categories?post=201588"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/tags?post=201588"},{"taxonomy":"contributors-categories","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/contributors-categories?post=201588"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}