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/xalpha/indicator.py b/xalpha/indicator.py index 79ac9f4..acc0243 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 26de273..dbaac74 100644 --- a/xalpha/info.py +++ b/xalpha/info.py @@ -935,11 +935,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 @@ -1004,7 +1009,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): 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"])