Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add order memo property #18

Merged
merged 5 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 42 additions & 22 deletions fugle_trade/order.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
"""
Define Order Objects
"""

import typing
from fugle_trade.constant import (
Action,
APCode,
Trade,
PriceFlag,
BSFlag
)

class OrderObject():
from fugle_trade.constant import Action, APCode, Trade, PriceFlag, BSFlag


class OrderObject:
ap_code: APCode
bs_flag: BSFlag
price_flag: PriceFlag
Expand All @@ -19,18 +15,20 @@ class OrderObject():
price: float
stock_no: str
quantity: int
user_def: str

def __init__(
self,
buy_sell: Action,
price: float,
stock_no: str,
quantity: int,
ap_code: APCode = APCode.Common,
bs_flag: BSFlag = BSFlag.ROD,
price_flag: PriceFlag = PriceFlag.Limit,
trade: Trade = Trade.Cash,
):
self,
buy_sell: Action,
price: float,
stock_no: str,
quantity: int,
ap_code: APCode = APCode.Common,
bs_flag: BSFlag = BSFlag.ROD,
price_flag: PriceFlag = PriceFlag.Limit,
trade: Trade = Trade.Cash,
user_def: str = "",
):

if type(buy_sell) is not Action:
raise TypeError("Please use fugleTrade.constant Action")
Expand All @@ -50,8 +48,14 @@ def __init__(
if quantity < 1 or quantity > 1000:
raise TypeError("quantity must within range 1 ~ 999")
elif ap_code == APCode.Emg:
if quantity < 1 or quantity > 499000 or (quantity > 1000 and quantity % 1000 != 0):
raise TypeError("quantity must within range 1 ~ 499000, or multiply of 1000")
if (
quantity < 1
or quantity > 499000
or (quantity > 1000 and quantity % 1000 != 0)
):
raise TypeError(
"quantity must within range 1 ~ 499000, or multiply of 1000"
)

if type(ap_code) is not APCode:
raise TypeError("Please use fugleTrade.constant APCode")
Expand All @@ -65,6 +69,9 @@ def __init__(
if type(trade) is not Trade:
raise TypeError("Please use fugleTrade.constant Trade")

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

self.ap_code = ap_code
self.price_flag = price_flag
self.bs_flag = bs_flag
Expand All @@ -73,6 +80,19 @@ def __init__(
self.price = price
self.stock_no = stock_no
self.quantity = quantity
self.user_def = user_def

def __str__(self):
return "ap_code: %s, price_flag: %s, bs_flag: %s, trade: %s, buy_sell: %s, price: %s, stock_no: %s, quantity: %s" % (self.ap_code, self.price_flag, self.bs_flag, self.trade, self.buy_sell, self.price, self.stock_no, self.quantity)
return (
"ap_code: %s, price_flag: %s, bs_flag: %s, trade: %s, buy_sell: %s, price: %s, stock_no: %s, quantity: %s"
% (
self.ap_code,
self.price_flag,
self.bs_flag,
self.trade,
self.buy_sell,
self.price,
self.stock_no,
self.quantity,
)
)
55 changes: 48 additions & 7 deletions fugle_trade/sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Expose as a single library for user to use

"""

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 @@ -75,7 +77,7 @@ def reset_password(self):

def place_order(self, order_object: OrderObject):
"""place order"""
return loads(self.__core.order(order_object))["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 @@ -137,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("0", start, end)
return loads(order_res_history)["data"]["order_result_history"]
order_res_history = self.__core.get_order_result_history(start, end, "0")
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 @@ -195,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 @@ -221,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
Loading
Loading