Exemplo n.º 1
0
    def test_bollinger_bands(self, window_length, k, mask_sid):
        closes = self.closes(mask_sid)
        result = self.run_graph(
            TermGraph({
                'f': BollingerBands(
                    window_length=window_length,
                    k=k,
                ),
            }),
            initial_workspace={
                USEquityPricing.close: AdjustedArray(
                    closes,
                    np.full_like(closes, True, dtype=bool),
                    {},
                    np.nan,
                ),
            },
            mask_sid=mask_sid,
        )['f']

        expected_upper, expected_middle, expected_lower = self.expected(
            window_length,
            k,
            closes,
        )

        assert_equal(result.upper, expected_upper)
        assert_equal(result.middle, expected_middle)
        assert_equal(result.lower, expected_lower)
Exemplo n.º 2
0
    def test_bollinger_bands(self, window_length, k, mask_last_sid):
        closes = self.closes(mask_last_sid=mask_last_sid)
        mask = ~np.isnan(closes)
        bbands = BollingerBands(window_length=window_length, k=k)

        expected = self.expected_bbands(window_length, k, closes)

        self.check_terms(
            terms={
                'upper': bbands.upper,
                'middle': bbands.middle,
                'lower': bbands.lower,
            },
            expected={
                'upper': expected[0],
                'middle': expected[1],
                'lower': expected[2],
            },
            initial_workspace={
                USEquityPricing.close: AdjustedArray(
                    data=closes,
                    mask=mask,
                    adjustments={},
                    missing_value=np.nan,
                ),
            },
            mask=self.build_mask(mask),
        )
Exemplo n.º 3
0
def make_pipeline():
    all_assets_filter = (USEquityPricing.close.latest > 0)
    returns_5 = Returns(window_length=5)
    rsi = RSI(inputs=[USEquityPricing.close])
    macd = MovingAverageConvergenceDivergenceSignal(mask=all_assets_filter)
    ema = ExponentialWeightedMovingAverage(mask=all_assets_filter,
                                           inputs=[USEquityPricing.close],
                                           window_length=30,
                                           decay_rate=(1 - (2.0 / (1 + 15.0))))
    mean_5 = SimpleMovingAverage(inputs=[USEquityPricing.close],
                                 window_length=5,
                                 mask=all_assets_filter)
    mean_10 = SimpleMovingAverage(inputs=[USEquityPricing.close],
                                  window_length=10,
                                  mask=all_assets_filter)
    bb = BollingerBands(inputs=[USEquityPricing.close], window_length=20, k=2)
    return Pipeline(
        columns={
            'returns_5': returns_5,
            'RSI': rsi,
            'MACD': macd,
            'EMA': ema,
            'SMA_5': mean_5,
            'SMA_10': mean_10,
            'bb_upper': bb.upper,
            'bb_middle': bb.middle,
            'bb_lower': bb.lower
        },
        screen=all_assets_filter,
    )
Exemplo n.º 4
0
def make_pipeline():
    """
    Create our pipeline.
    """

    # Base universe set to the Q1500US.

    base_universe = AverageDollarVolume(window_length=20).percentile_between(70, 100)

    latest_close = USEquityPricing.close.latest

    # 14-day RSI
    rsi_14 = RSI(
        inputs=[USEquityPricing.close],
        window_length=14,
        mask=base_universe
    )

    # 20-day, 2 standard deviation Bollinger bands
    bbLow, bbMid, bbHigh = BollingerBands(
        inputs=[USEquityPricing.close],
        window_length=10,
        k=2,
        mask=base_universe
    )

    rsiOverbought = rsi_14 > 70
    rsiOversold = rsi_14 < 30

    bbOverbought = latest_close > bbHigh
    bbOversold = latest_close < bbLow

    overbought = rsiOverbought | bbOverbought
    oversold = rsiOversold | bbOversold

    # Filter to select securities to short.
    shorts = overbought

    # Filter to select securities to long.
    longs = oversold

    # Filter for all securities that we want to trade.
    securities_to_trade = (shorts & longs)

    return Pipeline(
        columns={
            'longs': longs,
            'shorts': shorts
        },
        screen=(securities_to_trade),
    )
Exemplo n.º 5
0
 def test_bollinger_bands_output_ordering(self):
     bbands = BollingerBands(window_length=5, k=2)
     lower, middle, upper = bbands
     self.assertIs(lower, bbands.lower)
     self.assertIs(middle, bbands.middle)
     self.assertIs(upper, bbands.upper)
Exemplo n.º 6
0
 def test_bollinger_bands_output_ordering(self):
     bbands = BollingerBands(window_length=5, k=2)
     lower, middle, upper = bbands
     assert lower is bbands.lower
     assert middle is bbands.middle
     assert upper is bbands.upper
Exemplo n.º 7
0
def make_pipeline(underly_sid, shortable_sid):#,DAY_HACK_ENABLED=True,BB_WIN_LEN=126,WIN_LEN=20):
    sid = SidFactor() 
    pipeline_underly_sid = sid.eq(underly_sid[0])
    print("jzzz:", type(pipeline_underly_sid))
    print("MNAME:", MNAME)
    for i in underly_sid:
        pipeline_underly_sid = pipeline_underly_sid | sid.eq(i)
        print('jzzz:', pipeline_underly_sid)
    pipeline_shortable_sid = sid.eq(shortable_sid[0])
    for i in shortable_sid:
        pipeline_shortable_sid  = pipeline_shortable_sid  | sid.eq(i)
    #Filter for our choice of assets.
    bbands = BollingerBands(window_length=BB_WIN_LEN, k=sqrt(BB_WIN_LEN/5),mask = pipeline_underly_sid)
    lower, middle, upper = bbands
    #cud = Cud()#mask(pipeline underly_sid)
    returns_1D = Returns(window_length = 2, mask = pipeline_underly_sid)
    returns_6M = Returns(window_length = WIN_LEN, mask = pipeline_underly_sid)
    returns_6M = returns_6M if DAY_HACK_ENABLED else returns_6M - returns_1D
    returns_3M = Returns(window_length = 60, mask = pipeline_underly_sid)
    returns_3M = returns_3M if DAY_HACK_ENABLED else returns_3M - returns_1D
    returns_5M = Returns(window_length = WIN_LEN, mask = pipeline_underly_sid)#for es only
    returns_5M = returns_5M if DAY_HACK_ENABLED else returns_5M - returns_1D
    
    returns_1M = Returns(window_length = 20, mask = pipeline_underly_sid)#for es only
    returns_1M = returns_1M if DAY_HACK_ENABLED else returns_1M - returns_1D
    
    returns_5D = Returns(window_length =W_WIN_LEN,mask = pipeline_underly_sid)
    returns_5D = returns_5D if DAY_HACK_ENABLED else returns_5D - returns_1D
    returns_6D = Returns(window_length =7,mask = pipeline_underly_sid) - returns_1D
    annualizedVolatility_6M = AnnualizedVolatility(window_length = WIN_LEN,mask = pipeline_underly_sid)
    annualizedVolatility_3M = AnnualizedVolatility(window_length = int(WIN_LEN/2), mask = pipeline_underly_sid)
    annualizedVolatility_1M = AnnualizedVolatility(window_length = int(WIN_LEN/6), mask = pipeline_underly_sid)
    #annualizedVolatility_6M = ComputeVol6M(mask = pipeline_underly_sid)
    #annualizedVolatility_3M = ComputeVol3M(mask = pipeline_underly_sid)
    #annualizedVolatility_1M = ComputeVol1M(mask = pipeline_underly_sid)
    annualizedVolatility = max(annualizedVolatility_6M, annualizedVolatility_3M,annualizedVolatility_1M)
    sharpes_6M = returns_6M/annualizedVolatility_6M
    raw_signals = None
    if SHARPE:
        raw_signals = sharpes_6M
    else:
        raw_signals = returns_6M
    signals = raw_signals if LONG_ONLY else (raw_signals*raw_signals)
    #positive_return = (returns_6M > 0)
    #trigger = (returns 5D > -trigger_level) if LONG_ONLY else ((returns_5D < trigger_level)

    sigtop = signals.top(NUM_TOP_POSITIONS)
    raw_sig_top = raw_signals.top(NUM_TOP_POSITIONS)
    raw_sig_bot = raw_signals.bottom(NUM_TOP_POSITIONS)

    alongs = sigtop & raw_sig_top & (raw_signals > 0) & (returns_5D > -TRIGGER_LEVEL) # & (cud >= 13)
    ashorts = sigtop & raw_sig_bot &(raw_signals <= 0 ) & (returns_5D < TRIGGER_LEVEL) & pipeline_shortable_sid
    #alongs = (annualizedVolatility_6M> 0) #shorts# signals.top(NUN TOP POSITIONS)

    if not PIPE_ENABLED:
        alongs = (annualizedVolatility_6M > 0)
        ashorts = (annualizedVolatility_6M < 0)# if LONG ONLY else longs
    long_short_screen = alongs if LONG_ONLY else (alongs | ashorts)

    my_screen = (annualizedVolatility_6M > 0) if MNAME in ['es','bd','dm','ed','md']  else long_short_screen 
    #my_screen = long_short_screen if False else (annualizedVolatility_6M > 0)#hlw
    print('finger1',MNAME)
    rsi = RSI(window_length=10,mask = pipeline_underly_sid)
    print('rsipipe',annualizedVolatility_6M)
    print('fxxxDAY_HACK_ENABLED',DAY_HACK_ENABLED)
    #Only compute volatilities for assets that satisfy the requirements of our rolling strategy.
    return Pipeline(
        columns= {
            #'cud' : cud,
            'volatility' : annualizedVolatility,
            'sharpes_6W' : sharpes_6M,
            'drets': returns_1D,
            'wrets': returns_5D,
            'returns_6D': returns_6D,
            'returns_1M': returns_1M,
            'returns_3M': returns_3M,
            'returns_5M': returns_5M,
            'returns_6M': returns_6M,
            #'signals': (signals)*signals,
           'shorts' : ashorts,
            #'wrets' : returns_5D,
            'lower' : lower,
            'middle' :middle,
            'upper' :upper,
            #'rsi':rsi.top(3),
            'rsi_longs': rsi.top(3),
            'rsi_shorts': rsi.bottom(3),
            #'annualizedVolatility :annualizedVolatility_6M,
        },
        screen = my_screen
    )