The stochastic process, also known as the random process, illustrates the evolution of the system of random values over time. It is the best to use when one or more variables within the model are random. In this recipe, we will introduce how to simulate a random stock trading process with R.
In this recipe, you need to prepare your environment with R installed and a computer that can access the Internet. You should complete the previous step and have quantmod
or DJI data loaded in an R session.
Please perform the following steps to simulate trading stocks:
First, subset the 2014 data from the Dow Jones Industrial Average Index:
> DJI.2014 = DJI['2014']
We can then use the sample function to generate samples of
0
and1
:> set.seed(123) > randIDX = sample(c(0,1), nrow(DJI.2014), replace=TRUE)
Next, we subset the DJI close price with the index equal to
1
only, which denotes that we either buy or sell stock on that day:> stock...