Exemplo n.º 1
0
def get_estimated_netvalue():
    fund_rts = []
    try:
        tmp = get_query_result_set('select code from fund.fund_repo', cursor)
        funds_to_get = tmp
    except:
        pass
    #print('get rt for: ', end = ' ')
    for fund in funds_to_get:
        fund = 'F' + fund['code']
        try:
            #print(fund, end=' ', flush=True)
            ret = xa.get_rt(fund, double_check=False)
        except Exception as e:
            print(str(e.args))
            continue
        estimate = ret['estimate']
        current = ret['current']
        up = (estimate - current) / current
        name = ''
        name = ret['name']
        fund_rt = fund_rt_info(fund, current, estimate, ret['estimate_time'],
                               up * 100, name)
        fund_rts.append(fund_rt)
    #print(' ')
    return fund_rts
Exemplo n.º 2
0
def pred_ntf_oil(code, **kws):
    daily_holdings = kws.get("daily_holdings", holdings[code[2:]])
    rt_holdings = kws.get("rt_holdings", holdings["oil_rt"])
    try:
        ddprice, dprice = get_qdii_t(code, daily_holdings, rt_holdings)
    except NonAccurate as e:
        print(e.reason)
        return
    print(code, dprice, ddprice)
    r = xa.get_rt(code)
    cprice = r["current"]
    higher = kws.get("h", 1.04)
    lower = kws.get("l", 0.97)
    _type = kws.get("ntf_type", "pb")
    if cprice / dprice > higher or cprice / dprice < lower:
        notify(
            r["name"],
            "溢价率已达到 %s%%。T-1 日净值预估 %s, T 日净值实时预估 %s,实时价格 %s。"
            % (
                round((cprice / dprice - 1) * 100, 1),
                round(ddprice, 3),
                round(dprice, 3),
                round(cprice, 3),
            ),
            token=kws.get("token"),
            _type=_type,
        )
Exemplo n.º 3
0
def get_qdii_t(code, ttdict, tdict, percent=False):
    # predict realtime netvalue for d day, only possible for oil related lof
    nettt = get_qdii_tt(code, ttdict)
    t = 0
    n = 0
    today_str = dt.datetime.now(tz=tz_bj).strftime("%Y%m%d")
    for k, v in tdict.items():
        t += v
        if infos.get(k):
            url = infos[k].url
        else:
            url = k
        r = xa.get_rt(url)
        if percent or (not percent and not future_now.get(k)):
            c = v / 100 * (1 + r["percent"] / 100)
        else:
            print("use close to compare instead of directly percent for %s" %
                  k)
            funddf = get_daily(future_now[k])
            last_line = funddf[funddf["date"] < today_str].iloc[
                -1]  # TODO: check it is indeed date of last_on(today)
            c = v / 100 * r["current"] / last_line["close"]
        if r.get("currency") and r.get("currency") != "CNY":
            c = c * daily_increment(r["currency"] + "/CNY", today_str)
        n += c
    n += (100 - t) / 100
    nett = n * nettt
    return nettt, nett
Exemplo n.º 4
0
def pred_ntf_oil(*codes, **kws):
    higher = kws.get("h", 1.05)
    lower = kws.get("l", 0.96)
    title = kws.get("title", "基金溢价提示")
    _type = kws.get("ntf_type", "pb")
    rstr = ""
    for code in codes:
        daily_holdings = kws.get("daily_holdings", holdings[code[2:]])
        rt_holdings = kws.get("rt_holdings", holdings["oil_rt"])
        try:
            ddprice, dprice = get_qdii_t(code, daily_holdings, rt_holdings)
        except NonAccurate as e:
            print(e.reason)
            continue
        print(code, dprice, ddprice)
        r = xa.get_rt(code)
        cprice = r["current"]

        if cprice / dprice > higher or cprice / dprice < lower:
            rstr += "%s: 溢价率已达到 %s%%。T-1 日净值预估 %s, T 日净值实时预估 %s,实时价格 %s。\n" % (
                r["name"],
                round((cprice / dprice - 1) * 100, 1),
                round(ddprice, 3),
                round(dprice, 3),
                round(cprice, 3),
            )

    if rstr:
        notify(
            title,
            rstr,
            token=kws.get("token"),
            _type=_type,
        )
Exemplo n.º 5
0
def get_nonqdii_t(code, tdict, date=None):
    if not date:  # 今日实时净值
        last_value, last_date = get_newest_netvalue("F" + code[2:])
        tz_bj = dt.timezone(dt.timedelta(hours=8))
        today = dt.datetime.now(tz=tz_bj)
        yesterday = last_onday(today)
        yesterday_str = yesterday.strftime("%Y-%m-%d")
        last_value, last_date = get_newest_netvalue("F" + code[2:])
        if last_date != yesterday_str:
            raise DateMismatch(
                "%s netvalue has not been updated to the day before yesterday"
                % code)
        t = 0
        r = 100
        for k, v in tdict.items():
            if infos.get(k):
                url = infos[k].url
            else:
                url = k
            print(url)
            aim_current = xa.get_rt(url)
            delta1 = aim_current["percent"] / 100
            currency = aim_current["currency"]
            if currency == "JPY":
                delta2 = xa.get_rt("currencies/jpy-cny")["percent"] / 100
            elif currency == "USD":
                delta2 = xa.get_rt("currencies/usd-cny")["percent"] / 100
            elif currency == "CNY":
                delta2 = 0
            else:
                raise NonAccurate(
                    "%s transformation have not been implemented" % currency)

            r -= v
            t += v * (1 + delta1) * (1 + delta2) / 100

        t += r / 100
        return last_value * t
    # 过去净值同日预测 date 日, date 日一定是交易日
    date_str = date.replace("-", "").replace("/", "")
    funddf = xa.get_daily("F" + code[2:])
    last_value = funddf[funddf["date"] < date_str].iloc[-1]["close"]
    net = last_value * (1 + evaluate_fluctuation(tdict, date_str) / 100)
    return net
Exemplo n.º 6
0
def get_nonqdii_t(code, tdict, date=None):
    if not date:  # 今日实时净值
        last_value, last_date = get_newest_netvalue("F" + code[2:])
        today = dt.datetime.now(tz=tz_bj).replace(tzinfo=None)
        today_str = today.strftime("%Y-%m-%d")
        yesterday = last_onday(today)
        yesterday_str = yesterday.strftime("%Y-%m-%d")
        last_value, last_date = get_newest_netvalue("F" + code[2:])
        if last_date != yesterday_str:
            raise DateMismatch(
                code, "%s netvalue has not been updated to yesterday" % code)
        t = 0
        r = 100
        for k, v in tdict.items():
            if infos.get(k):
                url = infos[k].url
            else:
                url = k
            aim_current = xa.get_rt(url)
            delta1 = aim_current["percent"] / 100
            currency = aim_current["currency"]
            ## 关于当日货币换算的部分,1. 当日中间价涨幅 2. 当日汇率市价实时涨幅 3.1+2 哪个更合适待研究
            if currency == "JPY":
                delta2 = daily_increment("100JPY/CNY",
                                         today_str,
                                         yesterday_str,
                                         _check=yesterday_str)
                # delta2 = xa.get_rt("currencies/jpy-cny")["percent"] / 100
            elif currency == "USD":
                delta2 = daily_increment("USD/CNY",
                                         today_str,
                                         yesterday_str,
                                         _check=yesterday_str)
                # delta2 = xa.get_rt("currencies/usd-cny")["percent"] / 100
            elif currency == "EUR":
                delta2 = daily_increment("EUR/CNY",
                                         today_str,
                                         yesterday_str,
                                         _check=yesterday_str)
            elif currency == "CNY":
                delta2 = 0
            else:
                raise NonAccurate(
                    "%s transformation have not been implemented" % currency)

            r -= v
            t += v * (1 + delta1) * delta2 / 100

        t += r / 100
        return last_value * t
    # 过去净值同日预测 date 日, date 日一定是交易日
    date_str = date.replace("-", "").replace("/", "")
    funddf = xa.get_daily("F" + code[2:])
    last_value = funddf[funddf["date"] < date_str].iloc[-1]["close"]
    net = last_value * (1 + evaluate_fluctuation(tdict, date_str) / 100)
    return net
Exemplo n.º 7
0
def get_currency(code):
    # only works for HKD JPY USD GBD CNY EUR
    if code in infos:
        return infos[code].currency

    try:
        currency = xa.get_rt(code)["currency"]
        if currency is None:
            currency = "CNY"
        elif currency == "JPY":
            currency = "100JPY"
    except (TypeError, AttributeError, ValueError):
        currency = "CNY"
    return currency
Exemplo n.º 8
0
def get_currency(code):
    # 强制需要自带 cache,否则在回测 table 是,info 里没有的代码将很灾难。。。
    # only works for HKD JPY USD GBP CNY EUR, not very general when data source gets diverse more
    if code in infos:
        return infos[code].currency

    try:
        currency = xa.get_rt(code)["currency"]
        if currency is None:
            currency = "CNY"
        elif currency == "JPY":
            currency = "100JPY"
    except (TypeError, AttributeError, ValueError):
        currency = "CNY"
    return currency
Exemplo n.º 9
0
def _new_render_github(code, tmpl, date, cols="4c"):
    name = xa.get_rt(code)["name"]
    once = render_template(
        tmpl=tmpl, code=code, name=name, date=date, cols=cols
    )
    prev = (dt.datetime.now() - dt.datetime.strptime(date, "%Y-%m-%d")).days + 2
    for _ in range(prev):
        once = render(once, code)
    with open(
        os.path.join(
            os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
            "%s.html" % code,
        ),
        "w",
    ) as f:
        f.writelines([once])
Exemplo n.º 10
0
def get_qdii_t(
    code,
    ttdict,
    tdict,
):
    # predict realtime netvalue for d day, only possible for oil related lof
    nettt = get_qdii_tt(code, ttdict)
    t = 0
    n = 0
    today_str = dt.datetime.now().strftime("%Y%m%d")
    for k, v in tdict.items():
        t += v
        r = xa.get_rt(k)
        c = v / 100 * (1 + r["percent"] / 100)
        c = c * daily_increment(r["currency"] + "/CNY", today_str)
        n += c
    n += (100 - t) / 100
    nett = n * nettt
    return nettt, nett
Exemplo n.º 11
0
def test_get_ycharts():
    # ycharts 可能有时也需要代理了。。。。
    d = xa.get_daily(code="yc-companies/DBP", start="20200401", end="20200402")
    assert d.iloc[0]["close"] == 41.04

    d = xa.get_daily(
        code="yc-companies/DBP/net_asset_value", start="20200401", end="20200402"
    )
    assert d.iloc[0]["close"] == 40.7144

    d = xa.get_daily(code="yc-indices/^SPGSCICO", start="20200401", end="20200402")
    assert d.iloc[0]["close"] == 111.312

    d = xa.get_daily(
        code="yc-indices/^SPGSCICO/total_return_forward_adjusted_price",
        start="20200401",
        end="20200402",
    )
    assert d.iloc[0]["close"] == 169.821

    assert xa.get_rt("yc-companies/DBO")["currency"] == "USD"
Exemplo n.º 12
0
def test_get_investng_app():
    df = xa.get_daily(code="INA-currencies/usd-cny", end="20200307",
                      prev=30)  # 似乎外网链接有问题?
    assert round(df.iloc[-1]["close"], 4) == 6.9321
    assert xa.get_rt("INA-indices/germany-30")["name"] == "德国DAX30指数 (GDAXI)"
Exemplo n.º 13
0
def test_get_ttjj_rt_oversea():
    r = xa.get_rt("F968012")
    assert r["name"] == "中银香港高息债"
Exemplo n.º 14
0
def test_get_ttjj():
    assert xa.get_rt("F501018")["name"] == "南方原油A"
    assert xa.get_rt("F511600")["type"] == "货币型"
Exemplo n.º 15
0
def replace_text(otext, code=None, est_holdings=None, rt_holdings=None):
    print(otext)
    dtstr = otext.split(":")[1].split(";")[0]
    dtobj = dt.datetime.strptime(dtstr, "%Y-%m-%d-%H-%M")
    now = dt.datetime.now(tz=tz_bj)
    now = now.replace(tzinfo=None)
    if now >= dtobj:
        v = otext.split(">")[0].split(";")[1].split("-")[-3]
        vdtstr = otext.split(";")[1][:10]  # -
        if not est_holdings:
            est_holdings = holdings[code[2:]]  ## 动态仓位调整切入点
        today = now.strftime("%Y-%m-%d")
        if v == "value1":
            if not rt_holdings and not holdings.get(code[2:] + "rt"):
                rt_holdings = holdings["oil_rt"]  # 默认石油基金预测
            elif not rt_holdings:
                rt_holdings = holdings[code[2:] + "rt"]
            # 实时净值
            if today == vdtstr:
                try:
                    _, rtvalue = get_qdii_t(code, est_holdings, rt_holdings)
                    ntext = str(round(rtvalue, 3))
                    now_value = xa.get_rt(code)["current"]
                    prate = round((now_value / rtvalue - 1) * 100, 1)
                    ntext += f" ({now.strftime('%H:%M')})"
                    if prate > 0:
                        ntext += f'<p style="color: red;display: inline"> [{prate}%]</p>'
                    else:
                        ntext += f'<p style="color: green;display: inline"> [{prate}%]</p>'
                    ntext = (otext.split(">")[0] + ">" + ntext + "<" +
                             otext.split("<")[-1])
                except NonAccurate as e:
                    print(e.reason)
                    ntext = otext
            else:
                # 新的一天,不再预测实时
                # ntext = otext.split(">")[1].split("<")[0]
                ntext = "<".join(">".join(
                    otext.split(">")[1:]).split("<")[:-1])
        elif v == "value2":
            try:
                if last_onday(now).strftime("%Y-%m-%d") == vdtstr:
                    ntext = str(round(get_qdii_tt(code, est_holdings), 3))
                else:
                    ntext = str(
                        round(get_qdii_tt(code, est_holdings, date=vdtstr), 3))
            except NonAccurate as e:
                print(e.reason)
                ntext = otext
        elif v == "value3":
            # 真实净值
            fund_price = xa.get_daily(code="F" + code[2:], end=vdtstr)
            fund_line = fund_price[fund_price["date"] == vdtstr]
            if len(fund_line) == 0:
                value, date = get_newest_netvalue(
                    "F" +
                    code[2:])  # incase get_daily -1 didn't get timely update
            else:
                value = fund_line.iloc[0]["close"]
                date = fund_line.iloc[0]["date"].strftime("%Y-%m-%d")
            if date != vdtstr:
                ntext = otext
            else:
                ntext = str(value)
        elif v == "value4":  # non qdii 同日 qdii lof 的实时净值
            try:
                if today == vdtstr:
                    if now.hour > 9 and now.hour < 15:
                        v = get_nonqdii_t(code, est_holdings)
                        ntext = str(round(v, 3))
                        ntext += f" ({now.strftime('%H:%M')})"
                        ntext = (otext.split(">")[0] + ">" + ntext + "<" +
                                 otext.split("<")[-1])
                    else:
                        ntext = otext
                else:
                    v = get_nonqdii_t(code, est_holdings, date=vdtstr)
                    ntext = str(round(v, 3))

            except NonAccurate as e:
                print(e.reason)
                ntext = otext

        elif v == "4c":
            ntext = f"""<!--update:{next_onday(dtobj).strftime("%Y-%m-%d-%H-%M")};{next_onday(dtobj).strftime("%Y-%m-%d")}-4c--><!--end-->
<tr>
<td style='text-align:center;' >{dtobj.strftime("%Y-%m-%d")}</td>
<td style='text-align:center;' ><!--update:{(dtobj + dt.timedelta(hours=1)).strftime("%Y-%m-%d-%H-%M")};{dtobj.strftime("%Y-%m-%d")}-value1-->&nbsp;<!--end--></td>
<td style='text-align:center;' ><!--update:{(dtobj + dt.timedelta(days=1, hours=1)).strftime(
        "%Y-%m-%d-%H-%M"
    )};{dtobj.strftime("%Y-%m-%d")}-value2-->&nbsp;<!--end--></td>
<td style='text-align:center;' ><!--update:{(dtobj + dt.timedelta(days=1, hours=12)).strftime("%Y-%m-%d-%H-%M")};{dtobj.strftime("%Y-%m-%d")}-value3-->&nbsp;<!--end--></td>
</tr>
            """
        elif v == "3c":
            ntext = f"""<!--update:{next_onday(dtobj).strftime("%Y-%m-%d-%H-%M")};{next_onday(dtobj).strftime("%Y-%m-%d")}-3c--><!--end-->
<tr>
<td style='text-align:center;' >{dtobj.strftime("%Y-%m-%d")}</td>
<td style='text-align:center;' ><!--update:{(dtobj + dt.timedelta(days=1, hours=1)).strftime(
        "%Y-%m-%d-%H-%M"
    )};{dtobj.strftime("%Y-%m-%d")}-value2-->&nbsp;<!--end--></td>
<td style='text-align:center;' ><!--update:{(dtobj + dt.timedelta(days=1, hours=12)).strftime("%Y-%m-%d-%H-%M")};{dtobj.strftime("%Y-%m-%d")}-value3-->&nbsp;<!--end--></td>
</tr>
                        """
        elif v == "3crt":
            ntext = f"""<!--update:{next_onday(dtobj).strftime("%Y-%m-%d-%H-%M")};{next_onday(dtobj).strftime("%Y-%m-%d")}-3crt--><!--end-->
<tr>
<td style='text-align:center;' >{dtobj.strftime("%Y-%m-%d")}</td>
<td style='text-align:center;' ><!--update:{(dtobj + dt.timedelta(hours=1, minutes=30)).strftime(
        "%Y-%m-%d-%H-%M"
    )};{dtobj.strftime("%Y-%m-%d")}-value4-->&nbsp;<!--end--></td>
<td style='text-align:center;' ><!--update:{next_onday(dtobj).strftime("%Y-%m-%d-%H-%M")};{dtobj.strftime("%Y-%m-%d")}-value3-->&nbsp;<!--end--></td>
</tr>
                        """

    else:
        ntext = otext
    print("replaced as %s" % ntext)
    return ntext
Exemplo n.º 16
0
def test_get_xueqiu_rt():
    assert xa.get_rt("PDD")["currency"] == "USD"
    assert xa.get_rt("03333")["name"] == xa.get_rt("HK03333")["name"]
    assert isinstance(xa.get_rt("SH501018")["percent"], float)
Exemplo n.º 17
0
data = {
    "code": [],
    "name": [],
    "t1": [],
    "t0": [],
    "now": [],
    "t1rate": [],
    "t0rate": [],
    "positions": [],
}
for c in qdiis:
    p = xa.QDIIPredict(c, fetch=True, save=True)
    try:
        data["t1"].append(round(p.get_t1(return_date=False), 4))
        data["t1rate"].append(round(p.get_t1_rate(return_date=False), 2))
        try:
            data["t0"].append(round(p.get_t0(return_date=False), 4))
            data["t0rate"].append(round(p.get_t0_rate(return_date=False), 2))
        except ValueError:
            data["t0"].append("-")
            data["t0rate"].append("-")
        data["positions"].append(round(p.get_position(return_date=False), 3))
        data["now"].append(xa.get_rt(c)["current"])
        data["code"].append(c)
        data["name"].append(xa.get_rt(c)["name"])
    except xa.exceptions.NonAccurate:
        print("%s cannot be predicted exactly now" % c)
df = pd.DataFrame(data)
with open("demo.html", "w") as f:
    df.to_html(f)
Exemplo n.º 18
0
def test_get_investing_rt():
    assert xa.get_rt("currencies/usd-cny")["currency"] == None
    assert xa.get_rt("/indices/germany-30")["name"] == "德国DAX30指数 (GDAXI)"
    ext = xa.get_rt("equities/pinduoduo")["current_ext"]
    assert isinstance(ext, float) or (ext is None)
Exemplo n.º 19
0
def test_get_sina_rt():
    assert xa.get_rt("PDD", _from="sina")["currency"] == "USD"
    xa.get_rt("HK00700", double_check=True)  # 港股 sina 实时数据延迟, 代码前需加 rt_ 方可获取实时
    xa.get_rt("SH600000", double_check=True)
Exemplo n.º 20
0
    "t0rate": [],
    "position": [],
}
for c in qdiis:
    p = xa.QDIIPredict(c, fetch=True, save=True, positions=True)
    try:
        data["t1"].append(round(p.get_t1(return_date=False), 4))
        data["t1rate"].append(round(p.get_t1_rate(return_date=False), 2))
        try:
            data["t0"].append(round(p.get_t0(return_date=False), 4))
            data["t0rate"].append(round(p.get_t0_rate(return_date=False), 2))
        except ValueError:
            data["t0"].append("-")
            data["t0rate"].append("-")
        data["position"].append(round(p.get_position(return_date=False), 3))
        data["now"].append(xa.get_rt(c)["current"])
        data["code"].append(c)
        data["name"].append(xa.get_rt(c)["name"])
    except xa.exceptions.NonAccurate as e:
        print("%s cannot be predicted exactly now" % c)
        print(e.reason)
for c in nonqdiis:
    p = xa.RTPredict(c)
    data["t1"].append(xa.get_rt("F" + c[2:])["current"])
    data["t1rate"].append("-")
    data["t0"].append(round(p.get_t0(return_date=False), 4))
    data["t0rate"].append(round(p.get_t0_rate(return_date=False), 2))
    data["position"].append("-")
    data["now"].append(xa.get_rt(c)["current"])
    data["code"].append(c)
    data["name"].append(xa.get_rt(c)["name"])
Exemplo n.º 21
0
def test_get_ft_rt():
    assert xa.get_rt("FT-INX:IOM")["currency"] == "USD"
    assert xa.get_rt(
        "FTC-WTI+Crude+Oil")["name"] == "NYMEX Crude Oil Front Month"
Exemplo n.º 22
0
def test_get_ttjj():
    assert xa.get_rt("F501018")["name"] == "南方原油A"
Exemplo n.º 23
0
def cached_get_rt(code, **kws):
    return xa.get_rt(code, handler=False)
Exemplo n.º 24
0
def test_get_ttjj():
    assert xa.get_rt("F501018")["name"] == "南方原油A"
    assert xa.get_rt("F511600")["type"] == "货币型"
    assert xa.get_rt("F003816")["market"] == "CN"