Closed ayxemma closed 6 years ago
hi @ayxemma!
I can't reproduce this issue on master with the following script:
# repro.py
import pandas as pd
from zipline import run_algorithm
from zipline.api import get_datetime, record
def initialize(context):
pass
def before_trading_start(context, data):
record(tday=get_datetime())
result = run_algorithm(
start=pd.Timestamp('2014-12-01', tz='UTC'),
end=pd.Timestamp('2015-01-02', tz='UTC'),
initialize=initialize,
capital_base=10000,
before_trading_start=before_trading_start,
bundle='quandl',
)
print result.tday
(Note I ran this after running zipline ingest -b quandl
with an API key exported.)
For me, running this script produces the following:
$ python repro.py
2014-12-01 21:00:00+00:00 2014-12-01 13:45:00+00:00
2014-12-02 21:00:00+00:00 2014-12-02 13:45:00+00:00
2014-12-03 21:00:00+00:00 2014-12-03 13:45:00+00:00
2014-12-04 21:00:00+00:00 2014-12-04 13:45:00+00:00
2014-12-05 21:00:00+00:00 2014-12-05 13:45:00+00:00
2014-12-08 21:00:00+00:00 2014-12-08 13:45:00+00:00
2014-12-09 21:00:00+00:00 2014-12-09 13:45:00+00:00
2014-12-10 21:00:00+00:00 2014-12-10 13:45:00+00:00
2014-12-11 21:00:00+00:00 2014-12-11 13:45:00+00:00
2014-12-12 21:00:00+00:00 2014-12-12 13:45:00+00:00
2014-12-15 21:00:00+00:00 2014-12-15 13:45:00+00:00
2014-12-16 21:00:00+00:00 2014-12-16 13:45:00+00:00
2014-12-17 21:00:00+00:00 2014-12-17 13:45:00+00:00
2014-12-18 21:00:00+00:00 2014-12-18 13:45:00+00:00
2014-12-19 21:00:00+00:00 2014-12-19 13:45:00+00:00
2014-12-22 21:00:00+00:00 2014-12-22 13:45:00+00:00
2014-12-23 21:00:00+00:00 2014-12-23 13:45:00+00:00
2014-12-24 18:00:00+00:00 2014-12-24 13:45:00+00:00
2014-12-26 21:00:00+00:00 2014-12-26 13:45:00+00:00
2014-12-29 21:00:00+00:00 2014-12-29 13:45:00+00:00
2014-12-30 21:00:00+00:00 2014-12-30 13:45:00+00:00
2014-12-31 21:00:00+00:00 2014-12-31 13:45:00+00:00
2015-01-02 21:00:00+00:00 2015-01-02 13:45:00+00:00
Name: tday, dtype: datetime64[ns, UTC]
You'll notice that the recorded variable entry for 2014-12-24 shows up with a timestamp that's 3 hours earlier than the other days, because the day before christmas is a half day, but otherwise that day works just like a normal trading day.
Are you by any chance using schedule_function
to schedule your record
calls? If so, are you passing half_days=False
? That would cause your function to not run on the 24th, which would cause us to forward-fill the recorded values from the previous day when reporting results.
Thanks for the reply. i didn't put half_days=False
in my code, but i figured out that the problem is that in the if __name__ == '__main__':
function previously i comment out 'before_trading_start': before_trading,
, it still runs before_trading
, but after uncommenting it, the calendar does not have any problem.
perf = TradingAlgorithm(
env=env,
trading_calendar=get_calendar("NYSE"),
sim_params=create_simulation_parameters(
start=start,
end=end,
capital_base=CAPITAL_BASE,
trading_calendar=get_calendar("NYSE"),
data_frequency='daily'
),
**{
'initialize': initialize,
'handle_data': handle_data,
'before_trading_start': before_trading,
'analyze': analyze,
}
).run(data, overwrite_sim_params=False)
Dear Zipline Maintainers,
Before I tell you about my issue, let me describe my environment:
Environment
$ pip freeze
or$ conda list
Name Version Build Channel
alembic 0.9.9
backcall 0.1.0
backcall 0.1.0 py35_0
bcolz 0.12.1
bleach 2.1.3 py35_0
Bottleneck 1.2.1
ca-certificates 2018.03.07 0
certifi 2018.4.16 py35_0
chardet 3.0.4
click 6.7
contextlib2 0.5.5
cycler 0.10.0
cyordereddict 1.0.0
Cython 0.28.3
dbus 1.13.2 h714fa37_1
decorator 4.3.0 py35_0
decorator 4.3.0
empyrical 0.5.0
entrypoints 0.2.3 py35h48174a2_2
et-xmlfile 1.0.1
expat 2.2.5 he0dffb1_0
fontconfig 2.13.0 h9420a91_0
freetype 2.9.1 h8a8886c_0
glib 2.56.1 h000015b_0
gmp 6.1.2 h6c8ec71_1
gst-plugins-base 1.14.0 hbbd80ab_1
gstreamer 1.14.0 hb453b48_1
h5py 2.8.0
html5lib 1.0.1 py35h2f9c1c0_0
icu 58.2 h9c2bf20_1
idna 2.7
intel-openmp 2018.0.3 0
intervaltree 2.1.0
ipykernel 4.8.2 py35_0
ipython 6.4.0 py35_0
ipython-genutils 0.2.0
ipython_genutils 0.2.0 py35hc9e07d0_0
ipywidgets 7.2.1 py35_0
jdcal 1.4
jedi 0.12.0
jedi 0.12.0 py35_1
jinja2 2.10 py35h480ab6d_0
joblib 0.11
jpeg 9b h024ee3a_2
jsonschema 2.6.0 py35h4395190_0
jupyter 1.0.0 py35_4
jupyter_client 5.2.3 py35_0
jupyter_console 5.2.0 py35h4044a63_1
jupyter_core 4.4.0 py35ha89e94b_0
kiwisolver 1.0.1
libedit 3.1.20170329 h6b74fdf_2
libffi 3.2.1 hd88cf55_4
libgcc-ng 7.2.0 hdf63c60_3
libpng 1.6.34 hb9fc6fc_0
libsodium 1.0.16 h1bed415_0
libstdcxx-ng 7.2.0 hdf63c60_3
libuuid 1.0.3 h1bed415_2
libxcb 1.13 h1bed415_1
libxml2 2.9.8 h26e45fe_1
Logbook 1.4.0
lru-dict 1.1.6
Mako 1.0.7
markupsafe 1.0 py35h4f4fcf6_1
MarkupSafe 1.0
matplotlib 2.2.2
mistune 0.8.3 py35h14c3975_1
mkl 2018.0.3 1
mkl-service 1.1.2 py35h0fc7090_4
multipledispatch 0.5.0
nbconvert 5.3.1 py35hc5194e3_0
nbformat 4.4.0 py35h12e6e07_0
ncurses 6.1 hf484d3e_0
networkx 1.11
notebook 5.5.0 py35_0
numexpr 2.6.5
numpy 1.14.5
openpyxl 2.5.4
openssl 1.0.2o h20670df_0
pandas 0.19.1
pandas-datareader 0.5.0
pandoc 1.19.2.1 hea2e7c5_1
pandocfilters 1.4.2 py35h1565a15_1
parso 0.2.1 py35_0
patsy 0.5.0
pcre 8.42 h439df22_0
pexpect 4.6.0 py35_0
pickleshare 0.7.4
pickleshare 0.7.4 py35hd57304d_0
pip 10.0.1 py35_0
prompt-toolkit 1.0.15
prompt_toolkit 1.0.15 py35hc09de7a_0
ptyprocess 0.5.2
ptyprocess 0.5.2 py35h38ce0a3_0
pyfolio 0.8.0
pygments 2.2.0 py35h0f41973_0
Pygments 2.2.0
pymc3 3.4.1
pyparsing 2.2.0
pyqt 5.9.2 py35h751905a_0
python 3.5.5 hc3d631a_4
python-dateutil 2.7.3 py35_0
python-editor 1.0.3
pytz 2018.4
pyzmq 17.0.0 py35h14c3975_0
qt 5.9.6 h52aff34_0
qtconsole 4.3.1 py35h4626a06_0
readline 7.0 ha6073c6_4
requests 2.19.1
requests-file 1.4.3
requests-ftp 0.3.1
scikit-learn 0.19.1
scipy 1.1.0
seaborn 0.8.1
send2trash 1.5.0 py35_0
setuptools 39.2.0 py35_0
simplegeneric 0.8.1 py35_2
simplegeneric 0.8.1
sip 4.19.8 py35hf484d3e_0
six 1.11.0
six 1.11.0 py35h423b573_1
sortedcontainers 2.0.4
SQLAlchemy 1.2.8
sqlite 3.24.0 h84994c4_0
statsmodels 0.9.0
tables 3.4.4
terminado 0.8.1 py35_1
testpath 0.3.1 py35had42eaf_0
Theano 1.0.2
tk 8.6.7 hc745277_3
toolz 0.9.0
tornado 5.0.2 py35_0
tqdm 4.23.4
traitlets 4.3.2
traitlets 4.3.2 py35ha522a97_0
urllib3 1.23
wcwidth 0.1.7 py35hcd08066_0
wcwidth 0.1.7
webencodings 0.5.1 py35hb6cf162_1
wheel 0.31.1 py35_0
widgetsnbextension 3.2.1 py35_0
xlrd 1.1.0
xz 5.2.4 h14c3975_4
zeromq 4.2.5 h439df22_0
zipline 1.2.0
zlib 1.2.11 ha838bed_2
Now that you know a little about me, let me tell you about the issue I am having:
Description of Issue
context.tday=get_datetime().date()
to get the backtest date when run backtest as of each date under functiondef before_trading(context, data):
and recordcontext.tday
by running:'before_trading_start': before_trading,