Skip to content

Commit

Permalink
refactor: update response memo to user_def
Browse files Browse the repository at this point in the history
  • Loading branch information
zjkung1123 committed Jul 10, 2024
1 parent a8f7462 commit 1bec71a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 11 deletions.
10 changes: 5 additions & 5 deletions fugle_trade/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class OrderObject:
price: float
stock_no: str
quantity: int
memo: str
user_def: str

def __init__(
self,
Expand All @@ -27,7 +27,7 @@ def __init__(
bs_flag: BSFlag = BSFlag.ROD,
price_flag: PriceFlag = PriceFlag.Limit,
trade: Trade = Trade.Cash,
memo: str = "",
user_def: str = "",
):

if type(buy_sell) is not Action:
Expand Down Expand Up @@ -69,8 +69,8 @@ def __init__(
if type(trade) is not Trade:
raise TypeError("Please use fugleTrade.constant Trade")

if len(memo) > 50:
memo = memo[:50]
if len(user_def) > 50:
user_def = user_def[:50]

self.ap_code = ap_code
self.price_flag = price_flag
Expand All @@ -80,7 +80,7 @@ def __init__(
self.price = price
self.stock_no = stock_no
self.quantity = quantity
self.memo = memo
self.user_def = user_def

def __str__(self):
return (
Expand Down
52 changes: 46 additions & 6 deletions fugle_trade/sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

from typing import Any, Dict, List, Union
from fugle_trade_core.fugle_trade_core import CoreSDK
from fugle_trade.constant import APCode, PriceFlag
from fugle_trade.order import OrderObject
Expand Down Expand Up @@ -76,7 +77,7 @@ def reset_password(self):

def place_order(self, order_object: OrderObject):
"""place order"""
return loads(self.__core.order(order_object, order_object.memo))["data"]
return loads(self.__core.order(order_object, order_object.user_def))["data"]

def delete_order(self, order_result):
"""delete_order"""
Expand Down Expand Up @@ -138,27 +139,36 @@ def modify_price(
def get_order_results(self):
"""get order result data 取得當日委託明細"""
order_res = self.__core.get_order_results()
return loads(order_res)["data"]["order_results"]
order_results = loads(order_res)["data"]["order_results"]
return self.replace_key_in_dict(order_results, "memo", "user_def")

def get_order_results_by_date(self, start, end):
"""get order result data by date 用日期當作篩選條件委託明細"""
order_res_history = self.__core.get_order_result_history(start, end, "0")
return loads(order_res_history)["data"]["order_result_history"]
return self.replace_key_in_dict(
loads(order_res_history)["data"]["order_result_history"], "memo", "user_def"
)

def get_transactions(self, query_range):
"""get transactions data 成交明細"""
transactions_res = self.__core.get_transactions(query_range)
return loads(transactions_res)["data"]["mat_sums"]
return self.replace_key_in_dict(
loads(transactions_res)["data"]["mat_sums"], "memo", "user_def"
)

def get_transactions_by_date(self, start, end):
"""用日期當作篩選條件 get transactions data by date 成交明細"""
transactions_res = self.__core.get_transactions_by_date(start, end)
return loads(transactions_res)["data"]["mat_sums"]
return self.replace_key_in_dict(
loads(transactions_res)["data"]["mat_sums"], "memo", "user_def"
)

def get_inventories(self):
"""get inventories data 庫存資訊"""
inventories_res = self.__core.get_inventories()
return loads(inventories_res)["data"]["stk_sums"]
return self.replace_key_in_dict(
loads(inventories_res)["data"]["stk_sums"], "memo", "user_def"
)

def get_balance(self):
"""get balance data 餘額資訊"""
Expand Down Expand Up @@ -196,6 +206,7 @@ def get_machine_time(self):
return loads(key_info_res)["data"]

def on(self, param):

def inner(func):
"""
do operations with func, register func into __wsHandler
Expand All @@ -222,3 +233,32 @@ def recover_order_result(self, order_result):
else:
new_dict[key] = int(value)
return {**order_result, **new_dict}

def replace_key_in_dict(
self, data: Union[Dict[str, Any], List[Any]], old_key: str, new_key: str
) -> Union[Dict[str, Any], List[Any]]:
"""
Recursively replace keys in a dictionary or list of dictionaries according to a key mapping.
Parameters:
data (Union[Dict[str, Any], List[Any]]): The dictionary or list to update.
old_key (str): The key to replace.
new_key (str): The new key name.
Returns:
Union[Dict[str, Any], List[Any]]: The updated dictionary or list with keys replaced.
"""
if isinstance(data, list):
return [self.replace_key_in_dict(item, old_key, new_key) for item in data]
elif isinstance(data, dict):
new_dict = {}
for k, v in data.items():
new_key_name = new_key if k == old_key else k
new_dict[new_key_name] = (
self.replace_key_in_dict(v, old_key, new_key)
if isinstance(v, (dict, list))
else v
)
return new_dict
else:
return data
6 changes: 6 additions & 0 deletions tests/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def test_get_order_results(sdk):
assert result[0]["err_msg"] == ""
assert result[0]["avg_price"] == 0
assert result[0]["bs_flag"] == "R"
assert result[0]["user_def"] == ""

# Assertions for the second order result
assert result[1]["work_date"] == "20220222"
Expand All @@ -80,6 +81,7 @@ def test_get_order_results(sdk):
assert result[1]["err_msg"] == ""
assert result[1]["avg_price"] == 0
assert result[1]["bs_flag"] == "R"
assert result[1]["user_def"] == ""


def test_get_order_result_by_date(sdk):
Expand All @@ -105,6 +107,7 @@ def test_get_order_result_by_date(sdk):
assert result[0]["celable"] == 1
assert result[0]["avg_price"] == 0
assert result[0]["bs_flag"] == "R"
assert result[0]["user_def"] == ""

# Assertions for the second order result
assert result[1]["ack_date"] == "20220314"
Expand All @@ -127,13 +130,15 @@ def test_get_order_result_by_date(sdk):
assert result[1]["celable"] == "2"
assert result[1]["avg_price"] == 0
assert result[1]["bs_flag"] == "R"
assert result[1]["user_def"] == ""


def test_get_inventories(sdk):
result = sdk.get_inventories()
assert result[0]["stk_no"] == "0056"
assert result[0]["stk_na"] == "元大高股息"
assert result[0]["price_mkt"] == "33.49"
assert result[0]["stk_dats"][0]["user_def"] == ""


def test_get_settlements(sdk):
Expand All @@ -148,6 +153,7 @@ def test_get_transactions(sdk):
assert result[0]["stk_no"] == "2884"
assert result[0]["buy_sell"] == "B"
assert result[0]["qty"] == "1000"
assert result[0]["mat_dats"][0]["user_def"] == ""


def test_cancel_order_intraday_odd(sdk):
Expand Down

0 comments on commit 1bec71a

Please sign in to comment.