Upload capital_flow_analyzer.py
Browse files- capital_flow_analyzer.py +142 -21
capital_flow_analyzer.py
CHANGED
|
@@ -30,28 +30,149 @@ class CapitalFlowAnalyzer:
|
|
| 30 |
return cached_data
|
| 31 |
|
| 32 |
# 从akshare获取数据
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
try:
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
# 缓存结果
|
| 56 |
self.data_cache[cache_key] = (datetime.now(), result)
|
| 57 |
|
|
|
|
| 30 |
return cached_data
|
| 31 |
|
| 32 |
# 从akshare获取数据
|
| 33 |
+
try:
|
| 34 |
+
concept_data = ak.stock_fund_flow_concept(symbol=period)
|
| 35 |
+
|
| 36 |
+
# 记录实际的列名,以便调试
|
| 37 |
+
self.logger.info(f"Actual columns: {list(concept_data.columns)}")
|
| 38 |
+
self.logger.info(f"Column count: {len(concept_data.columns)}")
|
| 39 |
+
|
| 40 |
+
# 处理数据
|
| 41 |
+
result = []
|
| 42 |
+
for _, row in concept_data.iterrows():
|
| 43 |
+
try:
|
| 44 |
+
# 使用更灵活的方式获取列数据,适应不同的列名结构
|
| 45 |
+
item = {}
|
| 46 |
+
|
| 47 |
+
# 尝试多种可能的列名
|
| 48 |
+
if "序号" in concept_data.columns:
|
| 49 |
+
item["rank"] = int(row.get("序号", 0))
|
| 50 |
+
elif "排名" in concept_data.columns:
|
| 51 |
+
item["rank"] = int(row.get("排名", 0))
|
| 52 |
+
else:
|
| 53 |
+
item["rank"] = 0
|
| 54 |
+
|
| 55 |
+
# 行业/概念名称
|
| 56 |
+
if "行业" in concept_data.columns:
|
| 57 |
+
item["sector"] = row.get("行业", "")
|
| 58 |
+
elif "概念名称" in concept_data.columns:
|
| 59 |
+
item["sector"] = row.get("概念名称", "")
|
| 60 |
+
elif "名称" in concept_data.columns:
|
| 61 |
+
item["sector"] = row.get("名称", "")
|
| 62 |
+
else:
|
| 63 |
+
# 尝试找到包含"名称"的列
|
| 64 |
+
name_cols = [col for col in concept_data.columns if "名称" in col or "行业" in col]
|
| 65 |
+
if name_cols:
|
| 66 |
+
item["sector"] = row.get(name_cols[0], "")
|
| 67 |
+
else:
|
| 68 |
+
item["sector"] = ""
|
| 69 |
+
|
| 70 |
+
# 公司家数
|
| 71 |
+
if "公司家数" in concept_data.columns:
|
| 72 |
+
item["company_count"] = int(row.get("公司家数", 0))
|
| 73 |
+
else:
|
| 74 |
+
item["company_count"] = 0
|
| 75 |
+
|
| 76 |
+
# 行业指数
|
| 77 |
+
if "行业指数" in concept_data.columns:
|
| 78 |
+
item["sector_index"] = float(row.get("行业指数", 0))
|
| 79 |
+
elif "概念指数" in concept_data.columns:
|
| 80 |
+
item["sector_index"] = float(row.get("概念指数", 0))
|
| 81 |
+
else:
|
| 82 |
+
item["sector_index"] = 0
|
| 83 |
+
|
| 84 |
+
# 涨跌幅
|
| 85 |
+
if "阶段涨跌幅" in concept_data.columns:
|
| 86 |
+
item["change_percent"] = self._parse_percent(row.get("阶段涨跌幅", "0%"))
|
| 87 |
+
elif "涨跌幅" in concept_data.columns:
|
| 88 |
+
item["change_percent"] = self._parse_percent(row.get("涨跌幅", "0%"))
|
| 89 |
+
else:
|
| 90 |
+
# 尝试找到包含"涨跌"的列
|
| 91 |
+
change_cols = [col for col in concept_data.columns if "涨跌" in col]
|
| 92 |
+
if change_cols:
|
| 93 |
+
item["change_percent"] = self._parse_percent(row.get(change_cols[0], "0%"))
|
| 94 |
+
else:
|
| 95 |
+
item["change_percent"] = 0
|
| 96 |
+
|
| 97 |
+
# 资金流向数据
|
| 98 |
+
# 尝试多种可能的列名组合
|
| 99 |
+
if "流入资金" in concept_data.columns and "流出资金" in concept_data.columns:
|
| 100 |
+
item["inflow"] = float(row.get("流入资金", 0))
|
| 101 |
+
item["outflow"] = float(row.get("流出资金", 0))
|
| 102 |
+
item["net_flow"] = float(row.get("净额", 0))
|
| 103 |
+
elif "主力净流入-净额" in concept_data.columns:
|
| 104 |
+
item["inflow"] = 0 # 这种情况下可能没有单独的流入流出数据
|
| 105 |
+
item["outflow"] = 0
|
| 106 |
+
item["net_flow"] = float(row.get("主力净流入-净额", 0))
|
| 107 |
+
else:
|
| 108 |
+
# 尝试找到包含特定关键词的列
|
| 109 |
+
inflow_cols = [col for col in concept_data.columns if "流入" in col and "净" not in col]
|
| 110 |
+
outflow_cols = [col for col in concept_data.columns if "流出" in col and "净" not in col]
|
| 111 |
+
net_cols = [col for col in concept_data.columns if "净" in col or "净额" in col]
|
| 112 |
+
|
| 113 |
+
if inflow_cols:
|
| 114 |
+
item["inflow"] = float(row.get(inflow_cols[0], 0))
|
| 115 |
+
else:
|
| 116 |
+
item["inflow"] = 0
|
| 117 |
+
|
| 118 |
+
if outflow_cols:
|
| 119 |
+
item["outflow"] = float(row.get(outflow_cols[0], 0))
|
| 120 |
+
else:
|
| 121 |
+
item["outflow"] = 0
|
| 122 |
+
|
| 123 |
+
if net_cols:
|
| 124 |
+
item["net_flow"] = float(row.get(net_cols[0], 0))
|
| 125 |
+
else:
|
| 126 |
+
# 如果没有净额列,尝试计算
|
| 127 |
+
item["net_flow"] = item["inflow"] - item["outflow"]
|
| 128 |
+
|
| 129 |
+
result.append(item)
|
| 130 |
+
except Exception as e:
|
| 131 |
+
self.logger.warning(f"Error processing row in concept fund flow: {str(e)}")
|
| 132 |
+
continue
|
| 133 |
+
|
| 134 |
+
except ValueError as e:
|
| 135 |
+
self.logger.error(f"ValueError in stock_fund_flow_concept: {str(e)}")
|
| 136 |
+
# 如果出现ValueError(可能是列名不匹配),尝试不重命名列直接处理数据
|
| 137 |
try:
|
| 138 |
+
concept_data = ak.stock_fund_flow_concept(symbol=period)
|
| 139 |
+
self.logger.info(f"Retrying with original columns: {list(concept_data.columns)}")
|
| 140 |
+
|
| 141 |
+
# 简化处理逻辑,直接使用原始列名
|
| 142 |
+
result = []
|
| 143 |
+
for _, row in concept_data.iterrows():
|
| 144 |
+
try:
|
| 145 |
+
item = {}
|
| 146 |
+
# 尝试将每一列的数据都添加到结果中
|
| 147 |
+
for col in concept_data.columns:
|
| 148 |
+
try:
|
| 149 |
+
# 尝试转换为数值类型
|
| 150 |
+
if isinstance(row[col], str) and '%' in row[col]:
|
| 151 |
+
item[col] = self._parse_percent(row[col])
|
| 152 |
+
else:
|
| 153 |
+
try:
|
| 154 |
+
item[col] = float(row[col])
|
| 155 |
+
except (ValueError, TypeError):
|
| 156 |
+
item[col] = row[col]
|
| 157 |
+
except Exception:
|
| 158 |
+
item[col] = row[col]
|
| 159 |
+
|
| 160 |
+
# 添加一些标准化的键,以便与现有代码兼容
|
| 161 |
+
if "名称" in item or "概念名称" in item or "行业" in item:
|
| 162 |
+
item["sector"] = item.get("名称", item.get("概念名称", item.get("行业", "")))
|
| 163 |
+
|
| 164 |
+
if "涨跌幅" in item:
|
| 165 |
+
item["change_percent"] = item["涨跌幅"]
|
| 166 |
+
|
| 167 |
+
result.append(item)
|
| 168 |
+
except Exception as e:
|
| 169 |
+
self.logger.warning(f"Error processing row with original columns: {str(e)}")
|
| 170 |
+
continue
|
| 171 |
+
except Exception as e2:
|
| 172 |
+
self.logger.error(f"Failed to process with original columns: {str(e2)}")
|
| 173 |
+
# 如果再次失败,返回模拟数据
|
| 174 |
+
result = self._generate_mock_concept_fund_flow(period)
|
| 175 |
+
|
| 176 |
# 缓存结果
|
| 177 |
self.data_cache[cache_key] = (datetime.now(), result)
|
| 178 |
|