Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_policy_buyandhold():
allin = xa.policy.buyandhold(cm, "2015-06-01")
cm_t2 = xa.trade(cm, allin.status)
cm_m2 = xa.mulfix(cm_t2)
cm_m2.bcmkset(xa.indexinfo("1399971"))
assert round(cm_m2.correlation_coefficient("2018-07-01"), 3) == 0.980
assert round(cm_m2.information_ratio("2016-07-01"), 3) == -0.385
allin.sellout("2018-06-01")
cm_t2 = xa.trade(cm, allin.status)
assert round(cm_t2.xirrrate("2019-08-12", guess=-0.9), 2) == -0.33
import sys
sys.path.insert(0, "../")
import xalpha as xa
from xalpha.exceptions import FundTypeError
import pandas as pd
import pytest
ioconf = {"save": True, "fetch": True, "path": "pytest", "form": "csv"}
ca = xa.cashinfo(interest=0.0002, start="2015-01-01")
zzhb = xa.indexinfo("0000827", **ioconf)
hs300 = xa.fundinfo("000311")
zogqb = xa.mfundinfo("001211", **ioconf)
def test_fundreport():
# somehow fragile, to be checked
r = xa.FundReport("000827")
assert r.get_report()[0][:2] == "广发"
assert r.analyse_report(1)["bank"][:2] == "兴业"
assert r.show_report_list(type_=0)[0]["FUNDCODE"] == "000827"
assert r.get_report(id_="AN202003171376532533")[0][:2] == "广发"
def test_cash():
assert (
round(ca.price[ca.price["date"] == "2018-01-02"].iloc[0].netvalue, 4) == 1.2453
def test_mul():
with pytest.raises(Exception) as excinfo:
cm_m = xa.mulfix(cm_t, totmoney=200)
assert str(excinfo.value) == "You cannot sell first when you never buy"
with pytest.raises(Exception) as excinfo:
cm_m = xa.mulfix(cm_t, totmoney=300)
assert str(excinfo.value) == "the initial total cash is too low"
cm_m = xa.mulfix(cm_t, totmoney=500)
cm_m.bcmkset(xa.indexinfo("1399971"), start="2016-09-28")
assert round(cm_m.xirrrate("2018-07-29"), 3) == -0.129
assert round(cm_m.sharpe("2018-07-30"), 3) == -1.734
cm_m.v_netvalue(benchmark=False)
assert round(cm_m.total_return("2018-07-01"), 3) == -0.209
assert round(cm_m.benchmark_volatility("2018-07-22"), 3) == 0.192
assert round(cm_m.max_drawdown("2018-08-01")[2], 2) == -0.24
cm_m.v_tradevolume()
cm_m.v_tradecost()
hs300 = xa.fundinfo("000311", **ioconf)
len2 = len(hs300.price)
delete_csvlines(path=ioconf["path"] + "000311.csv")
hs300 = xa.fundinfo("000311", **ioconf)
len3 = len(hs300.price)
assert (len1 == len2) or (len1 - len2 == -1) # temp fixup
# there may be time lag for update of .js API, i.e. 天天基金的该 API 不一定能保证更新昨天的净值,即使不是 QDII
assert (len1 == len3) or (len1 - len3 == -1)
delete_csvlines(path=ioconf["path"] + "001211.csv")
zogqb2 = xa.mfundinfo("001211", **ioconf)
assert round(zogqb.price.iloc[-1].netvalue, 5) in [
round(zogqb2.price.iloc[-1].netvalue, 5),
round(zogqb2.price.iloc[-2].netvalue, 5),
]
delete_csvlines(path=ioconf["path"] + "0000827.csv")
zzhb2 = xa.indexinfo("0000827", **ioconf)
assert (len(zzhb2.price) == len(zzhb.price)) or (
(len(zzhb2.price) - len(zzhb.price)) == 1
)
def test_policy_indicator_points():
zz500 = xa.indexinfo("0000905")
zz500.psy()
st = xa.policy.indicator_points(
zz500,
col="PSYMA12",
start="2017-01-01",
buy=[(0.6, 1), (0.7, 1)],
sell=[(0.4, 1), (0.3, 1)],
buylow=False,
)
zz500_t = xa.trade(zz500, st.status)
assert zz500_t.dailyreport("2018-05-01").iloc[0].loc["基金收益总额"] == -6302.26