This article was first published on R programming , and kindly contributed to R-bloggers. In this post we will discuss about building a trading strategy using R. Before dwelling into the trading jargons using R let us spend some time understanding what R is.
R is an open source. It is a perfect tool for statistical analysis especially for data analysis. 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. Mean reversion 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. It is an exchange traded fund 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. We set a threshold level to compare the fluctuations in the price. 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 signal for sell. 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 Bayseian approach for threshold set up, take volatility into account. Once you are confident about the trading strategy backed by the back-testing results you could step into live trading. To explain in brief this would involve writing the strategy on a trading platform.
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.
We prefer R studio for coding and insist you use the same. You need to have certain packages installed before programming the strategy. 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. Output is an xts object though we use reclass function to ensure. Next Step Once you are familiar with these basics you could take a look at how to start using quantimod package in R. Begin with basic concepts like automated trading architecture , market microstructure , strategy backtesting system and order management system.
A Step by Step Guide appeared first on. To leave a comment for the author, please follow the link and comment on their blog: R news and tutorials contributed by R bloggers. Home About RSS add your blog! Here you will find daily news and tutorials about R , contributed by over bloggers.
There are many ways to follow us - By e-mail: If you are an R blogger yourself you are invited to add your own R content feed to this site Non-English R bloggers should add themselves- here.
Popular Searches googlevis heatmap twitter latex sales forecasting sql web Scraping eof hadoop Jeff Hemsley random forest 3 d clusters anova blotter boxplot coplot decision tree discriminant financial ggplot background grid colour how to import image file to r maps purrr rattle Trading bar chart barplot Binary climate contingency table data frame.
The Minimax Algorithm What is the tidyverse? Quantitative Trading Strategy Using R: A Step by Step Guide January 20, If you got this far, why not subscribe for updates from the site?
Full list of contributing R-bloggers. R-bloggers was founded by Tal Galili , with gratitude to the R community. Is powered by WordPress using a bavotasan.
Terms and Conditions for this website. Never miss an update! Subscribe to R-bloggers to receive e-mails with the latest R posts. You will not see this message again.More...