Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
8eacee2
Update README.md
LastAncientOne Dec 5, 2020
b48822f
Update README.md
LastAncientOne Dec 5, 2020
ec6890d
Add files via upload
LastAncientOne Dec 6, 2020
7b4fd5a
Add files via upload
LastAncientOne Dec 7, 2020
69f574a
Add files via upload
LastAncientOne Dec 8, 2020
5e5f1c6
Add files via upload
LastAncientOne Dec 9, 2020
1ae316f
Delete Python_5_scrape_yahoo_stock.py
LastAncientOne Dec 9, 2020
8eeb41a
Add files via upload
LastAncientOne Dec 9, 2020
d9c64ca
Add files via upload
LastAncientOne Dec 10, 2020
78a9669
Add files via upload
LastAncientOne Dec 11, 2020
2126fe6
Add files via upload
LastAncientOne Dec 12, 2020
f7e3274
Add files via upload
LastAncientOne Dec 12, 2020
b18ac56
Add files via upload
LastAncientOne Dec 15, 2020
184efe0
Add files via upload
LastAncientOne Dec 16, 2020
f3eacbe
Delete Python_10_Plot_Boxplot.py
LastAncientOne Dec 20, 2020
bf98f08
Delete Python_1_Dowload__YahooData.py
LastAncientOne Dec 20, 2020
3c74383
Delete Python_2_Modify_Date.py
LastAncientOne Dec 20, 2020
78f9db3
Delete Python_9_Plot_Scatter_Line.py
LastAncientOne Dec 20, 2020
a784030
Delete Python_3_save_csv.py
LastAncientOne Dec 20, 2020
460e965
Delete Python_4_Plot_Charts.py
LastAncientOne Dec 20, 2020
e193c86
Delete Python_5_Plot_Candlestick.py
LastAncientOne Dec 20, 2020
9f6e197
Delete Python_6_Plot_Area.py
LastAncientOne Dec 20, 2020
39725a1
Delete Python_7_Plot_Histogram.py
LastAncientOne Dec 20, 2020
da84e53
Delete Python_8_Plot_Bokeh_Candlestick.py
LastAncientOne Dec 20, 2020
b8e92fe
Add files via upload
LastAncientOne Dec 20, 2020
5ed129a
LICENSE
LastAncientOne Jan 7, 2021
915cca6
Add files via upload
LastAncientOne Feb 8, 2021
0487f70
Delete Python_11_scrape_yahoo_stock.py
LastAncientOne Feb 8, 2021
ada3561
Add files via upload
LastAncientOne Feb 8, 2021
ac41634
Add files via upload
LastAncientOne Feb 9, 2021
557b5f0
Update README.md
LastAncientOne Feb 11, 2021
0818a16
Update README.md
LastAncientOne Feb 12, 2021
78e87c2
Update README.md
LastAncientOne Feb 12, 2021
22e8b7f
Update README.md
LastAncientOne Feb 12, 2021
b7b8f0b
Update README.md
LastAncientOne Feb 13, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 LastAncientOne

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
29 changes: 29 additions & 0 deletions Python_01_Dowload__YahooData.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 27 08:09:11 2020

@author: Tin
"""
# Download stock historical data from Yahoo Finance

import yfinance as yf
yf.pdr_override()


# input
symbol = 'AAPL'
start = '2014-01-01'
end = '2018-01-01'


# dataframe
df = yf.download(symbol,start,end)

# View the first 5 rows
print('First 5 Rows')
print(df.head())
print('-'*80)

# View the last 5 rows
print('Last 5 Rows')
print(df.tail())
150 changes: 150 additions & 0 deletions Python_02_Modify_Date.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 27 08:09:11 2020

@author: Tin
"""
# Modify Yahoo Dataframe Date
import pandas as pd # Dataframe Library
pd.set_option('max_columns', None) # To show all columns

import yfinance as yf
yf.pdr_override()


# input
symbol = 'AAPL'
start = '2014-01-01'
end = '2018-01-01'


# dataframe
data = yf.download(symbol,start,end)

# View the first 5 rows
print('First 5 Rows')
print(data.head())
print('-'*80)


# Date becomes a columns
df = data.copy() # Copy the original data
dfn = df.reset_index()
print(dfn.head())
print('-'*80)


# Add Year, Month, Day
df['Year'] = df.index.year
df['Month'] = df.index.month
df['Day'] = df.index.day
print('Year, Month, & Day')
print(df.head())
print('-'*80)


# Convert Daily to Weekly
weekly = data.copy()
weekly = weekly.resample('W').last()
print('Weekly Data')
print(weekly.head())
print('-'*80)


# Convert Daily to Monthly
monthly = data.copy()
monthly = monthly.resample('1M').mean()
print('Monthly Data')
print(monthly.head())
print('-'*80)


# Choose Particular Year to analyze
monthly = data.copy()
monthly = monthly.reset_index()
y2017 = monthly[monthly['Date'].dt.year==2017]
print("Analyze Particular Year in Historical Data")
print(y2017)
print('-'*80)


month_name = data.copy()
# Convert Daily to Monthly
# 'BMS', which stands for "business month start frequency"
# 'BM', which stands for "business month end frequency"
month_name = month_name.asfreq('BM')
print('Number of the Month')
print(month_name.head())
print('-'*80)


import calendar
month_name['Month_Number'] = month_name.index.month
month_name['Month_ABBR'] = month_name['Month_Number'].apply(lambda x: calendar.month_abbr[x])
print('Abbreviation for Months')
print(month_name.head())
print('-'*80)


print('Month Name')
month_name['Month_Name'] = month_name['Month_Number'].apply(lambda x: calendar.month_name[x])
print(month_name.head())
print('-'*80)


# Pivot Table Date
df_months = pd.pivot_table(df, index=df.index.month, columns=df.index.year, values = 'Adj Close') # each months
print('Year by Year')
print(df_months)
print('-'*80)


df_days = pd.pivot_table(df, index=df.index.day, columns=df.index.year, values = 'Adj Close') # daily for one whole months
print('Year by Year in daily rows')
print(df_days)
print('-'*80)


df_all_columns = pd.pivot_table(df, index=df.index.month, columns=df.index.year)
print('All columns in yearly')
print(df_all_columns)
print('-'*80)


stock_data = df.copy()
stock_data['Year'] = df.index.year
stock_data['Month'] = df.index.month
stock_data['Day'] = df.index.day
stock_data['Week_Day'] = df.index.dayofweek
stock_data['Week_Day_Name'] = df.index.strftime('%A')
print('Number of day with M-F')
print(stock_data.tail(10))
print('-'*80)


approach1 = stock_data.groupby(['Year', 'Month']).first()['Adj Close']
print('# of Month')
print(approach1.tail(12))
print('-'*80)


approach2 = stock_data.groupby(['Year', 'Day']).first()['Adj Close']
print('# of Day')
print(approach2.tail(12))
print('-'*80)


print('Convert Date to String')
string_date = data.copy()
string_date['Date'] = string_date.index
print(string_date.head())
print('-'*80)


string_date['Date'] = string_date['Date'].dt.strftime("%Y%m%d").astype(int)
print('Convert Date to Numbers')
print(string_date.head())




36 changes: 36 additions & 0 deletions Python_03_save_csv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# -*- coding: utf-8 -*-
"""
Created on Fri Dec 4 18:55:42 2020

@author: Tin
"""
# Save Data to CSV
import warnings
warnings.filterwarnings("ignore")

import yfinance as yf
yf.pdr_override()

# input
symbol = 'AMD'
start = '2014-01-01'
end = '2019-01-01'

# Read data
data = yf.download(symbol,start,end)

# Output data into CSV
# To save in your certain folder, change the Users name
data.to_csv("C:/Users/Finance/Desktop/AMD.csv")



symbols = ['PFE','TGT','MA','UNH','VZ','V','WMT','GS']
start = '2014-01-11'
end = '2019-01-01'
stocks_info = yf.download(symbols, start, end)['Adj Close']
stocks_data = stocks_info.iloc[::]
print(stocks_data)

# Output data into CSV
stocks_data.to_csv("C:/Users/Finance/Desktop/stocks_data.csv")
147 changes: 147 additions & 0 deletions Python_04_Plot_Charts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 27 08:09:11 2020

@author: Tin
"""
# Plot Line Charts
import pandas as pd # Dataframe Library
import matplotlib.pyplot as plt # Plot Chart Library

pd.set_option('max_columns', None) # To show all columns

import yfinance as yf
yf.pdr_override()


# input
symbol = 'AAPL'
start = '2014-01-01'
end = '2018-01-01'


# dataframe
data = yf.download(symbol,start,end)

# View the first 5 rows
print('First 5 Rows')
print(data.head())
print('-'*80)

print('Line Chart')
plt.figure(figsize=(12,8))
plt.plot(data['Adj Close'])
plt.title("Stock Line Chart")
plt.legend(loc='best')
plt.xlabel("Date")
plt.ylabel("Price")
plt.show()
print('-'*80)


print('Line Chart with Grid')
plt.figure(figsize=(12,8))
plt.plot(data['Adj Close'])
plt.title("Stock Line Chart")
plt.grid()
plt.legend(loc='best')
plt.xlabel("Date")
plt.ylabel("Price")
plt.show()
print('-'*80)


print('Render the grid')
fig, ax = plt.subplots()
data.plot(kind='line', y= 'Adj Close', ax=ax)
# Turn on the grid
ax.grid()
plt.title("Stock Line Chart")
plt.legend(loc='best')
plt.xlabel("Date")
plt.ylabel("Price")
plt.show()
print('-'*80)


print('Customize the grid')
fig, ax = plt.subplots()
data.plot(kind='line', y= 'Adj Close', ax=ax)
# Don't allow the axis to be on top of your data
ax.set_axisbelow(True)
# Customize the grid
ax.grid(linestyle='-', linewidth='0.5', color='red')
plt.title("Stock Line Chart")
plt.xlabel("Date")
plt.ylabel("Price")
plt.show()
print('-'*80)


print('Major grid & Minor Grid')
plt.figure(figsize=(12,8))
plt.plot(data['Adj Close'])
plt.minorticks_on()
plt.grid(b=True, which='major', color='b', linestyle='-')
plt.grid(b=True, which='minor', color='r', linestyle='--')
plt.title("Stock Line Chart")
plt.xlabel("Date")
plt.ylabel("Price")
plt.show()
print('-'*80)


import seaborn as sns # Plot Library 0.9.0 Version
# conda install -c anaconda seaborn=0.9.0
plt.figure(figsize=(10,5))
sns.lineplot(data=data, x=data.index, y='Adj Close')
print('-'*80)

plt.figure(figsize=(10,5))
top = plt.subplot2grid((4,4), (0, 0), rowspan=3, colspan=4)
bottom = plt.subplot2grid((4,4), (3,0), rowspan=1, colspan=4)
top.plot(data.index, data['Adj Close'])
bottom.bar(data.index, data['Volume'])

# set the labels
top.axes.get_xaxis().set_visible(False)
top.set_title('Stock Price and Volume')
top.set_ylabel('Adj Closing Price')
bottom.set_ylabel('Volume')
print('-'*80)


# Candlestick
from mpl_finance import candlestick_ohlc
from matplotlib import dates as mdates

# Converting date to pandas datetime format
dfc = data.copy()
dfc = dfc.reset_index()
dfc['Date'] = pd.to_datetime(dfc['Date'])
dfc['Date'] = dfc['Date'].apply(mdates.date2num)
# dfc.head()

fig = plt.figure(figsize=(14,10))
ax1 = plt.subplot(2, 1, 1)
candlestick_ohlc(ax1,dfc.values, width=0.5, colorup='g', colordown='r', alpha=1.0)
ax1.xaxis_date()
ax1.xaxis.set_major_formatter(mdates.DateFormatter('%d-%m-%Y'))
ax1.set_title('Stock '+ symbol +' Closing Price')
ax1.set_ylabel('Price')




import plotly.graph_objs as go
# from plotly.offline import init_notebook_mode, iplot

df = data.copy()
# Plot OHLC Bar Chart
trace = go.Ohlc(x=df['12-2016'].index,
open=df['12-2016'].Open,
high=df['12-2016'].High,
low=df['12-2016'].Low,
close=df['12-2016'].Close)
data = [trace]
iplot(data, filename='simple_ohlc')
Loading