Back to Community
How to reproduce Daily with min simulation

I am trying to reproduce the exact result I got running the daily simulation with the minute simulation, so I came-up with

def handle_data(context, data):  
...
     exchange_time = pd.Timestamp(get_datetime()).tz_convert('US/Eastern')  
     if not( exchange_time.hour == 9 and exchange_time.minute == 0):  
         return 

    #Code to run once a day  

But I still get different results, so my question is on daily what time is the code executed at the open of the daily bar or at the close of the prev day?

What time should I use to get the same results?
Say on daily I have rebalance on day 30 of January on minute it should be 31st on open? or 30 on close?

3 responses

I must be missing something.

I did a very simple algo to check, with daily simulation, I send an order on Wednesday day 5 but it only got filled day 7 according to back-testing, so I added the Market order but it still didnt help, then even more interesting on minute simulation the orders does get filled on day 5 but I get 7 orders not 1?

What am I missing?
I just want an algo that has same result for daily and min simulation?

# Put any initialization logic here.  The context object will be passed to  
# the other methods in your algorithm.  
import pandas as pd  
from brokers.ib import IBExchange  
def initialize(context):  
    pass

# Will be called on every trade event for the securities you specify.  
def handle_data(context, data):  
    exchange_time = pd.Timestamp(get_datetime()).tz_convert('US/Eastern')  
    print exchange_time  
    if exchange_time.day == 5 and exchange_time.month == 1 and exchange_time.year == 2011  and exchange_time.hour == 10 and exchange_time.minute == 31:  
        order_target_percent(sid(24), 10, style=MarketOrder(exchange=IBExchange.SMART))

    #Code to run once a day  


Clone Algorithm
7
Loading...
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
# Put any initialization logic here.  The context object will be passed to
# the other methods in your algorithm.
import pandas as pd
from brokers.ib import IBExchange
def initialize(context):
    pass

# Will be called on every trade event for the securities you specify. 
def handle_data(context, data):
    exchange_time = pd.Timestamp(get_datetime()).tz_convert('US/Eastern')  
    print exchange_time
    if exchange_time.day == 5 and exchange_time.month == 1 and exchange_time.year == 2011  and exchange_time.hour == 10 and exchange_time.minute == 31:  
        
        order_target_percent(sid(24), 10, style=MarketOrder(exchange=IBExchange.SMART))

    #Code to run once a day  
    
There was a runtime error.

1) In minute mode you get several orders because you are trying to order more volume than actually traded in a given minute bar, so the order clears over multiple bars

2) By default in daily mode orders clear at the following day's closing price, regardless of what time of day you thought you placed the order. Look up the "trade at the open" slippage model for ways to change that behavior.

If you'd like to run something once per day, use schedule_function: https://www.quantopian.com/help#ide-schedulefunction

You can run your backtest in minute mode, which will receive price updates every minute. And your order will be executed once per day, at the time you specify. The fills will be based on slippage. By default, the backtester will transact up to 25% of the stock's volume per bar, to simulate market impact. If you're trying to trade a large number of shares, they will be filled across multiple, subsequent bars. This setting is customizable; you can tweak it or turn it off entirely for development. Here's the documentation: https://www.quantopian.com/help#ide-slippage

When you deploy your algo to live trading, set_slippage is ignored and the broker fills the orders based on the market conditions.

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.