forked from stefan-jansen/zipline-reloaded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__main__.py
469 lines (429 loc) · 11.7 KB
/
__main__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
import errno
import os
import click
import logging
import pandas as pd
import zipline
from zipline.data import bundles as bundles_module
from zipline.utils.calendar_utils import get_calendar
from zipline.utils.compat import wraps
from zipline.utils.cli import Date, Timestamp
from zipline.utils.run_algo import _run, BenchmarkSpec, load_extensions
from zipline.extensions import create_args
try:
__IPYTHON__
except NameError:
__IPYTHON__ = False
@click.group()
@click.option(
"-e",
"--extension",
multiple=True,
help="File or module path to a zipline extension to load.",
)
@click.option(
"--strict-extensions/--non-strict-extensions",
is_flag=True,
help="If --strict-extensions is passed then zipline will not "
"run if it cannot load all of the specified extensions. "
"If this is not passed or --non-strict-extensions is passed "
"then the failure will be logged but execution will continue.",
)
@click.option(
"--default-extension/--no-default-extension",
is_flag=True,
default=True,
help="Don't load the default zipline extension.py file in $ZIPLINE_HOME.",
)
@click.option(
"-x",
multiple=True,
help="Any custom command line arguments to define, in key=value form.",
)
@click.pass_context
def main(ctx, extension, strict_extensions, default_extension, x):
"""Top level zipline entry point."""
# install a logging handler before performing any other operations
logging.basicConfig(
format="[%(asctime)s-%(levelname)s][%(name)s]\n %(message)s",
level=logging.INFO,
datefmt="%Y-%m-%dT%H:%M:%S%z",
)
create_args(x, zipline.extension_args)
load_extensions(
default_extension,
extension,
strict_extensions,
os.environ,
)
def extract_option_object(option):
"""Convert a click.option call into a click.Option object.
Parameters
----------
option : decorator
A click.option decorator.
Returns
-------
option_object : click.Option
The option object that this decorator will create.
"""
@option
def opt():
pass
return opt.__click_params__[0]
def ipython_only(option):
"""Mark that an option should only be exposed in IPython.
Parameters
----------
option : decorator
A click.option decorator.
Returns
-------
ipython_only_dec : decorator
A decorator that correctly applies the argument even when not
using IPython mode.
"""
if __IPYTHON__:
return option
argname = extract_option_object(option).name
def d(f):
@wraps(f)
def _(*args, **kwargs):
kwargs[argname] = None
return f(*args, **kwargs)
return _
return d
DEFAULT_BUNDLE = "quandl"
@main.command()
@click.option(
"-f",
"--algofile",
default=None,
type=click.File("r"),
help="The file that contains the algorithm to run.",
)
@click.option(
"-t",
"--algotext",
help="The algorithm script to run.",
)
@click.option(
"-D",
"--define",
multiple=True,
help="Define a name to be bound in the namespace before executing"
" the algotext. For example '-Dname=value'. The value may be any "
"python expression. These are evaluated in order so they may refer "
"to previously defined names.",
)
@click.option(
"--data-frequency",
type=click.Choice({"daily", "minute"}),
default="daily",
show_default=True,
help="The data frequency of the simulation.",
)
@click.option(
"--capital-base",
type=float,
default=10e6,
show_default=True,
help="The starting capital for the simulation.",
)
@click.option(
"-b",
"--bundle",
default=DEFAULT_BUNDLE,
metavar="BUNDLE-NAME",
show_default=True,
help="The data bundle to use for the simulation.",
)
@click.option(
"--bundle-timestamp",
type=Timestamp(),
default=pd.Timestamp.utcnow(),
show_default=False,
help="The date to lookup data on or before.\n" "[default: <current-time>]",
)
@click.option(
"-bf",
"--benchmark-file",
default=None,
type=click.Path(exists=True, dir_okay=False, readable=True, path_type=str),
help="The csv file that contains the benchmark returns",
)
@click.option(
"--benchmark-symbol",
default=None,
type=click.STRING,
help="The symbol of the instrument to be used as a benchmark "
"(should exist in the ingested bundle)",
)
@click.option(
"--benchmark-sid",
default=None,
type=int,
help="The sid of the instrument to be used as a benchmark "
"(should exist in the ingested bundle)",
)
@click.option(
"--no-benchmark",
is_flag=True,
default=False,
help="If passed, use a benchmark of zero returns.",
)
@click.option(
"-s",
"--start",
type=Date(as_timestamp=True),
help="The start date of the simulation.",
)
@click.option(
"-e",
"--end",
type=Date(as_timestamp=True),
help="The end date of the simulation.",
)
@click.option(
"-o",
"--output",
default="-",
metavar="FILENAME",
show_default=True,
help="The location to write the perf data. If this is '-' the perf will"
" be written to stdout.",
)
@click.option(
"--trading-calendar",
metavar="TRADING-CALENDAR",
default="XNYS",
help="The calendar you want to use e.g. XLON. XNYS is the default.",
)
@click.option(
"--print-algo/--no-print-algo",
is_flag=True,
default=False,
help="Print the algorithm to stdout.",
)
@click.option(
"--metrics-set",
default="default",
help="The metrics set to use. New metrics sets may be registered in your"
" extension.py.",
)
@click.option(
"--blotter",
default="default",
help="The blotter to use.",
show_default=True,
)
@ipython_only(
click.option(
"--local-namespace/--no-local-namespace",
is_flag=True,
default=None,
help="Should the algorithm methods be " "resolved in the local namespace.",
)
)
@click.pass_context
def run(
ctx,
algofile,
algotext,
define,
data_frequency,
capital_base,
bundle,
bundle_timestamp,
benchmark_file,
benchmark_symbol,
benchmark_sid,
no_benchmark,
start,
end,
output,
trading_calendar,
print_algo,
metrics_set,
local_namespace,
blotter,
):
"""Run a backtest for the given algorithm."""
# check that the start and end dates are passed correctly
if start is None and end is None:
# check both at the same time to avoid the case where a user
# does not pass either of these and then passes the first only
# to be told they need to pass the second argument also
ctx.fail(
"must specify dates with '-s' / '--start' and '-e' / '--end'",
)
if start is None:
ctx.fail("must specify a start date with '-s' / '--start'")
if end is None:
ctx.fail("must specify an end date with '-e' / '--end'")
if (algotext is not None) == (algofile is not None):
ctx.fail(
"must specify exactly one of '-f' / "
"'--algofile' or"
" '-t' / '--algotext'",
)
trading_calendar = get_calendar(trading_calendar)
benchmark_spec = BenchmarkSpec.from_cli_params(
no_benchmark=no_benchmark,
benchmark_sid=benchmark_sid,
benchmark_symbol=benchmark_symbol,
benchmark_file=benchmark_file,
)
return _run(
initialize=None,
handle_data=None,
before_trading_start=None,
analyze=None,
algofile=algofile,
algotext=algotext,
defines=define,
data_frequency=data_frequency,
capital_base=capital_base,
bundle=bundle,
bundle_timestamp=bundle_timestamp,
start=start,
end=end,
output=output,
trading_calendar=trading_calendar,
print_algo=print_algo,
metrics_set=metrics_set,
local_namespace=local_namespace,
environ=os.environ,
blotter=blotter,
benchmark_spec=benchmark_spec,
custom_loader=None,
)
def zipline_magic(line, cell=None):
"""The zipline IPython cell magic."""
load_extensions(
default=True,
extensions=[],
strict=True,
environ=os.environ,
)
try:
return run.main(
# put our overrides at the start of the parameter list so that
# users may pass values with higher precedence
[
"--algotext",
cell,
"--output",
os.devnull, # don't write the results by default
]
+ (
[
# these options are set when running in line magic mode
# set a non None algo text to use the ipython user_ns
"--algotext",
"",
"--local-namespace",
]
if cell is None
else []
)
+ line.split(),
"%s%%zipline" % ((cell or "") and "%"),
# don't use system exit and propogate errors to the caller
standalone_mode=False,
)
except SystemExit as exc:
# https://github.com/mitsuhiko/click/pull/533
# even in standalone_mode=False `--help` really wants to kill us ;_;
if exc.code:
raise ValueError(
"main returned non-zero status code: %d" % exc.code
) from exc
@main.command()
@click.option(
"-b",
"--bundle",
default=DEFAULT_BUNDLE,
metavar="BUNDLE-NAME",
show_default=True,
help="The data bundle to ingest.",
)
@click.option(
"--assets-version",
type=int,
multiple=True,
help="Version of the assets db to which to downgrade.",
)
@click.option(
"--show-progress/--no-show-progress",
default=True,
help="Print progress information to the terminal.",
)
def ingest(bundle, assets_version, show_progress):
"""Ingest the data for the given bundle."""
bundles_module.ingest(
bundle,
os.environ,
pd.Timestamp.utcnow(),
assets_version,
show_progress,
)
@main.command()
@click.option(
"-b",
"--bundle",
default=DEFAULT_BUNDLE,
metavar="BUNDLE-NAME",
show_default=True,
help="The data bundle to clean.",
)
@click.option(
"-e",
"--before",
type=Timestamp(),
help="Clear all data before TIMESTAMP."
" This may not be passed with -k / --keep-last",
)
@click.option(
"-a",
"--after",
type=Timestamp(),
help="Clear all data after TIMESTAMP"
" This may not be passed with -k / --keep-last",
)
@click.option(
"-k",
"--keep-last",
type=int,
metavar="N",
help="Clear all but the last N downloads."
" This may not be passed with -e / --before or -a / --after",
)
def clean(bundle, before, after, keep_last):
"""Clean up data downloaded with the ingest command."""
bundles_module.clean(
bundle,
before,
after,
keep_last,
)
@main.command()
def bundles():
"""List all of the available data bundles."""
for bundle in sorted(bundles_module.bundles.keys()):
if bundle.startswith("."):
# hide the test data
continue
try:
ingestions = list(map(str, bundles_module.ingestions_for_bundle(bundle)))
except OSError as e:
if e.errno != errno.ENOENT:
raise
ingestions = []
# If we got no ingestions, either because the directory didn't exist or
# because there were no entries, print a single message indicating that
# no ingestions have yet been made.
for timestamp in ingestions or ["<no ingestions>"]:
click.echo("%s %s" % (bundle, timestamp))
if __name__ == "__main__":
main()