Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Code Not Running - AssertionError: open has wrong dimensions

Greetings! When I try to run the following code, I get the following message:

AssertionError: open has wrong dimensions  
There was a runtime error on line 40.

Line 40 is ----> hammer = tb.CDLHAMMER(open, high, low, close)

from quantopian.algorithm import attach_pipeline, pipeline_output  
from quantopian.pipeline import Pipeline  
from quantopian.pipeline.data.builtin import USEquityPricing  
from quantopian.pipeline.factors import AverageDollarVolume  
from quantopian.pipeline.filters.morningstar import Q1500US  
from datetime import datetime  
import numpy as np  
import pandas as pd  
from pandas import Series, DataFrame  
from scipy import stats as stats  
import sklearn  
import math as m  
import pytz  
import talib as tb  
import pprint



def initialize(context):  

    context.stocks_1 = symbols('ABT', 'ABBV', 'ACN', 'ACE', 'ADBE', 'ADT', 'AAP', 'AES', 'AET', 'AFL', 'AMG', 'A', 'GAS', 'APD', 'ARG', 'AKAM', 'AA', 'AGN', 'ALXN', 'ALLE', 'ADS', 'ALL', 'ALTR', 'MO', 'AMZN', 'AEE', 'AAL', 'AEP', 'AXP', 'AIG', 'AMT', 'AMP', 'ABC', 'AME', 'AMGN', 'APH', 'APC', 'ADI', 'AON', 'APA', 'AIV', 'AMAT', 'ADM', 'AIZ', 'T', 'ADSK', 'ADP', 'AN', 'AZO', 'AVGO', 'AVB', 'AVY', 'BHI', 'BLL', 'BAC', 'BK', 'BCR', 'BXLT', 'BAX', 'BBT', 'BDX', 'BBBY', 'BRK_B', 'BBY', 'BLX', 'HRB', 'BA', 'BWA', 'BXP', 'BMY', 'BRCM', 'BF-B', 'CHRW', 'CA', 'CVC', 'COG', 'CAM', 'CPB', 'COF', 'CAH', 'HSIC', 'KMX', 'CCL', 'CAT', 'CBG', 'CBS', 'CELG', 'CNP', 'CTL', 'CERN', 'CF', 'SCHW', 'CHK', 'CVX', 'CMG', 'CB', 'CI', 'XEC', 'CINF', 'CTAS', 'CSCO', 'C', 'CTXS', 'CLX', 'CME', 'CMS', 'COH', 'KO', 'CCE', 'CTSH', 'CL', 'CMCSA', 'CMA', 'CSC', 'CAG', 'COP', 'CNX', 'ED', 'STZ', 'GLW', 'COST', 'CCI', 'CSX', 'CMI', 'CVS', 'DHI', 'DHR', 'DRI', 'DVA', 'DE', 'DLPH', 'DAL', 'XRAY', 'DVN', 'DO', 'DTV', 'DFS', 'DISCA', 'DISCK', 'DG', 'DLTR', 'D', 'DOV', 'DOW', 'DPS', 'DTE', 'DD', 'DUK', 'DNB', 'ETFC', 'EMN', 'ETN', 'EBAY', 'ECL', 'EIX', 'EW', 'EA', 'EMC', 'EMR', 'ENDP', 'ESV', 'ETR', 'EOG', 'EQT', 'EFX', 'EQIX', 'EQR', 'ESS', 'EL', 'ES', 'EXC', 'EXPE', 'EXPD', 'ESRX', 'XOM', 'FFIV', 'FB', 'FAST', 'FDX', 'FIS', 'FITB', 'FSLR', 'FE', 'FLIR', 'FLS', 'FLR', 'FMC', 'FTI', 'F', 'FOSL', 'BEN', 'FCX', 'FTR', 'GME', 'GPS', 'GRMN', 'GD', 'GE', 'GGP', 'GIS', 'GM', 'GPC', 'GNW', 'GILD', 'GS', 'GT', 'GOOG', 'GWW', 'HAL', 'HBI', 'HOG', 'HAR', 'HRS', 'HIG', 'HAS', 'HCA', 'HCP', 'HCN', 'HP', 'HES', 'HPQ', 'HD', 'HON', 'HRL', 'HSP', 'HST', 'HCBK', 'HUM', 'HBAN', 'ITW', 'IR', 'INTC', 'ICE', 'IBM', 'IP', 'IPG', 'IFF', 'INTU', 'ISRG', 'IVZ', 'IRM', 'JEC', 'JBHT', 'JNJ', 'JCI', 'JOY', 'JPM')  

    context.stocks_2 = symbols('JNPR', 'KSU', 'K', 'KEY', 'GMCR', 'KMB', 'KIM', 'KMI', 'KLAC', 'KSS', 'KRFT', 'KR', 'LB', 'LLL', 'LH', 'LRCX', 'LM', 'LEG', 'LEN', 'LVLT', 'LUK', 'LLY', 'LNC', 'LLTC', 'LMT', 'L', 'LOW', 'LYB', 'MTB', 'MAC', 'M', 'MNK', 'MRO', 'MPC', 'MAR', 'MMC', 'MLM', 'MAS', 'MA', 'MAT', 'MKC', 'MCD', 'MHFI', 'MCK', 'MJN', 'MMV', 'MDT', 'MRK', 'MET', 'KORS', 'MCHP', 'MU', 'MSFT', 'MHK', 'TAP', 'MDLZ', 'MON', 'MNST', 'MCO', 'MS', 'MOS', 'MSI', 'MUR', 'MYL', 'NDAQ', 'NOV', 'NAVI', 'NTAP', 'NFLX', 'NWL', 'NFX', 'NEM', 'NWSA', 'NEE', 'NLSN', 'NKE', 'NI', 'NE', 'NBL', 'JWN', 'NSC', 'NTRS', 'NOC', 'NRG', 'NUE', 'NVDA', 'ORLY', 'OXY', 'OMC', 'OKE', 'ORCL', 'OI', 'PCAR', 'PLL', 'PH', 'PDCO', 'PAYX', 'PNR', 'PBCT', 'POM', 'PEP', 'PKI', 'PRGO', 'PFE', 'PCG', 'PM', 'PSX', 'PNW', 'PXD', 'PBI', 'PCL', 'PNC', 'RL', 'PPG', 'PPL', 'PX', 'PCP', 'PCLN', 'PFG', 'PG', 'PGR', 'PLD', 'PRU', 'PEG', 'PSA', 'PHM', 'PVH', 'QRVO', 'PWR', 'QCOM', 'DGX', 'RRC', 'RTN', 'O', 'RHT', 'REGN', 'RF', 'RSG', 'RAI', 'RHI', 'ROK', 'COL', 'ROP', 'ROST', 'R', 'CRM', 'SNDK', 'SCG', 'SLB', 'SNI', 'STX', 'SEE', 'SRE', 'SHW', 'SIAL', 'SPG', 'SWKS', 'SLG', 'SJM', 'SNA', 'SO', 'LUV', 'SWN', 'SE', 'STJ', 'SWK', 'SPLS', 'SBUX', 'HOT', 'STT', 'SRCL', 'SYK', 'STI', 'SYMC', 'SYY', 'TROW', 'TGT', 'TEL', 'TE', 'TGNA', 'THC', 'TDC', 'TSO', 'TXN', 'TXT', 'HSY', 'TRV', 'TMO', 'TIF', 'TWX', 'TWC', 'TMK', 'TSS', 'TSCO', 'RIG', 'TRIP', 'FOXA', 'TSN', 'TYC', 'UA', 'UNP', 'UNH', 'UPS', 'URI', 'UTX', 'UHS', 'UNM', 'URBN', 'VFC', 'VLO', 'VAR', 'VTR', 'VRSN', 'VZ', 'VRTX', 'VIAB', 'V', 'VNO', 'VMC', 'WMT', 'WBA', 'DIS', 'WM', 'WAT', 'ANTM', 'WFC', 'WDC', 'WU', 'WY', 'WHR', 'WFM', 'WMB', 'WEC', 'WYN', 'WYNN', 'XEL', 'XRX', 'XLNX', 'XL', 'XYL', 'YHOO', 'YUM', 'ZBH', 'ZION', 'ZTS')


def before_trading_start(context, data):  

    for stock in context.stocks_1:  
        open = data.history(context.stocks_1, 'open', 252, '1d')  
        high = data.history(context.stocks_1, 'high', 252, '1d')  
        low = data.history(context.stocks_1, 'low', 252, '1d')  
        close = data.history(context.stocks_1, 'close', 252, '1d')  

        open = np.array(open, dtype=float)  
        high = np.array(high, dtype=float)  
        low = np.array(low, dtype=float)  
        close = np.array(close, dtype=float)

        hammer = tb.CDLHAMMER(open, high, low, close)  
        dragon_fly_doji = tb.CDLDRAGONFLYDOJI(open, high, low, close)  

        return (hammer, dragon_fly_doji)  

    for stock in context.stocks_2:  
        open = data.history(context.stocks_2, 'open', 252, '1d')  
        high = data.history(context.stocks_2, 'high', 252, '1d')  
        low = data.history(context.stocks_2, 'low', 252, '1d')  
        close = data.history(context.stocks_2, 'close', 252, '1d')  

        open = np.array(open, dtype=float)  
        high = np.array(high, dtype=float)  
        low = np.array(low, dtype=float)  
        close = np.array(close, dtype=float)  

        shooting_star = tb.CDLSHOOTINGSTAR(open, high, low, close)  
        gravestone_doji = tb.CDLGRAVESTONEDOJI(open, high, low, close)  

        return (shooting_star, gravestone_doji)  


def handle_data(context, data):  

    context.stocks_total = [ ]  
    context.stocks_total.extend(context.stocks_1, context.stocks_2)  
    current_price = data[context.stocks_total].price  
    current_positions = context.portfolio.positions[context.stocks_total].amount  
    cash = context.portfolio.cash


    for stock in context.stocks_total:  

        candle_buy = [hammer, dragon_fly_doji]  
        candle_sell = [shooting_star, gravestone_doji]  

        if candle_buy in context.stocks_total:  

            num_of_shares = int(cash/current_price)  
            order(context.stocks_total, num_of_shares)  
            log.info('Buying Call Options')  


        elif candle_sell in context.stocks_total:  

            num_of_shares = int(cash/current_price)  
            order(context.stocks_total, num_of_shares)  
            log.info('Buying Put Options')  

        else:  

            pass

record leverage here

1 response

The talib functions expect a 1 dimensioned series as their input. In this case 'open' is a 2 dimensioned DataFrame which is indexed by date, and has all the assets in context.stocks_1 as columns. The error is simply stating that the 'open' parameter is expecting a single dimensioned series and has the wrong dimensions (ie a lot more than 1). You will have the same error with the other parameters and with the other talib function too. Maybe look at the talib documentation https://mrjbq7.github.io/ta-lib/func.html .

I'd suggest using pipeline and create a custom factor for each of your talib functions. Especially true since you are looking at daily data only. Take a look at this post for details on how to do that https://www.quantopian.com/posts/using-ta-lib-functions-in-pipeline .

Good luck.