def wx_report(account_id, wxuid, username, pay, income, profit, percent, buy_info, sell_info, total_profit, month_profit): if not wxuid: return summary = f'{strftime(time.time())} {username} 今日支出 {pay}, 收入 {income}, 利润 {profit}, 收益率 {percent}%' msg = f''' ### 用户 {username} ### 买入记录 | 币种 | 时间 |价格 | 成交量 | 成交额 | 手续费 | | ---- | ---- | ---- | ---- | ---- | ---- | ''' + \ '\n'.join([ f'| {each["currency"]} | {each["time"]} | {each["price"]} | {each["amount"]} | {each["vol"]} | {each["fee"]} |' for each in buy_info ]) + ''' ### 卖出记录 | 币种 | 时间 | 价格 | 成交量 | 成交额 | 手续费 | | ---- | ---- | ---- | ---- | ---- | ---- | ''' + \ '\n'.join([ f'| {each["currency"]} | {each["time"]} | {each["price"]} | {each["amount"]} | {each["vol"]} | {each["fee"]} |' for each in sell_info ]) + f''' ### 总结 - 支出: **{pay} USDT** - 收入: **{income} USDT** - 利润: **{profit} USDT** - 收益率: **{percent} %** - 月累计收益: **{month_profit} USDT** - 总累计收益: **{total_profit} USDT** ''' wx_push(content=msg, uids=wxuid, content_type=3, summary=summary) with get_session() as session: profit_id = Profit.get_id(session, account_id, pay, income) buy_records = Record.from_record_info(buy_info, profit_id, 'buy') sell_records = Record.from_record_info(sell_info, profit_id, 'sell') session.add_all(buy_records + sell_records) session.commit()
def vacuum(session: Session=None, table: str='', full=True): def _vacuum(): engine = session.bind connection = engine.raw_connection() connection.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT) cursor = connection.cursor() cursor.execute(f"VACUUM {'FULL' if full else ''} {table}") cursor.close() connection.close() if session: _vacuum() else: with get_session() as session: _vacuum()
class WxPusher(_WxPusher): token = TOKEN @classmethod def send_message(cls, content, **kwargs): """Send Message.""" payload = { 'appToken': cls.token, 'content': content, 'summary': kwargs.get('summary', content[:19]), 'contentType': kwargs.get('content_type', 0), 'topicIds': kwargs.get('topic_ids', []), 'uids': kwargs.get('uids', []), 'url': kwargs.get('url'), } url = f'{BASEURL}/send/message' return requests.post(url, json=payload).json() @classmethod def query_user(cls, page=1, page_size=20, uid=None): """Query users.""" payload = { 'appToken': cls.token, 'page': page, 'pageSize': page_size, 'uid': uid if uid else '' } url = f'{BASEURL}/fun/wxuser/v2' return requests.get(url, params=payload).json() @classmethod def get_user_name(cls, uid): result = cls.query_user(uid=uid) name = result['data']['records'][0]['nickName'] return name summary = summary or content[:20] _wx_push(content, uids, content_type, summary) with get_session() as session: message = Message(summary=summary, msg=content, uids=';'.join(uids), msg_type=content_type) session.add(message) session.commit()
def check_trade_tables(): with get_session() as session: inspector = inspect(session.bind) tables = [name for name in inspector.get_table_names() if name.startswith('trade')] print(tables) for table in tables: day = int(table.split('_')[1]) Trade = get_Trade(day) print(day, Trade.__tablename__) min_ts = float(session.query(func.min(Trade.ts)).scalar()) print(min_ts) if min_ts < day * MS_IN_DAY: print('move small') min_day = int(min_ts // MS_IN_DAY) move_trade(Trade, session, min_day, day) max_ts = float(session.query(func.max(Trade.ts)).scalar()) print(max_ts) if max_ts > (day + 1) * MS_IN_DAY: print('move big') max_day = int(max_ts // MS_IN_DAY) move_trade(Trade, session, day+1, max_day+1) vacuum(session)
def get_profit(account_id): with get_session() as session: month = datetime.date.fromtimestamp(time.time()).strftime('%Y-%m') total_profit = Profit.get_sum_profit(session, account_id) month_profit = Profit.get_sum_profit(session, account_id, month) return total_profit, month_profit
def trans(): redis_conn = Redis() with get_session() as session: write_target(redis_conn, session) write_trade(redis_conn, session)