| 41 | |
| 42 | |
| 43 | class CSV_API(CCommonStockApi): |
| 44 | def __init__(self, code, k_type=KL_TYPE.K_DAY, begin_date=None, end_date=None, autype=None): |
| 45 | self.headers_exist = True # 第一行是否是标题,如果是数据,设置为False |
| 46 | self.columns = [ |
| 47 | DATA_FIELD.FIELD_TIME, |
| 48 | DATA_FIELD.FIELD_OPEN, |
| 49 | DATA_FIELD.FIELD_HIGH, |
| 50 | DATA_FIELD.FIELD_LOW, |
| 51 | DATA_FIELD.FIELD_CLOSE, |
| 52 | # DATA_FIELD.FIELD_VOLUME, |
| 53 | # DATA_FIELD.FIELD_TURNOVER, |
| 54 | # DATA_FIELD.FIELD_TURNRATE, |
| 55 | ] # 每一列字段 |
| 56 | self.time_column_idx = self.columns.index(DATA_FIELD.FIELD_TIME) |
| 57 | super(CSV_API, self).__init__(code, k_type, begin_date, end_date, autype) |
| 58 | |
| 59 | def get_kl_data(self): |
| 60 | cur_path = os.path.dirname(os.path.realpath(__file__)) |
| 61 | k_type = self.k_type.name[2:].lower() |
| 62 | file_path = f"{cur_path}/../{self.code}_{k_type}.csv" |
| 63 | if not os.path.exists(file_path): |
| 64 | raise CChanException(f"file not exist: {file_path}", ErrCode.SRC_DATA_NOT_FOUND) |
| 65 | |
| 66 | for line_number, line in enumerate(open(file_path, 'r')): |
| 67 | if self.headers_exist and line_number == 0: |
| 68 | continue |
| 69 | data = line.strip("\n").split(",") |
| 70 | if len(data) != len(self.columns): |
| 71 | raise CChanException(f"file format error: {file_path}", ErrCode.SRC_DATA_FORMAT_ERROR) |
| 72 | if self.begin_date is not None and data[self.time_column_idx] < self.begin_date: |
| 73 | continue |
| 74 | if self.end_date is not None and data[self.time_column_idx] > self.end_date: |
| 75 | continue |
| 76 | yield CKLine_Unit(create_item_dict(data, self.columns)) |
| 77 | |
| 78 | def SetBasciInfo(self): |
| 79 | pass |
| 80 | |
| 81 | @classmethod |
| 82 | def do_init(cls): |
| 83 | pass |
| 84 | |
| 85 | @classmethod |
| 86 | def do_close(cls): |
| 87 | pass |
nothing calls this directly
no outgoing calls
no test coverage detected