class UserInputs: # user inputs ticker = input('enter stock ticker') print(mpf.available_styles()) timeframe = input('enter a timeframe:') long_ma = int( input('enter length for long SMA (used taken for derivative)')) short_ma = int(input('enter length for short SMA')) # load stock data start = dt.datetime(2000, 1, 1) end = dt.datetime.now() df = web.DataReader(ticker, 'yahoo', start, end)
def plotKLine(data, add_plot): data['Time'] = pd.to_datetime(data['Time'], unit='ms') data.index = data['Time'] data.rename(columns={ 'open': 'Open', 'close': 'Close', 'high': 'High', 'low': 'Low', 'Time': 'Date' }, inplace=True) # mpf.plot(data, type='candle',style='yahoo',figratio=(2,1), figsize=(20,11),mav=(2, 5, 10)) mpf.plot(data, type='candle', style='yahoo', addplot=add_plot, figratio=(2, 1), figsize=(20, 11)) print(mpf.available_styles())
# REF: https://kknews.cc/code/j5kgzae.html # 時間序列(time series)簡單的說就是各時間點上形成的數值序列,時間序列(time series)分析就是通過觀察歷史數據預測未來的值。 # 這個函數主要是返回固定頻率的時間索引 pd.date_range(start='2019-1-09', end='2019-1-31') # index_col : 做為索引的欄位(以數值或字串表示) # parse_dates : 格式化日期欄位(使為索引的欄位) #df = pd.read_csv('stocks.csv',index_col=0,parse_dates=True) #df = pd.read_csv('stocks.csv',index_col=1,parse_dates=True) #df = pd.read_csv('stocks.csv',index_col='Date',parse_dates=True) # 下面的註解是 for windows #df = pd.read_csv("..\\Files\\2002.csv", \ df = pd.read_csv("..//Files//2002.csv", \ header = 0, \ names = ['date', 'open', 'high', 'low', 'close', 'volume', \ 'marginTrading', 'shortSelling'], \ index_col = 'date', \ parse_dates = True) # 支援K線圖的種類 mpf.available_styles() mpf.plot(df, type = 'candle', \ title = 'iron 2002', \ style = 'charles', \ volume = True, \ ylabel= 'Price ($)', \ ylabel_lower = 'Ha ha Vol')
import pandas as pd import numpy as np import mplfinance as mpf import matplotlib.pyplot as plt print(mpf.available_styles()) daily = pd.read_csv(r'examples_data\SP500_NOV2019_Hist.csv', index_col=0, parse_dates=True) print(daily.head()) daily.index.name = "Date" # 简单定义: # 1.如果收盘价比开盘价低5,做黄色标记 # 2.如果开盘价比收盘价高5,做红色标记 red_list = [] yellow_list = [] for _, row in daily.iterrows(): if row["Open"] - row["Close"] > 5: red_list.append(row["Close"]) else: red_list.append(np.NaN) if row["Close"] - row["Open"] > 5: yellow_list.append(row["Open"]) else: yellow_list.append(np.NaN) add_plot = [
| | ------ 종가(Close) | | 저가(Low) """ #%% import mplfinance as mpf kospi = yf.download('^KS11', start="2019-10-01", end="2020-12-31") #%% 기본차트 mpf.plot(kospi) # type을 지정하지 않으면 OHLC형태의 차트가 출력 mpf.plot(kospi, type='candle', title='KOSPI', ylabel='Price') mpf.available_styles() # mplfinance plot에서 가능한 스타일 확인 mpf.plot(kospi, type='candle', style='sas', title='KOSPI', ylabel='Price') #%% 거래량(Volume) 추가 mpf.plot(kospi, type='candle', style='sas', title='KOSPI', ylabel='Price', volume=True) # NOTE 볼륨을 자동으로 가져와서 그려준다. #%% mpf.plot(kospi, type='candle', style='sas', title='KOSPI', ylabel='Price',