|
1 | 1 | # coding=utf-8 |
| 2 | +# pyright: reportAttributeAccessIssue=false, reportOptionalMemberAccess=false, reportOptionalSubscript=false, reportOptionalOperand=false, reportArgumentType=false, reportOperatorIssue=false, reportGeneralTypeIssues=false |
2 | 3 | # |
3 | 4 | # Copyright 2016 timercrack |
4 | 5 | # |
|
18 | 19 | from collections import defaultdict |
19 | 20 | import datetime |
20 | 21 | from decimal import Decimal |
| 22 | +from typing import Tuple, Any |
21 | 23 | import logging |
22 | 24 | from django.db.models import Q, F, Sum |
23 | 25 | from django.utils import timezone |
@@ -68,7 +70,7 @@ async def start(self): |
68 | 70 | now = int(today.strftime('%H%M')) |
69 | 71 | if today.isoweekday() < 6 and (820 <= now <= 1550 or 2010 <= now <= 2359): # 非交易时间查不到数据 |
70 | 72 | await self.refresh_account() |
71 | | - order_list = await self.query('Order') |
| 73 | + order_list = await self.query('Order') or [] |
72 | 74 | for order in order_list: |
73 | 75 | # 未成交订单 |
74 | 76 | if int(order['OrderStatus']) in range(1, 5) and order['OrderSubmitStatus'] == ApiStruct.OSS_Accepted: |
@@ -114,7 +116,7 @@ async def refresh_account(self): |
114 | 116 | async def refresh_position(self): |
115 | 117 | try: |
116 | 118 | logger.debug('更新持仓...') |
117 | | - pos_list = await self.query('InvestorPositionDetail') |
| 119 | + pos_list = await self.query('InvestorPositionDetail') or [] |
118 | 120 | self.__cur_pos.clear() |
119 | 121 | for pos in pos_list: |
120 | 122 | if 'empty' in pos and pos['empty'] is True or len(pos['InstrumentID']) > 6: |
@@ -157,7 +159,7 @@ async def refresh_instrument(self): |
157 | 159 | try: |
158 | 160 | logger.debug("更新合约...") |
159 | 161 | inst_dict = defaultdict(dict) |
160 | | - inst_list = await self.query('Instrument') |
| 162 | + inst_list = await self.query('Instrument') or [] |
161 | 163 | for inst in inst_list: |
162 | 164 | if inst['empty']: |
163 | 165 | continue |
@@ -360,7 +362,9 @@ async def cancel_order(self, order: dict): |
360 | 362 | await sub_client.close() |
361 | 363 | result = task.result()[0] |
362 | 364 | if 'ErrorID' in result: |
363 | | - logger.warning(f"撤销订单出错: {ctp_errors[result['ErrorID']]}") |
| 365 | + err_id = result['ErrorID'] |
| 366 | + err_msg = ctp_errors.get(err_id, f"未知错误码:{err_id}") |
| 367 | + logger.warning(f"撤销订单出错: {err_msg}") |
364 | 368 | return False |
365 | 369 | return True |
366 | 370 | except Exception as e: |
@@ -514,7 +518,7 @@ async def OnRtnOrder(self, _: str, order: dict): |
514 | 518 | return |
515 | 519 | if order["OrderSysID"]: |
516 | 520 | logger.debug(f"订单回报: {self.get_order_string(order)}") |
517 | | - order_obj, _ = self.save_order(order) |
| 521 | + order_obj, created = self.save_order(order) |
518 | 522 | if not order_obj: |
519 | 523 | return |
520 | 524 | signal = order_obj.signal |
@@ -722,7 +726,7 @@ def calculate(self, day, create_main_bar=True): |
722 | 726 | except Exception as e: |
723 | 727 | logger.warning(f'calculate 发生错误: {repr(e)}', exc_info=True) |
724 | 728 |
|
725 | | - def calc_signal(self, inst: Instrument, day: datetime.datetime) -> (Signal, Decimal): |
| 729 | + def calc_signal(self, inst: Instrument, day: datetime.datetime) -> tuple[Any | None, Decimal]: |
726 | 730 | try: |
727 | 731 | break_n = self.__strategy.param_set.get(code='BreakPeriod').int_value |
728 | 732 | atr_n = self.__strategy.param_set.get(code='AtrPeriod').int_value |
@@ -850,10 +854,11 @@ def calc_signal(self, inst: Instrument, day: datetime.datetime) -> (Signal, Deci |
850 | 854 | volume_ori = volume_ori if volume_ori else volume |
851 | 855 | logger.info(f"新信号: {sig}({volume_ori:.1f}手) " |
852 | 856 | f"预估保证金: {use_margin:.0f}({use_margin / 10000:.1f}万)") |
853 | | - return signal, use_margin |
| 857 | + return signal, Decimal(use_margin) |
854 | 858 | except Exception as e: |
855 | 859 | logger.warning(f'calc_signal 发生错误: {repr(e)}', exc_info=True) |
856 | | - return None, 0 |
| 860 | + return None, Decimal(0) |
| 861 | + return None, Decimal(0) |
857 | 862 |
|
858 | 863 | def calc_up_limit(self, inst: Instrument, bar: DailyBar): |
859 | 864 | settlement = bar.settlement |
|
0 commit comments