Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Why isnt this algorithm making any trades? What am i doing wrong?
def initialize ( context ) :  
    set_universe ( universe.DollarVolumeUniverse ( floor_percentile = 98 , ceiling_percentile = 100 ) )

def handle_data(context, data) :  
    uppers = {}  
    downers = {}  
    cash = context.portfolio.cash  
    exchange_time = get_datetime ( )  
    price_history = history ( bar_count = 30 , frequency = '1d' , field = 'price' )  
    if exchange_time.hour == 3 and exchange_time.minute == 40:  
       stocks_owned=context.portfolio.positions  
       list_of_SIDS=stocks_owned.keys()  
       for items in list_of_SIDS:  
           order_target(items, 0)  
    if exchange_time.hour == 3 and exchange_time.minute == 45:  
       for stock in data:  
           LCP_P = history(bar_count=2, frequency='1d', field='close_price')  
           curr_price = LCP_P.iloc[1]  
           last_closing = LCP_P.iloc[0]  
           pct_change = LCP_P.iloc[[1,0]].pct_change()  
           if pct_change >= 0:  
              if pct_change <=9:  
                 uppers[stock]=pct_change  
              else :  
                   pass  
           elif pct_change <= 0:  
                if pct_change >= -9:  
                   downers[stock]=pct_change  
           else :  
                pass  
       '''sorted_downers = [ (k, v) for v, k in sorted( [ (v, k) for k, v in downers.items()])]  
       sorted_uppers = [ (k, v) for v, k in sorted ( [ (v, k) for k, v in uppers.items()], reverse = True )]'''

#possible way of  

       sorted_downers=sorted(downers.items(), key=lambda x:x[1])  
       sorted_uppers=sorted(downers.items(), key=lambda x:x[1], reverse=True)  
       cut_sorted_downers=sorted_downers[:10]  
       cut_sorted_uppers=sorted_uppers[:10]  
       count_downers=len(cut_sorted_downers)  
       count_uppers=len(cut_sorted_uppers)  
       CP_uppers=(cash/2)/count_uppers  
       CP_downers=(cash/2)/count_downers  
       for item in cut_sorted_downers:  
           stock=item[0]  
           order_value(stock, CP_downers)  
       for item  in cut_sorted_uppers:  
           stock=item[0]  
           order_value(stock, (0-1)*(CP_uppers))
8 responses

thanks derek,
I replaced my exchange_time with yours and it tells me that pd. is undefined don't I have to import that earlier, if so: I don't know how or where.

thank you!

hmmmmmmmmm

still not working for what ever reason

when I change exchange_time to pd.Timestamp(get_datetime()).tz_convert('US/Eastern')

can I still use:

if exchange.time.hour == 3 and exchange_time.minute == 45:

how do I do that?

is that an OS setting or is that pulled from Quantopian's database?

oh ok, so that function is automatically set on the 24 hour clock?

Yes, that's exactly right - it runs on a 24 hour clock. Another option is to use the schedule_function() feature!

If you want something to run at 3:45PM you can do,

def initialize(context):  
    # this will run the function "close_positions" every day 15 minutes before the market closes  
    schedule_function(close_positions, time_rules.market_close(minutes=15))  


def handle_data(context,data):  
    pass

def close_positions(context,data):  
     stocks_owned=context.portfolio.positions  
     list_of_SIDS=stocks_owned.keys()  
     for items in list_of_SIDS:  
           order_target(items, 0)  
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.

Alisa, Thanks for the help