library(reticulate)
library(tidyverse)Python: Stock Market Analysis
Cargamos librerias de R y Pyhon
import pandas as pd
import yfinance as yf
import datetime
from datetime import date, timedelta
import plotly.graph_objects as go
import plotly.express as pxConfiguramos fecha de inicio y fin
today = date.today()
d1 = today.strftime("%Y-%m-%d")
end_date = d1
d2 = date.today() - timedelta(days=365)
d2 = d2.strftime("%Y-%m-%d")
start_date = d2
f'Start: {d2}, End: {d1}''Start: 2021-08-28, End: 2022-08-28'
Descargamos los datos de Yahoo Finance
data = yf.download(
'GOOG',
start= start_date,
end= end_date,
progress= False
)
data["Date"] = data.index
data = data[[
"Date", "Open", "High", "Low",
"Close", "Adj Close", "Volume"
]]
data.reset_index(
drop=True, inplace=True
)Candlestick Chart
figure = go.Figure(
data = [go.Candlestick(
x= data["Date"],
open= data["Open"],
high= data["High"],
low= data["Low"],
close= data["Close"]
)]
)
figure.update_layout(
title= "Google Stock Price",
xaxis_rangeslider_visible=True
)
#figure.show()Line Chart
figure = px.line(
data, x="Date", y="Close",
title= "Google Close Price"
)
figure.update_xaxes(
rangeslider_visible=True
)
#figure.show()Time period selectors
figure = px.line(
data, x="Date",y="Close",
title="Time period selectors"
)
figure.update_xaxes(
rangeselector=dict(
buttons=list([
dict(count=1, label="1m", step="month", stepmode="backward"),
dict(count=6, label="6m", step="month", stepmode="backward"),
dict(count=3, label="3m", step="month", stepmode="backward"),
dict(count=1, label="1y", step="year", stepmode="backward"),
dict(step="all")
])
)
)
#figure.show()