bbstrader 0.1.6__py3-none-any.whl → 0.1.8__py3-none-any.whl
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.
Potentially problematic release.
This version of bbstrader might be problematic. Click here for more details.
- bbstrader/__ini__.py +0 -1
- bbstrader/btengine/__init__.py +13 -9
- bbstrader/btengine/backtest.py +99 -702
- bbstrader/btengine/data.py +3 -1
- bbstrader/btengine/event.py +15 -9
- bbstrader/btengine/execution.py +66 -6
- bbstrader/btengine/performance.py +32 -20
- bbstrader/btengine/portfolio.py +33 -15
- bbstrader/btengine/strategy.py +1 -2
- bbstrader/metatrader/account.py +15 -8
- bbstrader/metatrader/rates.py +10 -6
- bbstrader/metatrader/risk.py +1 -2
- bbstrader/metatrader/trade.py +307 -239
- bbstrader/metatrader/utils.py +37 -29
- bbstrader/models/risk.py +39 -2
- bbstrader/trading/__init__.py +8 -1
- bbstrader/trading/execution.py +378 -932
- bbstrader/trading/strategies.py +840 -0
- bbstrader/tseries.py +613 -23
- {bbstrader-0.1.6.dist-info → bbstrader-0.1.8.dist-info}/METADATA +16 -7
- bbstrader-0.1.8.dist-info/RECORD +26 -0
- {bbstrader-0.1.6.dist-info → bbstrader-0.1.8.dist-info}/WHEEL +1 -1
- bbstrader/strategies.py +0 -681
- bbstrader/trading/run.py +0 -131
- bbstrader/trading/utils.py +0 -153
- bbstrader-0.1.6.dist-info/RECORD +0 -28
- {bbstrader-0.1.6.dist-info → bbstrader-0.1.8.dist-info}/LICENSE +0 -0
- {bbstrader-0.1.6.dist-info → bbstrader-0.1.8.dist-info}/top_level.txt +0 -0
bbstrader/metatrader/utils.py
CHANGED
|
@@ -4,6 +4,38 @@ import logging
|
|
|
4
4
|
from typing import List, NamedTuple, Optional
|
|
5
5
|
from enum import Enum
|
|
6
6
|
|
|
7
|
+
|
|
8
|
+
__all__ = [
|
|
9
|
+
"TIMEFRAMES",
|
|
10
|
+
"TimeFrame",
|
|
11
|
+
"TerminalInfo",
|
|
12
|
+
"AccountInfo",
|
|
13
|
+
"SymbolInfo",
|
|
14
|
+
"TickInfo",
|
|
15
|
+
"TradeRequest",
|
|
16
|
+
"OrderCheckResult",
|
|
17
|
+
"OrderSentResult",
|
|
18
|
+
"TradeOrder",
|
|
19
|
+
"TradePosition",
|
|
20
|
+
"TradeDeal",
|
|
21
|
+
"InvalidBroker",
|
|
22
|
+
"GenericFail",
|
|
23
|
+
"InvalidParams",
|
|
24
|
+
"HistoryNotFound",
|
|
25
|
+
"InvalidVersion",
|
|
26
|
+
"AuthFailed",
|
|
27
|
+
"UnsupportedMethod",
|
|
28
|
+
"AutoTradingDisabled",
|
|
29
|
+
"InternalFailSend",
|
|
30
|
+
"InternalFailReceive",
|
|
31
|
+
"InternalFailInit",
|
|
32
|
+
"InternalFailConnect",
|
|
33
|
+
"InternalFailTimeout",
|
|
34
|
+
"trade_retcode_message",
|
|
35
|
+
"raise_mt5_error",
|
|
36
|
+
"config_logger",
|
|
37
|
+
]
|
|
38
|
+
|
|
7
39
|
def config_logger(log_file: str, console_log=True):
|
|
8
40
|
# Configure the logger
|
|
9
41
|
logger = logging.getLogger(__name__)
|
|
@@ -55,34 +87,6 @@ class LogLevelFilter(logging.Filter):
|
|
|
55
87
|
return record.levelno in self.levels
|
|
56
88
|
|
|
57
89
|
|
|
58
|
-
__all__ = [
|
|
59
|
-
"TIMEFRAMES",
|
|
60
|
-
"TimeFrame",
|
|
61
|
-
"TerminalInfo",
|
|
62
|
-
"AccountInfo",
|
|
63
|
-
"SymbolInfo",
|
|
64
|
-
"TickInfo",
|
|
65
|
-
"TradeRequest",
|
|
66
|
-
"OrderCheckResult",
|
|
67
|
-
"OrderSentResult",
|
|
68
|
-
"TradeOrder",
|
|
69
|
-
"TradePosition",
|
|
70
|
-
"TradeDeal",
|
|
71
|
-
"GenericFail",
|
|
72
|
-
"InvalidParams",
|
|
73
|
-
"HistoryNotFound",
|
|
74
|
-
"InvalidVersion",
|
|
75
|
-
"AuthFailed",
|
|
76
|
-
"UnsupportedMethod",
|
|
77
|
-
"AutoTradingDisabled",
|
|
78
|
-
"InternalFailSend",
|
|
79
|
-
"InternalFailReceive",
|
|
80
|
-
"InternalFailInit",
|
|
81
|
-
"InternalFailConnect",
|
|
82
|
-
"InternalFailTimeout",
|
|
83
|
-
"trade_retcode_message",
|
|
84
|
-
"raise_mt5_error",
|
|
85
|
-
]
|
|
86
90
|
|
|
87
91
|
# TIMEFRAME is an enumeration with possible chart period values
|
|
88
92
|
# See https://www.mql5.com/en/docs/python_metatrader5/mt5copyratesfrom_py#timeframe
|
|
@@ -137,7 +141,6 @@ class TimeFrame(Enum):
|
|
|
137
141
|
W1 = "W1"
|
|
138
142
|
MN1 = "MN1"
|
|
139
143
|
|
|
140
|
-
|
|
141
144
|
class TerminalInfo(NamedTuple):
|
|
142
145
|
"""
|
|
143
146
|
Represents general information about the trading terminal.
|
|
@@ -467,6 +470,11 @@ class TradeDeal(NamedTuple):
|
|
|
467
470
|
comment: str
|
|
468
471
|
external_id: str
|
|
469
472
|
|
|
473
|
+
class InvalidBroker(Exception):
|
|
474
|
+
"""Exception raised for invalid broker errors."""
|
|
475
|
+
def __init__(self, message="Invalid broker."):
|
|
476
|
+
super().__init__(message)
|
|
477
|
+
|
|
470
478
|
|
|
471
479
|
class MT5TerminalError(Exception):
|
|
472
480
|
"""Base exception class for trading-related errors."""
|
bbstrader/models/risk.py
CHANGED
|
@@ -7,12 +7,14 @@ from hmmlearn.hmm import GaussianHMM
|
|
|
7
7
|
from abc import ABCMeta, abstractmethod
|
|
8
8
|
from matplotlib import cm, pyplot as plt
|
|
9
9
|
from matplotlib.dates import YearLocator, MonthLocator
|
|
10
|
-
from typing import Optional
|
|
10
|
+
from typing import Optional, Dict
|
|
11
|
+
from bbstrader.metatrader.rates import Rates
|
|
11
12
|
sns.set_theme()
|
|
12
13
|
|
|
13
14
|
__all__ = [
|
|
14
15
|
"RiskModel",
|
|
15
|
-
"HMMRiskManager"
|
|
16
|
+
"HMMRiskManager",
|
|
17
|
+
"build_hmm_models"
|
|
16
18
|
]
|
|
17
19
|
|
|
18
20
|
|
|
@@ -347,3 +349,38 @@ class HMMRiskManager(RiskModel):
|
|
|
347
349
|
ax.xaxis.set_minor_locator(MonthLocator())
|
|
348
350
|
ax.grid(True)
|
|
349
351
|
plt.show()
|
|
352
|
+
|
|
353
|
+
def build_hmm_models(symbol_list=None, **kwargs
|
|
354
|
+
) -> Dict[str, HMMRiskManager]:
|
|
355
|
+
mt5_data = kwargs.get("use_mt5_data", False)
|
|
356
|
+
data = kwargs.get("hmm_data")
|
|
357
|
+
tf = kwargs.get("time_frame", 'D1')
|
|
358
|
+
hmm_end = kwargs.get("hmm_end", 0)
|
|
359
|
+
sd = kwargs.get("session_duration", 23.0)
|
|
360
|
+
hmm_tickers = kwargs.get("hmm_tickers")
|
|
361
|
+
if hmm_tickers is not None:
|
|
362
|
+
symbols = hmm_tickers
|
|
363
|
+
else:
|
|
364
|
+
symbols = symbol_list
|
|
365
|
+
hmm_models = {symbol: None for symbol in symbols}
|
|
366
|
+
if data is not None:
|
|
367
|
+
if isinstance(data, pd.DataFrame):
|
|
368
|
+
hmm_data = data
|
|
369
|
+
hmm = HMMRiskManager(
|
|
370
|
+
data=hmm_data, verbose=True, iterations=1000, **kwargs)
|
|
371
|
+
for symbol in symbols:
|
|
372
|
+
hmm_models[symbol] = hmm
|
|
373
|
+
elif isinstance(data, dict):
|
|
374
|
+
for symbol, data in data.items():
|
|
375
|
+
hmm = HMMRiskManager(
|
|
376
|
+
data=data, verbose=True, iterations=1000, **kwargs)
|
|
377
|
+
hmm_models[symbol] = hmm
|
|
378
|
+
if mt5_data:
|
|
379
|
+
for symbol in symbols:
|
|
380
|
+
rates = Rates(symbol, tf, start_pos=hmm_end, session_duration=sd)
|
|
381
|
+
data = rates.get_rates_from_pos()
|
|
382
|
+
assert data is not None, f"No data for {symbol}"
|
|
383
|
+
hmm = HMMRiskManager(
|
|
384
|
+
data=data, verbose=True, iterations=1000, **kwargs)
|
|
385
|
+
hmm_models[symbol] = hmm
|
|
386
|
+
return hmm_models
|
bbstrader/trading/__init__.py
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
1
|
"""
|
|
2
2
|
Trading strategies execution module
|
|
3
|
+
|
|
4
|
+
This module is responsible for executing trading strategies.
|
|
5
|
+
It provides a framework for executing trading strategies and managing the trading process.
|
|
6
|
+
The module is designed to be flexible and extensible, allowing users to define their own trading
|
|
7
|
+
strategies and customize the trading process.
|
|
8
|
+
|
|
3
9
|
"""
|
|
4
|
-
from bbstrader.trading.
|
|
10
|
+
from bbstrader.trading.execution import ExecutionEngine
|
|
11
|
+
from bbstrader.trading.strategies import *
|