|
1
|
|
|
# |
|
2
|
|
|
# Copyright 2014 Quantopian, Inc. |
|
3
|
|
|
# |
|
4
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); |
|
5
|
|
|
# you may not use this file except in compliance with the License. |
|
6
|
|
|
# You may obtain a copy of the License at |
|
7
|
|
|
# |
|
8
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
|
9
|
|
|
# |
|
10
|
|
|
# Unless required by applicable law or agreed to in writing, software |
|
11
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
|
12
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13
|
|
|
# See the License for the specific language governing permissions and |
|
14
|
|
|
# limitations under the License. |
|
15
|
|
|
|
|
16
|
|
|
from functools import wraps |
|
17
|
|
|
|
|
18
|
|
|
import zipline.api |
|
19
|
|
|
from zipline.utils.algo_instance import get_algo_instance, set_algo_instance |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
class ZiplineAPI(object): |
|
23
|
|
|
""" |
|
24
|
|
|
Context manager for making an algorithm instance available to zipline API |
|
25
|
|
|
functions within a scoped block. |
|
26
|
|
|
""" |
|
27
|
|
|
|
|
28
|
|
|
def __init__(self, algo_instance): |
|
29
|
|
|
self.algo_instance = algo_instance |
|
30
|
|
|
|
|
31
|
|
|
def __enter__(self): |
|
32
|
|
|
""" |
|
33
|
|
|
Set the given algo instance, storing any previously-existing instance. |
|
34
|
|
|
""" |
|
35
|
|
|
self.old_algo_instance = get_algo_instance() |
|
36
|
|
|
set_algo_instance(self.algo_instance) |
|
37
|
|
|
|
|
38
|
|
|
def __exit__(self, _type, _value, _tb): |
|
39
|
|
|
""" |
|
40
|
|
|
Restore the algo instance stored in __enter__. |
|
41
|
|
|
""" |
|
42
|
|
|
set_algo_instance(self.old_algo_instance) |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
def api_method(f): |
|
46
|
|
|
# Decorator that adds the decorated class method as a callable |
|
47
|
|
|
# function (wrapped) to zipline.api |
|
48
|
|
|
@wraps(f) |
|
49
|
|
|
def wrapped(*args, **kwargs): |
|
50
|
|
|
# Get the instance and call the method |
|
51
|
|
|
return getattr(get_algo_instance(), f.__name__)(*args, **kwargs) |
|
52
|
|
|
# Add functor to zipline.api |
|
53
|
|
|
setattr(zipline.api, f.__name__, wrapped) |
|
54
|
|
|
zipline.api.__all__.append(f.__name__) |
|
55
|
|
|
f.is_api_method = True |
|
56
|
|
|
return f |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
def require_not_initialized(exception): |
|
60
|
|
|
""" |
|
61
|
|
|
Decorator for API methods that should only be called during or before |
|
62
|
|
|
TradingAlgorithm.initialize. `exception` will be raised if the method is |
|
63
|
|
|
called after initialize. |
|
64
|
|
|
|
|
65
|
|
|
Usage |
|
66
|
|
|
----- |
|
67
|
|
|
@require_not_initialized(SomeException("Don't do that!")) |
|
68
|
|
|
def method(self): |
|
69
|
|
|
# Do stuff that should only be allowed during initialize. |
|
70
|
|
|
""" |
|
71
|
|
|
def decorator(method): |
|
72
|
|
|
@wraps(method) |
|
73
|
|
|
def wrapped_method(self, *args, **kwargs): |
|
74
|
|
|
if self.initialized: |
|
75
|
|
|
raise exception |
|
76
|
|
|
return method(self, *args, **kwargs) |
|
77
|
|
|
return wrapped_method |
|
78
|
|
|
return decorator |
|
79
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
def require_initialized(exception): |
|
82
|
|
|
""" |
|
83
|
|
|
Decorator for API methods that should only be called after |
|
84
|
|
|
TradingAlgorithm.initialize. `exception` will be raised if the method is |
|
85
|
|
|
called before initialize has completed. |
|
86
|
|
|
|
|
87
|
|
|
Usage |
|
88
|
|
|
----- |
|
89
|
|
|
@require_initialized(SomeException("Don't do that!")) |
|
90
|
|
|
def method(self): |
|
91
|
|
|
# Do stuff that should only be allowed after initialize. |
|
92
|
|
|
""" |
|
93
|
|
|
def decorator(method): |
|
94
|
|
|
@wraps(method) |
|
95
|
|
|
def wrapped_method(self, *args, **kwargs): |
|
96
|
|
|
if not self.initialized: |
|
97
|
|
|
raise exception |
|
98
|
|
|
return method(self, *args, **kwargs) |
|
99
|
|
|
return wrapped_method |
|
100
|
|
|
return decorator |
|
101
|
|
|
|