Skip to content

Instantly share code, notes, and snippets.

@yongghongg
Last active June 19, 2021 12:08
Show Gist options
  • Save yongghongg/47fe7d86029dd8ff5e83a7daa40efa06 to your computer and use it in GitHub Desktop.
Save yongghongg/47fe7d86029dd8ff5e83a7daa40efa06 to your computer and use it in GitHub Desktop.
def get_stock_price(ticker):
data = yf.download(ticker, start="2021-01-01")
return data
def add_EMA(price, day):
return price.ewm(span=day).mean()
def add_STOCH(close, low, high, period, k, d=0):
STOCH_K = ((close - low.rolling(window=period).min()) / (high.rolling(window=period).max() - low.rolling(window=period).min())) * 100
STOCH_K = STOCH_K.rolling(window=k).mean()
if d == 0:
return STOCH_K
else:
STOCH_D = STOCH_K.rolling(window=d).mean()
return STOCH_D
stock_code = "7233"
price_chart_df = get_stock_price(stock_code + ".KL")
open = price_chart_df['Open']
close = price_chart_df['Close']
high = price_chart_df['High']
low = price_chart_df['Low']
price_chart_df['EMA18'] = add_EMA(close,18)
price_chart_df['EMA50'] = add_EMA(close,50)
price_chart_df['EMA100'] = add_EMA(close,100)
price_chart_df['STOCH_%K(5,3,3)'] = add_STOCH(close, low, high, 5, 3)
price_chart_df['STOCH_%D(5,3,3)'] = add_STOCH(close, low, high, 5, 3, 3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment