Back to Community
I want this algorithm to only run handle_data every 5 minutes, or at least only make trades every five minutes. How can I do this?

I know of the schedule_function but I don't think that will work like this with minute data as it won't stop the algo from running just reset it periodically. Any suggestions?

Clone Algorithm
32
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: 55c753e029ac8a0c6b849926
There was a runtime error.
8 responses

This is a really easy way of doing it. It counts each minute and when handle_data() has been called 5 times, it executes the logic.

Happy coding

EDIT: I forgot you should also have if not get_open_orders(stock): before the order_target_percent line.

Clone Algorithm
57
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: 55c77b6e1671de0c55cc1c0e
There was a runtime error.

What does 'if not get_open_orders(stock):' do?
And how does 'context.minuteCounter' work?

minuteCounter is a variable which gets incremented every update (every minute) and whenever it exceeds 5 minutes, your logic runs and the variable is reset. The not get_open_orders(stock) makes sure that you don't send orders for a stock for which you already have orders sent for. That's a good precaution if you don't want to bother properly accounting for outstanding orders, because otherwise, if your existing orders and the new ones you send both get filled, you'll exceed your desired position.

Note that instead of the minute counter variable as suggested by James Jack, I'd just do

    if get_datetime().minute % 5 != 0: # Only run when the minute is divisible by 5 .  
        return    # And don't execute the rest of this function

    stock = context.stocks  
    prices = history(bar_count = 13, frequency = '1m', field = 'price')  
    ....

It gets incremented automatically by quantopian? Also is there a way to log what time the orders took place just so I can double check that it's working?

Hi Ari,

If you're talking about my example code, the algo increments context.minuteCounter without help from Quantopian.

If you're talking about Alex's suggestion of if get_datetime().minute % 5 != 0 then there is no counter. Instead the current time is used, which is updated by Quantopian.

To log the current time you can use print "the time is {}".format(get_datetime('US/Eastern').time())

Since we know that on a typical trading day there are 390 minutes, we can create schedule_functions in a loop that fire very 5 minutes, if the day is cut short e.g. a holiday, or half day, then the extra schedule_functions will never execute.Try this...

def initialize(context):  
    for minute in range(0, 390, 5):  
        schedule_function(trade, date_rules.every_day(), time_rules.market_open(minute)  
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.

The approaches presented here are actually only workarounds for a missing generalization of the API to arbitrary scheduling intervals between 1m and 1w. Are there any plans to improve the API in this regard?

total_minutes_pipeline = 6*60 + 5

for i in range(1, total_minutes_five_minute):  
    if i % 5 == 0:  
        schedule_function(  
        five_minute,  
            date_rules.every_day(),  
            time_rules.market_open(minutes=i),  
            True  
                           )  

This is from the help docs. This sets the function, 'five_minute()' to run every 5 minutes.

I would not try to change how often 'handle_data' runs, rather define a new function scheduled every 5 minutes.