Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Need someone to help with leverage on this

I need help specific to my code that you guys KNOW will work pls.

5 responses

You need this:

    for stock in stocks:  
        last_close = context.output.get_value(stock, 'close')  
        min_close = context.output.get_value (stock, 'min_close')  
        max_close = context.output.get_value (stock, 'max_close')  
        number_of_open_positions= len(openorders)  
        currentcash= context.portfolio.cash  
        buyprice= context.portfolio.positions[stock].cost_basis  
        curprice=  data.current(stock, 'price')  
        # for stuff last_close >= max_close  
    #for stock in stocks:  
        #percentgain= and currentcash>50000  

    if data.can_trade(stock):  
        if (last_close < min_close):  
            if get_open_orders():  
                return  
            else:  
                order_target_percent(stock, 0.5)  
        elif (last_close >= max_close or (buyprice!=0 and (buyprice-curprice)/buyprice<=-.05) and leverage<2.95):  
                if get_open_orders():  
                    return  
                else:  
                    order_target_percent(stock, 0)  

to be all one For Loop. This shouldn't be separate and also you should redo it like this:

open_orders = get_open_orders()  
if data.can_trade(stock) & (stock not in open_orders):  
    #Since parent if-statement will be inside for loop (as stated above)  
    if (stock not in context.security_list):  
        if (last_close < min_close):  
            order_target_percent(stock, 0.5)  
        elif (last_close >= max_close or (buyprice!=0 and (buyprice-curprice)/buyprice<=-.05) and leverage<2.95):  
                rder_target_percent(stock, 0)  

This should solve some of what you're experiencing

But leverage. You will want to use a set weight like this:

open_orders = get_open_orders()  
if (len(securities_long_or_short) > 0):  
    wgt = 1. / len(securities_long_or_short)  
    for (stock in securities_long_or_short):  
        if data.can_trade(stock) & (stock not in open_orders):  
            order_target_percent(stock, wgt)  
  1. (aka 1.0) would mean you are leveraging all the money you have in your account. Although it is possible to go slightly over 1.0 leverage this way because you could end up with like $50 and end up ordering AAPL shares which throw you into margin

thanks for spending the time on a post...

I implemented it, and quantopian freezes, never finishes the pipeline...

Sometimes it can take a while for it to initialize the pipeline. It happens to me also. If anything, try starting a new algorithm and make the changes and see if it still does the same thing :)

got it to work, thanks so much