Back to Community
Backtest: Buy 1 hour after open, sell 10 min before close. Is this possible?

Hi the algorithm is a very simple algorithm that I have used as an example I am struggling to understand some very basic parts of quantopian.
The algorithm buys 1 hour after open, and should sell 10 min before close.

I would be grateful if you could assist me with the following:

a) is this backtest on minute bars or daily bars? if daily, then how is it submitting orders 1 hour after open?
b) it appears orders aren't being closed at the end of my day despite my function, why is this?
c) it appears leverage isn't being controlled? Is this due to b)?

The code is attached and also below:

def initialize(context):  
    # Rebalance every day, 1 hour after market open.  
    schedule_function(open_all, date_rules.every_day(), time_rules.market_open(hours=1))  
    # Close all orders at 10 minutes before end of day  
    schedule_function(func=close_all, date_rule=date_rules.every_day(), time_rule=time_rules.market_close(minutes=10)  )  
def before_trading_start(context, data):  
    context.sec_list = [sid(24),    sid(114),   sid(122),   sid(630)]  
def open_all(context,data):  
    for equity in context.sec_list:  
        order_percent(equity,0.25)  
        log.info(equity,"opened")  
def close_all(context, data):  
    for equity in context.sec_list:  
        order_percent(equity, 0)  
        log.info(equity,"liquidated")  
Clone Algorithm
9
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 5a0cd4e3629bfb41e15b253a
There was a runtime error.
2 responses

a) Read about schedule function: https://www.quantopian.com/tutorials/getting-started#lesson7
Your date and time rules in English: "Execute 'open_all' function every day 1 hour after open"

b) You need order_target_percent, not order_percent. order_percent is not relative to portfolio, so passing 0 does not close position

c) yes, it is due to b

Correction: scratch my answer to b)... it is relative to portfolio val but you need negative percent to sell
go to: https://www.quantopian.com/help
search for 'order_percent'