Skip to content

Instantly share code, notes, and snippets.

View yongghongg's full-sized avatar
🏠
Working from home

Yonghong Tan yongghongg

🏠
Working from home
View GitHub Profile
macd = MACD(df, 12, 26, 9)
stochastic = Stochastic(df, 14, 3)
plots = [
mpf.make_addplot((macd['macd']), color='#606060', panel=2, ylabel='MACD (12,26,9)', secondary_y=False),
mpf.make_addplot((macd['signal']), color='#1f77b4', panel=2, secondary_y=False),
mpf.make_addplot((macd['bar_positive']), type='bar', color='#4dc790', panel=2),
mpf.make_addplot((macd['bar_negative']), type='bar', color='#fd6b6c', panel=2),
mpf.make_addplot((stochastic[['%D', '%SD', 'UL', 'DL']]), ylim=[0, 100], panel=3, ylabel='Stoch (14,3)')
]
mpf.plot(df, type='candle', style='yahoo', mav=(5,20), volume=True, addplot=plots, panel_ratios=(3,1,3,3), figscale=1.5)
# Stochastic
def Stochastic(df, window, smooth_window):
stochastic = pd.DataFrame()
stochastic['%K'] = ((df['Close'] - df['Low'].rolling(window).min()) \
/ (df['High'].rolling(window).max() - df['Low'].rolling(window).min())) * 100
stochastic['%D'] = stochastic['%K'].rolling(smooth_window).mean()
stochastic['%SD'] = stochastic['%D'].rolling(smooth_window).mean()
stochastic['UL'] = 80
stochastic['DL'] = 20
return stochastic
# Add MACD as subplot
def MACD(df, window_slow, window_fast, window_signal):
macd = pd.DataFrame()
macd['ema_slow'] = df['Close'].ewm(span=window_slow).mean()
macd['ema_fast'] = df['Close'].ewm(span=window_fast).mean()
macd['macd'] = macd['ema_slow'] - macd['ema_fast']
macd['signal'] = macd['macd'].ewm(span=window_signal).mean()
macd['diff'] = macd['macd'] - macd['signal']
macd['bar_positive'] = macd['diff'].map(lambda x: x if x > 0 else 0)
macd['bar_negative'] = macd['diff'].map(lambda x: x if x < 0 else 0)
# Add moving averages and volume
mpf.plot(df, type='candle', mav=(5,20), volume=True)
# plot candlestick chart
mpf.plot(df, type='candle')
import pandas as pd
import matplotlib.pyplot as plt
import yfinance as yf
import mplfinance as mpf
# download stock price data
symbol = 'AAPL'
df = yf.download(symbol, period='6mo')
name: Update S&P500 Symbol List
on:
schedule:
- cron: '0 0 1 * *'
workflow_dispatch:
jobs:
update_symbol_list:
name: Update S&P500 symbol list periodically
# import packages
import pandas as pd
from pymongo import MongoClient
import os
# scrape the list of S&P 500
url = 'https://en.wikipedia.org/wiki/List_of_S%26P_500_companies'
payload = pd.read_html(url)
symbol_list = payload[0]
# import packages
import pandas as pd
from pymongo import MongoClient
# scrape the list of S&P 500
url = 'https://en.wikipedia.org/wiki/List_of_S%26P_500_companies'
payload = pd.read_html(url)
symbol_list = payload[0]
# save to local csv file
We can make this file beautiful and searchable if this error is corrected: It looks like row 10 should actually have 9 columns, instead of 5. in line 9.
Symbol,Security,SEC filings,GICS Sector,GICS Sub-Industry,Headquarters Location,Date first added,CIK,Founded
MMM,3M,reports,Industrials,Industrial Conglomerates,"Saint Paul, Minnesota",1976-08-09,66740,1902
AOS,A. O. Smith,reports,Industrials,Building Products,"Milwaukee, Wisconsin",2017-07-26,91142,1916
ABT,Abbott,reports,Health Care,Health Care Equipment,"North Chicago, Illinois",1964-03-31,1800,1888
ABBV,AbbVie,reports,Health Care,Pharmaceuticals,"North Chicago, Illinois",2012-12-31,1551152,2013 (1888)
ABMD,Abiomed,reports,Health Care,Health Care Equipment,"Danvers, Massachusetts",2018-05-31,815094,1981
ACN,Accenture,reports,Information Technology,IT Consulting & Other Services,"Dublin, Ireland",2011-07-06,1467373,1989
ATVI,Activision Blizzard,reports,Communication Services,Interactive Home Entertainment,"Santa Monica, California",2015-08-31,718877,2008
ADM,ADM,reports,Consumer Staples,Agricultural Products,"Chicago, Illinois",1981-07-29,7084,1902
ADBE,Adobe,reports,Information Technology,Application Softw