Exemplo n.º 1
0
 def update(self, dates):
     """Update alpha performance metrics for the **same** day after market close."""
     dnames = self.db.alpha.distinct('dname')
     si, ei = map(self.dates.index, [dates[0], dates[-1]])
     sywg_returns = quote.fetch('returns', self.dates[si-20], self.dates[ei])
     cnt = 0
     for dname in dnames:
         if self.options['alphas'] and dname not in self.options['alphas']:
             continue
         cursor = self.db.alpha.find(
                 {'dname': dname, 'date': {'$gte': self.dates[si-20], '$lte': self.dates[ei]}},
                 {'_id': 0, 'dvalue': 1, 'date': 1})
         alpha = pd.DataFrame({row['date']: row['dvalue'] for row in cursor}).T
         if len(alpha) == 0:
             continue
         analyser = Analyser(api.scale(api.neutralize(alpha)), data=sywg_returns)
         for date in dates:
             if date not in alpha.index:
                 continue
             key = {'alpha': dname, 'date': date}
             metrics = self.get_metrics(analyser, date)
             self.collection.update(key, {'$set': metrics}, upsert=True)
         cnt += 1
     if len(dates) == 1:
         self.logger.info('UPSERT documents for {} alphas into (c: [{}]) of (d: [{}]) on {}',
                 cnt, self.collection.name, self.db.name, dates[0])
     else:
         self.logger.info('UPSERT documents for {} alphas into (c: [{}]) of (d: [{}]) from {} to {}',
                 cnt, self.collection.name, self.db.name, dates[0], dates[-1])
Exemplo n.º 2
0
 def __init__(self, alpha, n, rank=None):
     self.alpha = api.format(alpha)
     self.rank_alpha = self.alpha.rank(axis=1, ascending=False)
     self.rank_alpha = self.rank_alpha[self.rank_alpha <= n]
     if rank is None:
         self.alpha = (self.rank_alpha <= n).astype(float)
     else:
         if rank < 0:
             self.alpha = self.alpha[self.rank_alpha <= n]
         else:
             self.alpha = rank - np.floor(self.rank_alpha / (n + 1) * rank)
     self.alpha = api.scale(self.alpha)
     self.dates = dateutil.to_datestr(self.alpha.index)
Exemplo n.º 3
0
    def __init__(self, alpha=None, data=None, index_data=None):
        if alpha is not None:
            self.alpha = api.scale(alpha)
            self.intervals = self.alpha.index.date

        self.IC, self.rIC = {}, {}
        self.AC, self.rAC = {}, {}
        self.turnover = None
        self.returns = None

        if data is not None:
            self.data = data.ix[self.alpha.index]
        if index_data is not None:
            self.index_data = index_data.ix[self.alpha.index]
Exemplo n.º 4
0
    def __init__(self, alpha, data, index_data=None):
        self.alpha = api.scale(alpha)
        self.dates = np.unique(self.alpha.index.date)
        self.intervals = len(self.alpha) / len(self.dates)

        self.IC_t, self.rIC_t = None, None
        self.IC_h, self.rIC_h = None, None
        self.IC_d, self.rIC_d = None, None
        self.AC_t, self.rAC_t = None, None
        self.AC_h, self.rAC_h = None, None
        self.AC_d, self.rAC_d = None, None
        self.turnover_t, self.turnover_h, self.turnover_d = None, None, None
        self.turnover, self.returns = None, None

        self.data = data.ix[self.alpha.index]
        if index_data is not None:
            self.index_data = index_data.ix[self.alpha.index]
Exemplo n.º 5
0
    if args.alpha:
        if not args.alphas:
            args.alphas = []
        if args.alpha not in args.alphas:
            args.alphas.append(args.alpha)
    assert args.alphas

    alphas, dates = {}, None
    for alpha in args.alphas:
        try:
            alphadf = read_frame(alpha, args.ftype)
            if dates is None:
                dates = alphadf.index
            else:
                dates = dates.union(alphadf.index)
            alphas[alpha] = api.scale(api.neutralize(alphadf))
        except:
            print '[WARNING] Failed to parse file', alpha

    sywg_returns = sywgquote_fetcher.fetch('returns',
                                           dates[0].strftime('%Y%m%d'),
                                           dates[-1].strftime('%Y%m%d'),
                                           level=args.level)
    for alpha, alphadf in alphas.iteritems():
        analyser = Analyser(alphadf, data=sywg_returns)
        alphas[alpha] = analyser

    pool = multiprocessing.Pool(args.threads)
    res = pool.imap_unordered(
        worker, ((alpha, analyser, args.cost, args.by, args.group, args.freq)
                 for alpha, analyser in alphas.iteritems()))
Exemplo n.º 6
0
    if args.alpha:
        if not args.alphas:
            args.alphas = []
        if args.alpha not in args.alphas:
            args.alphas.append(args.alpha)
    assert args.alphas

    alphas, dates = {}, None
    for alpha in args.alphas:
        try:
            alphadf = read_frame(alpha, args.ftype)
            if dates is None:
                dates = alphadf.index
            else:
                dates = dates.union(alphadf.index)
            alphas[alpha] = api.scale(api.neutralize(alphadf))
        except:
            print '[WARNING] Failed to parse file', alpha

    sywg_returns = sywgquote_fetcher.fetch('returns', dates[0].strftime('%Y%m%d'), dates[-1].strftime('%Y%m%d'), level=args.level)
    for alpha, alphadf in alphas.iteritems():
        analyser = Analyser(alphadf, data=sywg_returns)
        alphas[alpha] = analyser

    pool = multiprocessing.Pool(args.threads)
    res = pool.imap_unordered(worker, ((alpha, analyser, args.cost, args.by, args.group, args.freq) for alpha, analyser in alphas.iteritems()))
    pool.close()
    pool.join()

    for alpha, summary in res:
        print alpha
Exemplo n.º 7
0
 def test_scale(self):
     df = pd.DataFrame(np.random.randn(20, 40))
     df = api.scale(df)
     self.assertTrue(np.allclose(np.abs(df).sum(axis=1), 1))