Skip to content

Commit

Permalink
0.9.35 新增 get_all_weights 方法用于一次性获取所有
Browse files Browse the repository at this point in the history
  • Loading branch information
zengbin93 committed Nov 11, 2023
1 parent e8be8c4 commit 9bc3fc8
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions czsc/traders/rwc.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
class RedisWeightsClient:
"""策略持仓权重收发客户端"""

version = "V231014"
version = "V231111"

def __init__(self, strategy_name, redis_url, **kwargs):
"""
Expand Down Expand Up @@ -289,7 +289,7 @@ def get_hist_weights(self, symbol, sdt, edt) -> pd.DataFrame:

weights = []
for i in range(len(key_list)):
dt = pd.to_datetime(key_list[i].split(":")[-1]) # type: ignore
dt = pd.to_datetime(key_list[i].split(":")[-1])
weight, price, ref = rows[i]
weight = weight if weight is None else float(weight)
price = price if price is None else float(price)
Expand All @@ -300,3 +300,19 @@ def get_hist_weights(self, symbol, sdt, edt) -> pd.DataFrame:
weights.append((self.strategy_name, symbol, dt, weight, price, ref))
dfw = pd.DataFrame(weights, columns=['strategy_name', 'symbol', 'dt', 'weight', 'price', 'ref'])
return dfw

def get_all_weights(self):
"""获取所有权重数据"""
keys = self.get_keys(f"{self.key_prefix}:{self.strategy_name}*")
if keys is None or len(keys) == 0: # type: ignore
return pd.DataFrame()

keys = [x for x in keys if len(x.split(":")[-1]) == 14] # type: ignore
with self.r.pipeline() as pipe:
for key in keys:
pipe.hgetall(key)
rows = pipe.execute()
df = pd.DataFrame(rows)
df['dt'] = pd.to_datetime(df['dt'])
df['weight'] = df['weight'].astype(float)
return df

0 comments on commit 9bc3fc8

Please sign in to comment.