コード例 #1
0
async def import_regime_model(tab_name):
    with open(output_path + "%s.json" % tab_name, 'r') as load_f:
        items = json.load(load_f)
        db = Connection.getInstance()
        for index, obj in enumerate(items):
            history = obj["history"]
            history = json.dumps(history)
            res = await findRegimeModel(obj["model_name"])
            if res:
                model = await RegimeModel.find(res.id)
                historys = await update_regime_model(db, obj["model_name"],
                                                     obj["factor_code"],
                                                     obj["ngegimes"])
                historys = [{
                    "Regime": history["Regime"],
                    "Date": float(history["Date"])
                } for history in historys]
                model.factor_code = obj["factor_code"]
                model.ngegimes = int(obj["ngegimes"])
                model.history = json.dumps(historys)
                await model.update()
            else:
                factor = RegimeModel(factor_code=obj["factor_code"],
                                     model_name=obj["model_name"],
                                     ngegimes=obj["ngegimes"],
                                     history=history)
                await factor.save()
コード例 #2
0
    def __init__(self, folio, userid=None, model_ticker=None,regime_model_name=None):

        self.db = Connection.getInstance()
        self.folio = folio
        self.Name = folio.Name
        self.user_id = userid
        self.model_ticker = model_ticker
        self.regime_model_name = regime_model_name
コード例 #3
0
async def toFolioPerformance(user_id, folio_id, folio, timestamp, allocations):

    port = port_folio({"folio_id": folio.id, "Allocations": allocations, "Name": folio.name})
    modify_at = toPreTimestamp(folio.created_at, day_num=365)
    db = Connection.getInstance()
    effective_date = datetime.datetime.utcfromtimestamp(int(folio.created_at))
    effective_date = str(effective_date).split(" ")[0] + " 00:00:00"
    pre_at = int(time.mktime(time.strptime(effective_date, '%Y-%m-%d %H:%M:%S')))
    proforma = await port.calc_proforma(db, modify_at, pre_at, folioPerformance=False, modify_num=365)
    if len(proforma):
        dates = proforma.index
        rets = proforma.values
        performance = {int(d.timestamp()):float(str(p))  for d, p in zip(dates, rets)}
        await updateFolioPerformance(folio_id, user_id, proforma=performance, pre=1, modify_at=timestamp)
コード例 #4
0
ファイル: coinReport.py プロジェクト: techbanca/coinai
async def get_benchmarks(db=None, modify_time=time.time()):
    """
    returns list of benchmarks
    """
    if not db:
        db = Connection.getInstance()
    benchdata = await report_dao.findReferenceData("DefaultBenchmarks")
    if benchdata:
        data_dict = {}
        for t in benchdata.data.decode().split(','):
            data_dict[t.split("^")[-1]] = await db.get_risk_factor(
                t.strip(), modify_time)
        return data_dict
    else:
        return {}
コード例 #5
0
ファイル: helper.py プロジェクト: tspannhw/coinai
            self.help_dict[field_name] = field

    def help(self, obj):
        obj_dict = obj.__dict__
        fields = [self.help_dict[f] for f in self.field_list if f in obj_dict]
        return [[
            f.label,
            f.format(obj_dict[f.key]) if float(obj_dict[f.key]) *
            100 < 1000 else ">1000%"
        ] if f.key == "ann_return" else [f.label,
                                         f.format(obj_dict[f.key])]
                for f in fields]

    def init_help(self):
        fields = [self.help_dict[f] for f in self.field_list]
        return [[f.label, "0"] for f in fields]


if __name__ == '__main__':

    helper = StatsHelper()

    from analytics.basicstats import BasicStats
    from datalib.datalib import Connection

    coinid = 'e042001c-9a43-11e6-bfb2-14109fdf0df7'
    db = Connection.getInstance()
    coin = db.get_coin(coinid)
    stats = BasicStats(coin.Performance)
    res = helper.help(stats)
コード例 #6
0
ファイル: coinReport.py プロジェクト: techbanca/coinai
 def __init__(self, item_id, tickers, isPortfolio=True, started_at=""):
     self.db = Connection.getInstance()
     self.item_id = item_id
     self.tickers = tickers
     self.started_at = started_at
     self.isPortfolio = isPortfolio
コード例 #7
0
ファイル: coinReport.py プロジェクト: techbanca/coinai
 def __init__(self, item_id, start_at=None, isPortFolio=True):
     self.db = Connection.getInstance()
     self.item_id = item_id
     self.start_at = start_at
     self.isPortFolio = isPortFolio
コード例 #8
0
ファイル: coinReport.py プロジェクト: techbanca/coinai
    def __init__(self, item, bench_ticker=None, modify_at=None):

        self.item = item
        self.db = Connection.getInstance()
        self.bench_ticker = bench_ticker
        self.modify_at = modify_at
コード例 #9
0
ファイル: coinReport.py プロジェクト: techbanca/coinai
async def portfolio_ratios(port_folio=None, folio_id="", db=None, lang="en"):

    try:
        if not db:
            db = Connection.getInstance()

        if not port_folio:
            if folio_id:
                port_folio = await folios_dao.findFolio(folio_id)
                if not port_folio:
                    return None
            else:
                return None

        helper = StatsHelper(lang=lang)
        proforma = await folios_dao.GetFolioPerformance(port_folio.id)
        if proforma:
            fts = proforma["pre_data"]["data"]
            now_data = [nowData["data"] for nowData in proforma["now_data"]]
            now_fts = []
            for d in now_data:
                fts.extend(d)
                now_fts.extend(d)

            prof_fts = [perf["NetReturn"] for perf in now_fts]
            dates = [
                datetime.fromtimestamp(p['EffectiveDate']) for p in now_fts
            ]
            fts_data = calculate_fts(prof_fts, dates)

            dates = [datetime.fromtimestamp(p['EffectiveDate']) for p in fts]
            prof_array = [perf["NetReturn"] for perf in fts]
            fts = Series(data=prof_array, index=dates, name=port_folio.name)

            T = len(fts)
            referenceData = await report_dao.findReferenceData('Risk Free Rate'
                                                               )
            if referenceData:
                rf = float(referenceData.data) * 0.01
            else:
                rf = 0

            stats_all = BasicStats(fts,
                                   risk_free=rf,
                                   folio_name=port_folio.name)
            roll_windows = windows[stats_all.freq[0]]
            roll_results = []
            ratio_items = {
                "Cumulative Return": [],
                "Period volatility": {
                    "volatility": "",
                    "day_7_volatility": ""
                },
                "Max Drawdown": {
                    "max_drawdown": "",
                    "day_7_drawdown": ""
                },
                "entire_history": "",
                "day_7_history": ""
            }

            resultsAll = helper.help(stats_all)
            result_dict = dict(resultsAll)
            ratio_items["entire_history"] = fts_data[
                "entire_history"] if fts_data else 0.0
            ratio_items["day_7_history"] = fts_data[
                "day_7_history"] if fts_data else 0.0
            ratio_items["Period volatility"]["volatility"] = fts_data[
                "total_volatility"] if fts_data else 0.0
            ratio_items["Max Drawdown"]["max_drawdown"] = fts_data[
                "total_max_drawdown"] if fts_data else 0.0
            ratio_items["Cumulative Return"].append(
                result_dict.get(_("Cumulative Return", lang), ""))
            for index, w in enumerate(roll_windows):
                if T >= w:
                    roll_fts = fts[-1 * w:]
                    ratio_data = helper.help(BasicStats(roll_fts,
                                                        risk_free=rf))
                    ratio_dict = dict(ratio_data)
                    val = ratio_dict.get(_("Cumulative Return", lang), "")
                    ratio_items["Cumulative Return"].append(val)
                    if index == 0:
                        ratio_items["Max Drawdown"][
                            "day_7_drawdown"] = fts_data[
                                "week_max_drawdown"] if fts_data else 0.0
                        ratio_items["Period volatility"][
                            "day_7_volatility"] = fts_data[
                                "week_volatility"] if fts_data else 0.0
                    roll_results.append([w, ratio_data])
            await folios_dao.updateFolioHistory(port_folio.id, ratio_items)

            reportData = {
                "ResultsAll": resultsAll,
                "RollResults": roll_results,
                "Coin": {
                    "Name": port_folio.name
                }
            }
            params = "ratios_%s" % lang
            await db.set_cache(port_folio.id,
                               "portfolio",
                               params,
                               data=reportData,
                               modify_at=port_folio.modify_at)
        else:
            roll_results = []
            ratio_items = {
                "Cumulative Return": [],
                "Period volatility": {
                    "volatility": ""
                },
                "Max Drawdown": {
                    "max_drawdown": "",
                    "day_7_drawdown": ""
                }
            }

            resultsAll = helper.init_help()
            result_dict = dict(resultsAll)

            ratio_items["Period volatility"]["volatility"] = result_dict.get(
                "% Volatility", "")
            ratio_items["Max Drawdown"]["max_drawdown"] = result_dict.get(
                "Max Drawdown", "")
            ratio_items["Cumulative Return"].append(
                result_dict.get("Cumulative Return", ""))
            await folios_dao.updateFolioHistory(port_folio.id, ratio_items)

            reportData = {
                "ResultsAll": resultsAll,
                "RollResults": roll_results,
                "Coin": {
                    "Name": port_folio.name
                }
            }

    except Exception as e:
        errMsg = getErrorMsg()
        error("portfolio_ratios exception is: %s, errMsg: %s" %
              (str(e), str(errMsg)))
        reportData = {}

    return reportData