Example #1
0
def test_engine(refdata, stocks, alpaca_tradeapi):
    def list_symbols():
        return ['A', 'AA']

    mock_iex.get_available_symbols(refdata)
    mock_iex.get_key_stats(stocks)
    mock_iex.get_chart(stocks)
    mock_tradeapi.list_assets(alpaca_tradeapi)

    eng = LivePipelineEngine(list_symbols)
    ADV = AverageDollarVolume(window_length=20, )
    top5 = ADV.top(5, groupby=Sector())
    pipe = Pipeline({
        'top5': top5,
        'close': USEquityPricing.close.latest,
    },
                    screen=top5)

    df = eng.run_pipeline(pipe)
    assert sorted(df.columns) == ['close', 'top5']

    pipe = Pipeline({
        'close': USEquityPricing.close.latest,
    })

    df = eng.run_pipeline(pipe)
    assert df.shape == (2, 1)
    assert df.close['AA'] > 40
def initialize(context):
    # The initialize method is called at the very start of your script's
    # execution. You can set up anything you'll be needing later here. The
    # context argument will be received by all pylivetrader methods in
    # your script, and you can store information on it that you'd like to
    # share between methods, or in later trades

    # let's create our pipeline and attach it to pylivetrader execution
    top5 = AverageDollarVolume(window_length=20).top(5)
    pipe = Pipeline({
        'close': USEquityPricing.close.latest,
    }, screen=top5)

    # this line connects the pipeline to pylivetrader. this is done once,
    # and we get a new and it's stored in the context. we will get a fresh list
    # of assets every morning in before_trading_start()
    context.attach_pipeline(pipe, "pipe")
Example #3
0
def make_pipeline(context):
    advFilter = curVsAvgVolFilter(context.params.get('lookback'))
    # midToLargeFilter = isMidToLargeCap(context.params.get('lookback'))
    smaSlow = SimpleMovingAverage(
        inputs=[USEquityPricing.close],
        window_length=context.params.get('smaSlowLookback'))
    smaFast = SimpleMovingAverage(
        inputs=[USEquityPricing.close],
        window_length=context.params.get('smaFastLookback'))
    top50 = AverageDollarVolume(window_length=20).top(50)

    pipe = Pipeline(screen=top50)
    pipe.add(advFilter, 'advFilter')
    pipe.add(smaSlow, 'smaSlow')
    pipe.add(smaFast, 'smaFast')
    # pipe.add(midToLargeFilter, 'midToLargeFilter')

    return pipe
from pipeline_live.engine import LivePipelineEngine
from pipeline_live.data.sources.iex import list_symbols
from pipeline_live.data.alpaca.pricing import USEquityPricing
from pipeline_live.data.alpaca.factors import AverageDollarVolume
from pipeline_live.data.polygon.fundamentals import PolygonCompany
from zipline.pipeline import Pipeline
from dotenv import load_dotenv

load_dotenv()

engine = LivePipelineEngine(list_symbols)
top5 = AverageDollarVolume(window_length=20).top(5)
pipe = Pipeline(
    {
        "close": USEquityPricing.close.latest,
        "marketcap": PolygonCompany.marketcap.latest,
    },
    screen=top5,
)

result = engine.run_pipeline(pipe)
print(result)
Example #5
0
def make_pipeline(context):
    """
    Create our pipeline.
    """

    # Filter for primary share equities. IsPrimaryShare is a built-in filter.
    primary_share = IsPrimaryShare()

    # Not when-issued equities.
    #not_wi = ~IEXCompany.symbol.latest.endswith('.WI')
    not_wi = ~PolygonCompany.symbol.latest.endswith(".WI")

    # Equities without LP in their name, .matches does a match using a regular
    # expression
    #not_lp_name = ~IEXCompany.companyName.latest.matches('.* L[. ]?P.?$')
    not_lp_name = ~PolygonCompany.name.latest.matches(".* L[. ]?P.?$")

    # Equities whose most recent Morningstar market cap is not null have
    # fundamental data and therefore are not ETFs.
    #have_market_cap = IEXKeyStats.marketcap.latest >= 1
    have_market_cap = PolygonCompany.marketcap.latest >= 1

    # At least a certain price
    price = USEquityPricing.close.latest
    AtLeastPrice = (price >= context.MyLeastPrice)
    AtMostPrice = (price <= context.MyMostPrice)

    # Filter for stocks that pass all of our previous filters.
    tradeable_stocks = (primary_share
                        & not_wi
                        & not_lp_name
                        & have_market_cap
                        & AtLeastPrice
                        & AtMostPrice)

    LowVar = 6
    HighVar = 40

    log.info('''
Algorithm initialized variables:
 context.MaxCandidates %s
 LowVar %s
 HighVar %s''' % (context.MaxCandidates, LowVar, HighVar))

    # High dollar volume filter.
    base_universe = AverageDollarVolume(
        window_length=20,
        mask=tradeable_stocks).percentile_between(LowVar, HighVar)

    # Short close price average.
    ShortAvg = SimpleMovingAverage(inputs=[USEquityPricing.close],
                                   window_length=3,
                                   mask=base_universe)

    # Long close price average.
    LongAvg = SimpleMovingAverage(inputs=[USEquityPricing.close],
                                  window_length=45,
                                  mask=base_universe)

    percent_difference = (ShortAvg - LongAvg) / LongAvg

    # Filter to select securities to long.
    stocks_worst = percent_difference.bottom(context.MaxCandidates)
    securities_to_trade = (stocks_worst)

    return Pipeline(
        columns={'stocks_worst': stocks_worst},
        screen=(securities_to_trade),
    )