Ejemplo n.º 1
0
def daily_trade(universe=None, date=None, days_back=90):
    """Returns a list of all the current traded spread (which are set to Trade in the universe)

    :param universe: list: Not necessary, if not specified will use Excel.
    :param date: If specified, returns the list for a specific date (today is set to default)
    :param days_back: int - Number of business days to expiration
    :return: DataFrame
    """
    log = logging.getLogger(__name__)

    log.debug('Daily Trade list')
    universe = list(
        get_universe()['CTicker']) if universe is None else universe
    day = dt.datetime.now().strftime('%Y-%m-%d') if date is None else date
    dfs = []
    for ct in universe:
        fc = cc.FutureChain(ct, i.FutureType.Spread)
        fc.initialize_contracts(cc.Status.Active,
                                nac=4)  # Trade the first 2 spreads
        dfs.append(fc.chain)
    rdf = pd.concat(dfs)
    rdf['DaysTo'] = rdf.apply(
        lambda x: len(pd.date_range(day, x['LastDate'], freq=o.BDay())),
        axis=1)
    rdf = rdf[rdf['DaysTo'] <= days_back + 3]  # Added 3

    return rdf.reset_index(drop=True)
Ejemplo n.º 2
0
def initialize_cerebro_for_single_bt(stem, cash, mode):
    """Initialize Cerebro for a backtest. Needs cash, commissions, data and analyser.

    :param stem: str - Customized ticker
    :param cash: float - Portfolio start value
    :param mode: Enum Mode - Which mode to run: Backtest or Live?
    :return: object Cerebro, object FutureChain - These are both needed to run a backtest
    """
    log = logging.getLogger(__name__)

    # Setup Cerebro
    cerebro = ob.Backtest.setup_cerebro(cash)
    # Future Chain
    status = occ.Status.Expired if mode == Mode.Backtest else occ.Status.ActiveLive
    fc = occ.FutureChain(stem, oci.FutureType.Spread)
    fc.initialize_contracts(status, initialize_data=True)
    start, end = fc.get_start_end(extra_days=True)
    # Commissions - Note: When throwing margin rejects => due to missing data
    cerebro.broker.setcommission(commission=fc.commission * 2,
                                 margin=fc.margin,
                                 mult=fc.point)
    # Data
    end = dt.datetime.today().strftime(
        '%Y-%m-%d'
    ) if mode == Mode.Live else end  # Needed to stop at today's bar
    cerebro = oru.add_reference_data(cerebro, start, end)
    cerebro = oru.add_chain_data(cerebro, [fc])

    return cerebro, fc
Ejemplo n.º 3
0
def test_initialize_data():
    fc = occ.FutureChain('LH', oci.FutureType.Outright)
    fc.initialize_contracts(occ.Status.Expired,
                            filter='H',
                            initialize_data=True)
    test = []
    for ct in fc.contracts:
        test.extend(fc.data[ct]['OI'])
Ejemplo n.º 4
0
def test_get_start_end():
    fc = occ.FutureChain('LH', oci.FutureType.Outright)
    fc.initialize_contracts(occ.Status.Active, initialize_data=True)
    start, end = fc.get_start_end(extra_days=True)
    print('Start: {} - End: {}'.format(start, end))
    assert isinstance(start, str)
    assert isinstance(end, str)
    assert isinstance(dt.datetime.strptime(start, '%Y-%m-%d'), dt.datetime)
    assert isinstance(dt.datetime.strptime(end, '%Y-%m-%d'), dt.datetime)
Ejemplo n.º 5
0
def test_get_dates():
    stem = 'W_'
    fc = occ.FutureChain(stem, oci.FutureType.Spread)
    fc.initialize_contracts(occ.Status.Expired, we_trade=False)
    fc.initialize_data()
    start, end = fc.get_start_end(extra_days=True)
    print('Start: {} - End: {}'.format(start, end))
    calendar = pmc.get_calendar('CME').valid_days(start, end)
    dates = ore.get_dates(ore.Events.SpotLimit, calendar, fc.chain, 2)
    assert isinstance(dates, list)
    assert isinstance(dates[0], dt.date)
    print(dates)
Ejemplo n.º 6
0
def test_chain_last__ticker_filter():
    fc = occ.FutureChain('LB', oci.FutureType.Spread)
    fc.initialize_contracts(occ.Status.Active, initialize_data=True)

    print(fc.last_date(0))
    print(fc.ticker(1))
    assert isinstance(fc.last_date(0), dt.datetime)
    import pandas_market_calendars as pmc
    calendar = pmc.get_calendar('CME').valid_days('2018-01-01', '2018-12-31')
    print(calendar.get_loc(fc.last_date(0)))
    assert isinstance(fc.ticker(1), str)
    contracts = fc.contracts[0:2]
    fc.filter_chain(contracts)
    assert len(fc.contracts) == len(contracts)
Ejemplo n.º 7
0
def generate_tickers_df(stem, status=occ.Status.Active):
    """Generate a DataFrame of symbols for a given stem for data download

    :param stem: str - Customized Stem
    :param status: Enum Status - Status needed for download
    :return: DataFrame with Ticker Column
    """
    log = logging.getLogger(__name__)

    dfs = []
    # Get number of active contracts and number of contracts in between
    # TODO: Populate database.json with values for all contracts!
    try:
        dl = oci.get(stem, 'Download')
        nac, ncb = dl['nac'], dl['ncb']
    except oci.InstrumentError:
        log.warning('{} - Set Download Parameters!'.format(stem))
        nac, ncb = 4, 1  # Default behaviour when no parameters
    params = {'as_dataframe': True, 'data_download': True}
    ot = occ.FutureChain(stem, oci.FutureType.Outright).initialize_contracts(
        status, we_trade=False, nac=nac, **params)
    st = occ.FutureChain(stem, oci.FutureType.Spread).initialize_contracts(
        status,
        we_trade=(False if stem != 'ED' else True),
        nac=nac - 1,
        ncb=ncb,
        **params)
    bt = occ.FutureChain(stem, oci.FutureType.Butterfly).initialize_contracts(
        status, we_trade=True, nac=nac -
        2, ncb=ncb, **params) if stem == 'ED' else None
    dfs.append(ot)
    dfs.append(st)
    if bt is not None:
        dfs.append(bt)
    sdf = pd.concat(dfs)

    return sdf.reset_index(drop=True)
Ejemplo n.º 8
0
def test_filter_chain_contracts():
    fc = occ.FutureChain('LB', oci.FutureType.Spread)
    odf = fc.initialize_contracts(occ.Status.Expired)
    # All the contracts with a specific letter!
    df = fc.initialize_contracts(occ.Status.Expired,
                                 filter='X',
                                 as_dataframe=True)
    print(df.head())
    # Check filtering with an incorrect letter works
    df = fc.initialize_contracts(occ.Status.Expired, filter='Z')
    assert len(odf) == len(df)
    # Last number of contracts
    nfilt = 5
    df = fc.initialize_contracts(occ.Status.Expired,
                                 filter=nfilt,
                                 as_dataframe=True)
    print(df.tail())
    assert len(df) == nfilt
    # Check too high number of contracts ignored
    df = fc.initialize_contracts(occ.Status.Expired, filter=1000)
    assert len(odf) == len(df)
Ejemplo n.º 9
0
def initialize_cerebro(stems, cash, mode, future_type=oci.FutureType.Spread):
    """Initialize Cerebro for a backtest. This is the multi-market version.

    :param stems: str - Customized ticker
    :param cash: float - Portfolio start value
    :param mode: Enum Mode - Which mode to run: Backtest or Live?
    :param future_type: obj FutureType - Which type of Future do we run the backtest on
    :return: object Cerebro, object FutureChain - These are both needed to run a backtest
    """
    log = logging.getLogger(__name__)

    # Setup Cerebro
    cerebro = ob.Backtest.setup_cerebro(cash)
    # Future Chains
    status = occ.Status.Expired if mode == Mode.Backtest else occ.Status.ActiveLive
    chains = []
    min_start = str(dt.date.today())
    max_end = '1900-01-01'
    for stem in stems:
        fc = occ.FutureChain(stem, oci.FutureType.Spread)
        fc.initialize_contracts(status, days_back=90, initialize_data=True)
        try:
            # Get Start/End
            start, end = fc.get_start_end(extra_days=True)
            min_start = min(start, min_start)
            max_end = max(end, max_end)
            # Add chain to the list if start/end ok
            chains.append(fc)
        except occ.ChainError:
            log.error(
                '{} discarded as there is a problem with the chain/data!'.
                format(stem))
    # Data
    end = dt.datetime.today().strftime(
        '%Y-%m-%d'
    ) if mode == Mode.Live else max_end  # Needed to stop at today's bar
    cerebro = oru.add_reference_data(cerebro, min_start, end)
    cerebro = oru.add_chain_data(cerebro, chains, with_comms=True)

    return cerebro, chains
Ejemplo n.º 10
0
def test_future_chain_contracts(stem, future_type):
    status = occ.Status.Expired
    fc = occ.FutureChain(stem, future_type)
    # Check Contracts
    df = fc.initialize_contracts(status, as_dataframe=True)
    print(df)
    assert isinstance(df, pd.DataFrame)
    assert len(df.columns) == 2
    assert set(df.columns) == {'Ticker', 'LastDate'}
    # Temporary - Check len against old table.py module
    # if future_type == ci.FutureType.Outright:
    #    ot = ct.outright_table(stem, status)
    #    print('Comparison: new: {} - old: {}'.format(len(df), len(ot)))
    # elif future_type == ci.FutureType.Spread:
    #    st = ct.spread_table(stem, status)
    #    print('Comparison: new: {} - old: {}'.format(len(df), len(st)))
    # Check Data
    data = fc.initialize_data()
    first_key = list(data.keys())[0]
    print('First Key: {}'.format(first_key))
    print('Data check:\n{}'.format(data[first_key].head()))
    assert isinstance(data[first_key], pd.DataFrame)