Skip to content

Commit 235e516

Browse files
authored
DOC: Add return type annotations for buy and sell methods (kernc#975)
* ENH: Explicitly import annotations everywhere Type annotations are now explicitly imported from `__future__` to allow forward references and postponed evaluation (faster import time). See PEP 563 for details. * ENH: add return type annotations for `buy`/`sell` The return type annotations are now added for `buy` and `sell` methods. The documentation is updated to mention that the `Order` is returned. Now it should be crystal clear how to cancel a non-executed order. This should address kernc#957.
1 parent c8e2b37 commit 235e516

File tree

6 files changed

+21
-5
lines changed

6 files changed

+21
-5
lines changed

backtesting/_plotting.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import os
24
import re
35
import sys

backtesting/_stats.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from typing import TYPE_CHECKING, List, Union
24

35
import numpy as np

backtesting/_util.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import warnings
24
from numbers import Number
35
from typing import Dict, List, Optional, Sequence, Union, cast

backtesting/backtesting.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
66
from backtesting import Backtest, Strategy
77
"""
8+
9+
from __future__ import annotations
10+
811
import multiprocessing as mp
912
import os
1013
import sys
@@ -200,9 +203,10 @@ def buy(self, *,
200203
stop: Optional[float] = None,
201204
sl: Optional[float] = None,
202205
tp: Optional[float] = None,
203-
tag: object = None):
206+
tag: object = None) -> 'Order':
204207
"""
205-
Place a new long order. For explanation of parameters, see `Order` and its properties.
208+
Place a new long order and return it. For explanation of parameters, see `Order`
209+
and its properties.
206210
207211
See `Position.close()` and `Trade.close()` for closing existing positions.
208212
@@ -218,9 +222,10 @@ def sell(self, *,
218222
stop: Optional[float] = None,
219223
sl: Optional[float] = None,
220224
tp: Optional[float] = None,
221-
tag: object = None):
225+
tag: object = None) -> 'Order':
222226
"""
223-
Place a new short order. For explanation of parameters, see `Order` and its properties.
227+
Place a new short order and return it. For explanation of parameters, see `Order`
228+
and its properties.
224229
225230
See also `Strategy.buy()`.
226231
@@ -745,7 +750,7 @@ def new_order(self,
745750
tp: Optional[float] = None,
746751
tag: object = None,
747752
*,
748-
trade: Optional[Trade] = None):
753+
trade: Optional[Trade] = None) -> Order:
749754
"""
750755
Argument size indicates whether the order is long or short
751756
"""

backtesting/lib.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
[issue tracker]: https://github.com/kernc/backtesting.py
1212
"""
1313

14+
from __future__ import annotations
15+
1416
from collections import OrderedDict
1517
from inspect import currentframe
1618
from itertools import compress

backtesting/test/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
"""Data and utilities for testing."""
2+
3+
from __future__ import annotations
4+
25
import pandas as pd
36

47

0 commit comments

Comments
 (0)