bbstrader 0.2.0__py3-none-any.whl → 0.2.1__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 +7 -7
- bbstrader/btengine/__init__.py +7 -7
- bbstrader/btengine/backtest.py +30 -26
- bbstrader/btengine/data.py +92 -81
- bbstrader/btengine/event.py +2 -1
- bbstrader/btengine/execution.py +18 -16
- bbstrader/btengine/performance.py +11 -7
- bbstrader/btengine/portfolio.py +35 -36
- bbstrader/btengine/strategy.py +113 -92
- bbstrader/config.py +12 -10
- bbstrader/core/data.py +4 -5
- bbstrader/core/utils.py +57 -0
- bbstrader/ibkr/utils.py +0 -0
- bbstrader/metatrader/__init__.py +5 -5
- bbstrader/metatrader/account.py +117 -121
- bbstrader/metatrader/rates.py +81 -78
- bbstrader/metatrader/risk.py +23 -37
- bbstrader/metatrader/trade.py +154 -138
- bbstrader/metatrader/utils.py +3 -3
- bbstrader/models/__init__.py +5 -5
- bbstrader/models/factors.py +17 -12
- bbstrader/models/ml.py +371 -305
- bbstrader/models/optimization.py +14 -12
- bbstrader/models/portfolio.py +44 -35
- bbstrader/models/risk.py +15 -9
- bbstrader/trading/__init__.py +2 -2
- bbstrader/trading/execution.py +245 -179
- bbstrader/trading/scripts.py +8 -4
- bbstrader/trading/strategies.py +78 -65
- bbstrader/tseries.py +124 -98
- {bbstrader-0.2.0.dist-info → bbstrader-0.2.1.dist-info}/LICENSE +1 -1
- {bbstrader-0.2.0.dist-info → bbstrader-0.2.1.dist-info}/METADATA +2 -1
- bbstrader-0.2.1.dist-info/RECORD +37 -0
- bbstrader-0.2.0.dist-info/RECORD +0 -36
- {bbstrader-0.2.0.dist-info → bbstrader-0.2.1.dist-info}/WHEEL +0 -0
- {bbstrader-0.2.0.dist-info → bbstrader-0.2.1.dist-info}/top_level.txt +0 -0
bbstrader/metatrader/utils.py
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
from datetime import datetime
|
|
2
|
-
import MetaTrader5 as MT5
|
|
3
|
-
import logging
|
|
4
|
-
from typing import List, NamedTuple, Optional
|
|
5
2
|
from enum import Enum
|
|
3
|
+
from typing import NamedTuple, Optional
|
|
6
4
|
|
|
5
|
+
import MetaTrader5 as MT5
|
|
7
6
|
|
|
8
7
|
__all__ = [
|
|
9
8
|
"TIMEFRAMES",
|
|
@@ -421,6 +420,7 @@ class TradeDeal(NamedTuple):
|
|
|
421
420
|
|
|
422
421
|
class InvalidBroker(Exception):
|
|
423
422
|
"""Exception raised for invalid broker errors."""
|
|
423
|
+
|
|
424
424
|
def __init__(self, message="Invalid broker."):
|
|
425
425
|
super().__init__(message)
|
|
426
426
|
|
bbstrader/models/__init__.py
CHANGED
|
@@ -3,8 +3,8 @@ The `models` module provides a foundational framework for implementing various q
|
|
|
3
3
|
|
|
4
4
|
It is designed to be a versatile base module for different types of models used in financial analysis and trading.
|
|
5
5
|
"""
|
|
6
|
-
from bbstrader.models.risk import *
|
|
7
|
-
from bbstrader.models.optimization import *
|
|
8
|
-
from bbstrader.models.portfolio import *
|
|
9
|
-
from bbstrader.models.factors import *
|
|
10
|
-
from bbstrader.models.ml import *
|
|
6
|
+
from bbstrader.models.risk import * # noqa: F403
|
|
7
|
+
from bbstrader.models.optimization import * # noqa: F403
|
|
8
|
+
from bbstrader.models.portfolio import * # noqa: F403
|
|
9
|
+
from bbstrader.models.factors import * # noqa: F403
|
|
10
|
+
from bbstrader.models.ml import * # noqa: F403
|
bbstrader/models/factors.py
CHANGED
|
@@ -1,20 +1,22 @@
|
|
|
1
|
+
from datetime import datetime
|
|
2
|
+
from typing import Dict, List
|
|
3
|
+
|
|
1
4
|
import pandas as pd
|
|
2
|
-
import numpy as np
|
|
3
5
|
import yfinance as yf
|
|
4
|
-
|
|
6
|
+
|
|
7
|
+
from bbstrader.btengine.data import EODHDataHandler, FMPDataHandler
|
|
5
8
|
from bbstrader.metatrader.rates import download_historical_data
|
|
6
|
-
from bbstrader.btengine.data import FMPDataHandler, EODHDataHandler
|
|
7
|
-
from typing import List, Dict, Literal, Union
|
|
8
9
|
from bbstrader.tseries import (
|
|
9
10
|
find_cointegrated_pairs,
|
|
10
|
-
select_candidate_pairs,
|
|
11
11
|
select_assets,
|
|
12
|
+
select_candidate_pairs,
|
|
12
13
|
)
|
|
13
14
|
|
|
14
15
|
__all__ = [
|
|
15
16
|
"search_coint_candidate_pairs",
|
|
16
17
|
]
|
|
17
18
|
|
|
19
|
+
|
|
18
20
|
def search_coint_candidate_pairs(
|
|
19
21
|
securities: pd.DataFrame | List[str] = None,
|
|
20
22
|
candidates: pd.DataFrame | List[str] = None,
|
|
@@ -193,13 +195,14 @@ def search_coint_candidate_pairs(
|
|
|
193
195
|
)
|
|
194
196
|
top_pairs = []
|
|
195
197
|
p_start = pd.Timestamp(end) - pd.DateOffset(years=1)
|
|
196
|
-
periods = pd.date_range(
|
|
198
|
+
periods = pd.date_range(
|
|
199
|
+
start=p_start, end=pd.Timestamp(end), freq='BQE')
|
|
197
200
|
npairs = max(round(npairs/2), 1)
|
|
198
201
|
for period in periods:
|
|
199
202
|
s_start = period - pd.DateOffset(years=2) + pd.DateOffset(days=1)
|
|
200
203
|
print(f"Searching for pairs in period: {s_start} - {period}")
|
|
201
204
|
pairs = find_cointegrated_pairs(
|
|
202
|
-
securities, candidates, n=npairs, start=str(s_start), stop=str(period), coint=True)
|
|
205
|
+
securities, candidates, n=npairs, start=str(s_start), stop=str(period), coint=True)
|
|
203
206
|
pairs['period'] = period
|
|
204
207
|
top_pairs.append(pairs)
|
|
205
208
|
top_pairs = pd.concat(top_pairs)
|
|
@@ -207,7 +210,7 @@ def search_coint_candidate_pairs(
|
|
|
207
210
|
raise ValueError(
|
|
208
211
|
"No pairs found in the specified period."
|
|
209
212
|
"Please adjust the date range or increase the number of pairs."
|
|
210
|
-
|
|
213
|
+
)
|
|
211
214
|
return top_pairs.head(npairs*2)
|
|
212
215
|
|
|
213
216
|
def _process_asset_data(securities, candidates, universe, rolling_window):
|
|
@@ -217,11 +220,11 @@ def search_coint_candidate_pairs(
|
|
|
217
220
|
candidates = select_assets(
|
|
218
221
|
candidates, n=universe, rolling_window=rolling_window)
|
|
219
222
|
return securities, candidates
|
|
220
|
-
|
|
223
|
+
|
|
221
224
|
if (securities is not None and candidates is not None
|
|
222
225
|
and isinstance(securities, pd.DataFrame)
|
|
223
226
|
and isinstance(candidates, pd.DataFrame)
|
|
224
|
-
|
|
227
|
+
):
|
|
225
228
|
if isinstance(securities.index, pd.MultiIndex) and isinstance(candidates.index, pd.MultiIndex):
|
|
226
229
|
securities, candidates = _process_asset_data(
|
|
227
230
|
securities, candidates, universe, rolling_window)
|
|
@@ -259,9 +262,11 @@ def search_coint_candidate_pairs(
|
|
|
259
262
|
securities_data, candidates_data = _process_asset_data(
|
|
260
263
|
securities_data, candidates_data, universe, rolling_window)
|
|
261
264
|
if period_search:
|
|
262
|
-
top_pairs = _period_search(
|
|
265
|
+
top_pairs = _period_search(
|
|
266
|
+
start, end, securities_data, candidates_data).head(npairs)
|
|
263
267
|
else:
|
|
264
|
-
top_pairs = find_cointegrated_pairs(
|
|
268
|
+
top_pairs = find_cointegrated_pairs(
|
|
269
|
+
securities_data, candidates_data, n=npairs, coint=True)
|
|
265
270
|
if select:
|
|
266
271
|
return select_candidate_pairs(top_pairs, period=True if period_search else False)
|
|
267
272
|
else:
|