Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Creating buy low sell high strategy

How should i create a buy low sell high strategy in quantopian.

My idea
Assumption: the stock is trending higher

pre_stock_price= price of  the previous stock  
new_stock_price= price of the latest stock  
stock_price_per= ((new_stock_price - pre_stock_price)/pre_stock_price)  
if stock_price_per > 0.1 or stock_price_per < -0.05  
sell stock  
buy stock at current price  
end if  

I know this strategy is not a good one but I am using it to my own learning purpose. I have so far written this much code.

def initialize(context):  
    context.aapl = sid(24)  
    set_commission(commission.PerShare(cost=0.01))  
    context.max_notional = 100000.1  
    context.min_notional = -100000.0  
def handle_data( context , data ):  
    price = data[context.aapl].price  

the problem is how i will compare a new apple stock price with an old apple stock price.

7 responses

What do you want to do exactly ? You can save old stock prices using history function what is your aim

@nishant My problem is how to execute my algorithm into a quantopian programming script. i don’t know more about the syntax of quantopian and learning curve is very high in this platform. I am trying my best to learn quantopian by making simpler programmes. The history function you mentioned like this

apple_history = history(1000,'1d','price') # prices of thousand days with 1 day interval  

then how to fetch prices and used it to backtest my algorithm. can you show me a simple script to do this

PS=Kindley please forgive me if i am confusing you. As english is not my first language its difficult to convey my idea properly

If you are new to python than check out online docs on python.

There is this post which has 2 strategies implemented as per condition.

https://www.quantopian.com/posts/multi-strategy-example

Reading and understanding it will help you about the syntax of quanotpian.
Clone it and click backtest you will understand the process.

I my self made the programme in qunantopia for Buy low- sell high. But I have some programming error in my code. I am not able to change old_price to new_price (see line 24 old_price = new_price). it also shows warning Undefined name 'old_price'

So using the python builtin global really carries no value in the Quantopian IDE, if you want a variable to act as a gloabl variable and be accessible during every call to handle_data() you need to bind it to context so instead of declaring it as global like you did, it its better to just bind the variable to context.

inititalize(context):  
    context.old_price = 0  

This will allow what ever value you set to it to persist. This goes for any variable you bind to context.

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.

James thanks for the advice I have changed my old_price variable and bind it to context. and now its working fine. The one problem is I am not able to record/plot my gain/loss percentage (last commented line). Showing invalid syntax error, any idea what is happening there.

Minor question: Is using stock( AAPL) as a benchmark in backtesting is good?

I'd agree that using AAPL as your benchmark is a good idea as it is the main security you are trading, anything else would be unrepresentative of your strategy. Bear in mind that hard coding any symbols in usually results in some bias to your strategy unless you are specifically trying to manipulate those securities.

In regard to your question about the plotting syntax error you have a space between gain_loss percentage