Back to Community
Hull Moving Average/Top Percentage Gainers Algo

Hello everyone,
I'm not very fluent in Python, but I'm also very tired and distracted right now so I'm looking for another set of eyes to help me with identifying the issues with this algo. I'm getting two errors... an "Inputs all NaN" Exception from the HMA declaration after 'wmaDiffs' and I'm also getting the error:
"KeyError: Equity(27479, symbol=u'GNK', asset_name=u'GENCO SHIPPING & TRADING LTD', exchange=u'NEW YORK STOCK EXCHANGE', start_date=Timestamp('2005-07-22 00:00:00+0000', tz='UTC'), end_date=Timestamp('2016-08-01 00:00:00+0000', tz='UTC'), first_traded=None, auto_close_date=Timestamp('2016-08-04 00:00:00+0000', tz='UTC'))" I know that the key error is probably from missing data between the data arrays but I'm not exactly sure how to fix it. If anyone can help, I would greatly appreciate it:

from quantopian.pipeline import Pipeline, CustomFactor  
from quantopian.algorithm import attach_pipeline, pipeline_output  
from quantopian.pipeline.data.builtin import USEquityPricing  
from quantopian.pipeline.factors import SimpleMovingAverage, AverageDollarVolume, Latest  
import talib  
import numpy as np  
import pandas as pd  
import math

HMAPeriods   = 42  
PricePeriods = 17

class PercentChanged(CustomFactor):  
inputs = [USEquityPricing.close]  
window_length = 5

def compute(self, today, assets, out, close):  
    out[:] = (close[-1] - close[0]) / close[0]  

class MAType():  
SMA   = 0; EMA   = 1; WMA   = 2; DEMA  = 3; TEMA  = 4;  
TRIMA = 5; KAMA  = 6; MAMA  = 7; T3    = 8  

def initialize(context):  
context.LOW_RSI =50  
changeInterval = 30  


pennyStocksMask = (USEquityPricing.close.latest) > 1.0  
overPricedMask = (USEquityPricing.close.latest) < 20.0

percentageChange = PercentChanged(window_length = changeInterval, mask = pennyStocksMask and overPricedMask)  
price = Latest(inputs=[USEquityPricing.close],window_length=1)  
pipe_screen = ((price > 1.0) & (price < 20.0))

pipe_columns = {'price':price, 'percentageChange':percentageChange}

pipe = Pipeline(columns=pipe_columns,screen=pipe_screen)  
attach_pipeline(pipe, 'example')  

schedule_function(rebalance, date_rules.every_day(), time_rules.market_open())  


def before_trading_start(context, data):  
output = pipeline_output('example')  
context.my_securities = output.sort('percentageChange', ascending=False).iloc[:50]  
print len(context.my_securities)

context.security_list = context.my_securities.index  

log.info("\n" + str(context.my_securities.head(5)))  

def rebalance(context, data):  

# Load historical data for the stocks  
prices = data.history(context.security_list, 'price', 40, '1d')  

rsis = {}  

# Iterate over the list of stocks  
for stock in context.security_list:  
   closes   = data.history(context.security_list, 'price', 40, '1d')  
   closes   = closes.dropna(axis=1)  
   valid   = [sid for sid in closes if sid in data]  
   closes   = closes[valid]  
   wmaA    = closes.apply(talib.MA,   timeperiod = HMAPeriods / 2, matype = MAType.WMA).dropna() * 2.0  
   wmaB     = closes.apply(talib.MA,   timeperiod = HMAPeriods, matype = MAType.WMA).dropna()  
   wmaDiffs = wmaA - wmaB  
   hma      = wmaDiffs.apply(talib.MA, timeperiod = math.sqrt(HMAPeriods), matype = MAType.WMA)

   rsi = talib.RSI(prices[stock], timeperiod=14)[-1]  
   rsis[stock] = rsi  
   current_position = context.portfolio.positions[stock].amount  

   if hma[stock][-1] < hma[stock][-2] and current_position > 0 and data.can_trade(stock):  
        order_target(stock, 0)  

   elif hma[stock][-1] > hma[stock][-2] and rsi < context.LOW_RSI and current_position == 0 and data.can_trade(stock):  
        order_target_percent(stock, 1)  
6 responses

Is the version you're running accurately indented? The one you're showing here is not.

Yes, my Algo is indented properly. It's too bad I can't send a failed backtest or pictures of my program.

Hello James - if you click the "attach" button you can include your "full backtest" and we can clone that backtest to reproduce the error.

I hope that helps.

Attach button: http://screencast.com/t/zYWZOdnC3dfd

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Sorry... I thought you could only attach a backtest if the backtest was successful.

Clone Algorithm
24
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 579fc74c4c7da50ffff58fbb
There was a runtime error.

Hi James, there are a few issues here:

  • When you set valid = [sid for sid in closes if sid in data] on line 65:
    • closes is a DataFrame with Equities as the column labels. To iterate through these Equities, iterate through closes.columns.
    • Checking if an Equity is in data is deprecated. Instead, you should check using data.can_trade, as explained here.
      So the line should look like this:
valid = [equity for equity in closes.columns if data.can_trade(equity)]  
  • When you assign wmaB on line 68, you get nothing back. This is because you're trying to compute a moving average with lookback length HMAPeriods (which equals 42), and you only have 40 days of price data. So either reduce HMAPeriods under 40 or increase your data.history call to give more than 42 days of data.
Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Thank you very much for your help! It seems to be working, but only for small backtest periods. However, I am still receiving a similar KEYERROR message for longer backtest lengths. I have attached the backtest.

Clone Algorithm
24
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 57a9140d998d81101254cb58
There was a runtime error.