Skip to content

Instantly share code, notes, and snippets.

@ChengLuFred
Last active April 3, 2020 21:18
Show Gist options
  • Save ChengLuFred/94533d37b948e0add916d5abd6eeed5d to your computer and use it in GitHub Desktop.
Save ChengLuFred/94533d37b948e0add916d5abd6eeed5d to your computer and use it in GitHub Desktop.
[Quantmod] #R

Download Data

Download stock prices from yahoo finance

getSymbols.yahoo(Symbols = "MSFT",
                 env = globalenv(),
                 index.class  = 'Date',
                 from = "1980-01-01",
                 to = "2019-12-31",
                 periodicity = "daily")

get the close price

close_price <- Cl(MSFT)

compute the periodical return

monthly_log_return <- periodReturn(MSFT,
                                   period='monthly',
                                   subset=NULL,
                                   type='log')

plot the return

plot(weekly_log_return, 
     main = "Time Series Plot of MSFT's Weekly Log Returns")

slice a time series

traning_data <- window(weekly_log_return, start = '1986-03-13', end = '2017-12-31')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment