Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
First algorithm: maximizing alpha

Hi Everyone,

As I'm new to Python and Quantopian, I decided to try making an algorithm to try to find optimal portfolio weights to maximize Alpha.
I realize there are problems with the practicality of using a scheme like this, but am just trying to get used to using Python.

The code seems to be doing just what it should in finding the appropriate portfolio weights (based on looking at the backtest logs), but right at the end when I try to order_target_percent securities (which I have commented out for now) based on those weights, I get the error:

110 Error Runtime exception: ValueError: cannot convert float NaN to integer.

First, I don't understand why it would need to convert to an integer if I want to input a decimal/percent.
Second, when I test the values with isnan() it returns false.

Does anyone know what I'm missing?

Thanks very much!

3 responses

Hi Aleks,

Looks like you ran into a bug! Your code should be able to run as written and I'm sorry for the inconvenience.

I'm filing an internal ticket to fix the issue and I will let you know once we release a fix.

Thanks,
Alisa

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.

Oh ok, thanks for taking a look at it Alisa!
Maybe that means I'm not crazy afterall.

Sorry to revive an old thread but just playing around trying to teach myself to debug/code fix etc.

Cloned this and ran it, and still ran into same issue Alex had.

I did a quick fix below in case it helps anyone but how do avoid having to type this for each [#]? This was only five so easy but if dealing with larger data sets what is the code?

newWeights = maxalpha  
#: Convert to 0 instead  ... taken from Seong Lee  
if np.isfinite(newWeights[0]) != True:  
    newWeights[0] = 0  
if np.isfinite(newWeights[1]) != True:  
    newWeights[1] = 0  
if np.isfinite(newWeights[2]) != True:  
    newWeights[2] = 0  
if np.isfinite(newWeights[3]) != True:  
    newWeights[3] = 0  
if np.isfinite(newWeights[4]) != True:  
    newWeights[4] = 0  
if np.isfinite(newWeights[5]) != True:  
    newWeights[5] = 0   

I did try the below but it did not work:

newWeights = maxalpha  
#: Convert to 0 instead  
for x in range(0,5):  
    if np.isfinite(newWeights[x]) != True:  
        newWeights[x] = 0