Skip to content

Commit

Permalink
feat: add taiwan_stock_info_with_warrant, taiwan_stock_tick_snapshot,…
Browse files Browse the repository at this point in the history
… taiwan_futures_snapshot, taiwan_options_snapshot (#258)

* feat: add taiwan_stock_info_with_warrant, taiwan_stock_tick_snapshot

* feat: add taiwan_futures_snapshot, taiwan_options_snapshot
  • Loading branch information
linsamtw committed Jun 22, 2023
1 parent 2ebd9b0 commit e03c123
Show file tree
Hide file tree
Showing 6 changed files with 384 additions and 5 deletions.
141 changes: 140 additions & 1 deletion FinMind/data/data_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,27 @@ def taiwan_stock_info(self, timeout: int = None) -> pd.DataFrame:
)
return stock_info

def taiwan_securities_trader_info(self, timeout: int = None) -> pd.DataFrame:
def taiwan_stock_info_with_warrant(
self, timeout: int = None
) -> pd.DataFrame:
"""get 台股總覽(包含權證)
:param timeout (int): timeout seconds, default None
:return: 台股總覽 TaiwanStockInfoWithWarrant
:rtype pd.DataFrame
:rtype column industry_category (str)
:rtype column stock_id (str)
:rtype column stock_name (str)
:rtype column type (str)
"""
stock_info = self.get_data(
dataset=Dataset.TaiwanStockInfoWithWarrant, timeout=timeout
)
return stock_info

def taiwan_securities_trader_info(
self, timeout: int = None
) -> pd.DataFrame:
"""get 證券商資訊表
:param timeout (int): timeout seconds, default None
Expand Down Expand Up @@ -1461,6 +1481,125 @@ def us_stock_price(
)
return stock_price

def taiwan_stock_tick_snapshot(
self,
stock_id: str = "",
timeout: int = None,
) -> pd.DataFrame:
"""get 台股即時資訊 taiwan_stock_tick_snapshot (只限 sponsor 會員使用)
:param stock_id (str): 股票代號("2330")
:param timeout (int): timeout seconds, default None
:return: 台股即時資訊 taiwan_stock_tick_snapshot
:rtype pd.DataFrame
:rtype column open (float)
:rtype column high (float)
:rtype column low (float)
:rtype column close (float)
:rtype column change_price (float)
:rtype column change_rate (float)
:rtype column average_price (float)
:rtype column volume (int)
:rtype column total_volume (int)
:rtype column amount (int)
:rtype column total_amount (int)
:rtype column yesterday_volume (int)
:rtype column buy_price (float)
:rtype column buy_volume (int)
:rtype column sell_price (float)
:rtype column sell_volume (int)
:rtype column volume_ratio (float)
:rtype column date (str)
:rtype column stock_id (str)
:rtype column TickType (int)
"""
taiwan_stock_tick_snapshot = self.get_taiwan_stock_tick_snapshot(
dataset=Dataset.TaiwanStockTickSnapshot,
data_id=stock_id,
timeout=timeout,
)
return taiwan_stock_tick_snapshot

def taiwan_futures_snapshot(
self,
futures_id: str = "",
timeout: int = None,
) -> pd.DataFrame:
"""get 台股期貨即時資訊 taiwan_futures_snapshot (只限 sponsor 會員使用)
(目前支援台指期、約 10 秒更新一次)
:param futures_id (str): 股票代號("TXF")
:param timeout (int): timeout seconds, default None
:return: 台股期貨即時資訊 taiwan_futures_snapshot
:rtype pd.DataFrame
:rtype column open (float)
:rtype column high (float)
:rtype column low (float)
:rtype column close (float)
:rtype column change_price (float)
:rtype column change_rate (float)
:rtype column average_price (float)
:rtype column volume (int)
:rtype column total_volume (int)
:rtype column amount (int)
:rtype column total_amount (int)
:rtype column yesterday_volume (int)
:rtype column buy_price (float)
:rtype column buy_volume (int)
:rtype column sell_price (float)
:rtype column sell_volume (int)
:rtype column volume_ratio (float)
:rtype column date (str)
:rtype column futures_id (str)
:rtype column TickType (int)
"""
futures_snapshot = self.get_taiwan_futures_snapshot(
dataset=Dataset.TaiwanFuturesSnapshot,
data_id=futures_id,
timeout=timeout,
)
return futures_snapshot

def taiwan_options_snapshot(
self,
options_id: str = "",
timeout: int = None,
) -> pd.DataFrame:
"""get 台股選擇權即時資訊 taiwan_options_snapshot (只限 sponsor 會員使用)
(目前支援台指選擇權、約 10 秒更新一次)
:param options_id (str): 股票代號("TXO")
:param timeout (int): timeout seconds, default None
:return: 台股選擇權即時資訊 taiwan_options_snapshot
:rtype pd.DataFrame
:rtype column open (float)
:rtype column high (float)
:rtype column low (float)
:rtype column close (float)
:rtype column change_price (float)
:rtype column change_rate (float)
:rtype column average_price (float)
:rtype column volume (int)
:rtype column total_volume (int)
:rtype column amount (int)
:rtype column total_amount (int)
:rtype column yesterday_volume (int)
:rtype column buy_price (float)
:rtype column buy_volume (int)
:rtype column sell_price (float)
:rtype column sell_volume (int)
:rtype column volume_ratio (float)
:rtype column date (str)
:rtype column options_id (str)
:rtype column TickType (int)
"""
options_snapshot = self.get_taiwan_options_snapshot(
dataset=Dataset.TaiwanOptionsSnapshot,
data_id=options_id,
timeout=timeout,
)
return options_snapshot


class Feature:
def __init__(self, data_loader: DataLoader):
Expand Down
74 changes: 74 additions & 0 deletions FinMind/data/finmind_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,80 @@ def get_data(
response = request_get(url, params, timeout).json()
return pd.DataFrame(response["data"])

def get_taiwan_stock_tick_snapshot(
self,
dataset: Dataset,
data_id: str = "",
timeout: int = None,
) -> pd.DataFrame:
"""
:param params: finmind api參數
:return:
"""
params = dict(
dataset=dataset,
data_id=data_id,
user_id=self.__user_id,
password=self.__password,
token=self.__api_token,
device=self.__device,
)
params = self._compatible_api_version(params)
url = (
f"{self.__api_url}/{self.__api_version}/taiwan_stock_tick_snapshot"
)
logger.debug(params)
response = request_get(url, params, timeout).json()
return pd.DataFrame(response["data"])

def get_taiwan_futures_snapshot(
self,
dataset: Dataset,
data_id: str = "",
timeout: int = None,
) -> pd.DataFrame:
"""
:param params: finmind api參數
:return:
"""
params = dict(
dataset=dataset,
data_id=data_id,
user_id=self.__user_id,
password=self.__password,
token=self.__api_token,
device=self.__device,
)
params = self._compatible_api_version(params)
url = f"{self.__api_url}/{self.__api_version}/taiwan_futures_snapshot"
logger.debug(params)
response = request_get(url, params, timeout).json()
return pd.DataFrame(response["data"])

def get_taiwan_options_snapshot(
self,
dataset: Dataset,
data_id: str = "",
timeout: int = None,
) -> pd.DataFrame:
"""
:param params: finmind api參數
:return:
"""
params = dict(
dataset=dataset,
data_id=data_id,
user_id=self.__user_id,
password=self.__password,
token=self.__api_token,
device=self.__device,
)
params = self._compatible_api_version(params)
url = f"{self.__api_url}/{self.__api_version}/taiwan_options_snapshot"
logger.debug(params)
response = request_get(url, params, timeout).json()
return pd.DataFrame(response["data"])

def get_datalist(self, dataset: str, timeout: int = None) -> pd.DataFrame:
# 測試不支援以token方式獲取
if not self.__user_id:
Expand Down
4 changes: 4 additions & 0 deletions FinMind/schema/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Dataset(str, Enum):
USStockPrice = "USStockPrice"
TaiwanStockDividendResult = "TaiwanStockDividendResult"
TaiwanStockInfo = "TaiwanStockInfo"
TaiwanStockInfoWithWarrant = "TaiwanStockInfoWithWarrant"
TaiwanStockSecuritiesLending = "TaiwanStockSecuritiesLending"
TaiwanFutOptTickInfo = "TaiwanFutOptTickInfo"
TaiwanFutOptDailyInfo = "TaiwanFutOptDailyInfo"
Expand Down Expand Up @@ -69,6 +70,9 @@ class Dataset(str, Enum):
TaiwanStockGovernmentBankBuySell = "TaiwanStockGovernmentBankBuySell"
TaiwanSecuritiesTraderInfo ="TaiwanSecuritiesTraderInfo"
TaiwanStockMarketValue = "TaiwanStockMarketValue"
TaiwanStockTickSnapshot = "taiwan_stock_tick_snapshot"
TaiwanFuturesSnapshot = "taiwan_futures_snapshot"
TaiwanOptionsSnapshot = "taiwan_options_snapshot"


class Version(str, Enum):
Expand Down
20 changes: 18 additions & 2 deletions tests/data/test_data_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,27 @@ def test_taiwan_stock_info(data_loader):
["industry_category", "stock_id", "stock_name", "type", "date"],
)


def test_taiwan_stock_info_with_warrant(data_loader):
stock_info = data_loader.taiwan_stock_info_with_warrant()
assert_data(
stock_info,
["industry_category", "stock_id", "stock_name", "type", "date"],
)
assert len(stock_info) > 10000


def test_taiwan_securities_trader_info(data_loader):
securities_trader_info = data_loader.taiwan_securities_trader_info()
assert_data(
securities_trader_info,
["securities_trader_id", "securities_trader", "date", "address", "phone"],
[
"securities_trader_id",
"securities_trader",
"date",
"address",
"phone",
],
)


Expand Down Expand Up @@ -216,7 +232,7 @@ def test_taiwan_stock_daily(data_loader):
),
(
{
"stock_id": "00774C", # 股利資料時間晚於股價資料時間
"stock_id": "00774C", # 股利資料時間晚於股價資料時間
"start_date": "1900-01-01",
"end_date": "2023-04-22",
"expect_result": {
Expand Down

0 comments on commit e03c123

Please sign in to comment.