From 2377cc914d36f526fbc89d33af9854af78da22fa Mon Sep 17 00:00:00 2001 From: cqzhao Date: Sat, 21 May 2022 18:32:04 +0800 Subject: [PATCH 1/2] fix some bug --- debug.py | 46 +++++++++++++++++++++++++++++++++++++++++++++ setup.py | 2 +- xalpha/__init__.py | 2 +- xalpha/indicator.py | 17 ++++++++--------- xalpha/info.py | 18 ++++++++++++------ xalpha/record.py | 4 +++- 6 files changed, 71 insertions(+), 18 deletions(-) create mode 100644 debug.py diff --git a/debug.py b/debug.py new file mode 100644 index 0000000..5232a39 --- /dev/null +++ b/debug.py @@ -0,0 +1,46 @@ +import logging +import pandas as pd +from pyecharts import options as opts +from pyecharts.charts import Pie, ThemeRiver + +from xalpha.cons import convert_date, myround, yesterdaydash, yesterdayobj +from xalpha.evaluate import evaluate +from xalpha.exceptions import FundTypeError, TradeBehaviorError +from xalpha.record import record, irecord +from xalpha.indicator import indicator +from xalpha.info import cashinfo, fundinfo, mfundinfo, get_fund_holdings +from xalpha.trade import ( + bottleneck, + trade, + turnoverrate, + vtradevolume, + xirrcal, + itrade, + vtradecost, +) +from xalpha.multiple import mul,mulfix +from xalpha.universal import get_fund_type, ttjjcode, get_rt, get_industry_fromxq +import xalpha.universal as xu + +#status = pd.DataFrame([ +# ['2019-12-31',-0.04], +# ['2020-06-09',2738.22], +# ['2020-08-05',3668.81], +# ['2020-08-10',793.97], +# ['2020-12-31',83.93], +# ['2021-12-16',1271.55], +# ['2021-12-31',111.29] +#],columns=['date','mf']) +#status["date"] = pd.to_datetime(status['date']) +#cashobj = cashinfo(start='2019-04-24') +#trade(cashobj,status) + +status = pd.DataFrame([ + ['2012-02-17',100000.0], + ['2012-03-15',-0.005], + ['2012-03-27',103902.230], + ['2012-03-29',-0.005], +],columns=['date','159915']) +status["date"] = pd.to_datetime(status['date']) +infoobj = fundinfo('F159915') +trade(infoobj,status) \ No newline at end of file diff --git a/setup.py b/setup.py index b2c3078..5ec7d85 100644 --- a/setup.py +++ b/setup.py @@ -16,7 +16,7 @@ setuptools.setup( name="xalpha", - version="0.11.5", + version="0.11.5a", author="refraction-ray", author_email="refraction-ray@protonmail.com", description="all about fund investment", diff --git a/xalpha/__init__.py b/xalpha/__init__.py index c4a38fe..6942cc9 100644 --- a/xalpha/__init__.py +++ b/xalpha/__init__.py @@ -1,4 +1,4 @@ -__version__ = "0.11.5" +__version__ = "0.11.5a" __author__ = "refraction-ray" __name__ = "xalpha" diff --git a/xalpha/indicator.py b/xalpha/indicator.py index 0ecaf9b..13220b1 100644 --- a/xalpha/indicator.py +++ b/xalpha/indicator.py @@ -218,15 +218,14 @@ def max_drawdown(self, date=yesterdayobj()): :returns: three elements tuple, the first two are the date obj of start and end of the time window, the third one is the drawdown amplitude in unit 1. """ - li = [ - (row["date"], row["netvalue"]) - for i, row in self.price[self.price["date"] <= date].iterrows() - ] - res = [] - for i, _ in enumerate(li): - for j in range(i + 1, len(li)): - res.append((li[i][0], li[j][0], (li[j][1] - li[i][1]) / li[i][1])) - return min(res, key=lambda x: x[2]) + tmpprice = self.price.loc[:,:] + tmpprice.loc[:,'drawdown'] = (tmpprice['netvalue']-tmpprice['netvalue'].expanding(1).max())/tmpprice['netvalue'].expanding(1).max() + tmpprice = tmpprice.loc[tmpprice['date']<=date,:] + iid = tmpprice.loc[:,'drawdown'].argmin() + lastrow = tmpprice.iloc[iid] + newhigh = tmpprice.loc[tmpprice.loc[:,'drawdown']>=-0.0001,:] + beginrow = newhigh.loc[newhigh['date']<=lastrow['date'],:].iloc[-1] + return beginrow['date'],lastrow['date'],lastrow['drawdown'] ## 以上基本为聚宽提供的整体量化指标,以下是其他短线技术面指标 diff --git a/xalpha/info.py b/xalpha/info.py index 07145d0..1c589e7 100644 --- a/xalpha/info.py +++ b/xalpha/info.py @@ -933,11 +933,16 @@ def update(self): if self.code.startswith("96"): return self._hk_update() lastdate = self.price.iloc[-1].date - diffdays = (yesterdayobj() - lastdate).days - if ( - diffdays == 0 - ): ## for some QDII, this value is 1, anyways, trying update is compatible (d+2 update) - return None + if dt.datetime.today().time()>=dt.time(20,00): + # if over 20:00, update today's netvalue + # diffdays = (today_obj() - lastdate).days + diffdays = 0 + else: + diffdays = (yesterdayobj() - lastdate).days + if ( + diffdays == 0 + ): ## for some QDII, this value is 1, anyways, trying update is compatible (d+2 update) + return None self._updateurl = ( "http://fund.eastmoney.com/f10/F10DataApi.aspx?type=lsjz&code=" + self.code @@ -1002,7 +1007,8 @@ def update(self): df = df.iloc[::-1] ## reverse the time order df = df[df["date"].isin(opendate)] df = df.reset_index(drop=True) - df = df[df["date"] <= yesterdayobj()] + #df = df[df["date"] <= yesterdayobj()] + df = df[df["date"] <= today_obj()] if len(df) != 0: self.price = self.price.append(df, ignore_index=True, sort=True) return df diff --git a/xalpha/record.py b/xalpha/record.py index 6b01f67..c69dc41 100644 --- a/xalpha/record.py +++ b/xalpha/record.py @@ -146,7 +146,9 @@ def __init__(self, path="input.csv", **readkwds): ] if "fee" not in df.columns: df = df.assign(fee=[0] * len(df)) - df = df.sort_values(by="date", ascending=True) + # 优先使用date进行排序,date相同的时候,使用index排序,即使用记录的顺序 + df = df.reset_index() + df = df.sort_values(by=["date","index"], ascending=True) self.status = df def filter(self, code, start=None, end=None): From c5ccaea1da92e94410361080f5e3fcac84656fc4 Mon Sep 17 00:00:00 2001 From: cqzhao Date: Tue, 9 Jul 2024 11:22:24 +0800 Subject: [PATCH 2/2] update api of xueqiu --- xalpha/universal.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xalpha/universal.py b/xalpha/universal.py index 02ac624..a07b397 100644 --- a/xalpha/universal.py +++ b/xalpha/universal.py @@ -142,7 +142,7 @@ def get_token(): :return: """ r = rget("https://xueqiu.com", headers={"user-agent": "Mozilla"}) - return r.cookies["xq_a_token"] + return r.cookies["xq_a_token"],r.cookies["u"] def get_historical_fromxq(code, count, type_="before", full=False): @@ -162,7 +162,7 @@ def get_historical_fromxq(code, count, type_="before", full=False): url.format( code=code, tomorrow=int(tomorrow_ts() * 1000), count=count, type_=type_ ), - cookies={"xq_a_token": get_token()}, + cookies={"xq_a_token": get_token()[0],"u":get_token()[1]}, headers={"user-agent": "Mozilla/5.0"}, ) df = pd.DataFrame(data=r["data"]["item"], columns=r["data"]["column"])