From 56bc68f9e999fca8f453da6c1c97b648df431e5b Mon Sep 17 00:00:00 2001 From: Ivan Vaigult Date: Tue, 2 May 2023 23:49:46 +0100 Subject: [PATCH 1/2] 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. --- backtesting/_plotting.py | 2 ++ backtesting/_stats.py | 2 ++ backtesting/_util.py | 2 ++ backtesting/backtesting.py | 3 +++ backtesting/lib.py | 2 ++ backtesting/test/__init__.py | 3 +++ 6 files changed, 14 insertions(+) diff --git a/backtesting/_plotting.py b/backtesting/_plotting.py index 844318aa..4e10aba3 100644 --- a/backtesting/_plotting.py +++ b/backtesting/_plotting.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import os import re import sys diff --git a/backtesting/_stats.py b/backtesting/_stats.py index f2bb4f7c..28418b64 100644 --- a/backtesting/_stats.py +++ b/backtesting/_stats.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from typing import TYPE_CHECKING, List, Union import numpy as np diff --git a/backtesting/_util.py b/backtesting/_util.py index ab6d2916..a38cdfc2 100644 --- a/backtesting/_util.py +++ b/backtesting/_util.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import warnings from numbers import Number from typing import Dict, List, Optional, Sequence, Union, cast diff --git a/backtesting/backtesting.py b/backtesting/backtesting.py index 9c168703..33a55e5a 100644 --- a/backtesting/backtesting.py +++ b/backtesting/backtesting.py @@ -5,6 +5,9 @@ from backtesting import Backtest, Strategy """ + +from __future__ import annotations + import multiprocessing as mp import os import sys diff --git a/backtesting/lib.py b/backtesting/lib.py index 14c01efd..7375a7fe 100644 --- a/backtesting/lib.py +++ b/backtesting/lib.py @@ -11,6 +11,8 @@ [issue tracker]: https://github.com/kernc/backtesting.py """ +from __future__ import annotations + from collections import OrderedDict from inspect import currentframe from itertools import compress diff --git a/backtesting/test/__init__.py b/backtesting/test/__init__.py index 2f4b2875..babf4212 100644 --- a/backtesting/test/__init__.py +++ b/backtesting/test/__init__.py @@ -1,4 +1,7 @@ """Data and utilities for testing.""" + +from __future__ import annotations + import pandas as pd From b5c9cdb16554d6331adc2dd5962c82e4998bf35d Mon Sep 17 00:00:00 2001 From: Ivan Vaigult Date: Wed, 3 May 2023 00:15:43 +0100 Subject: [PATCH 2/2] 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 #957. --- backtesting/backtesting.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/backtesting/backtesting.py b/backtesting/backtesting.py index 33a55e5a..b4b1e1fb 100644 --- a/backtesting/backtesting.py +++ b/backtesting/backtesting.py @@ -203,9 +203,10 @@ def buy(self, *, stop: Optional[float] = None, sl: Optional[float] = None, tp: Optional[float] = None, - tag: object = None): + tag: object = None) -> 'Order': """ - Place a new long order. For explanation of parameters, see `Order` and its properties. + Place a new long order and return it. For explanation of parameters, see `Order` + and its properties. See `Position.close()` and `Trade.close()` for closing existing positions. @@ -221,9 +222,10 @@ def sell(self, *, stop: Optional[float] = None, sl: Optional[float] = None, tp: Optional[float] = None, - tag: object = None): + tag: object = None) -> 'Order': """ - Place a new short order. For explanation of parameters, see `Order` and its properties. + Place a new short order and return it. For explanation of parameters, see `Order` + and its properties. See also `Strategy.buy()`. @@ -733,7 +735,7 @@ def new_order(self, tp: Optional[float] = None, tag: object = None, *, - trade: Optional[Trade] = None): + trade: Optional[Trade] = None) -> Order: """ Argument size indicates whether the order is long or short """