Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Algo Not Selling

I have the following code, but for some reason It's not selling. I want to have it sell on close the days it buys, and after consulting the documentation It seems like my order call has the right parameters. What am I doing wrong?

def initialize(context):  
    context.nvda = sid(6984)  
    context.max_notional = 1000000.1  
    context.min_notional = -1000000.0


# Will be called on every trade event for the securities you specify.  
def handle_data(context, data):  
    # Implement your algorithm logic here.  
    theOpen = data[context.nvda].open_price  
    curPrice = data[context.nvda].price  
    amount = 2000/curPrice  
    if curPrice >= (theOpen + .08) and curPrice <= (theOpen + 1.2):  
        #order(sid(19725),amount, theOpen * 1.02, (theOpen*.98))  
        order(context.nvda, amount, data[context.nvda].close_price)  

    # data[sid(X)] holds the trade event data for that security.  
    # data.portfolio holds the current portfolio state.

    # Place orders with the order(SID, amount) method.

    # TODO: implement your own logic here.  
    #order(sid(24), 50)  
7 responses

Hello Aloke,

The order method will sell/short when the number of shares is negative. I made a simple tweak to your algorithm to illustrate:

amount = -2000/curPrice # positive for buy, negative for sell/short  

The modified algorithm is attached.

Grant

Gotcha, thanks! I'm trying to write an algorithm that buys a stock when it's up 1 point from its open and sells at that day's close (very simple, I know). I feel like doing something this simple shouldn't be so difficult. Why would a limit order like this not work?

order(context.nvda, amount, data[context.nvda].close_price)  

Hello Aloke,

There's a bit of a learning curve...hang in there! What you are proposing can be done in Quantopian. You'll need to write an algorithm to run on minute data, since you'll be checking the price every minute to see if it is up 1 point from its open.

I don't have time now to work up an example...perhaps someone else can lend a hand?

Grant

Aloke, sorry you're having problems figuring this out. I'd like to get you going, and I'd like to figure out how to improve the experience so that the next newcomer can figure it out more easily.

Please do me a favor and check out the backtesting section of the FAQ. Once you've read that, I think you'll understand what's going on.

In daily mode, looking at your code:

order(context.nvda, amount, data[context.nvda].close_price)  

That is placing a limit order at today's closing price, and that order is processed tomorrow.

So, does that explain it? If not, let's figure it out.

If that does explain it, do you have any suggestions on how to make this more clear in the future? Thanks.

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.

Dan thanks for your help, unfortunately the FAQ didn't exactly solve the problem. The main problem I'm facing is that I want to use minute tick data but I would want to execute max once a day. Since the code will be executed every minute, I don't see how I could save a global variable of the day to compare to each minute. Essentially what I'm wondering is if you can declare a variable (i.e. date using DateTime object) that can be accessed throughout multiple minute data if that makes sense?

Cheers,
Aloke

@Aloke, do you see the "context" variables in your example code?

    context.nvda = sid(6984)  
    context.max_notional = 1000000.1  
    context.min_notional = -1000000.0  

You can create a flag or datetime variable the same way.

def initialize(context):  
    context.tradedate = None

def handle_data(context, data):  
    # get current date (but not the time portion)  
    date = get_datetime().date()  
    # check trade date before continuing  
    if context.tradedate != None and context.tradedate == date:  
        # already traded today, do nothing  
        return

    # if we sent a trade order ...  
    if mysentorder == True:  
        # remember current date  
        context.tradedate = date  

Gotcha, much simpler than I thought. Thanks!