fix bug for set price_anomaly

This commit is contained in:
blade 2025-08-21 15:36:35 +08:00
parent d397986bad
commit ccc5637f9e
2 changed files with 6 additions and 4 deletions

View File

@ -131,7 +131,7 @@ class MarketMonitorMain:
) )
return return
realtime_row = real_time_data.iloc[-1] realtime_row = real_time_data.iloc[-1]
real_time_data = self.calculate_price_anomaly(real_time_data, realtime_row) realtime_row = self.calculate_price_anomaly(real_time_data, realtime_row)
if only_output_huge_volume: if only_output_huge_volume:
if realtime_row["huge_volume"] == 1: if realtime_row["huge_volume"] == 1:
logger.info(f"监控到巨量: {symbol} {bar} 窗口大小: {self.window_size}") logger.info(f"监控到巨量: {symbol} {bar} 窗口大小: {self.window_size}")
@ -216,16 +216,18 @@ class MarketMonitorMain:
def calculate_price_anomaly(self, data: pd.DataFrame, realtime_row: pd.Series): def calculate_price_anomaly(self, data: pd.DataFrame, realtime_row: pd.Series):
k = 2 k = 2
# 计算均值和标准差 # 计算均值和标准差
data = data.copy()[0:-1] data = data.copy().iloc[:-1]
# 避免 SettingWithCopyWarning先复制一份当前行的独立副本
realtime_row = realtime_row.copy()
pct_chg = realtime_row["pct_chg"] pct_chg = realtime_row["pct_chg"]
pct_chg_mean = data['pct_chg'].mean() pct_chg_mean = data['pct_chg'].mean()
pct_chg_std = data['pct_chg'].std() pct_chg_std = data['pct_chg'].std()
pct_chg_upper_bound = pct_chg_mean + k * pct_chg_std pct_chg_upper_bound = pct_chg_mean + k * pct_chg_std
pct_chg_lower_bound = pct_chg_mean - k * pct_chg_std pct_chg_lower_bound = pct_chg_mean - k * pct_chg_std
if pct_chg > pct_chg_upper_bound or pct_chg < pct_chg_lower_bound: if pct_chg > pct_chg_upper_bound or pct_chg < pct_chg_lower_bound:
realtime_row["price_anomaly"] = True realtime_row.loc["price_anomaly"] = True
else: else:
realtime_row["price_anomaly"] = False realtime_row.loc["price_anomaly"] = False
return realtime_row return realtime_row