Example #1
0
class IndustryNeutOperation(GroupNeutOperation):
    """Class to neutralize alpha by industry classifications.

    :param str standard: Industry classification standard, currently only supports: ('SW2014', 'ZX'). Default: 'SW2014'
    """

    def __init__(self, standard='SW2014', **kwargs):
        super(IndustryNeutOperation, self).__init__(**kwargs)
        self.standard = standard
        self.industry = IndustryFetcher(datetime_index=True)
        self.group = None

    def operate(self, alpha, group='sector', simple=False, date=None):
        alpha = alpha[np.isfinite(alpha)]
        if isinstance(alpha, pd.Series):
            group = self.industry.fetch_daily(group, date).dropna()
            nalpha = alpha.ix[group.index]
            nalpha = nalpha.groupby(group).transform(lambda x: x-x.mean())
            return nalpha.reindex(index=alpha.index)

        window = np.unique(dateutil.to_datestr(alpha.index))
        if simple:
            self.group = self.industry.fetch_daily(group, window[-1])
        else:
            self.group = self.industry.fetch_window(group, window)
        return super(IndustryNeutOperation, self).operate(alpha)
Example #2
0
class IndustryNeutOperation(GroupNeutOperation):
    """Class to neutralize alpha by industry classifications.

    :param str standard: Industry classification standard, currently only supports: ('SW2014', 'ZX'). Default: 'SW2014'
    """
    def __init__(self, standard='SW2014', **kwargs):
        super(IndustryNeutOperation, self).__init__(**kwargs)
        self.standard = standard
        self.industry = IndustryFetcher(datetime_index=True)
        self.group = None

    def operate(self, alpha, group='sector', simple=False, date=None):
        alpha = alpha[np.isfinite(alpha)]
        if isinstance(alpha, pd.Series):
            group = self.industry.fetch_daily(group, date).dropna()
            nalpha = alpha.ix[group.index]
            nalpha = nalpha.groupby(group).transform(lambda x: x - x.mean())
            return nalpha.reindex(index=alpha.index)

        window = np.unique(dateutil.to_datestr(alpha.index))
        if simple:
            self.group = self.industry.fetch_daily(group, window[-1])
        else:
            self.group = self.industry.fetch_window(group, window)
        return super(IndustryNeutOperation, self).operate(alpha)
Example #3
0
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter
datefmt = DateFormatter('%Y%m%d')
from matplotlib.backends.backend_pdf import PdfPages

import magic

from orca.mongo.industry import IndustryFetcher
from orca.mongo.index import IndexQuoteFetcher
from orca.mongo.components import ComponentsFetcher
from orca.mongo.sywgquote import SYWGQuoteFetcher
from orca.mongo.kday import UnivFetcher

industry_fetcher = IndustryFetcher(datetime_index=True, reindex=True)
indexquote_fetcher = IndexQuoteFetcher(datetime_index=True)
components_fetcher = ComponentsFetcher(datetime_index=True)
sywgquote_fetcher = SYWGQuoteFetcher(datetime_index=True, use_industry=True)
univ_fetcher = UnivFetcher(datetime_index=True, reindex=True)

from orca.utils import dateutil
from orca.utils.io import read_frame
from orca.operation import api


class Weight(object):
    """Class to analyse portfolio weight decomposition through time."""
    def __init__(self, alpha, n, rank=None):
        self.alpha = api.format(alpha)
        self.rank_alpha = self.alpha.rank(axis=1, ascending=False)
Example #4
0
 def __init__(self, standard='SW2014', **kwargs):
     super(IndustryRankOperation, self).__init__(**kwargs)
     self.standard = standard
     self.industry = IndustryFetcher(datetime_index=True)
     self.group = None
Example #5
0
 def __init__(self, standard='SW2014', **kwargs):
     super(IndustryNeutOperation, self).__init__(**kwargs)
     self.standard = standard
     self.industry = IndustryFetcher(datetime_index=True)
     self.group = None
Example #6
0
"""
.. moduleauthor:: Li, Wang <*****@*****.**>
"""

import os
from string import Template

import pandas as pd
import logbook
logbook.set_datetime_format('local')
logger = logbook.Logger('assets')

from orca.mongo.barra import BarraFetcher
barra_fetcher = BarraFetcher('short')
from orca.mongo.industry import IndustryFetcher
industry_fetcher = IndustryFetcher()
from orca.mongo.components import ComponentsFetcher
components_fetcher = ComponentsFetcher()

def get_board(sid):
    if sid[:2] == '60':
        return 'SH'
    elif sid[:2] == '30':
        return 'CYB'
    elif sid[:3] == '002':
        return 'ZXB'
    else:
        return 'SZ'

def generate_path(path_pattern, date):
    return Template(path_pattern).substitute(YYYYMMDD=date, YYYYMM=date[:6], YYYY=date[:4], MM=date[4:6], DD=date[6:8])
Example #7
0
    TradingDaysFilter,
    ActiveFilter,
    ComponentsFilter,
    IndustryFilter,
)
from orca.universe.rules import (
    startswith,
    count_gt,
)

window = dateutil.get_startfrom(DATES, '20140104', 50)
close = QuoteFetcher(datetime_index=True,
                     reindex=True).fetch_window('close', window)
hs300 = ComponentsFetcher(datetime_index=True,
                          reindex=True).fetch_window('HS300', window)
sector = IndustryFetcher(datetime_index=True,
                         reindex=True).fetch_window('sector', window)


class SpecialTestCase(unittest.TestCase):
    def test_ticker_filter(self):
        sh = TickerFilter(startswith('60')).filter_daily(window[0])
        dct = {sid: sid[:2] == '60' for sid in SIDS}
        self.assertEqual(sh.to_dict(), dct)

    def test_trading_days_filter(self):
        trd_filter = TradingDaysFilter(50, count_gt(40), delay=0)
        trd1 = trd_filter.filter_daily(window[-1])
        trd2 = close.count() > 40
        self.assertTrue(series_equal(trd1, trd2))

    def test_active_filter(self):
Example #8
0
"""
.. moduleauthor:: Li, Wang <*****@*****.**>
"""

import os
from string import Template

import pandas as pd
import logbook
logbook.set_datetime_format('local')
logger = logbook.Logger('assets')

from orca.mongo.barra import BarraFetcher
barra_fetcher = BarraFetcher('short')
from orca.mongo.industry import IndustryFetcher
industry_fetcher = IndustryFetcher()
from orca.mongo.components import ComponentsFetcher
components_fetcher = ComponentsFetcher()


def get_board(sid):
    if sid[:2] == '60':
        return 'SH'
    elif sid[:2] == '30':
        return 'CYB'
    elif sid[:3] == '002':
        return 'ZXB'
    else:
        return 'SZ'

Example #9
0
"""
.. moduleauthor:: Li, Wang <*****@*****.**>
"""

from orca.mongo.industry import IndustryFetcher
industry_fetcher = IndustryFetcher()


def industry2name(pdobj):
    pdobj = pdobj.copy()
    ind_name = industry_fetcher.fetch_info()
    pdobj.index = [ind_name[ind] for ind in pdobj.index]
    return pdobj


def name2industry(pdobj):
    pdobj = pdobj.copy()
    name_ind = {v: k for k, v in industry_fetcher.fetch_info().iteritems()}
    pdobj.index = [name_ind[name] for name in pdobj.index]
    return pdobj


import pandas as pd

from orca import DATES
from orca.mongo.quote import QuoteFetcher
quote_fetcher = QuoteFetcher(datetime_index=True)

import dateutil

Example #10
0
 def setUp(self):
     self.fetcher = IndustryFetcher()
     self.dates = dateutil.get_startfrom(DATES, '20140101', 20)
Example #11
0
class IndustryFetcherTestCase(unittest.TestCase):

    def setUp(self):
        self.fetcher = IndustryFetcher()
        self.dates = dateutil.get_startfrom(DATES, '20140101', 20)

    def tearDown(self):
        self.fetcher = None

    def test_fetch_window1(self):
        df1_1 = self.fetcher.fetch_window('level1', self.dates)
        df1_2 = self.fetcher.fetch_window('sector', self.dates)
        df2_1 = self.fetcher.fetch_window('level2', self.dates)
        df2_2 = self.fetcher.fetch_window('industry', self.dates)
        df3_1 = self.fetcher.fetch_window('level3', self.dates)
        df3_2 = self.fetcher.fetch_window('subindustry', self.dates)
        self.assertTrue(frames_equal(df1_1, df1_2)
                      & frames_equal(df2_1, df2_2)
                      & frames_equal(df3_1, df3_2))

    def test_fetch_window2(self):
        df1 = self.fetcher.fetch_window('level1', self.dates)
        df2 = self.fetcher.fetch_window('level1', self.dates, standard='ZX')
        self.assertFalse(frames_equal(df1, df2))

    def test_fetch_info1(self):
        d0 = self.fetcher.fetch_info('name', level=0)
        d1 = self.fetcher.fetch_info('name', level=1)
        d2 = self.fetcher.fetch_info('name', level=2)
        d3 = self.fetcher.fetch_info('name', level=3)
        self.assertEqual(len(d0), len(d1)+len(d2)+len(d3))

    def test_fetch_info2(self):
        d0 = self.fetcher.fetch_info('index', level=0)
        d1 = self.fetcher.fetch_info('index', level=1)
        d2 = self.fetcher.fetch_info('index', level=2)
        d3 = self.fetcher.fetch_info('index', level=3)
        self.assertEqual(len(d0), len(d1)+len(d2)+len(d3))
Example #12
0
"""
.. moduleauthor:: Li, Wang <*****@*****.**>
"""

import pandas as pd

from orca import SIDS
from orca.mongo.industry import IndustryFetcher

FETCHER = IndustryFetcher()


def get_board(sid):
    if sid[:2] == '60':
        return 'SH'
    elif sid[:2] == '30':
        return 'CYB'
    elif sid[:3] == '002':
        return 'ZXB'
    return 'SZ'


BOARD = pd.Series({sid: get_board(sid) for sid in SIDS})


def group_by_board(df):
    if isinstance(df, pd.Series):
        group = BOARD.ix[df.index]
        return df.groupby(group)
    group = BOARD.ix[df.columns]
    return df.groupby(group, axis=1)