Answered step by step
Verified Expert Solution
Question
1 Approved Answer
write a full code for Algorithmic Trading in R language for one stock using Neural Network package in R: Price prediction - based trading. Build
write a full code for Algorithmic Trading in R language for one stock using Neural Network package in R:
Price predictionbased trading. Build a model to predicting the price for the coming day. This could include various key factors from previous days such as opening price, closing price, high price, low price, volume, Relative Strength Index RSI Exponential Moving Average EMA etc, or just values from the previous days trading. On top of this prediction you would then build a trading rule which would choose to buysellhold based on the current and predicted values for example
Key Components
With the above you will need to train a model of some sort NN so will need some notion of fitness or performance. This is obviously problemdependent but at some point will need to evaluate profit: for whichever trading rule you are developing you will need to iterate through the training data and calculate your earnings through returns or sales on some notional amount of investment lets say if your trading rule was to be invoked.
You will also need to keep some data in reserve for backtesting: ie keep some unseen historical data in reserve on which to evaluate the profitability of your final rule.
Also, as with forecasting and prediction, you will be working with time series data, so the size of the window needs to be considered this will depend upon the characteristics of the data you are using
Some Useful Resources
A useful package to assist in doing this is quantmod designed to support the rapid development and evaluation of trading models. As you will have seen, amongst other things it makes getting hold of data very straightforward and also provides functions for the opening and closing prices, high and low values, volume etc. just take a look at some of the examples.
Another value package is TTR which again provides a myriad of functions for building trading rules, but in particular ones for RSI and EMA. If you use quantmod then TTR will be installed by default as quantmod depends on it
A couple of hints on handling the data...
When using the quantmod package the data is in a time series structure called xts You can check this as follows for example:
getSymbolsGOOGsrc"yahoo"
strGOOG # this will give you the structure
headGOOG #will give you the first few lines of the data
This means it has to be extracted for use within the fitness function. There are several ways to doing this but one fairly simple one is to use the coredata function. eg
gdat coredataGOOG # Create a d array of all the data. Or
gdatfr data.framedateindexGOOG coredataGOOG # Create a data frame with the data and optionally maintain the date as an index
We could then a vector of the opening prices using one of the following:
opdat gdat # or
opdat gdatfr$GOOG.Open
Also, one of the other things you might find is that the volume values are too high for a GP to handle without losing accuracy so you could try scaling these down by several orders of magnitude.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started