How to write/backtest a basic range breakout strategy?

 Hi All, I am new to this. Have a intermediate knowledge of python. Can anyone help me how to backtest a simple breakout strategy in this platform using python?

- it doesn't have to be profitable, for educational purpose
- say I have a range with prices p1 and p2 (p1> p > p2), if current price p reaches P1+0.50 Buy say AAPL 500 shares and exit at 'r' points Stop loss p2
- if price reaches p2-0.50 short AAPL Stop loss p1 and target 'r'

Thanks in advance
Update Backtest

You can use the Market order, limit order and stop market order to implement this strategy

if not self.Portfolio.Invested and self.Securities["AAPL"].Price>p1+0.5:
self.MarketOrder("AAPL", 500)
if self.Portfolio["AAPL"].IsLong:
self.LimitOrder("AAPL", -500, self.Securities["AAPL"].Price + r)
self.StopMarketOrder("AAPL", -500, p2)

if not self.Portfolio.Invested and self.Securities["AAPL"].Price<p2-0.5:
self.MarketOrder("AAPL", -500)
if self.Portfolio["AAPL"].IsShort:
self.LimitOrder("AAPL", 500, self.Securities["AAPL"].Price - r)
self.StopMarketOrder("AAPL", 500, p1)

For details, please see the order ticket documentation.


Hi Jing,

Thanks for writing back.

Correct me if I am wrong but this looks like in simple words buy if not yet bought, AAPL 500 shares if price is > p1+0.5, and similar for short, so when I run the strategy at a particular time it will buy even if the price is too high (p1+0.5) or too low than p2-0.05

I am rephrasing the strategy, your advise appreciated.

- Start the strategy at Market Open
- Monitor 'AAPL' 5 Mins chart for 1 Hour
- After 1 Hour we have RangeHigh = p1, RangeLow = p2 and current price = p (p1 > p >p2)
- at 1hour 01 Min after market open if p is (p1 > p >p2) I want to place a long limit order at p1+0.50 [Order_1] and short limit order at p2-0.50 [Order_2], 500 shares. and will monitor my positions every 15 mins.
if p is scaling up my long order [Order_1] will be executed and the short order [Order_1] will become stoploss
we will place a long target of 'r' points [Order_3]

if p is scaling down my short order [Order_2] will be executed and long order [Order_1] will become stoploss
we will place a short target of 'r' points [Order_3]

- if long or short target is reached we will cancel the stoploss and will exit the trade, if long or short stoploss is hit, before target we will cancel the target order [Order_3] and exit the trade.

(There could be cases where there is partial fill etc, but I will do it myself when I actually implement)

your help appreciated to get started.


For details about the StopMarketOrder() and LimitOrder(), please see the documentation. In this part, you can still find how to update and cancel the order

For five-minute timeframe, you should request the minute resolution data and use the data consolidator to aggregate the minute data into 5-minute resolution.

Update Backtest


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 QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect 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. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


This discussion is closed