Skip to content

Commit

Permalink
fix precached
Browse files Browse the repository at this point in the history
  • Loading branch information
refraction-ray committed Apr 2, 2020
1 parent 728f3e5 commit a31a341
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 21 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog
## Unreleased
### fixed
* 防止 precached 之前的日线数据无法抓取
* 为 imul 增加 istatus 关键字参数作为冗余防止误输入

## v0.8.2 - 2020.04.02
### added
Expand Down
7 changes: 7 additions & 0 deletions doc/source/advance.rst
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,13 @@ xalpha 的数据来自天天基金,英为财情,雪球,彭博,标普,
场内账单的例子请参考 tests/demo3.csv. 其列头分别是 date,code,value,share,fee。date 格式为20200202。code 对应场内代码,开头需包含 SH 或 SZ。value 是成交的净值单价。
share 代表成交的份数。fee 代表手续费,也可以不计,则默认为0,建议记录以得到交易盈利的更好全景。

场内账单的读入使用 ``ist=xa.irecord(path)``. 其既可以传入专门的场内投资组合类 ``xa.imul(status=ist)``,
也可以和场内记账单一起传入投资组合类 ``xa.mul(status=st, istatus=ist)`` 进行场内外投资结果的汇总。

.. Note::

场内账单处理逻辑为了保持和场外一致,也只处理到截止昨天的交易,因此可能出现和现时实盘不符的情形。



QDII 净值预测
Expand Down
8 changes: 8 additions & 0 deletions xalpha/cons.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@
# date obj of today
today = lambda: dt.datetime.combine(dt.date.today(), dt.time.min)

tz_bj = dt.timezone(dt.timedelta(hours=8))


def today_obj():
now = dt.datetime.now(tz=tz_bj)
return now.replace(hour=0, minute=0, second=0, microsecond=0).replace(tzinfo=None)


# string for yesterday, only used for indexinfo url
yesterday = lambda: dt.datetime.strftime(
(dt.datetime.now() - dt.timedelta(1)), "%Y%m%d"
Expand Down
9 changes: 6 additions & 3 deletions xalpha/multiple.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,17 +376,20 @@ def unitvalue(self, date=yesterdayobj()):


class imul(mul):
def __init__(self, *fundtradeobj, status=None):
def __init__(self, *fundtradeobj, status=None, istatus=None):
"""
对场内投资组合进行分析的类
:param fundtradeobj: itrade objects.
:param status: 场内格式记账单,或 irecord 对象。
"""
if isinstance(status, irecord):
status = status.status

if not fundtradeobj:
fundtradeobj = []
if not status:
status = istatus
if isinstance(status, irecord):
status = status.status
for code in status.code.unique():
fundtradeobj.append(itrade(code, status))
self.fundtradeobj = tuple(fundtradeobj)
Expand Down
6 changes: 2 additions & 4 deletions xalpha/toolbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from functools import wraps, lru_cache
import logging

from xalpha.cons import opendate, yesterday, next_onday, last_onday, scale_dict
from xalpha.cons import opendate, yesterday, next_onday, last_onday, scale_dict, tz_bj
from xalpha.universal import (
get_rt,
get_bar,
Expand Down Expand Up @@ -644,9 +644,7 @@ def __init__(self, code, t1dict=None, t0dict=None, positions=False):
# t0 实时净值自然不 cache
self.positions = positions
self.position_zero = sum([v for _, v in self.t1dict.items()])
self.now = dt.datetime.now(tz=dt.timezone(dt.timedelta(hours=8))).replace(
tzinfo=None
)
self.now = dt.datetime.now(tz=tz_bj).replace(tzinfo=None)
self.today = self.now.replace(hour=0, minute=0, second=0, microsecond=0)
self.t1_type = "未计算"
self.bar_cache = {}
Expand Down
27 changes: 13 additions & 14 deletions xalpha/universal.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
pass

from xalpha.info import fundinfo, mfundinfo
from xalpha.cons import rget, rpost, rget_json, rpost_json, yesterday, opendate
from xalpha.cons import rget, rpost, rget_json, rpost_json, tz_bj, today_obj
from xalpha.provider import data_source
from xalpha.exceptions import DataPossiblyWrong, ParserFailure

Expand All @@ -46,11 +46,6 @@
logger = logging.getLogger(__name__)


def today_obj():
now = dt.datetime.today()
return now.replace(hour=0, minute=0, second=0, microsecond=0)


def tomorrow_ts():
dto = dt.datetime.now() + dt.timedelta(1)
return dto.timestamp()
Expand Down Expand Up @@ -88,7 +83,6 @@ def get_history(


def ts2pdts(ts):
tz_bj = dt.timezone(dt.timedelta(hours=8))
dto = dt.datetime.fromtimestamp(ts / 1000, tz=tz_bj).replace(tzinfo=None)
return dto.replace(
hour=0, minute=0, second=0, microsecond=0
Expand Down Expand Up @@ -1089,6 +1083,9 @@ def wrapper(*args, **kws):
prefix = ioconf.get("prefix", "")
key = prefix + key
# print("xdebug: %s" % ioconf.get("backend", "no"))
if precached:
precached = precached.replace("/", "").replace("-", "")
precached_obj = dt.datetime.strptime(precached, "%Y%m%d")
if not prev:
prev = 365
if not end:
Expand Down Expand Up @@ -1184,10 +1181,12 @@ def wrapper(*args, **kws):

except (FileNotFoundError, exc.ProgrammingError, KeyError):
if precached:
kws["start"] = precached.replace("/", "").replace("-", "")
kws["end"] = (today_obj() - dt.timedelta(days=1)).strftime(
"%Y%m%d"
)
if start_obj > precached_obj:
kws["start"] = precached
if end_obj < today_obj():
kws["end"] = (
today_obj() - dt.timedelta(days=1)
).strftime("%Y%m%d")
is_changed = True
df0 = f(*args, **kws)

Expand Down Expand Up @@ -1418,7 +1417,7 @@ def get_bar(code, prev=120, interval=3600):
interval = 60
if len(code.split("/")) == 2:
code = get_investing_id(code)
r = rget_json(
r = rget(
"https://cn.investing.com/common/modules/js_instrument_chart/api/data.php?pair_id={code}&pair_id_for_news={code}\
&chart_type=area&pair_interval={interval}&candle_count={prev}&events=yes&volume_series=yes&period=".format(
code=code, prev=str(prev), interval=str(interval)
Expand All @@ -1434,9 +1433,9 @@ def get_bar(code, prev=120, interval=3600):
"X-Requested-With": "XMLHttpRequest",
},
)
if not r:
if not r.text:
return # None
tz_bj = dt.timezone(dt.timedelta(hours=8))
r = r.json()
df = pd.DataFrame(r["candles"], columns=["date", "close", "0", "1"])
df = df.drop(["0", "1"], axis=1)
df["date"] = df["date"].apply(
Expand Down

0 comments on commit a31a341

Please sign in to comment.