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

I was wondering if there was a way that I could create a function that bought based on a previous buy order. For example I am purchasing shares when they are above a certain average but then I want to sell the shares if the next price is higher than the price I bought it for.

I was trying to define a variable like this:

FirstPrice = price > average  
SecondPrice = price > FirstPrice  

but I am not sure if this is how I should be doing this. This is how I have it set up for now but it is not really working for me.

if FirstPrice==True and notional > context.min_notional:  
    order(context.jblu, 10)  
if SecondPrice==True and notional > context.min_notional:  
   order(context.jblu, -10)  
if price < (average) and notional > context.min_notional:  
    order(context.jblu, 0)  
5 responses

My second pasted code in the post above is not working correctly, sorry for that.

Jeremy,

You might want to investigate the various order types on the help page for your application (sounds like a limit order with a negative number of shares could work).

Grant

When you define your variables, the FirstPrice defines as a Boolean. Then, you are defining SecondPrice as a Boolean - but you are comparing a Boolean to a price. So you have some logic issues you need to work through there.

Here is some pseudocode that might be better:

if (price is good):  
    order(context.jblu, 10)

if (context.portfolio[context.jblu].amount > 0) and (price > context.portfolio.positions[context.jblu].cost_basis):  
    order(context.jblu, -10)  
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.

Thanks for the responses. But Dan, just to clarify, in the second part of the code you posted, the shares I had previously bought in "if (price is good)" will be sold as the price continues to go up?

That is correct. I can't tell for sure if that's what you want or not, but yes, that's what would happen.