How can I set time_rules offset beyond 12 hours for BTC trading?

67 views
Skip to first unread message

Yeongbae JEON

unread,
Mar 21, 2018, 5:38:59 PM3/21/18
to Zipline Python Opensource Backtester
Hello, This post is extension of the previous post

Because BTC trading is running for 24 hours, I want to schedule a function every two hour. However, time_rules.market_open() gives following error
offset must be in between 1 minute and 12 hours, inclusive.

Below is my code
%%zipline -b gdax-bundle --start 2018-1-3 --end 2018-1-8 --data-frequency minute --trading-calendar GDAX
from zipline.api import symbol, get_datetime
from zipline.api import schedule_function, date_rules, time_rules

def initialize(context):
    context
.btc = symbol("BTC")
   
for i in range(0,24,2):
        schedule_function
(rebalance,
                          date_rules
.every_day(),
                          time_rules
.market_open(hours=i))

def rebalance(context, data):
   
print(get_datetime())
   
def handle_data(context, data):
   
pass

I can use handle_data to call rebalance function every two hour Since handle_data is called every minute. However I think it is better to use schedule_function.
Does anyone know how to set offset of time_rules beyond 12?

Ed Bartosh

unread,
Mar 21, 2018, 6:03:51 PM3/21/18
to Yeongbae JEON, Zipline Python Opensource Backtester
Hello,

I personally find it more clear to use handle_data for this purpose.
However, you can work around that limitation this way:

for i in range(0,12,2):
        schedule_function
(rebalance,
                          date_rules
.every_day(),
                          time_rules
.market_open(hours=i))
        schedule_function(rebalance,
                          date_rules
.every_day(),
                          time_rules
.market_close(hours=i+2))


It's a bit ugly, but should work. I didn't test it though.

Regards,
Ed


--
You received this message because you are subscribed to the Google Groups "Zipline Python Opensource Backtester" group.
To unsubscribe from this group and stop receiving emails from it, send an email to zipline+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
BR,
Ed

Yeongbae JEON

unread,
Mar 21, 2018, 9:33:12 PM3/21/18
to Zipline Python Opensource Backtester
Hello, thank you for help. Your code works with little change. Because market_open and market_close does not allow zero offset, I added "minutes=1" parameter to time_rules.market_open

%%zipline -b gdax-bundle --start 2018-1-3 --end 2018-1-8 --data-frequency minute --trading-calendar GDAX
from zipline.api import symbol, get_datetime
from zipline.api import schedule_function, date_rules, time_rules

def initialize(context):
    context
.btc = symbol("BTC")

   
for i in range(0,12,2):
        schedule_function
(rebalance,
                          date_rules
.every_day(),
                          time_rules
.market_open(hours=i, minutes=1))
        schedule_function
(rebalance,
                          date_rules
.every_day(),
                          time_rules
.market_close(hours=i+2))


def rebalance(context, data):
   
print(get_datetime())
   
def handle_data(context, data):
   
pass

Here is part of result:
2018-01-03 00:00:00+00:00
2018-01-03 02:00:00+00:00
2018-01-03 04:00:00+00:00
2018-01-03 06:00:00+00:00
2018-01-03 08:00:00+00:00
2018-01-03 10:00:00+00:00
2018-01-03 11:59:00+00:00
2018-01-03 13:59:00+00:00
2018-01-03 15:59:00+00:00
2018-01-03 17:59:00+00:00
2018-01-03 19:59:00+00:00
2018-01-03 21:59:00+00:00
2018-01-04 00:00:00+00:00
2018-01-04 02:00:00+00:00
2018-01-04 04:00:00+00:00
2018-01-04 06:00:00+00:00
2018-01-04 08:00:00+00:00
2018-01-04 10:00:00+00:00
2018-01-04 11:59:00+00:00
2018-01-04 13:59:00+00:00
2018-01-04 15:59:00+00:00
2018-01-04 17:59:00+00:00
2018-01-04 19:59:00+00:00
2018-01-04 21:59:00+00:00
2018-01-05 00:00:00+00:00
2018-01-05 02:00:00+00:00
2018-01-05 04:00:00+00:00
2018-01-05 06:00:00+00:00
2018-01-05 08:00:00+00:00

there is one minute off, but it is ok for me. Thank you.

Ed Bartosh

unread,
Mar 22, 2018, 8:39:13 AM3/22/18
to Yeongbae JEON, Zipline Python Opensource Backtester
Hello,

The output still looks a bit inconstent to my taste. How about this?

#!/usr/bin/env python

from zipline.api import get_datetime, schedule_function, date_rules, time_rules

def initialize(context):
    for i in range(0, 12, 2):
        schedule_function(rebalance, 
                          date_rules.every_day(),
                          time_rules.market_open(hours=i, minutes=1))
        schedule_function(rebalance, 
                          date_rules.every_day(),
                          time_rules.market_close(hours=i+2, minutes=-1))

def rebalance(context, data):
    print(get_datetime(tz='UTC'))


$ zipline run -b gdax-bundle -f ./test.py -s 2018-01-02 -e 2018-01-03 --trading-calendar GDAX --data-frequency minute -o /dev/null 2>/dev/null
2018-01-02 00:00:00+00:00
2018-01-02 02:00:00+00:00
2018-01-02 04:00:00+00:00
2018-01-02 06:00:00+00:00
2018-01-02 08:00:00+00:00
2018-01-02 10:00:00+00:00
2018-01-02 12:00:00+00:00
2018-01-02 14:00:00+00:00
2018-01-02 16:00:00+00:00
2018-01-02 18:00:00+00:00
2018-01-02 20:00:00+00:00
2018-01-02 22:00:00+00:00
2018-01-03 00:00:00+00:00
2018-01-03 02:00:00+00:00
2018-01-03 04:00:00+00:00
2018-01-03 06:00:00+00:00
2018-01-03 08:00:00+00:00
2018-01-03 10:00:00+00:00
2018-01-03 12:00:00+00:00
2018-01-03 14:00:00+00:00
2018-01-03 16:00:00+00:00
2018-01-03 18:00:00+00:00
2018-01-03 20:00:00+00:00
2018-01-03 22:00:00+00:00

Regards,
Ed

--
You received this message because you are subscribed to the Google Groups "Zipline Python Opensource Backtester" group.
To unsubscribe from this group and stop receiving emails from it, send an email to zipline+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
BR,
Ed
Reply all
Reply to author
Forward
Message has been deleted
0 new messages