예제 #1
0
    def test_specific_assets(self):
        assets = self.asset_finder.retrieve_all(self.ASSET_FINDER_EQUITY_SIDS)

        class SidFactor(CustomFactor):
            """A factor that just returns each asset's sid."""
            inputs = ()
            window_length = 1

            def compute(self, today, sids, out):
                out[:] = sids

        pipe = Pipeline(columns={
            'sid': SidFactor(),
            'evens': StaticAssets(assets[::2]),
            'odds': StaticAssets(assets[1::2]),
            'first_five': StaticAssets(assets[:5]),
            'last_three': StaticAssets(assets[-3:]),
        }, )

        start, end = self.trading_days[[-10, -1]]
        results = self.run_pipeline(pipe, start, end).unstack()

        sids = results.sid.astype(int64_dtype)

        assert_equal(results.evens, ~(sids % 2).astype(bool))
        assert_equal(results.odds, (sids % 2).astype(bool))
        assert_equal(results.first_five, sids < 5)
        assert_equal(results.last_three, sids >= 7)
예제 #2
0
    def test_specific_assets(self):
        assets = self.asset_finder.retrieve_all(self.ASSET_FINDER_EQUITY_SIDS)

        self._check_filters(
            evens=StaticAssets(assets[::2]),
            odds=StaticAssets(assets[1::2]),
            first_five=StaticAssets(assets[:5]),
            last_three=StaticAssets(assets[-3:]),
        )
예제 #3
0
def initialize(context):
    """
    Called once at the start of the algorithm.
    """

    c = context

    c.etf_universe = StaticAssets(
        symbols('XLY', 'XLP', 'XLE', 'XLF', 'XLV', 'XLI', 'XLB', 'XLK', 'XLU'))
    c.alphas = pd.DataFrame()

    # Rebalance every day, 1 hour after market open.
    schedule_function(
        rebalance,
        date_rules.every_day(),
        time_rules.market_open(hours=1),
    )

    # Record tracking variables at the end of each day.
    schedule_function(
        record_vars,
        date_rules.every_day(),
        time_rules.market_close(),
    )

    # Create our dynamic stock selector.
    attach_pipeline(make_pipeline(context), 'pipeline')
    attach_pipeline(make_pipeinit(context), 'pipeinit')

    c.first_trading_day = True
    c.factor_name_list = make_factor().keys()
예제 #4
0
def make_pipeline(assets):
    pipe = Pipeline(
    columns={
        'price': USEquityPricing.close.latest,
    },
    screen=StaticAssets(assets)
                )
    return pipe
def compute_signals():
    signals = MLSignal()
    #     predictions = SignalData.predictions.latest
    return Pipeline(columns={
        'longs': signals.top(N_LONGS, mask=signals > 0),
        'shorts': signals.bottom(N_SHORTS, mask=signals < 0)
    },
                    screen=StaticAssets(assets))
예제 #6
0
def make_pipeline(context):

    screen = StaticAssets(context.universe)

    momentum_factor = MomentumFactor()
    pipe = Pipeline(columns={
        'momentum_score': momentum_factor,
    })
    pipe.set_screen(screen)
    return pipe
예제 #7
0
def make_pipeline():
    russell_universe = StaticAssets(
        symbols(
            ['AAPL', 'AA', 'KKD', 'MON', 'SPY', 'XOM', 'JNJ', 'HD', 'MSFT']))

    filt = AverageDollarVolume(window_length=30, mask=russell_universe) > 10e6

    pipeline = Pipeline(columns={
        "1y_returns": Returns(window_length=252),
    },
                        screen=filt)
    return pipeline
예제 #8
0
def make_pipeline():
    base_universe = StaticAssets(symbols(['AAPL', 'SPY']))

    pipe = Pipeline(columns={
        'close':
        USEquityPricing.close.latest,
        'prev':
        Previous([USEquityPricing.close], window_length=2, mask=base_universe),
        'ret':
        Returns(window_length=2, mask=base_universe),
        'excess_return':
        ExcessReturn(window_length=2, mask=base_universe),
    },
                    screen=base_universe)
    return pipe
    def get_moving_average(self,
                           start_date,
                           end_date,
                           assets=None,
                           window_length=63):
        moving_average = SimpleMovingAverage(inputs=[USEquityPricing.close],
                                             window_length=window_length)

        pipeline = Pipeline(columns={'moving_average': moving_average})

        if assets is not None:
            pipeline.set_screen(StaticAssets(assets))

        df = self._run_pipeline(pipeline, start_date, end_date)

        return df
예제 #10
0
def my_pipeline(context):

    '''
    Define the pipline data columns
    '''
    # Create filter for just the ETFs we want to trade
    universe = StaticAssets(context.MY_ETFS.index)

    # Create any factors we need
    # latest_price is just used in case we don't have current price for an asset
    latest_price = Latest(inputs =[USEquityPricing.close], mask=universe)

    return Pipeline(
            columns = {
                'latest_price' : latest_price,
            },
            screen = universe,
            )
예제 #11
0
def initialize(context):

    spy_tlt = StaticAssets([symbol('AAPL'), symbol('IBM')])

    print(symbol('AAPL'), symbols('AAPL'))

    # Create an arbitrary factor
    price = USEquityPricing.close.latest

    pipe = Pipeline(screen=spy_tlt, columns={
        'price': price,
    })

    attach_pipeline(pipe, 'my_pipeline')

    schedule_function(rebalance, date_rules.every_day())

    pass
def make_pipeline():
    """
    A function to create our dynamic stock selector (pipeline). Documentation
    on pipeline can be found here:
    https://www.quantopian.com/help#pipeline-title
    """

    base_universe = StaticAssets(
        symbols('XLY', 'XLP', 'XLE', 'XLF', 'XLV', 'XLI', 'XLB', 'XLK', 'XLU'))

    # Factor of yesterday's close price.
    yesterday_close = USEquityPricing.close.latest

    pipeline = Pipeline(columns={
        'close': yesterday_close,
    },
                        screen=base_universe)
    return pipeline
예제 #13
0
def make_pipeline(assets):

    WEIGHT1 = 1.0
    WEIGHT2 = 1.0
    WEIGHT3 = 1.0
    WEIGHT4 = 1.0

    etf_universe = StaticAssets(assets)

    day20_ret = Returns(inputs=[USEquityPricing.close],
                        window_length=21,
                        mask=etf_universe)
    day3mo_ret = Returns(inputs=[USEquityPricing.close],
                         window_length=63,
                         mask=etf_universe)
    day6mo_ret = Returns(inputs=[USEquityPricing.close],
                         window_length=126,
                         mask=etf_universe)
    day1yr_ret = Returns(inputs=[USEquityPricing.close],
                         window_length=252,
                         mask=etf_universe)

    volatility = AnnualizedVolatility(mask=etf_universe)
    score = ((WEIGHT1 * day20_ret) + (WEIGHT1 * day3mo_ret) +
             (WEIGHT3 * day6mo_ret) + (WEIGHT3 * day1yr_ret)) / (volatility)

    high = USEquityPricing.high.latest
    low = USEquityPricing.low.latest
    open_price = USEquityPricing.open.latest
    close = USEquityPricing.close.latest
    volume = USEquityPricing.volume.latest

    pipe = Pipeline(columns={
        'Score': score,
        'Day20': day20_ret,
        'high': high,
        'low': low,
        'close': close,
        'open_price': open_price,
        'volume': volume,
    },
                    screen=etf_universe)
    return pipe
    def get_returns(self, start_date, end_date, assets=None):
        open_return = Returns(window_length=2, inputs=[USEquityPricing.open])
        close_return = Returns(window_length=2, inputs=[USEquityPricing.close])
        inter_day_return = InterDayReturns()
        intra_day_return = IntraDayReturns()

        pipeline = Pipeline(
            columns={
                'open_return': open_return,
                'close_return': close_return,
                'inter_day_return': inter_day_return,
                'intra_day_return': intra_day_return
            })

        if assets is not None:
            pipeline.set_screen(StaticAssets(assets))

        df = self._run_pipeline(pipeline, start_date, end_date)
        df['open_return'] = df.groupby(
            df.index.get_level_values('asset'))['open_return'].shift(-1)
        df.dropna(inplace=True)

        return df
def initialize(context):
    context.universe = StaticAssets(symbols(
        'XLY',  # Select SPDR U.S. Consumer Discretionary
        'XLP',  # Select SPDR U.S. Consumer Staples
        'XLE',  # Select SPDR U.S. Energy
        'XLF',  # Select SPDR U.S. Financials
        'XLV',  # Select SPDR U.S. Healthcare
        'XLI',  # Select SPDR U.S. Industrials
        'XLB',  # Select SPDR U.S. Materials
        'XLK',  # Select SPDR U.S. Technology
        'XLU',  # Select SPDR U.S. Utilities
    ))

    #     my_pipe = Pipeline()
    my_pipe = make_pipeline(context)

    # context.universe = StaticAssets(etfs)
    attach_pipeline(my_pipe, 'my_pipeline')

    schedule_function(func=rebalance,
                      date_rule=date_rules.month_end(days_offset=2),
                      # date_rule=date_rules.every_day(),
                      time_rule=time_rules.market_close(minutes=30))
    def get_universe(self,
                     start_date,
                     end_date,
                     assets=None,
                     min_avg_dollar_vol=1e6,
                     min_price=4.0):
        adv = AverageDollarVolume(window_length=20)
        latest_close = USEquityPricing.close.latest

        # min_market_cap = market_cap >= 100e6 # Need market cap data
        min_adv = (adv >= min_avg_dollar_vol)
        min_latest_close = (latest_close >= min_price)
        screen = (min_adv & min_latest_close)

        if assets is not None:
            screen = (StaticAssets(assets) & screen)

        pipeline = Pipeline(columns={'latest_close': latest_close},
                            screen=screen)

        df = self._run_pipeline(pipeline, start_date, end_date)
        df.drop(['latest_close'], axis=1, inplace=True)

        return df
예제 #17
0
import pandas as pd
from zipline.pipeline import Pipeline
from zipline.pipeline.data import USEquityPricing
from sharadar.pipeline.factors import MarketCap
from sharadar.pipeline.factors import EV
from sharadar.pipeline.factors import Fundamentals
from sharadar.pipeline.engine import load_sharadar_bundle, symbol, symbols, make_pipeline_engine
from zipline.pipeline.filters import StaticAssets
import time
import datetime
from zipline.pipeline.factors import CustomFactor, DailyReturns, Returns
from sharadar.pipeline.engine import BundleLoader
from sharadar.pipeline.factors import ForwardsReturns

universe = StaticAssets(symbols(['IBM', 'F', 'AAPL']))
pipe = Pipeline(columns={
    'Close': USEquityPricing.close.latest,
    'monthly_ret': Returns(window_length=2),
    'monthly_fret': ForwardsReturns(window_length=3, mask=universe)
},
                screen=universe)

engine = make_pipeline_engine()
pipe_start = pd.to_datetime('2020-04-06', utc=True)
stocks = engine.run_pipeline(pipe, pipe_start)
print(stocks)
import pandas as pd
from zipline.pipeline import Pipeline
from zipline.pipeline.data import USEquityPricing
from sharadar.pipeline.factors import (MarketCap, EV, Fundamentals,
                                       LogFundamentalsTrend, LogTimeTrend,
                                       InvestmentToAssets,
                                       InvestmentToAssetsTrend)
from sharadar.pipeline.engine import load_sharadar_bundle, symbol, symbols, make_pipeline_engine
from zipline.pipeline.filters import StaticAssets
from zipline.pipeline.factors import CustomFactor
import numpy as np

#universe = StaticAssets(symbols(['IBM', 'F', 'AAPL']))
universe = StaticAssets(symbols(['IBM']))
pipe = Pipeline(columns={
    'ia': InvestmentToAssets(),
    'ia_trend': InvestmentToAssetsTrend(mask=universe).trend
},
                screen=universe)

engine = make_pipeline_engine()
pipe_date = pd.to_datetime('2020-06-01', utc=True)
stocks = engine.run_pipeline(pipe, pipe_date)
print(stocks)

#NO PERIODIC
#                                                      ey  ey_trend  \
#2017-09-07 00:00:00+00:00 Equity(199059 [AAPL])  0.055782  0.000066
#                          Equity(199623 [IBM])   0.085316 -0.000013
#                          Equity(199713 [F])     0.083262  0.000290
예제 #19
0
import pandas as pd
from zipline.pipeline import Pipeline
from zipline.pipeline.data import USEquityPricing
from sharadar.pipeline.engine import symbol, symbols, make_pipeline_engine
from zipline.pipeline.filters import StaticAssets

tickers = symbols(['TR1M', 'TR1Y', 'RATEINF'])
print(tickers)

pipe = Pipeline(columns={
    'Close': USEquityPricing.close.latest,
},
                screen=StaticAssets(tickers))

engine = make_pipeline_engine()
pipe_start = pd.to_datetime('2020-02-03', utc=True)
pipe_end = pd.to_datetime('2020-02-07', utc=True)
stocks = engine.run_pipeline(pipe, pipe_start, pipe_end)
print("stocks.shape [close]", stocks)

print(symbol('TR1M').to_dict())
pipe = Pipeline(columns={
    'close': USEquityPricing.close.latest,
    'adv': AverageDollarVolume(window_length=3),
    'sharesbas_arq': Fundamentals(field='sharesbas_arq'),
    'sharefactor_arq': Fundamentals(field='sharefactor_arq'),
    'mkt_cap': MarketCap(),
    'ev': EV(),
    'debt': Fundamentals(field='debtusd_arq'),
    'cash': Fundamentals(field='cashnequsd_arq'),
    'EvEbit': EvEbit(),
    'EvEbitda': EvEbitda(),
    'PriceBook': PriceBook(),
    'PriceEarnings': PriceEarnings(),
    'PriceSales': PriceSales()
},
                screen=StaticAssets(symbols(['AAPL'])))

pipe_start = pd.to_datetime('2021-03-15', utc=True)
stocks = spe.run_pipeline(pipe, pipe_start)
print(stocks.iloc[0])
assert stocks.iloc[0]['EvEbit'] == 28.90
assert stocks.iloc[0]['EvEbitda'] == 25.20
assert stocks.iloc[0]['PriceBook'] == 31.40
assert stocks.iloc[0]['PriceEarnings'] == 32.60
assert stocks.iloc[0]['PriceSales'] == 7.10
assert stocks.iloc[0]['cash'] == 36010000000.00
assert stocks.iloc[0]['close'] == 121.03
assert stocks.iloc[0]['debt'] == 112043000000.00
assert stocks.iloc[0]['ev'] == 2157589000000.00
assert stocks.iloc[0]['mkt_cap'] == 2081556000000.00
assert stocks.iloc[0]['sharefactor_arq'] == 1.00
from sharadar.pipeline.factors import Exchange, Sector, IsDomesticCommonStock, MarketCap, Fundamentals, EV
from zipline.pipeline.factors import AverageDollarVolume
from sharadar.pipeline.universes import TradableStocksUS
from sharadar.loaders.ingest_macro import ingest

bundle = load_sharadar_bundle()


def ingest_macro():
    start = bundle.equity_daily_bar_reader.first_trading_day
    print("Adding macro data from %s ..." % (start))
    print(ingest(start))


#ingest_macro()

spe = make_pipeline_engine()

pipe_start = pd.to_datetime('2021-01-04', utc=True)
pipe_end = bundle.equity_daily_bar_reader.last_available_dt
macro = symbols([
    'TR3M', 'TR6M', 'TR1Y', 'TR2Y', 'TR3Y', 'TR5Y', 'TR7Y', 'TR10Y', 'TR20Y',
    'CBOND', 'INDPRO', 'INDPROPCT', 'PMICMP', 'UNRATE', 'RATEINF'
])
pipe = Pipeline(columns={
    'Close': USEquityPricing.close.latest,
},
                screen=StaticAssets(macro))
stocks = spe.run_pipeline(pipe, pipe_start, pipe_end)
print(stocks.tail(30))
예제 #22
0
import pandas as pd
from sharadar.pipeline.universes import UniverseWriter, UniverseReader, NamedUniverse
from zipline.pipeline.filters import StaticAssets
from sharadar.pipeline.engine import symbols, make_pipeline_engine
from sharadar.util.output_dir import get_output_dir
import os
from zipline.pipeline import Pipeline
from zipline.pipeline.data import USEquityPricing

pipe_start = pd.to_datetime('2009-02-03', utc=True)
pipe_end = pd.to_datetime('2020-02-07', utc=True)
screen = StaticAssets(symbols(['IBM', 'F', 'AAPL']))

universes_db_path = os.path.join(get_output_dir(), "universes.sqlite")

universe_name = 'my_universe1'
UniverseWriter(universes_db_path).write(universe_name, screen, pipe_start,
                                        pipe_end)

sids = UniverseReader(universes_db_path).get_sid(universe_name, '2020-02-07')
print(sids)

sids = UniverseReader(universes_db_path).get_sid(universe_name, '2002-02-07')
print(sids)

spe = make_pipeline_engine()

pipe = Pipeline(columns={
    'Close': USEquityPricing.close.latest,
},
                screen=NamedUniverse('my_universe1'))
예제 #23
0
def initialize(context):
    dates = pd.date_range('2018-01-01', '2018-09-28')
    # assets = bundle_data.asset_finder.lookup_symbols(['A', 'AAL'], as_of_date=None)
    # assets = bundle_data.asset_finder
    sids = bundle_data.asset_finder.sids
    assets = [sid(item) for item in sids]

    # The values for Column A will just be a 2D array of numbers ranging from 1 -> N.
    column_A_frame = pd.DataFrame(
        data=np.arange(len(dates) * len(assets), dtype=float).reshape(len(dates), len(assets)),
        index=dates,
        columns=sids,
    )

    # Column B will always provide True for 0 and False for 1.
    column_B_frame = pd.DataFrame(data={sids[0]: True, sids[1]: False}, index=dates)

    loaders = {
        MyDataSet.column_A: DataFrameLoader(MyDataSet.column_A, column_A_frame),
        MyDataSet.column_B: DataFrameLoader(MyDataSet.column_B, column_B_frame),
    }

    def my_dispatcher(column):
        return loaders[column]

    # Set up pipeline engine

    # Loader for pricing
    pipeline_loader = USEquityPricingLoader(
        bundle_data.equity_daily_bar_reader,
        bundle_data.adjustment_reader,
    )

    def choose_loader(column):
        if column in USEquityPricing.columns:
            return pipeline_loader
        return my_dispatcher(column)

    engine = SimplePipelineEngine(
        get_loader=choose_loader,
        calendar=trading_calendar.all_sessions,
        asset_finder=bundle_data.asset_finder,
    )

    p = Pipeline(
        columns={
            'price': USEquityPricing.close.latest,
            'col_A': MyDataSet.column_A.latest,
            'col_B': MyDataSet.column_B.latest
        },
        screen=StaticAssets(assets)
    )

    df = engine.run_pipeline(
        p,
        pd.Timestamp('2016-01-07', tz='utc'),
        pd.Timestamp('2016-01-07', tz='utc')
    )

    df = df.sort_values(by=['price'], axis=0, ascending=False)

    print(df)