How to get reliance data from yahoo finance in Python?
df= yf.download('RELIANCE.NS',start='2012-01-01',end='2021-09-04',
progress =False)
What is alternative sources to get financial data?
There are number of alternative sources like Quandal,intrinio,google others.
df.reset_index(drop=False, inplace=True)
df.rename(columns={'Date': 'ds', 'Adj Close': 'y'}, inplace=True)
df=DataFrame(df)
df
index | ds | Open | High | Low | Close | y | Volume | |
---|---|---|---|---|---|---|---|---|
0 | 0 | 2012-01-02 | 345.128540 | 351.542725 | 340.348846 | 349.957764 | 323.036774 | 8679938 |
1 | 1 | 2012-01-03 | 352.780975 | 360.037201 | 351.839905 | 358.922760 | 331.312103 | 9455771 |
2 | 2 | 2012-01-04 | 360.284851 | 362.043182 | 353.325836 | 354.712677 | 327.425934 | 8557084 |
3 | 3 | 2012-01-05 | 354.143066 | 359.071350 | 343.791199 | 346.465851 | 319.813477 | 13364666 |
4 | 4 | 2012-01-06 | 345.252350 | 358.600830 | 345.054230 | 355.406097 | 328.065948 | 9495456 |
... | ... | ... | ... | ... | ... | ... | ... | ... |
2377 | 2377 | 2021-08-30 | 2250.000000 | 2275.850098 | 2236.800049 | 2270.250000 | 2270.250000 | 6473487 |
2378 | 2378 | 2021-08-31 | 2276.899902 | 2283.750000 | 2242.250000 | 2258.149902 | 2258.149902 | 12223037 |
2379 | 2379 | 2021-09-01 | 2273.000000 | 2292.899902 | 2263.000000 | 2267.100098 | 2267.100098 | 5143640 |
2380 | 2380 | 2021-09-02 | 2255.000000 | 2307.800049 | 2255.000000 | 2294.399902 | 2294.399902 | 4595048 |
2381 | 2381 | 2021-09-03 | 2310.000000 | 2395.000000 | 2302.500000 | 2388.500000 | 2388.500000 | 14151629 |
2382 rows × 8 columns
train_indices = df.ds.apply(lambda x: x.year) < 2021
df_train = df.loc[train_indices].dropna()
df_test = df.loc[~train_indices].reset_index(drop=True)
model_prophet = Prophet(seasonality_mode='additive')
model_prophet.add_seasonality(name='monthly', period=30.5,
fourier_order=5)
How to plot close price of 2022 reliance?
model_prophet.fit(df_train)
df_future = model_prophet.make_future_dataframe(periods=365)
df_pred = model_prophet.predict(df_future)
model_prophet.plot(df_pred)
model_prophet.plot_components(df_pred)
df_yah= yf.download('RELIANCE.NS',start='2012-01-01',end='2021-09-04',
progress =False)
import pandas as pd
import numpy as np
from pandas import Series, DataFrame
data=DataFrame(df_yah)
data
Open | High | Low | Close | Adj Close | Volume | |
---|---|---|---|---|---|---|
Date | ||||||
2012-01-02 | 345.128540 | 351.542725 | 340.348846 | 349.957764 | 323.036774 | 8679938 |
2012-01-03 | 352.780975 | 360.037201 | 351.839905 | 358.922760 | 331.312103 | 9455771 |
2012-01-04 | 360.284851 | 362.043182 | 353.325836 | 354.712677 | 327.425934 | 8557084 |
2012-01-05 | 354.143066 | 359.071350 | 343.791199 | 346.465851 | 319.813477 | 13364666 |
2012-01-06 | 345.252350 | 358.600830 | 345.054230 | 355.406097 | 328.065948 | 9495456 |
... | ... | ... | ... | ... | ... | ... |
2021-08-30 | 2250.000000 | 2275.850098 | 2236.800049 | 2270.250000 | 2270.250000 | 6473487 |
2021-08-31 | 2276.899902 | 2283.750000 | 2242.250000 | 2258.149902 | 2258.149902 | 12223037 |
2021-09-01 | 2273.000000 | 2292.899902 | 2263.000000 | 2267.100098 | 2267.100098 | 5143640 |
2021-09-02 | 2255.000000 | 2307.800049 | 2255.000000 | 2294.399902 | 2294.399902 | 4595048 |
2021-09-03 | 2310.000000 | 2395.000000 | 2302.500000 | 2388.500000 | 2388.500000 | 14151629 |
2382 rows × 6 columns
DataFrame(data, columns=['Close', 'Volume'])
Close | Volume | |
---|---|---|
Date | ||
2012-01-02 | 349.957764 | 8679938 |
2012-01-03 | 358.922760 | 9455771 |
2012-01-04 | 354.712677 | 8557084 |
2012-01-05 | 346.465851 | 13364666 |
2012-01-06 | 355.406097 | 9495456 |
... | ... | ... |
2021-08-30 | 2270.250000 | 6473487 |
2021-08-31 | 2258.149902 | 12223037 |
2021-09-01 | 2267.100098 | 5143640 |
2021-09-02 | 2294.399902 | 4595048 |
2021-09-03 | 2388.500000 | 14151629 |
2382 rows × 2 columns
frame2=DataFrame(data,columns=['Close'])
frame2
Close | |
---|---|
Date | |
2012-01-02 | 349.957764 |
2012-01-03 | 358.922760 |
2012-01-04 | 354.712677 |
2012-01-05 | 346.465851 |
2012-01-06 | 355.406097 |
... | ... |
2021-08-30 | 2270.250000 |
2021-08-31 | 2258.149902 |
2021-09-01 | 2267.100098 |
2021-09-02 | 2294.399902 |
2021-09-03 | 2388.500000 |
2382 rows × 1 columns
import cufflinks as cf
from plotly.offline import iplot, init_notebook_mode
init_notebook_mode()
goog = data.resample('M') \
.last() \
.rename(columns={'Adj Close': 'adj_close'}) \
.adj_close
goog # convert daily into monthly goog(RIL)
Date
2012-01-31 373.580566
2012-02-29 375.249329
2012-03-31 343.176544
2012-04-30 340.661957
2012-05-31 326.532379
...
2021-05-31 2153.372803
2021-06-30 2110.649902
2021-07-31 2035.300049
2021-08-31 2258.149902
2021-09-30 2388.500000
Freq: M, Name: adj_close, Length: 117, dtype: float64
train_indices = goog.index.year < 2021
goog_train = goog[train_indices]
goog_test = goog[~train_indices]
test_length = len(goog_test)
goog.plot(title="Relian's Stock Price");
How to plot daily stock return?
ses_1 = SimpleExpSmoothing(goog_train).fit(smoothing_level=0.2)
ses_forecast_1 = ses_1.forecast(test_length)
ses_2 = SimpleExpSmoothing(goog_train).fit(smoothing_level=0.5)
ses_forecast_2 = ses_2.forecast(test_length)
ses_3 = SimpleExpSmoothing(goog_train).fit()
alpha = ses_3.model.params['smoothing_level']
ses_forecast_3 = ses_3.forecast(test_length)
goog.plot(color="g",
title='Simple Exponential Smoothing',
label='Actual',
legend=True)
ses_forecast_1.plot(color="r", legend=True,
label=r'$\alpha=0.2$')
ses_1.fittedvalues.plot(color="r")
ses_forecast_2.plot(color="k", legend=True,
label=r'$\alpha=0.5$')
ses_2.fittedvalues.plot(color="k")
ses_forecast_3.plot(color="b", legend=True,
label=r'$\alpha={0:.4f}$'.format(alpha))
ses_3.fittedvalues.plot(color="b")
0 Comments