Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Stock Prediction-Intraday is one of the trading norms of the stock market, buy shares at the opening time of the market and then sell the same at the closing time of the same day.

Today we are dealing with one of the data sets, based on daily data of seven years from 2014 to 2021.

We are going to use a simple machine learning algorithm to understand the data, analyze and make predictions based on an algorithm.

In this tutorial, we took randomly one of the stocks for analysis and prediction. You can try some other stocks based on your view and interest.

One of the suggestions is that you need to watch the stocks for at least 3 months closely and make your own conclusions with the help of these predictions.

These prediction ideas you can make use of long-term investment. For intraday, you need to know about some kind of strategies.

Suppose if you are going against the market trend chances higher for losing money. One of the simple strategies we already explained in one of our old posts click here to read.

```library(prophet)
library(lubridate)
library(ggplot2)
library(pacman)
pacman::p_load(data.table, fixest, BatchGetSymbols, finreportr, ggplot2, lubridate)```

## Set parameters

```first.date <- Sys.Date() - 2500
last.date <- Sys.Date()
freq.data <- "daily"
tickers <- c("BALKRISIND.NS")```

We are taking daily data from 2014-07-01 to 2021-05-05.

How to choose lottery numbers?

## Getting Data

```stocks <- BatchGetSymbols(tickers = tickers,
first.date = first.date,
last.date = last.date,
freq.data = freq.data,
do.cache = FALSE,
data<-stocks\$df.tickers
data<-na.omit(data)

Following details will get for analysis.

```price.open price.high price.low price.close volume price.adjusted   ref.date
2   367.2603   371.1934  357.9189    364.2366 124870       342.4004 2014-07-02
3   363.8187   367.3586  358.1648    361.9259  30469       340.2281 2014-07-03
4   359.0743   369.2268  359.0743    365.3428  29728       343.4402 2014-07-04
5   362.8600   367.9485  354.9691    358.0419  74821       336.5770 2014-07-07
6   356.7390   360.8688  347.5944    348.8972  79854       327.9806 2014-07-08
7   346.1194   348.5285  330.4850    341.2030 402494       320.7476 2014-07-09
2 BALKRISIND.NS         0.004678743        0.004678642
3 BALKRISIND.NS        -0.006344423       -0.006344118
4 BALKRISIND.NS         0.009441102        0.009441052
5 BALKRISIND.NS        -0.019983830       -0.019983871
6 BALKRISIND.NS        -0.025540681       -0.025540653
7 BALKRISIND.NS        -0.022053048       -0.022053126

str(data)```

The dataset contains total 1680 observations and 10 variables.

## Q plot

Let’s plot the dataset for understanding.

`qplot(data\$ref.date, data\$price.close,data=data)`

It is clearly evident that the data set is not stationary. Let make use of log transformation and convert it into stationary data.

What is mean by best standard deviation?

### Log transformation

```ds <- data\$ref.date
y <- data\$price.close
df <- data.frame(ds, y)

After log transforamtion the data set should like this.

```ds        y
1 2014-07-02 364.2366
2 2014-07-03 361.9259
3 2014-07-04 365.3428
4 2014-07-07 358.0419
5 2014-07-08 348.8972
6 2014-07-09 341.2030```

Stock forecasting we are using prophet package

```m <- prophet(df)
future <- make_future_dataframe(m, periods = 30)```

periods indicate the number of days need to forecast.

Minimum number of units in an experimental design

`forecast <- predict(m, future)`

## Model performance & Stock Prediction

```pred <- forecast\$yhat[1:dim(df)[1]]
actual <- m\$history\$y
plot(actual, pred)```
`summary(lm(pred~actual))`

Call:

```lm(formula = pred ~ actual)
Residuals:
Min       1Q   Median       3Q      Max
-261.419  -41.156   -4.332   39.322  304.031
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 27.155044   3.728168   7.284 4.97e-13 ***
actual       0.965945   0.004164 231.989  < 2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 69.56 on 1678 degrees of freedom
Multiple R-squared:  0.9698,  Adjusted R-squared:  0.9697
F-statistic: 5.382e+04 on 1 and 1678 DF,  p-value: < 2.2e-16```

Adjusted R square is 96% quite good model.

When you are dealing with time series you need to get an idea about some of the trends like weekly and yearly.

## Plot forecast

`prophet_plot_components(m, forecast)`

Now you can see some of the trends like weekly Monday price is going down and Thursday and Friday it’s going up and some seasonal trend based on yearly data.

Decision Trees in R

`tail(forecast) `

You can see predicted values in yhat

```ds    trend additive_terms additive_terms_lower additive_terms_upper
1705 2021-05-29 1811.291      -20.84209            -20.84209            -20.84209
1706 2021-05-30 1813.041      -20.02460            -20.02460            -20.02460
1707 2021-05-31 1814.791      -24.12734            -24.12734            -24.12734
1708 2021-06-01 1816.542      -22.08570            -22.08570            -22.08570
1709 2021-06-02 1818.292      -20.96707            -20.96707            -20.96707
1710 2021-06-03 1820.042      -17.90472            -17.90472            -17.90472
weekly weekly_lower weekly_upper    yearly yearly_lower yearly_upper
1705  2.092700     2.092700     2.092700 -22.93479    -22.93479    -22.93479
1706  2.092701     2.092701     2.092701 -22.11730    -22.11730    -22.11730
1707 -2.826402    -2.826402    -2.826402 -21.30094    -21.30094    -21.30094
1708 -1.585587    -1.585587    -1.585587 -20.50011    -20.50011    -20.50011
1709 -1.239506    -1.239506    -1.239506 -19.72756    -19.72756    -19.72756
1710  1.089445     1.089445     1.089445 -18.99416    -18.99416    -18.99416
multiplicative_terms multiplicative_terms_lower multiplicative_terms_upper
1705                    0                          0                          0
1706                    0                          0                          0
1707                    0                          0                          0
1708                    0                          0                          0
1709                    0                          0                          0
1710                    0                          0                          0
yhat_lower yhat_upper trend_lower trend_upper     yhat
1705   1702.054   1876.785    1811.189    1811.291 1790.449
1706   1702.299   1882.703    1812.854    1813.041 1793.017
1707   1699.517   1875.155    1814.448    1814.791 1790.664
1708   1707.877   1885.060    1816.108    1816.543 1794.456
1709   1702.521   1889.921    1817.741    1818.366 1797.325
1710   1708.005   1898.301    1819.334    1820.132 1802.137
plot(m, forecast)```

The plot is showing an increasing trend for the next 30 days.

Applying the knowledge of machine learning and algorithms to daily life allows us to make better decisions instead of random guesses.

`Disclaimer:- For any kind of investment please consult your financial advisor, we are not recommending any stocks or trading ideas.`

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.