Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Help With Stock Returns

Hello,

First post on Quantopian, very excited to use it more.

While trying to learn, I created the following day of the week strategy (randomly chosen parameters)


def initialize(context):  
  context.aapl = sid(24)  
  context.flag = 0  
def handle_data(context, data):  


    if data[context.aapl].datetime.weekday() == 3 and data[context.aapl].returns > 5:  
        context.flag = 1  
    if data[context.aapl].datetime.weekday() == 4 and context.flag == 1:  
        context.flag = 2  
        print("buy")  
        order(context.aapl, +100)  
    if data[context.aapl].datetime.weekday() == 1 and context.flag == 2:  
        order(context.aapl, -100)  
        context.flag = 0  
        print("sell")  

        #if returns > 0.05, strat return is -1.98, sharpe -2.68  
        #if returns > 5.00, strat return is -1.98, sharpe -2.68  

The idea is to buy the apple stocks on Fridays if Thursdays were profitable, and then to sell it on Tuesdays.

I set a threshold for the Thursday returns: if the returns are greater than 0.05, i.e. 5%, buy, etc. But whatever I set the threshold to, I get the exact same results. Even if I set it to the almost impossible return of 500%! I was wondering if someone can point as to where the error in my logic or code is.

Best,

Ryan

4 responses
.returns()

Don't forget the () to make it a function call.

Oh thank you very much. This explains, it's working now. As it wasn't giving an error, what was .returns returning without the ()?

The value was set to a string representation of the function object:

<function _transform at 0x7154cf8>  

Python is very forgiving and allows you to compare different types to each other without giving an error.

Clearly this comparison thought the string was 'bigger' than the number.

Thanks again Dennis. Guess I need to do some research into Python before I start dabbling with Quantopian!