Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
View limit minimum error and problem analyzing data

Hi All,

I am currently attempting to build my first trading model using HHV, Short_Ema, Long_Ema and ATR as indicators for my trades. If the 50 EMA is higher than the 200 day EMA, the stock is appended to my stock list, then if the close breaks the 50-day HHV, the stock is purchased.

I've managed to write the buying functions, and the handle data section of the algorithm, but I've been having issues with the analyzing part of my algorithm. Please find attached below my code, any and all feedback is greatly appreciated.

Thanks a lot!

ticker_list = 'AAPL'  
portfolio_size = 3000  
close = DataReader(ticker_list,'yahoo', startdate, enddate)['Close']  
high = DataReader(ticker_list,'yahoo', startdate, enddate)['High']  
low = DataReader(ticker_list,'yahoo', startdate, enddate)['Low']

def initialize(context):  
    context.time = 0  
    context.asset = symbol(SELECTED_STOCK)  
    context.set_commission(commission.PerShare(cost=0.0, min_trade_cost=0))  
    context.has_position = False  
def handle_data(context, data):  
    context.time += 1  
    if context.time < period:  
        return

    hhv = ta.MAX(close, timeperiod=period)  
    llv = ta.MIN(close, timeperiod=period)  
    short_ema = ta.EMA(close, timeperiod=50)  
    long_ema= ta.EMA(close, timeperiod=200)  
    triple_atr = 3*(ta.ATR(high, low, close, timeperiod=14))  
    context.stock_list = []  


    # Stock selection logic  
    for ticker in ticker_list:  
        if (short_ema > long_ema) & (close > short_sma):  
            context.stock_list.append(ticker)  
    # Trading logic  
    for stock in context.stock_list:  
        if (hhv > close) & (not context.has_position):  
            order_percent(context.asset, 1.0)  
            context.has_position = True  
        elif (close < triple_atr) & (context.has_position):  
            order_target(context.asset, 0)  
            context.has_position = False

    record(price=data.current(context.asset, 'Close'),  
           hhv=hhv,  
           llv=llv)

def analyze(context, perf):  
    fig, ax = plt.subplots(3, 1, sharex=True, figsize=[16, 9])

    #portfolio value  
    perf.portfolio_value.plot(ax=ax[0])  
    ax[0].set_ylabel('portfolio value in $')  
    # asset  
    perf.plot(ax=ax[1])  
    ax[1].set_ylabel('price in $')  
    # mark transactions  
    perf_trans = perf.loc[[t != [] for t in perf.transactions]]  
    buys = perf_trans.loc[[t[0]['amount'] > 0 for t in perf_trans.transactions]]  
    sells = perf_trans.loc[[t[0]['amount'] < 0 for t in perf_trans.transactions]]  
    ax[1].plot(buys.index, perf.price.loc[buys.index], '^', markersize=10, color='g', label='buy')  
    ax[1].plot(sells.index, perf.price.loc[sells.index], 'v', markersize=10, color='r', label='sell')  
    ax[1].legend()  
    # daily returns  
    perf.returns.plot(ax=ax[2])  
    ax[2].set_ylabel('daily returns')

    fig.suptitle('HHV and LLV Strategy - Apple', fontsize=16)  
    plt.legend()  
    plt.show()  
np.seterr(divide='ignore', invalid='ignore')

start= pd.to_datetime('2016-08-01', utc=True) #date column -> pd.to_datetime?  
end=pd.to_datetime('2020-08-12', utc=True) #date column -> pd.to_datetime?  
perf = zipline.run_algorithm(  
    start=start, end=end,  
    initialize=initialize,  
    analyze=analyze,  
    capital_base=portfolio_size,  
    data_frequency = 'daily',  
    bundle='quandl')