web page: https://rpubs.com/staszkiewicz/FM_03_StockTS
Note data folder:
Strategy 1. Learn how to import the prepared data from the exel. cvs ifiel Cw10_idm_d the above is our file for testig
Distiguish between classical vector or dataframe and the time serries object
Maniputlate wiith the objcect
Define basic operation on time serries
Data: The data we use is a classical export of a time serries for one
Read the file to object called idm (a pomp up window will rund)
idm<-read.csv(file.choose())
This commend ‘idm<-read.csv(file.choose())’ allows you to pick up a file while the path you do not remeber, it needs human intervation, but it is easy for beginner.
First the class of the object ‘idm’
class(idm)
## [1] "data.frame"
Let us now see it as the form of the Excel view
View(idm)
Second th class of the column
class(idm$Open)
## [1] "numeric"
Now we convert the data farem into the time serries object
We used the ‘include = FALSE’ to prevent the function to show output as it to loog, try it yourself from the console
We used ‘ts(idm)’ to add up the timing into the data. We now are able to plot the time serries.
plot(idm)#this gives you the all eht pictues (see it polots all the data)
plot(idm)#this gives you the all eht pictues
Let us plot only the openig value
plot(idm$Open)
Let us summarize the data we have, first we will she first 5 entires, than the descriptive statistics of variables available
head(idm$Open)
## [1] 145.32 139.74 136.38 134.15 135.26 135.26
summary(idm)
## Date Open Minimum Maximum
## Length:3085 Min. : 0.42 Min. : 0.44 Min. : 0.42
## Class :character 1st Qu.: 2.08 1st Qu.: 2.08 1st Qu.: 2.08
## Mode :character Median : 213.12 Median : 222.72 Median : 206.80
## Mean : 371.41 Mean : 379.46 Mean : 361.57
## 3rd Qu.: 522.24 3rd Qu.: 529.92 3rd Qu.: 512.64
## Max. :2846.08 Max. :2904.16 Max. :2786.88
## Closlure Volume
## Min. : 0.44 Min. : 1
## 1st Qu.: 2.08 1st Qu.: 1011
## Median : 211.20 Median : 2579
## Mean : 370.84 Mean : 7996
## 3rd Qu.: 522.24 3rd Qu.: 6376
## Max. :2815.84 Max. :318216
Let us attache the data to the worplace
attach(idm)
And let us not assing the names to the collumns
names(idm)
## [1] "Date" "Open" "Minimum" "Maximum" "Closlure" "Volume"
Now we are able to refere to the colum wituth the’\(' no 'idm\)Open’ but direclty ‘Open’
Let us see some example
plot(Minimum)
or just a factio between 1:20 frist quotings
plot(Minimum[1:20])
We might calculate the standard deviation of minimums e.g.
sd(Minimum)
## [1] 522.2307
Variance
var(Minimum)
## [1] 272724.9
or that way
sd(Minimum)^2
## [1] 272724.9
zam<-ts(idm$Closlure,start=c(2005,7,28),frequency =365)
zam<-ts(idm$Closlure,start=c(2005,7,28),frequency =365)
and let us plot
plot(zam)
zam<-ts(Closlure,start=c(2005,7,28),frequency =240)
and let us plot agian
plot(zam)
dek_zam<-stl(zam,"periodic")
let us see what it does
plot(dek_zam)
You should now print help by yourself use: help(“stl”)
To get more information about the function just typ help(ts)
objects() # list all object
## [1] "dek_zam" "idm" "zam"
search() # check in the help what this does
## [1] ".GlobalEnv" "idm" "package:stats"
## [4] "package:graphics" "package:grDevices" "package:utils"
## [7] "package:datasets" "package:methods" "Autoloads"
## [10] "package:base"
help(rm) # this is a help for the removal of the objects
## uruchamianie serwera httpd dla pomocy ... wykonano
help(ls) # this is the help for the list
rm(list=ls()) # now we clear up the entire eniviroment (house keeping)
# its a good practice do not attach the datat to the enironment
# we did it for the education purposes and simplicity
Now we move to the more practical aspect and wi will use the libarary(quantmod) to operate on the stocks
Now we move to the more practical aspect and wi will use the libarary(quantmod) to operate on the stocks
#install.packages("quantmod") /if you do not have installed perviously
library(quantmod) # Load the package
## Ładowanie wymaganego pakietu: xts
## Ładowanie wymaganego pakietu: zoo
##
## Dołączanie pakietu: 'zoo'
## Następujące obiekty zostały zakryte z 'package:base':
##
## as.Date, as.Date.numeric
## Ładowanie wymaganego pakietu: TTR
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
getSymbols(“AAPL”) # Download daily prices of Apple stock from Yahoo
getSymbols("AAPL") # Download daily prices of Apple stock from Yahoo
## [1] "AAPL"
dim(AAPL) # (dimension): See the size of the downloaded data.
dim(AAPL) # (dimension): See the size of the downloaded data.
## [1] 4475 6
class(AAPL) # see we have a specific class of data XTS and ZOO - read more on Internet
class(AAPL) # see we have a specific class of data XTS and ZOO - read more on Internet
## [1] "xts" "zoo"
head(AAPL) # See the first 6 rows of the data
head(AAPL) # See the first 6 rows of the data
## AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted
## 2007-01-03 3.081786 3.092143 2.925000 2.992857 1238319600 2.527394
## 2007-01-04 3.001786 3.069643 2.993571 3.059286 847260400 2.583492
## 2007-01-05 3.063214 3.078571 3.014286 3.037500 834741600 2.565094
## 2007-01-08 3.070000 3.090357 3.045714 3.052500 797106800 2.577761
## 2007-01-09 3.087500 3.320714 3.041071 3.306071 3349298400 2.791895
## 2007-01-10 3.383929 3.492857 3.337500 3.464286 2952880000 2.925504
tail(AAPL) # See the last 6 rows of the data
tail(AAPL) # See the last 6 rows of the data
## AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted
## 2024-10-04 227.90 228.00 224.13 226.80 37245100 226.80
## 2024-10-07 224.50 225.69 221.33 221.69 39505400 221.69
## 2024-10-08 224.30 225.98 223.25 225.77 31855700 225.77
## 2024-10-09 225.23 229.75 224.83 229.54 33591100 229.54
## 2024-10-10 227.78 229.50 227.17 229.04 28183500 229.04
## 2024-10-11 229.30 229.41 227.34 227.55 31668000 227.55
chartSeries(AAPL,theme=“white”) # Plot the daily price and volume
chartSeries(AAPL,theme="white") # Plot the daily price and volume
chartSeries(AAPL) #Not shown giving the same plot with black background.
chartSeries(AAPL) #Not shown giving the same plot with black background.
limit the timespan: from=“2005-01-02”, to=“2010-12-31”
getSymbols("AAPL",from="2005-01-02", to="2010-12-31")
## [1] "AAPL"
head(AAPL)
## AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted
## 2005-01-03 1.156786 1.162679 1.117857 1.130179 691992000 0.9544083
## 2005-01-04 1.139107 1.169107 1.124464 1.141786 1096810400 0.9642097
## 2005-01-05 1.151071 1.165179 1.143750 1.151786 680433600 0.9726549
## 2005-01-06 1.154821 1.159107 1.130893 1.152679 705555200 0.9734088
## 2005-01-07 1.160714 1.243393 1.156250 1.236607 2227450400 1.0442834
## 2005-01-10 1.246964 1.262500 1.212143 1.231429 1725309600 1.0399119
The subcommand “src” is used to specify the data source. The default is Yahoo.
getSymbols("UNRATE",src="FRED") #Download unemployment rates from FRED.
## [1] "UNRATE"
head(UNRATE)
## UNRATE
## 1948-01-01 3.4
## 1948-02-01 3.8
## 1948-03-01 4.0
## 1948-04-01 3.9
## 1948-05-01 3.5
## 1948-06-01 3.6
chartSeries(UNRATE,theme="white") # Plot monthly unemployment rates
### Download data from Google.
#getSymbols("INTC",src="google") # Download data from Google.
If you try this ‘getSymbols(“INTC”,src=“google”) # Download data from Google.’ Please uncomment and try by youself:
You will get the followning worning: Error: ‘getSymbols.google’ is defunct. Google Finance stopped providing data in March, 2018. You could try setting src = “yahoo” instead. See help(“Defunct”) and help(“quantmod-defunct”)
getSymbols("^TNX") # Download CBOE 10-year Treasures Notes
## Warning: ^TNX contains missing values. Some functions will not work if objects
## contain missing values in the middle of the series. Consider using na.omit(),
## na.approx(), na.fill(), etc to remove or replace them.
## [1] "TNX"
Note List of symbols from Yaoo you have in Niezbednik
Recall form statistic “stationarity”
You might find it useful https://bookdown.org/JakeEsprabens/431-Time-Series/introduction-to-time-series.html
Performe stationarity and unit root tests on you selected share both on:
levels
logs of levels
differences of lesves
differences of logs