You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
2.3 KiB

import json
from pathlib import Path
from decimal import Decimal
from utils import dingding, smtp
import datetime
import time
import pytz
def as_num(x):
# 32f表示保留32位小数点的float型
y = '{:.32f}'.format(x)
return(y)
def get_current_timestamp():
"""
获取系统的时间.
:return:
"""
return int(time.time() * 1000)
def alert(msg):
print(msg)
dingding.dingding_at_me(msg)
smtp.send_email(msg)
def cn_time():
"""
chinese time
https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
"""
return datetime.datetime.now(pytz.timezone('Asia/Shanghai'))
def _get_trader_dir(temp_name: str):
"""
Get path where trader is running in.
"""
cwd = Path.cwd()
temp_path = cwd.joinpath(temp_name)
if temp_path.exists():
return cwd, temp_path
if not temp_path.exists():
temp_path.mkdir()
return cwd, temp_path
TRADER_DIR, TEMP_DIR = _get_trader_dir("trader")
def get_file_path(filename: str):
"""
Get path for temp file with filename.
"""
return TEMP_DIR.joinpath(filename)
def get_folder_path(folder_name: str):
"""
Get path for temp folder with folder name.
"""
folder_path = TEMP_DIR.joinpath(folder_name)
if not folder_path.exists():
folder_path.mkdir()
return folder_path
def load_json(filename: str):
"""
Load data from json file in temp path.
"""
filepath = get_file_path(filename)
if filepath.exists():
with open(filepath, mode="r", encoding="UTF-8") as f:
data = json.load(f)
return data
else:
save_json(filename, {})
return {}
def save_json(filename: str, data: dict):
"""
Save data into json file in temp path.
"""
filepath = get_file_path(filename)
with open(filepath, mode="w+", encoding="UTF-8") as f:
json.dump(
data,
f,
indent=4,
ensure_ascii=False
)
def round_to(value: float, target: float) -> float:
"""
Round price to price tick value.
"""
value = Decimal(str(value))
target = Decimal(str(target))
#round() 方法返回浮点数x的四舍五入值。
##eg:
## quantity = round_to(float(config.quantity), float(config.min_qty))
rounded = float(int(round(value / target)) * target)
return rounded