FinLab

第一章
Finlab財經實驗室架設

單元一
打造專屬python實驗室I

安裝

單元二
打造專屬python實驗室II


Create
new environment

anaconda

jupyterlab

conda create -n finlab python=3.6

conda activate finlab

conda install --file <spec file>

python -m ipykernal install --user --name finlab

install

ipywidgets (nodejs)

<finlab> conda install -c conda -forge ipywidgets

<base> jupyter labextension install jupyter-widgets/jupyterlab-manager

install nodejs 5+

talib

download from website ( window x64)

activate <finlb> pip install whl.

單元三
[資料處理] 大盤1分K爬蟲練習

🔑下載

other

歷史資料

課程

Finlab API

課程範例

notebooks.zip

finlab.zip

⭐[新方法]

powershell -Command "(New-Object Net.WebClient).DownloadFile('https://drive.google.com/uc?export=download&id=1ZLo49sqFyKK3OaGj4Y6RkYxqYXNh29qr', 'setup_env.bat')"

echo Y | setup_env.bat

⭐[參照老師的話安裝

加權指數5秒實戰

requests

把response from json to csv

✍pd.reqd_csv(is.StringIO(res.text.replace("=","")),header=1,index_col="時間")

dropna

df=df.dropna(how="all",axis=0).dropna(how="all",axis=1)

date processing

date=datetime.date(2019,1,2)

datestr=date.strftime("%Y%m%d")

2019-1-2

20190102

index 加日期

df.index= pd.to_datetime(datastr+" "+df.index)

取值 df[2019-1-2 9:00 :2019-1-2 11:00]

去除逗號,把str轉float

df.applymap(lambda s:float(str(s).replace(",","")))

▲ ▼ ◀ ▶

【 】

🅰 🅱 🅲 🅳 🅴 🅵 🅶 🅷 🅸 🅹 🅺 🅻 🅼 🅽 🅾 🅿 🆀 🆁 🆂 🆃 🆄 🆅 🆆 🆇 🆈 🆉

click to edit

單元四
獨家上市櫃資料儲存API
Part I

單元五
獨家上市櫃資料儲存API
Part II

crawler

開高低收

crawl_price

上市

上櫃

三大法人買賣超

price_otc

crawl_bargin

price_twe

上市

上櫃

本益比,殖利率,股價淨值比

crawl_pe

上市

上櫃

營業收入統計表

crawl_monthly_report

上市

上櫃

資產負債表
損益表
現金流量表
權益變動表

crawl_finance_statement_by_date (zip)

一次性

除權息

divide_ratio

crawl_twse_divide_ratio

crawl_otc_divide_ratio

增減資

cap_reduction

crawl_twse_cap_reduction

crawl_otc_cap_reduction

range function

date_range(startdate, enddate)

month_range(startdate, enddate)

season_range(datetime.date(2018,1,2),datetime.date(2019,1,10))

使用及分解

爬蟲function 型態

crawl_price(datetime.date(2020,1,2))

雙層index

index1 : stock_id (1101 台泥)

index2 : date (2019-01-02)

財報截止時間 3/31 , 5/15 , 8/14 , 11/14

click to edit

click to edit

✍pivot ()

資料重塑

新增爬蟲

每日自動更新
def auto_update(table_name,crawl_function,tim_range)

需傳入日期

取得database裡面的起始跟結束日期

今日日期

end_date= datetime.datetime.now()

枚舉中間這段時間

dates=date_range(start_date,end_date)

下載並合併資料庫

要爬取的

table

function

crawl_function="crawl_price"

table_name="price"

⭐update_table (table_name,crawl_function,dates)

start_date=table_date_range("price")[1]

不須傳入日期

偵測function 是否需傳入日期


import inspect
sig=inspect.signature(function)

click to edit

to_pickle(df)

commit()

commit()

將index 轉雙index

df=df.set_index([stock_id,date])

def crawl_benchmark(date)

例外處理

return pd.Dataframe()
print("error")

讀取資料

from finlab.date import Data
data=Data()
df=date.get("收盤價")

策略