PyAlgoEngine 0.5.2__tar.gz → 0.5.4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {pyalgoengine-0.5.2 → pyalgoengine-0.5.4}/PKG-INFO +1 -1
- {pyalgoengine-0.5.2 → pyalgoengine-0.5.4}/PyAlgoEngine.egg-info/PKG-INFO +1 -1
- {pyalgoengine-0.5.2 → pyalgoengine-0.5.4}/PyAlgoEngine.egg-info/SOURCES.txt +5 -5
- {pyalgoengine-0.5.2 → pyalgoengine-0.5.4}/algo_engine/__init__.py +3 -3
- {pyalgoengine-0.5.2 → pyalgoengine-0.5.4}/algo_engine/apps/backtest/tester.py +2 -2
- {pyalgoengine-0.5.2/algo_engine/back_test → pyalgoengine-0.5.4/algo_engine/backtest}/__main__.py +2 -2
- {pyalgoengine-0.5.2/algo_engine/back_test → pyalgoengine-0.5.4/algo_engine/backtest}/metrics.py +29 -29
- {pyalgoengine-0.5.2 → pyalgoengine-0.5.4}/algo_engine/strategy/strategy_engine.py +3 -3
- {pyalgoengine-0.5.2 → pyalgoengine-0.5.4}/LICENSE +0 -0
- {pyalgoengine-0.5.2 → pyalgoengine-0.5.4}/PyAlgoEngine.egg-info/dependency_links.txt +0 -0
- {pyalgoengine-0.5.2 → pyalgoengine-0.5.4}/PyAlgoEngine.egg-info/requires.txt +0 -0
- {pyalgoengine-0.5.2 → pyalgoengine-0.5.4}/PyAlgoEngine.egg-info/top_level.txt +0 -0
- {pyalgoengine-0.5.2 → pyalgoengine-0.5.4}/README.md +0 -0
- {pyalgoengine-0.5.2 → pyalgoengine-0.5.4}/algo_engine/apps/__init__.py +0 -0
- {pyalgoengine-0.5.2 → pyalgoengine-0.5.4}/algo_engine/apps/backtest/__init__.py +0 -0
- {pyalgoengine-0.5.2 → pyalgoengine-0.5.4}/algo_engine/apps/backtest/doc_server.py +0 -0
- {pyalgoengine-0.5.2 → pyalgoengine-0.5.4}/algo_engine/apps/backtest/web_app.py +0 -0
- {pyalgoengine-0.5.2 → pyalgoengine-0.5.4}/algo_engine/apps/bokeh_server.py +0 -0
- {pyalgoengine-0.5.2 → pyalgoengine-0.5.4}/algo_engine/apps/demo/__init__.py +0 -0
- {pyalgoengine-0.5.2 → pyalgoengine-0.5.4}/algo_engine/apps/demo/test.py +0 -0
- {pyalgoengine-0.5.2/algo_engine/back_test → pyalgoengine-0.5.4/algo_engine/backtest}/__init__.py +0 -0
- {pyalgoengine-0.5.2/algo_engine/back_test → pyalgoengine-0.5.4/algo_engine/backtest}/replay.py +0 -0
- {pyalgoengine-0.5.2/algo_engine/back_test → pyalgoengine-0.5.4/algo_engine/backtest}/sim_match.py +0 -0
- {pyalgoengine-0.5.2 → pyalgoengine-0.5.4}/algo_engine/base/__init__.py +0 -0
- {pyalgoengine-0.5.2 → pyalgoengine-0.5.4}/algo_engine/base/console_utils.py +0 -0
- {pyalgoengine-0.5.2 → pyalgoengine-0.5.4}/algo_engine/base/finance_decimal.py +0 -0
- {pyalgoengine-0.5.2 → pyalgoengine-0.5.4}/algo_engine/base/market_utils.py +0 -0
- {pyalgoengine-0.5.2 → pyalgoengine-0.5.4}/algo_engine/base/technical_analysis.py +0 -0
- {pyalgoengine-0.5.2 → pyalgoengine-0.5.4}/algo_engine/base/telemetrics.py +0 -0
- {pyalgoengine-0.5.2 → pyalgoengine-0.5.4}/algo_engine/base/trade_utils.py +0 -0
- {pyalgoengine-0.5.2 → pyalgoengine-0.5.4}/algo_engine/engine/__init__.py +0 -0
- {pyalgoengine-0.5.2 → pyalgoengine-0.5.4}/algo_engine/engine/algo_engine.py +0 -0
- {pyalgoengine-0.5.2 → pyalgoengine-0.5.4}/algo_engine/engine/event_engine.py +0 -0
- {pyalgoengine-0.5.2 → pyalgoengine-0.5.4}/algo_engine/engine/market_engine.py +0 -0
- {pyalgoengine-0.5.2 → pyalgoengine-0.5.4}/algo_engine/engine/trade_engine.py +0 -0
- {pyalgoengine-0.5.2 → pyalgoengine-0.5.4}/algo_engine/monitor/__init__.py +0 -0
- {pyalgoengine-0.5.2 → pyalgoengine-0.5.4}/algo_engine/monitor/advanced_data_interface.py +0 -0
- {pyalgoengine-0.5.2 → pyalgoengine-0.5.4}/algo_engine/profile/__init__.py +0 -0
- {pyalgoengine-0.5.2 → pyalgoengine-0.5.4}/algo_engine/profile/cn.py +0 -0
- {pyalgoengine-0.5.2 → pyalgoengine-0.5.4}/algo_engine/strategy/__init__.py +0 -0
- {pyalgoengine-0.5.2 → pyalgoengine-0.5.4}/algo_engine/utils/__init__.py +0 -0
- {pyalgoengine-0.5.2 → pyalgoengine-0.5.4}/algo_engine/utils/commit_regularizer.py +0 -0
- {pyalgoengine-0.5.2 → pyalgoengine-0.5.4}/algo_engine/utils/data_utils.py +0 -0
- {pyalgoengine-0.5.2 → pyalgoengine-0.5.4}/setup.cfg +0 -0
- {pyalgoengine-0.5.2 → pyalgoengine-0.5.4}/setup.py +0 -0
|
@@ -15,11 +15,11 @@ algo_engine/apps/backtest/tester.py
|
|
|
15
15
|
algo_engine/apps/backtest/web_app.py
|
|
16
16
|
algo_engine/apps/demo/__init__.py
|
|
17
17
|
algo_engine/apps/demo/test.py
|
|
18
|
-
algo_engine/
|
|
19
|
-
algo_engine/
|
|
20
|
-
algo_engine/
|
|
21
|
-
algo_engine/
|
|
22
|
-
algo_engine/
|
|
18
|
+
algo_engine/backtest/__init__.py
|
|
19
|
+
algo_engine/backtest/__main__.py
|
|
20
|
+
algo_engine/backtest/metrics.py
|
|
21
|
+
algo_engine/backtest/replay.py
|
|
22
|
+
algo_engine/backtest/sim_match.py
|
|
23
23
|
algo_engine/base/__init__.py
|
|
24
24
|
algo_engine/base/console_utils.py
|
|
25
25
|
algo_engine/base/finance_decimal.py
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
__version__ = "0.5.
|
|
1
|
+
__version__ = "0.5.4"
|
|
2
2
|
|
|
3
3
|
import logging
|
|
4
4
|
import os
|
|
@@ -16,14 +16,14 @@ else:
|
|
|
16
16
|
def set_logger(logger: logging.Logger):
|
|
17
17
|
base.set_logger(logger=logger)
|
|
18
18
|
engine.set_logger(logger=logger.getChild('Engine'))
|
|
19
|
-
|
|
19
|
+
backtest.set_logger(logger=logger.getChild('BackTest'))
|
|
20
20
|
strategy.set_logger(logger=logger.getChild('Strategy'))
|
|
21
21
|
apps.set_logger(logger=logger.getChild('Apps'))
|
|
22
22
|
|
|
23
23
|
|
|
24
24
|
from . import base
|
|
25
25
|
from . import engine
|
|
26
|
-
from . import
|
|
26
|
+
from . import backtest
|
|
27
27
|
from . import strategy
|
|
28
28
|
from . import apps
|
|
29
29
|
|
|
@@ -5,10 +5,10 @@ from typing import Literal
|
|
|
5
5
|
|
|
6
6
|
import numpy as np
|
|
7
7
|
|
|
8
|
-
from algo_engine.
|
|
8
|
+
from algo_engine.backtest.metrics import TradeMetrics
|
|
9
9
|
from . import LOGGER
|
|
10
10
|
from .web_app import WebApp
|
|
11
|
-
from ...
|
|
11
|
+
from ...backtest import SimMatch, ProgressiveReplay
|
|
12
12
|
from ...base import MarketData, TradeReport, TradeInstruction
|
|
13
13
|
from ...profile import Profile, PROFILE
|
|
14
14
|
|
{pyalgoengine-0.5.2/algo_engine/back_test → pyalgoengine-0.5.4/algo_engine/backtest}/metrics.py
RENAMED
|
@@ -13,16 +13,16 @@ class TradeMetrics(object):
|
|
|
13
13
|
self.total_pnl = 0.
|
|
14
14
|
self.total_cash_flow = 0.
|
|
15
15
|
|
|
16
|
-
self.
|
|
17
|
-
self.
|
|
18
|
-
self.
|
|
19
|
-
self.
|
|
16
|
+
self.current_pnl = 0.
|
|
17
|
+
self.current_cash_flow = 0.
|
|
18
|
+
self.current_trade_batch = {'cash_flow': 0., 'pnl': 0., 'turnover': 0., 'trades': []}
|
|
19
|
+
self.market_price = None
|
|
20
20
|
|
|
21
21
|
def update(self, market_price: float):
|
|
22
|
-
self.
|
|
22
|
+
self.market_price = market_price
|
|
23
23
|
self.total_pnl = self.exposure * market_price + self.total_cash_flow
|
|
24
|
-
self.
|
|
25
|
-
self.
|
|
24
|
+
self.current_pnl = self.exposure * market_price + self.current_cash_flow
|
|
25
|
+
self.current_trade_batch['pnl'] = self.exposure * market_price + self.current_trade_batch['cash_flow']
|
|
26
26
|
|
|
27
27
|
def add_trades(self, side: int, price: float, timestamp: float, volume: float = None, trade_id: int | str = None):
|
|
28
28
|
assert side in {1, -1}, f"trade side must in {1, -1}, got {side}."
|
|
@@ -50,9 +50,9 @@ class TradeMetrics(object):
|
|
|
50
50
|
self.exposure += volume * side
|
|
51
51
|
self.total_cash_flow -= volume * side * price
|
|
52
52
|
self.total_pnl = self.exposure * price + self.total_cash_flow
|
|
53
|
-
self.
|
|
54
|
-
self.
|
|
55
|
-
self.
|
|
53
|
+
self.current_cash_flow -= volume * side * price
|
|
54
|
+
self.current_pnl = self.exposure * price + self.current_cash_flow
|
|
55
|
+
self.market_price = price
|
|
56
56
|
|
|
57
57
|
self.trades[trade_id] = trade_log = dict(
|
|
58
58
|
side=side,
|
|
@@ -60,22 +60,22 @@ class TradeMetrics(object):
|
|
|
60
60
|
timestamp=timestamp,
|
|
61
61
|
price=price,
|
|
62
62
|
exposure=self.exposure,
|
|
63
|
-
cash_flow=self.
|
|
64
|
-
pnl=self.
|
|
63
|
+
cash_flow=self.current_cash_flow,
|
|
64
|
+
pnl=self.current_pnl
|
|
65
65
|
)
|
|
66
66
|
|
|
67
|
-
if 'init_side' not in self.
|
|
68
|
-
self.
|
|
67
|
+
if 'init_side' not in self.current_trade_batch:
|
|
68
|
+
self.current_trade_batch['init_side'] = side
|
|
69
69
|
|
|
70
|
-
self.
|
|
71
|
-
self.
|
|
72
|
-
self.
|
|
73
|
-
self.
|
|
70
|
+
self.current_trade_batch['cash_flow'] -= volume * side * price
|
|
71
|
+
self.current_trade_batch['pnl'] = self.exposure * price + self.current_trade_batch['cash_flow']
|
|
72
|
+
self.current_trade_batch['turnover'] += abs(volume) * price
|
|
73
|
+
self.current_trade_batch['trades'].append(trade_log)
|
|
74
74
|
|
|
75
75
|
if not self.exposure:
|
|
76
|
-
self.trade_batch.append(self.
|
|
77
|
-
self.
|
|
78
|
-
self.
|
|
76
|
+
self.trade_batch.append(self.current_trade_batch)
|
|
77
|
+
self.current_trade_batch = {'cash_flow': 0., 'pnl': 0., 'turnover': 0., 'trades': []}
|
|
78
|
+
self.current_pnl = self.current_cash_flow = 0.
|
|
79
79
|
|
|
80
80
|
def add_trades_batch(self, trade_logs: pd.DataFrame):
|
|
81
81
|
for timestamp, row in trade_logs.iterrows(): # type: float, dict
|
|
@@ -92,10 +92,10 @@ class TradeMetrics(object):
|
|
|
92
92
|
self.total_pnl = 0.
|
|
93
93
|
self.total_cash_flow = 0.
|
|
94
94
|
|
|
95
|
-
self.
|
|
96
|
-
self.
|
|
97
|
-
self.
|
|
98
|
-
self.
|
|
95
|
+
self.current_pnl = 0.
|
|
96
|
+
self.current_cash_flow = 0.
|
|
97
|
+
self.current_trade_batch = {'cash_flow': 0., 'pnl': 0., 'turnover': 0., 'trades': []}
|
|
98
|
+
self.market_price = None
|
|
99
99
|
|
|
100
100
|
@property
|
|
101
101
|
def summary(self):
|
|
@@ -121,11 +121,11 @@ class TradeMetrics(object):
|
|
|
121
121
|
info_dict['turnover'] += trade_batch['turnover']
|
|
122
122
|
|
|
123
123
|
info_dict['win_rate'] = info_dict['win_count'] / info_dict['trade_count'] if info_dict['trade_count'] else 0.
|
|
124
|
-
info_dict['average_gain'] = info_dict['total_gain'] / info_dict['win_count'] / self.
|
|
125
|
-
info_dict['average_loss'] = info_dict['total_loss'] / info_dict['lose_count'] / self.
|
|
124
|
+
info_dict['average_gain'] = info_dict['total_gain'] / info_dict['win_count'] / self.market_price if info_dict['win_count'] else 0.
|
|
125
|
+
info_dict['average_loss'] = info_dict['total_loss'] / info_dict['lose_count'] / self.market_price if info_dict['lose_count'] else 0.
|
|
126
126
|
info_dict['gain_loss_ratio'] = -info_dict['average_gain'] / info_dict['average_loss'] if info_dict['average_loss'] else 1.
|
|
127
|
-
info_dict['long_avg_pnl'] = np.average([_['pnl'] for _ in long_trades]) / self.
|
|
128
|
-
info_dict['short_avg_pnl'] = np.average([_['pnl'] for _ in short_trades]) / self.
|
|
127
|
+
info_dict['long_avg_pnl'] = np.average([_['pnl'] for _ in long_trades]) / self.market_price if (long_trades := [_ for _ in self.trade_batch if _['init_side'] == 1]) else np.nan
|
|
128
|
+
info_dict['short_avg_pnl'] = np.average([_['pnl'] for _ in short_trades]) / self.market_price if (short_trades := [_ for _ in self.trade_batch if _['init_side'] == -1]) else np.nan
|
|
129
129
|
info_dict['ttl_pnl.no_leverage'] = np.sum([trade_batch['pnl'] for trade_batch in self.trade_batch])
|
|
130
130
|
info_dict['net_pnl.optimistic'] = info_dict['ttl_pnl.no_leverage'] - (0.00034 + 0.000023) / 2 * info_dict['turnover']
|
|
131
131
|
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import abc
|
|
2
2
|
import datetime
|
|
3
3
|
import time
|
|
4
|
+
from collections.abc import Callable
|
|
4
5
|
from functools import cached_property
|
|
5
|
-
from typing import Callable
|
|
6
6
|
|
|
7
7
|
from . import LOGGER
|
|
8
|
-
from ..
|
|
8
|
+
from ..backtest import SimMatch, ProgressiveReplay
|
|
9
9
|
from ..base import MarketData, TradeReport, TradeInstruction, TransactionSide
|
|
10
10
|
from ..engine import PositionManagementService, TOPIC, EVENT_ENGINE
|
|
11
11
|
|
|
@@ -35,7 +35,7 @@ class StrategyEngineTemplate(object, metaclass=abc.ABCMeta):
|
|
|
35
35
|
|
|
36
36
|
@property
|
|
37
37
|
def dma(self):
|
|
38
|
-
return self.position_tracker.dma
|
|
38
|
+
return self.position_tracker.dma
|
|
39
39
|
|
|
40
40
|
@property
|
|
41
41
|
def risk_profile(self):
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{pyalgoengine-0.5.2/algo_engine/back_test → pyalgoengine-0.5.4/algo_engine/backtest}/__init__.py
RENAMED
|
File without changes
|
{pyalgoengine-0.5.2/algo_engine/back_test → pyalgoengine-0.5.4/algo_engine/backtest}/replay.py
RENAMED
|
File without changes
|
{pyalgoengine-0.5.2/algo_engine/back_test → pyalgoengine-0.5.4/algo_engine/backtest}/sim_match.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|