Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Trade Frequency and Trading Multiple Securities

Sorry for being a complete noob, but I'm having some trouble with this.

I'd like to trade anyone of the 10 securities listed under context.secs and I only want to trade, at most, 5 times per day. Can someone please help me with this?

Thanks in advance,
Jamie

3 responses

Hey Jamie,

Great question. The best way to do this is to iterate through all the stocks in context.secs in a for loop (https://docs.python.org/2/tutorial/controlflow.html) and have a counter that increments everytime you order a stock. Having that counter allows you to make sure that you're never ordering more than 5 securities per day. And if the counter ever goes past 5, the algorithm will simply not order the security.

Take a look at the code I've posted, play around with it, and let me know if you have any questions with it


def initialize(context):  
    #trading top 10 holdings of SP500  
    # context.secs = sid(8554)  
    context.secs =   [ sid(24),  
                       sid(5061),  
                       sid(8347),  
                       sid(46631),  
                       sid(4151),  
                       sid(1091),  
                       sid(3149),  
                       sid(8151),  
                       sid(5938),  
                       sid(25006)]

def handle_data(context, data):  
    #: Define number of stocks traded  
    num_stocks = 0  
    #: Iterate over each security in context.secs  
    for stock in context.secs:  
        #: Make sure that we have data for that security  
        if stock in data:  
            s_mean = data[stock].mavg(60)  
            l_mean = data[stock].mavg(120)

            current_price = data[stock].price  
            qty = context.portfolio.positions_value  
            cash = context.portfolio.cash

            #: Check that we're only ordering, at most, 5 stocks per day  
            if num_stocks < 5:  
                # condition required to enter a buy trade.  
                if s_mean < l_mean and cash > current_price:  
                    # place the buy order, in dollars  
                    order_value(stock, 9000)  
                    log.info("Buying %s" % (stock.symbol))  
                    num_stocks += 1

                # condition required to enter the sell trade  
                elif s_mean > l_mean and qty > 10000:  
                    # place sell order, in dollars  
                    order_value(stock, -3000)  
                    log.info("Selling %s" % (stock.symbol))  
                    num_stocks += 1  
    # graphs  
    #record(stock_price=data[context.security].price)  
    record(cash = context.portfolio.cash)  

Seong

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, Seong!