tbapi.common.decorators
1from functools import wraps 2from tbapi.common.converters import * 3from enum import Enum, IntFlag 4from tbapi.imports import _python_interop_service 5from tbapi.common.script_details import * 6import os, sys 7 8 9def with_wrapper_factory(decorator): 10 """ 11 Decorator for internal use only. 12 """ 13 def wrapped(cls): 14 cls = decorator(cls) 15 16 if os.environ.get("INSTANTIATION_ENABLED") != "1": 17 return cls 18 19 module = sys.modules[cls.__module__] 20 current_file = os.path.abspath(module.__file__) 21 orig_init = getattr(cls, "__init__", lambda self, *a, **kw: None) 22 23 @wraps(orig_init) 24 def __init__(self, *args, **kwargs): 25 if "_existing" not in kwargs or kwargs["_existing"] is None: 26 wrapper_value = _python_interop_service.CreateWrapperInstance( 27 current_file, cls.__name__, self 28 ) 29 kwargs["_existing"] = wrapper_value 30 self._value = wrapper_value 31 orig_init(self, *args, **kwargs) 32 33 if hasattr(self, "_value") and self._value is not None: 34 _python_interop_service.TryInvokePostInit(self._value) 35 36 cls.__init__ = __init__ 37 return cls 38 39 return wrapped 40 41 42def tb_indicator(arg=None, *, display_name: str = ""): 43 """ 44 Todo: description (indicator) 45 """ 46 if callable(arg): 47 cls = arg 48 cls.__is_indicator__ = True 49 cls.__script_details__ = IndicatorDetails(display_name) 50 return with_wrapper_factory(lambda c: c)(cls) 51 52 @with_wrapper_factory 53 def decorator(cls): 54 cls.__is_indicator__ = True 55 cls.__script_details__ = IndicatorDetails(display_name) 56 return cls 57 return decorator 58 59 60def tb_drawing(arg=None, *, points_count: int = 1): 61 """ 62 Todo: description (drawing) 63 """ 64 if callable(arg): 65 cls = arg 66 cls.__is_drawing__ = True 67 cls.__script_details__ = DrawingDetails(points_count) 68 return with_wrapper_factory(lambda c: c)(cls) 69 70 @with_wrapper_factory 71 def decorator(cls): 72 cls.__is_drawing__ = True 73 cls.__script_details__ = DrawingDetails(points_count) 74 return cls 75 return decorator 76 77 78def tb_strategy(arg=None): 79 """ 80 Todo: description (strategy) 81 """ 82 if callable(arg): 83 cls = arg 84 cls.__is_strategy__ = True 85 return with_wrapper_factory(lambda c: c)(cls) 86 87 @with_wrapper_factory 88 def decorator(cls): 89 cls.__is_strategy__ = True 90 return cls 91 return decorator 92 93 94def tb_bar_type(arg=None): 95 """ 96 Todo: description (bar_type) 97 """ 98 if callable(arg): 99 cls = arg 100 cls.__is_bar_type__ = True 101 return with_wrapper_factory(lambda c: c)(cls) 102 103 @with_wrapper_factory 104 def decorator(cls): 105 cls.__is_bar_type__ = True 106 return cls 107 return decorator 108 109 110def tb_tms(arg=None): 111 """ 112 Todo: description (tms) 113 """ 114 if callable(arg): 115 cls = arg 116 cls.__is_tms__ = True 117 return with_wrapper_factory(lambda c: c)(cls) 118 119 @with_wrapper_factory 120 def decorator(cls): 121 cls.__is_tms__ = True 122 return cls 123 return decorator 124 125 126def tb_class(origin_type): 127 """ 128 Decorator for internal use only. 129 """ 130 def decorator(cls): 131 cls._value = None 132 cls.__tb_decorated__ = True 133 orig_new = getattr(cls, "__new__", object.__new__) 134 135 def __new__(subcls, *args, **kwargs): 136 _existing = kwargs.pop("_existing", None) 137 138 obj = orig_new(subcls) 139 140 if cls is not subcls: 141 return obj 142 143 if hasattr(obj, "_value") and obj._value is not None: 144 return obj 145 146 if _existing is not None: 147 obj._value = _existing 148 else: 149 processed_args = [] 150 for arg in args: 151 if isinstance(arg, (Enum, IntFlag)): 152 processed_args.append(get_origin_enum(cls, arg)) 153 elif hasattr(arg, "_value"): 154 processed_args.append(arg._value) 155 else: 156 processed_args.append(arg) 157 obj._value = origin_type(*processed_args) 158 159 return obj 160 161 def __init__(self, *args, **kwargs): 162 pass 163 164 def __repr__(self): 165 return f"{cls.__name__}({self._value.ToString()})" 166 167 cls.__new__ = staticmethod(__new__) 168 cls.__init__ = __init__ 169 cls.__repr__ = __repr__ 170 return cls 171 172 return decorator 173 174 175def tb_interface(origin_type): 176 """ 177 Decorator for internal use only. 178 """ 179 def decorator(cls): 180 cls._value = None 181 cls.__tb_decorated__ = True 182 orig_new = getattr(cls, "__new__", object.__new__) 183 184 def __new__(subcls, *args, **kwargs): 185 _existing = kwargs.pop("_existing", None) 186 187 if _existing is not None: 188 obj = orig_new(subcls) 189 obj._value = _existing 190 return obj 191 192 if cls is subcls: 193 raise TypeError(f"Cannot instantiate class {cls.__name__} directly") 194 195 obj = orig_new(subcls) 196 return obj 197 198 def __init__(self, *args, **kwargs): 199 pass 200 201 def __repr__(self): 202 return f"{cls.__name__}({self._value.ToString()})" 203 204 cls.__new__ = staticmethod(__new__) 205 cls.__init__ = __init__ 206 cls.__repr__ = __repr__ 207 return cls 208 209 return decorator
def
with_wrapper_factory(decorator):
10def with_wrapper_factory(decorator): 11 """ 12 Decorator for internal use only. 13 """ 14 def wrapped(cls): 15 cls = decorator(cls) 16 17 if os.environ.get("INSTANTIATION_ENABLED") != "1": 18 return cls 19 20 module = sys.modules[cls.__module__] 21 current_file = os.path.abspath(module.__file__) 22 orig_init = getattr(cls, "__init__", lambda self, *a, **kw: None) 23 24 @wraps(orig_init) 25 def __init__(self, *args, **kwargs): 26 if "_existing" not in kwargs or kwargs["_existing"] is None: 27 wrapper_value = _python_interop_service.CreateWrapperInstance( 28 current_file, cls.__name__, self 29 ) 30 kwargs["_existing"] = wrapper_value 31 self._value = wrapper_value 32 orig_init(self, *args, **kwargs) 33 34 if hasattr(self, "_value") and self._value is not None: 35 _python_interop_service.TryInvokePostInit(self._value) 36 37 cls.__init__ = __init__ 38 return cls 39 40 return wrapped
Decorator for internal use only.
def
tb_indicator(arg=None, *, display_name: str = ''):
43def tb_indicator(arg=None, *, display_name: str = ""): 44 """ 45 Todo: description (indicator) 46 """ 47 if callable(arg): 48 cls = arg 49 cls.__is_indicator__ = True 50 cls.__script_details__ = IndicatorDetails(display_name) 51 return with_wrapper_factory(lambda c: c)(cls) 52 53 @with_wrapper_factory 54 def decorator(cls): 55 cls.__is_indicator__ = True 56 cls.__script_details__ = IndicatorDetails(display_name) 57 return cls 58 return decorator
Todo: description (indicator)
def
tb_drawing(arg=None, *, points_count: int = 1):
61def tb_drawing(arg=None, *, points_count: int = 1): 62 """ 63 Todo: description (drawing) 64 """ 65 if callable(arg): 66 cls = arg 67 cls.__is_drawing__ = True 68 cls.__script_details__ = DrawingDetails(points_count) 69 return with_wrapper_factory(lambda c: c)(cls) 70 71 @with_wrapper_factory 72 def decorator(cls): 73 cls.__is_drawing__ = True 74 cls.__script_details__ = DrawingDetails(points_count) 75 return cls 76 return decorator
Todo: description (drawing)
def
tb_strategy(arg=None):
79def tb_strategy(arg=None): 80 """ 81 Todo: description (strategy) 82 """ 83 if callable(arg): 84 cls = arg 85 cls.__is_strategy__ = True 86 return with_wrapper_factory(lambda c: c)(cls) 87 88 @with_wrapper_factory 89 def decorator(cls): 90 cls.__is_strategy__ = True 91 return cls 92 return decorator
Todo: description (strategy)
def
tb_bar_type(arg=None):
95def tb_bar_type(arg=None): 96 """ 97 Todo: description (bar_type) 98 """ 99 if callable(arg): 100 cls = arg 101 cls.__is_bar_type__ = True 102 return with_wrapper_factory(lambda c: c)(cls) 103 104 @with_wrapper_factory 105 def decorator(cls): 106 cls.__is_bar_type__ = True 107 return cls 108 return decorator
Todo: description (bar_type)
def
tb_tms(arg=None):
111def tb_tms(arg=None): 112 """ 113 Todo: description (tms) 114 """ 115 if callable(arg): 116 cls = arg 117 cls.__is_tms__ = True 118 return with_wrapper_factory(lambda c: c)(cls) 119 120 @with_wrapper_factory 121 def decorator(cls): 122 cls.__is_tms__ = True 123 return cls 124 return decorator
Todo: description (tms)
def
tb_class(origin_type):
127def tb_class(origin_type): 128 """ 129 Decorator for internal use only. 130 """ 131 def decorator(cls): 132 cls._value = None 133 cls.__tb_decorated__ = True 134 orig_new = getattr(cls, "__new__", object.__new__) 135 136 def __new__(subcls, *args, **kwargs): 137 _existing = kwargs.pop("_existing", None) 138 139 obj = orig_new(subcls) 140 141 if cls is not subcls: 142 return obj 143 144 if hasattr(obj, "_value") and obj._value is not None: 145 return obj 146 147 if _existing is not None: 148 obj._value = _existing 149 else: 150 processed_args = [] 151 for arg in args: 152 if isinstance(arg, (Enum, IntFlag)): 153 processed_args.append(get_origin_enum(cls, arg)) 154 elif hasattr(arg, "_value"): 155 processed_args.append(arg._value) 156 else: 157 processed_args.append(arg) 158 obj._value = origin_type(*processed_args) 159 160 return obj 161 162 def __init__(self, *args, **kwargs): 163 pass 164 165 def __repr__(self): 166 return f"{cls.__name__}({self._value.ToString()})" 167 168 cls.__new__ = staticmethod(__new__) 169 cls.__init__ = __init__ 170 cls.__repr__ = __repr__ 171 return cls 172 173 return decorator
Decorator for internal use only.
def
tb_interface(origin_type):
176def tb_interface(origin_type): 177 """ 178 Decorator for internal use only. 179 """ 180 def decorator(cls): 181 cls._value = None 182 cls.__tb_decorated__ = True 183 orig_new = getattr(cls, "__new__", object.__new__) 184 185 def __new__(subcls, *args, **kwargs): 186 _existing = kwargs.pop("_existing", None) 187 188 if _existing is not None: 189 obj = orig_new(subcls) 190 obj._value = _existing 191 return obj 192 193 if cls is subcls: 194 raise TypeError(f"Cannot instantiate class {cls.__name__} directly") 195 196 obj = orig_new(subcls) 197 return obj 198 199 def __init__(self, *args, **kwargs): 200 pass 201 202 def __repr__(self): 203 return f"{cls.__name__}({self._value.ToString()})" 204 205 cls.__new__ = staticmethod(__new__) 206 cls.__init__ = __init__ 207 cls.__repr__ = __repr__ 208 return cls 209 210 return decorator
Decorator for internal use only.