@@ -196,10 +196,10 @@ def __repr__(self): return '.9999'
196
196
197
197
def buy (self , * ,
198
198
size : float = _FULL_EQUITY ,
199
- limit : float = None ,
200
- stop : float = None ,
201
- sl : float = None ,
202
- tp : float = None ):
199
+ limit : Optional [ float ] = None ,
200
+ stop : Optional [ float ] = None ,
201
+ sl : Optional [ float ] = None ,
202
+ tp : Optional [ float ] = None ):
203
203
"""
204
204
Place a new long order. For explanation of parameters, see `Order` and its properties.
205
205
@@ -213,10 +213,10 @@ def buy(self, *,
213
213
214
214
def sell (self , * ,
215
215
size : float = _FULL_EQUITY ,
216
- limit : float = None ,
217
- stop : float = None ,
218
- sl : float = None ,
219
- tp : float = None ):
216
+ limit : Optional [ float ] = None ,
217
+ stop : Optional [ float ] = None ,
218
+ sl : Optional [ float ] = None ,
219
+ tp : Optional [ float ] = None ):
220
220
"""
221
221
Place a new short order. For explanation of parameters, see `Order` and its properties.
222
222
@@ -382,11 +382,11 @@ class Order:
382
382
"""
383
383
def __init__ (self , broker : '_Broker' ,
384
384
size : float ,
385
- limit_price : float = None ,
386
- stop_price : float = None ,
387
- sl_price : float = None ,
388
- tp_price : float = None ,
389
- parent_trade : 'Trade' = None ):
385
+ limit_price : Optional [ float ] = None ,
386
+ stop_price : Optional [ float ] = None ,
387
+ sl_price : Optional [ float ] = None ,
388
+ tp_price : Optional [ float ] = None ,
389
+ parent_trade : Optional [ 'Trade' ] = None ):
390
390
self .__broker = broker
391
391
assert size != 0
392
392
self .__size = size
@@ -696,12 +696,12 @@ def __repr__(self):
696
696
697
697
def new_order (self ,
698
698
size : float ,
699
- limit : float = None ,
700
- stop : float = None ,
701
- sl : float = None ,
702
- tp : float = None ,
699
+ limit : Optional [ float ] = None ,
700
+ stop : Optional [ float ] = None ,
701
+ sl : Optional [ float ] = None ,
702
+ tp : Optional [ float ] = None ,
703
703
* ,
704
- trade : Trade = None ):
704
+ trade : Optional [ Trade ] = None ):
705
705
"""
706
706
Argument size indicates whether the order is long or short
707
707
"""
@@ -963,7 +963,8 @@ def _close_trade(self, trade: Trade, price: float, time_index: int):
963
963
self .closed_trades .append (trade ._replace (exit_price = price , exit_bar = time_index ))
964
964
self ._cash += trade .pl
965
965
966
- def _open_trade (self , price : float , size : int , sl : float , tp : float , time_index : int ):
966
+ def _open_trade (self , price : float , size : int ,
967
+ sl : Optional [float ], tp : Optional [float ], time_index : int ):
967
968
trade = Trade (self , size , price , time_index )
968
969
self .trades .append (trade )
969
970
# Create SL/TP (bracket) orders.
@@ -1202,11 +1203,11 @@ def run(self, **kwargs) -> pd.Series:
1202
1203
def optimize (self , * ,
1203
1204
maximize : Union [str , Callable [[pd .Series ], float ]] = 'SQN' ,
1204
1205
method : str = 'grid' ,
1205
- max_tries : Union [int , float ] = None ,
1206
- constraint : Callable [[dict ], bool ] = None ,
1206
+ max_tries : Optional [ Union [int , float ] ] = None ,
1207
+ constraint : Optional [ Callable [[dict ], bool ] ] = None ,
1207
1208
return_heatmap : bool = False ,
1208
1209
return_optimization : bool = False ,
1209
- random_state : int = None ,
1210
+ random_state : Optional [ int ] = None ,
1210
1211
** kwargs ) -> Union [pd .Series ,
1211
1212
Tuple [pd .Series , pd .Series ],
1212
1213
Tuple [pd .Series , pd .Series , dict ]]:
@@ -1292,6 +1293,7 @@ def maximize(stats: pd.Series, _key=maximize):
1292
1293
raise TypeError ('`maximize` must be str (a field of backtest.run() result '
1293
1294
'Series) or a function that accepts result Series '
1294
1295
'and returns a number; the higher the better' )
1296
+ assert callable (maximize ), maximize
1295
1297
1296
1298
have_constraint = bool (constraint )
1297
1299
if constraint is None :
@@ -1303,6 +1305,7 @@ def constraint(_):
1303
1305
raise TypeError ("`constraint` must be a function that accepts a dict "
1304
1306
"of strategy parameters and returns a bool whether "
1305
1307
"the combination of parameters is admissible or not" )
1308
+ assert callable (constraint ), constraint
1306
1309
1307
1310
if return_optimization and method != 'skopt' :
1308
1311
raise ValueError ("return_optimization=True only valid if method='skopt'" )
@@ -1320,7 +1323,7 @@ def __getattr__(self, item):
1320
1323
return self [item ]
1321
1324
1322
1325
def _grid_size ():
1323
- size = np .prod ([len (_tuple (v )) for v in kwargs .values ()])
1326
+ size = int ( np .prod ([len (_tuple (v )) for v in kwargs .values ()]) )
1324
1327
if size < 10_000 and have_constraint :
1325
1328
size = sum (1 for p in product (* (zip (repeat (k ), _tuple (v ))
1326
1329
for k , v in kwargs .items ()))
0 commit comments