{"id":187395,"date":"2023-03-27T12:16:24","date_gmt":"2023-03-27T16:16:24","guid":{"rendered":"https:\/\/ibkrcampus.com\/?p=187395"},"modified":"2023-03-27T14:39:00","modified_gmt":"2023-03-27T18:39:00","slug":"quantitative-trading-strategy-using-quantstrat-package-in-r-a-step-by-step-guide","status":"publish","type":"post","link":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/quantitative-trading-strategy-using-quantstrat-package-in-r-a-step-by-step-guide\/","title":{"rendered":"Quantitative Trading Strategy Using Quantstrat Package In R: A Step By Step Guide"},"content":{"rendered":"\n<p>In this post, we will be building a&nbsp;<a href=\"https:\/\/blog.quantinsti.com\/how-to-design-quant-trading-strategies-using-r\/\" target=\"_blank\" rel=\"noreferrer noopener\">trading strategy using R<\/a>. Before dwelling into the trading jargons using R let us spend some time understanding what R is. R is an open source. There are more than 4000 add-on packages, 18000 plus members of LinkedIn\u2019s group and close to 80 R Meetup groups currently in existence. It is a perfect tool for statistical analysis especially for data analysis. The concise setup of Comprehensive R Archive Network knows as CRAN provides you the list of packages along with the base installation required. There are lot of packages available depending upon the analysis needs to be done. To implement the trading strategy, we will use the package called quantstrat.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-four-step-process-of-any-basic-trading-strategy\"><strong>Four Step Process of Any Basic Trading Strategy<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Hypothesis formation<\/li>\n\n\n\n<li>Testing<\/li>\n\n\n\n<li>Refining<\/li>\n\n\n\n<li>Production<\/li>\n<\/ol>\n\n\n\n<p>Our hypothesis is formulated as \u201cmarket is mean reverting\u201d.&nbsp;<a href=\"https:\/\/quantra.quantinsti.com\/course\/python-mean-reversion-strategies-ernest-chan\" target=\"_blank\" rel=\"noreferrer noopener\">Mean reversion<\/a>&nbsp;is a theory that suggests that the prices eventually move back to their average value. The second step involves testing the hypothesis for which we formulate a strategy on our hypothesis and compute indicators, signals and performance metrics. The testing phase can be broken down into three steps, getting the data, writing the strategy and analyzing the output. In this example we consider NIFTY-Bees. It is an&nbsp;<a href=\"https:\/\/blog.quantinsti.com\/exchange-traded-funds\/\" target=\"_blank\" rel=\"noreferrer noopener\">exchange-traded fund<\/a>&nbsp;managed by Goldman Sachs. NSE has huge volume for the instrument hence we consider this. The image below shows the Open-High-Low-Close price of the same.<\/p>\n\n\n\n<p>We set a threshold level to compare the fluctuations in the price. If the price increases\/decreases, we update the threshold column. The closing price is compared with the upper band and with the lower band. When the upper band is crossed, it is a signal for sell. Similarly, when the lower band is crossed, it is a buy signal.<\/p>\n\n\n\n<p><strong>The coding section can be summarized as follows,<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Adding indicators<\/li>\n\n\n\n<li>Adding signals<\/li>\n\n\n\n<li>Adding rules<\/li>\n<\/ul>\n\n\n\n<p>A helicopter view towards the output of the strategy is given in the diagram below.<\/p>\n\n\n\n<p>Thus our hypothesis that market is mean reverting is supported. Since this is back-testing we have room for refining the trading parameters that would improve our average returns and the profits realized. This can be done by setting different threshold levels, more strict entry rules, stop loss etc. One could choose more data for back-testing, use Bayesian approach for a threshold set up, take volatility into account.<\/p>\n\n\n\n<p>Once you are confident about the&nbsp;<a href=\"https:\/\/quantra.quantinsti.com\/course\/quantitative-trading-strategies-models\" target=\"_blank\" rel=\"noreferrer noopener\">trading strategy<\/a>&nbsp;backed by the back-testing results you could step into live trading. The production environment is a big topic in itself and it\u2019s out of scope in the article\u2019s context. To explain in brief this would involve writing the strategy on a&nbsp;<a href=\"https:\/\/www.quantinsti.com\/category\/programming-and-trading-tools\/trading-platforms\/\" target=\"_blank\" rel=\"noreferrer noopener\">trading platform<\/a>.<\/p>\n\n\n\n<p>As mentioned earlier, we would be building the model using quantstrat package. Quantstrat provides a generic infrastructure to model and backtest signal-based quantitative strategies. It is a high-level abstraction layer (built on xts, FinancialInstrument, blotter, etc.) that allows you to build and test strategies in very few lines of code.<\/p>\n\n\n\n<p><strong>The key features of quantstrat are,<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Supports strategies which include indicators, signals, and rules<\/li>\n\n\n\n<li>Allows strategies to be applied to multi-asset portfolios<\/li>\n\n\n\n<li>Supports market, limit, stoplimit, and stoptrailing order types<\/li>\n\n\n\n<li>Supports order sizing and parameter optimization<\/li>\n<\/ul>\n\n\n\n<p>In this post we build a strategy that includes indicators, signals, and rules.<\/p>\n\n\n\n<p><strong>For a generic signal based model following are the objects one should consider,<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Instruments- Contain market data<\/li>\n\n\n\n<li>Indicators- Quantitative values derived from market data<\/li>\n\n\n\n<li>Signals- Result of interaction between market data and indicators<\/li>\n\n\n\n<li>Rules- Generate orders using market data, indicators and signals.<\/li>\n<\/ul>\n\n\n\n<p>Without much ado let\u2019s discuss the coding part. We prefer R studio for coding and insist you use the same. You need to have certain packages installed before&nbsp;<a href=\"https:\/\/www.quantinsti.com\/category\/programming-and-trading-tools\/python\/\" target=\"_blank\" rel=\"noreferrer noopener\">programming the strategy<\/a>.<\/p>\n\n\n\n<p><strong>The following set of commands installs the necessary packages.<\/strong><\/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=\"\">install.packages(\"quantstrat\", repos=\"https:\/\/R-Forge.R-project.org\")\ninstall.packages(\"blotter\", repos=\"https:\/\/R-Forge.R-project.org\")\ninstall.packages(\"FinancialInstrument\", repos=\"https:\/\/R-Forge.R-project.org\")<\/pre>\n\n\n\n<p>Once you have installed the packages you import them for further usage.<\/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=\"\">require(quantstrat)<\/pre>\n\n\n\n<p>Read the data from csv file and convert it into xts object.<\/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=\"\">ym_xts<\/pre>\n\n\n\n<p>We initialize the portfolio with the stock, currency, initial equity and the strategy type.<\/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=\"\">stock.str='NSEI' # stock we trying it on\ncurrency('INR')\nstock(stock.str,currency='INR',multiplier=1)\ninitEq=1000\ninitDate = index(NSEI[1])#should always be before\/start of data\n#Declare mandatory names to be used\nportfolio.st='MeanRev'\naccount.st='MeanRev'\ninitPortf(portfolio.st,symbols=stock.str, initDate=initDate)\ninitAcct(account.st,portfolios='MeanRev', initDate=initDate)\ninitOrders(portfolio=portfolio.st,initDate=initDate)<\/pre>\n\n\n\n<p>Add position limit if you wish to trade more than once on the same side.<\/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=\"\">addPosLimit(portfolio.st, stock.str, initDate, 1, 1 )<\/pre>\n\n\n\n<p>Create the strategy object.<\/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=\"\">stratMR<\/pre>\n\n\n\n<p>We build a function that computes the thresholds are which we want to trade. If price moves by thresh1 we update threshold to new price. New bands for trading are Threshold+\/-Thresh2. Output is an xts object though we use reclass function to ensure.<\/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=\"\">THTFunc&lt;-function(CompTh=NSEI,Thresh=6, Thresh2=3){\nnumRow(tht+Thresh)){ tht&lt;-xa[i]}\n   if(xa[i]&lt;(tht-Thresh)){ tht&lt;-xa[i]}\n   xb[i]<\/pre>\n\n\n\n<p>Add the indicator, signal and the trading rule.<\/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=\"\">stratMR<\/pre>\n\n\n\n<p>Run the strategy and have a look at the order book.<\/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=\"\">out&lt;-try(applyStrategy(strategy=stratMR , portfolios='MeanRev') )\n# look at the order book\ngetOrderBook('MeanRev')\nend_t&lt;-Sys.time()<\/pre>\n\n\n\n<p>Update the portfolio and view the trade statistics<\/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=\"\">updatePortf('MeanRev', stock.str)\nchart.Posn(Portfolio='MeanRev',Symbol=stock.str)\ntradeStats('MeanRev', stock.str)\nView(t(tradeStats('MeanRev')))\n.Th2 = c(.3,.4)\n.Th1 = c(.5,.6)\nrequire(foreach)\nrequire(doParallel)\nregisterDoParallel(cores=2)\nstratMR&lt;-add.distribution(stratMR,paramset.label='THTFunc',component.type= 'indicator',component.label = 'THTT',\n                         variable = list(Thresh = .Th1),label = 'THTT1')\nstratMR&lt;-add.distribution(stratMR,paramset.label='THTFunc',component.type= 'indicator',component.label = 'THTT',\n                         variable = list(Thresh2 = .Th2),label = 'THTT2')\nresults&lt;-apply.paramset(stratMR, paramset.label='THTFunc', portfolio.st=portfolio.st, account.st=account.st, nsamples=4, verbose=TRUE)\nstats<\/pre>\n\n\n\n<p>Here is the complete code<\/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=\"\">require(quantstrat)\nym_xts (tht+Thresh)){ tht&lt;-xa[i]}\n   if(xa[i]&lt;(tht-Thresh)){ tht&lt;-xa[i]}\n   xb[i]<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Next Step<\/strong><\/h3>\n\n\n\n<p>Once you are familiar with these basics you could take a look at how to start using&nbsp;<a href=\"https:\/\/blog.quantinsti.com\/a-guide-on-r-quantmod-package-how-to-get-started\/\">quantimod package in R<\/a>. Or in case you&#8217;re good at C++, take a look at an example&nbsp;<a href=\"https:\/\/blog.quantinsti.com\/an-example-of-a-trading-strategy-coded-in-c\/\">strategy coded in C++<\/a>.<\/p>\n\n\n\n<p><em>Originally posted on <a href=\"https:\/\/blog.quantinsti.com\/quantitative-trading-strategy-using-r\/\">QuantInsti<\/a> blog.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post, we will be building a trading strategy using R.<\/p>\n","protected":false},"author":186,"featured_media":169884,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[339,343,338,342],"tags":[15007,1408,15008,487,6591],"contributors-categories":[13654],"class_list":{"0":"post-187395","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-science","8":"category-programing-languages","9":"category-ibkr-quant-news","10":"category-r-development","11":"tag-quantitative-trading-strategy","12":"tag-quantmod","13":"tag-quantstrat-package","14":"tag-r","15":"tag-rstats","16":"contributors-categories-quantinsti"},"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>Quantitative Trading Strategy Using Quantstrat Package In R: A Step By Step Guide<\/title>\n<meta name=\"description\" content=\"In this post, we will be building a trading strategy using R.\" \/>\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\/187395\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Quantitative Trading Strategy Using Quantstrat Package In R: A Step By Step Guide | IBKR Campus US\" \/>\n<meta property=\"og:description\" content=\"In this post, we will be building a trading strategy using R.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/quantitative-trading-strategy-using-quantstrat-package-in-r-a-step-by-step-guide\/\" \/>\n<meta property=\"og:site_name\" content=\"IBKR Campus US\" \/>\n<meta property=\"article:published_time\" content=\"2023-03-27T16:16:24+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-03-27T18:39:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2022\/11\/algo-course.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t<meta property=\"og:image:height\" content=\"720\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Contributor Author\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Contributor Author\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 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\\\/quantitative-trading-strategy-using-quantstrat-package-in-r-a-step-by-step-guide\\\/#article\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/quantitative-trading-strategy-using-quantstrat-package-in-r-a-step-by-step-guide\\\/\"\n\t            },\n\t            \"author\": {\n\t                \"name\": \"Contributor Author\",\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/person\\\/e823e46b42ca381080387e794318a485\"\n\t            },\n\t            \"headline\": \"Quantitative Trading Strategy Using Quantstrat Package In R: A Step By Step Guide\",\n\t            \"datePublished\": \"2023-03-27T16:16:24+00:00\",\n\t            \"dateModified\": \"2023-03-27T18:39:00+00:00\",\n\t            \"mainEntityOfPage\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/quantitative-trading-strategy-using-quantstrat-package-in-r-a-step-by-step-guide\\\/\"\n\t            },\n\t            \"wordCount\": 828,\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\\\/quantitative-trading-strategy-using-quantstrat-package-in-r-a-step-by-step-guide\\\/#primaryimage\"\n\t            },\n\t            \"thumbnailUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2022\\\/11\\\/algo-course.jpg\",\n\t            \"keywords\": [\n\t                \"Quantitative Trading Strategy\",\n\t                \"quantmod\",\n\t                \"Quantstrat Package\",\n\t                \"R\",\n\t                \"rstats\"\n\t            ],\n\t            \"articleSection\": [\n\t                \"Data Science\",\n\t                \"Programming Languages\",\n\t                \"Quant\",\n\t                \"R Development\"\n\t            ],\n\t            \"inLanguage\": \"en-US\"\n\t        },\n\t        {\n\t            \"@type\": \"WebPage\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/quantitative-trading-strategy-using-quantstrat-package-in-r-a-step-by-step-guide\\\/\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/quantitative-trading-strategy-using-quantstrat-package-in-r-a-step-by-step-guide\\\/\",\n\t            \"name\": \"Quantitative Trading Strategy Using Quantstrat Package In R: A Step By Step Guide | 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\\\/quantitative-trading-strategy-using-quantstrat-package-in-r-a-step-by-step-guide\\\/#primaryimage\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/quantitative-trading-strategy-using-quantstrat-package-in-r-a-step-by-step-guide\\\/#primaryimage\"\n\t            },\n\t            \"thumbnailUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2022\\\/11\\\/algo-course.jpg\",\n\t            \"datePublished\": \"2023-03-27T16:16:24+00:00\",\n\t            \"dateModified\": \"2023-03-27T18:39:00+00:00\",\n\t            \"description\": \"In this post, we will be building a trading strategy using R.\",\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\\\/quantitative-trading-strategy-using-quantstrat-package-in-r-a-step-by-step-guide\\\/\"\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\\\/quantitative-trading-strategy-using-quantstrat-package-in-r-a-step-by-step-guide\\\/#primaryimage\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2022\\\/11\\\/algo-course.jpg\",\n\t            \"contentUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2022\\\/11\\\/algo-course.jpg\",\n\t            \"width\": 1280,\n\t            \"height\": 720,\n\t            \"caption\": \"TWS Algos\"\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\\\/e823e46b42ca381080387e794318a485\",\n\t            \"name\": \"Contributor Author\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/author\\\/contributor-author\\\/\"\n\t        }\n\t    ]\n\t}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Quantitative Trading Strategy Using Quantstrat Package In R: A Step By Step Guide","description":"In this post, we will be building a trading strategy using R.","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\/187395\/","og_locale":"en_US","og_type":"article","og_title":"Quantitative Trading Strategy Using Quantstrat Package In R: A Step By Step Guide | IBKR Campus US","og_description":"In this post, we will be building a trading strategy using R.","og_url":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/quantitative-trading-strategy-using-quantstrat-package-in-r-a-step-by-step-guide\/","og_site_name":"IBKR Campus US","article_published_time":"2023-03-27T16:16:24+00:00","article_modified_time":"2023-03-27T18:39:00+00:00","og_image":[{"width":1280,"height":720,"url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2022\/11\/algo-course.jpg","type":"image\/jpeg"}],"author":"Contributor Author","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Contributor Author","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"NewsArticle","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/quantitative-trading-strategy-using-quantstrat-package-in-r-a-step-by-step-guide\/#article","isPartOf":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/quantitative-trading-strategy-using-quantstrat-package-in-r-a-step-by-step-guide\/"},"author":{"name":"Contributor Author","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/person\/e823e46b42ca381080387e794318a485"},"headline":"Quantitative Trading Strategy Using Quantstrat Package In R: A Step By Step Guide","datePublished":"2023-03-27T16:16:24+00:00","dateModified":"2023-03-27T18:39:00+00:00","mainEntityOfPage":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/quantitative-trading-strategy-using-quantstrat-package-in-r-a-step-by-step-guide\/"},"wordCount":828,"publisher":{"@id":"https:\/\/ibkrcampus.com\/campus\/#organization"},"image":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/quantitative-trading-strategy-using-quantstrat-package-in-r-a-step-by-step-guide\/#primaryimage"},"thumbnailUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2022\/11\/algo-course.jpg","keywords":["Quantitative Trading Strategy","quantmod","Quantstrat Package","R","rstats"],"articleSection":["Data Science","Programming Languages","Quant","R Development"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/quantitative-trading-strategy-using-quantstrat-package-in-r-a-step-by-step-guide\/","url":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/quantitative-trading-strategy-using-quantstrat-package-in-r-a-step-by-step-guide\/","name":"Quantitative Trading Strategy Using Quantstrat Package In R: A Step By Step Guide | IBKR Campus US","isPartOf":{"@id":"https:\/\/ibkrcampus.com\/campus\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/quantitative-trading-strategy-using-quantstrat-package-in-r-a-step-by-step-guide\/#primaryimage"},"image":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/quantitative-trading-strategy-using-quantstrat-package-in-r-a-step-by-step-guide\/#primaryimage"},"thumbnailUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2022\/11\/algo-course.jpg","datePublished":"2023-03-27T16:16:24+00:00","dateModified":"2023-03-27T18:39:00+00:00","description":"In this post, we will be building a trading strategy using R.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/quantitative-trading-strategy-using-quantstrat-package-in-r-a-step-by-step-guide\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/quantitative-trading-strategy-using-quantstrat-package-in-r-a-step-by-step-guide\/#primaryimage","url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2022\/11\/algo-course.jpg","contentUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2022\/11\/algo-course.jpg","width":1280,"height":720,"caption":"TWS Algos"},{"@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\/e823e46b42ca381080387e794318a485","name":"Contributor Author","url":"https:\/\/www.interactivebrokers.com\/campus\/author\/contributor-author\/"}]}},"jetpack_featured_media_url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2022\/11\/algo-course.jpg","_links":{"self":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/187395","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\/186"}],"replies":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/comments?post=187395"}],"version-history":[{"count":0,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/187395\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media\/169884"}],"wp:attachment":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media?parent=187395"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/categories?post=187395"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/tags?post=187395"},{"taxonomy":"contributors-categories","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/contributors-categories?post=187395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}