overfitting 0.3.0__tar.gz → 0.3.1__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.
Potentially problematic release.
This version of overfitting might be problematic. Click here for more details.
- {overfitting-0.3.0 → overfitting-0.3.1}/PKG-INFO +1 -1
- overfitting-0.3.1/overfitting/__init__.py +3 -0
- {overfitting-0.3.0 → overfitting-0.3.1}/overfitting/broker.py +1 -1
- overfitting-0.3.1/overfitting/data/__init__.py +4 -0
- {overfitting-0.3.0/overfitting → overfitting-0.3.1/overfitting/data}/data.py +2 -35
- overfitting-0.3.1/overfitting/data/multi_currency.py +38 -0
- overfitting-0.3.1/overfitting/indicator.py +207 -0
- {overfitting-0.3.0 → overfitting-0.3.1}/overfitting/order.py +1 -0
- {overfitting-0.3.0 → overfitting-0.3.1}/overfitting/strategy.py +4 -2
- overfitting-0.3.0/overfitting/plot/performance.py → overfitting-0.3.1/overfitting/visualization/report.py +2 -2
- {overfitting-0.3.0/overfitting → overfitting-0.3.1/overfitting/visualization}/viewer.py +0 -2
- {overfitting-0.3.0 → overfitting-0.3.1}/overfitting.egg-info/PKG-INFO +1 -1
- {overfitting-0.3.0 → overfitting-0.3.1}/overfitting.egg-info/SOURCES.txt +9 -7
- {overfitting-0.3.0 → overfitting-0.3.1}/pyproject.toml +1 -1
- overfitting-0.3.0/overfitting/__init__.py +0 -4
- overfitting-0.3.0/overfitting/utils/__init__.py +0 -0
- {overfitting-0.3.0 → overfitting-0.3.1}/LICENSE +0 -0
- {overfitting-0.3.0 → overfitting-0.3.1}/README.md +0 -0
- {overfitting-0.3.0 → overfitting-0.3.1}/overfitting/entities/__init__.py +0 -0
- {overfitting-0.3.0 → overfitting-0.3.1}/overfitting/entities/order.py +0 -0
- {overfitting-0.3.0/overfitting/utils → overfitting-0.3.1/overfitting}/error.py +0 -0
- {overfitting-0.3.0 → overfitting-0.3.1}/overfitting/execution/__init__.py +0 -0
- {overfitting-0.3.0 → overfitting-0.3.1}/overfitting/execution/slippage.py +0 -0
- {overfitting-0.3.0 → overfitting-0.3.1}/overfitting/position.py +0 -0
- {overfitting-0.3.0/overfitting/plot → overfitting-0.3.1/overfitting/visualization}/__init__.py +0 -0
- {overfitting-0.3.0/overfitting/plot → overfitting-0.3.1/overfitting/visualization}/graph.py +0 -0
- {overfitting-0.3.0 → overfitting-0.3.1}/overfitting.egg-info/dependency_links.txt +0 -0
- {overfitting-0.3.0 → overfitting-0.3.1}/overfitting.egg-info/requires.txt +0 -0
- {overfitting-0.3.0 → overfitting-0.3.1}/overfitting.egg-info/top_level.txt +0 -0
- {overfitting-0.3.0 → overfitting-0.3.1}/setup.cfg +0 -0
|
@@ -5,7 +5,7 @@ from overfitting.order import Order
|
|
|
5
5
|
from overfitting.position import Position
|
|
6
6
|
from overfitting.execution.slippage import SlippageModel
|
|
7
7
|
from overfitting.entities.order import OrderType
|
|
8
|
-
from overfitting.
|
|
8
|
+
from overfitting.error import EmptyOrderParameters, InvalidOrderParameters, LiquidationError
|
|
9
9
|
|
|
10
10
|
class Broker:
|
|
11
11
|
def __init__(self,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import pandas as pd
|
|
2
2
|
import numpy as np
|
|
3
3
|
from pandas.api.types import is_datetime64_any_dtype, is_integer_dtype, is_float_dtype
|
|
4
|
-
from overfitting.
|
|
4
|
+
from overfitting.error import InitializationError
|
|
5
5
|
|
|
6
6
|
REQUIRED_OHLC = ("open", "high", "low", "close")
|
|
7
7
|
|
|
@@ -84,37 +84,4 @@ class Data(dict):
|
|
|
84
84
|
if key in {"index", "columns", "n"}:
|
|
85
85
|
object.__setattr__(self, key, value)
|
|
86
86
|
else:
|
|
87
|
-
raise AttributeError("Data is read-only; modify the source DataFrame before wrapping.")
|
|
88
|
-
|
|
89
|
-
class MultiCurrency(dict):
|
|
90
|
-
|
|
91
|
-
symbols: tuple[str, ...] # List of symbols in this container
|
|
92
|
-
index: np.ndarray # datetime64[ns] index
|
|
93
|
-
n: int # Number of rows per symbol
|
|
94
|
-
|
|
95
|
-
def __init__(self, frames: dict[str, pd.DataFrame]):
|
|
96
|
-
if not isinstance(frames, dict) or not frames:
|
|
97
|
-
raise InitializationError("Data must be non-empty dict Type - dict[str, pd.DataFrame]")
|
|
98
|
-
|
|
99
|
-
payload = {}
|
|
100
|
-
first_ts = None
|
|
101
|
-
for symbol, df in frames.items():
|
|
102
|
-
d = Data(df)
|
|
103
|
-
if first_ts is None:
|
|
104
|
-
first_ts = d.index
|
|
105
|
-
else:
|
|
106
|
-
# Requires identical timestamps for NOW
|
|
107
|
-
# TODO IMNPLEMENT AUTO FILL for uniform timestamps
|
|
108
|
-
if len(d.index) != len(first_ts) or not np.array_equal(d.index, first_ts):
|
|
109
|
-
raise InitializationError(
|
|
110
|
-
f"Len Timestamps for {symbol} are not equal with the other symbols.")
|
|
111
|
-
|
|
112
|
-
payload[symbol] = d
|
|
113
|
-
|
|
114
|
-
super().__init__(payload)
|
|
115
|
-
object.__setattr__(self, "symbols", tuple(payload.keys()))
|
|
116
|
-
object.__setattr__(self, "index", first_ts)
|
|
117
|
-
object.__setattr__(self, "n", len(first_ts))
|
|
118
|
-
|
|
119
|
-
def __len__(self) -> int:
|
|
120
|
-
return self.n
|
|
87
|
+
raise AttributeError("Data is read-only; modify the source DataFrame before wrapping.")
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import pandas as pd
|
|
2
|
+
import numpy as np
|
|
3
|
+
from pandas.api.types import is_datetime64_any_dtype, is_integer_dtype, is_float_dtype
|
|
4
|
+
from overfitting.error import InitializationError
|
|
5
|
+
from overfitting.data import Data
|
|
6
|
+
|
|
7
|
+
class MultiCurrency(dict):
|
|
8
|
+
|
|
9
|
+
symbols: tuple[str, ...] # List of symbols in this container
|
|
10
|
+
index: np.ndarray # datetime64[ns] index
|
|
11
|
+
n: int # Number of rows per symbol
|
|
12
|
+
|
|
13
|
+
def __init__(self, frames: dict[str, pd.DataFrame]):
|
|
14
|
+
if not isinstance(frames, dict) or not frames:
|
|
15
|
+
raise InitializationError("Data must be non-empty dict Type - dict[str, pd.DataFrame]")
|
|
16
|
+
|
|
17
|
+
payload = {}
|
|
18
|
+
first_ts = None
|
|
19
|
+
for symbol, df in frames.items():
|
|
20
|
+
d = Data(df)
|
|
21
|
+
if first_ts is None:
|
|
22
|
+
first_ts = d.index
|
|
23
|
+
else:
|
|
24
|
+
# Requires identical timestamps for NOW
|
|
25
|
+
# TODO IMNPLEMENT AUTO FILL for uniform timestamps
|
|
26
|
+
if len(d.index) != len(first_ts) or not np.array_equal(d.index, first_ts):
|
|
27
|
+
raise InitializationError(
|
|
28
|
+
f"Len Timestamps for {symbol} are not equal with the other symbols.")
|
|
29
|
+
|
|
30
|
+
payload[symbol] = d
|
|
31
|
+
|
|
32
|
+
super().__init__(payload)
|
|
33
|
+
object.__setattr__(self, "symbols", tuple(payload.keys()))
|
|
34
|
+
object.__setattr__(self, "index", first_ts)
|
|
35
|
+
object.__setattr__(self, "n", len(first_ts))
|
|
36
|
+
|
|
37
|
+
def __len__(self) -> int:
|
|
38
|
+
return self.n
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import pandas as pd
|
|
2
|
+
from abc import ABC, abstractmethod
|
|
3
|
+
from overfitting import Strategy
|
|
4
|
+
|
|
5
|
+
class Indicator(ABC):
|
|
6
|
+
def __init__(self, strategy: Strategy, symbol: str):
|
|
7
|
+
self.strategy: Strategy = strategy
|
|
8
|
+
self.symbol: str = symbol
|
|
9
|
+
self._values: pd.Series | pd.DataFrame | None = None
|
|
10
|
+
self.compute()
|
|
11
|
+
|
|
12
|
+
@property
|
|
13
|
+
def values(self) -> pd.Series | pd.DataFrame | None:
|
|
14
|
+
return self._values
|
|
15
|
+
|
|
16
|
+
def get_series(self, name: str) -> pd.Series:
|
|
17
|
+
d = self.strategy.broker._d(self.symbol)
|
|
18
|
+
values = getattr(d, name, None)
|
|
19
|
+
if values is None:
|
|
20
|
+
raise AttributeError(f"Column '{name}' not found for symbol '{self.symbol}'")
|
|
21
|
+
|
|
22
|
+
return pd.Series(values)
|
|
23
|
+
|
|
24
|
+
@abstractmethod
|
|
25
|
+
def compute(self):
|
|
26
|
+
pass
|
|
27
|
+
|
|
28
|
+
def value(self, i: int):
|
|
29
|
+
if self._values is None:
|
|
30
|
+
raise ValueError(f"{self.__class__.__name__} has not computed yet")
|
|
31
|
+
return self._values.iloc[i]
|
|
32
|
+
|
|
33
|
+
def __getitem__(self, i: int):
|
|
34
|
+
return self.value(i)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class SMA(Indicator):
|
|
38
|
+
def __init__(self, strategy: Strategy, symbol: str, source: str = "close", window: int = 20):
|
|
39
|
+
self.source = source
|
|
40
|
+
self.window = window
|
|
41
|
+
super().__init__(strategy, symbol)
|
|
42
|
+
|
|
43
|
+
def compute(self):
|
|
44
|
+
s = self.get_series(self.source)
|
|
45
|
+
self._values = s.rolling(self.window).mean().shift(1)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class EMA(Indicator):
|
|
49
|
+
def __init__(self, strategy: Strategy, symbol: str, source: str = "close", span: int = 20):
|
|
50
|
+
self.source = source
|
|
51
|
+
self.span = span
|
|
52
|
+
super().__init__(strategy, symbol)
|
|
53
|
+
|
|
54
|
+
def compute(self):
|
|
55
|
+
s = self.get_series(self.source)
|
|
56
|
+
self._values = s.ewm(span=self.span, adjust=False).mean().shift(1)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
class WMA(Indicator):
|
|
60
|
+
def __init__(self, strategy: Strategy, symbol: str, source: str = "close", window: int = 20):
|
|
61
|
+
self.source = source
|
|
62
|
+
self.window = window
|
|
63
|
+
super().__init__(strategy, symbol)
|
|
64
|
+
|
|
65
|
+
def compute(self):
|
|
66
|
+
s = self.get_series(self.source)
|
|
67
|
+
weights = pd.Series(range(1, self.window + 1), dtype=float)
|
|
68
|
+
|
|
69
|
+
def _wma(x):
|
|
70
|
+
return (x * weights).sum() / weights.sum()
|
|
71
|
+
|
|
72
|
+
self._values = s.rolling(self.window).apply(_wma, raw=False).shift(1)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
class RSI(Indicator):
|
|
76
|
+
def __init__(self, strategy: Strategy, symbol: str, source: str = "close", window: int = 14):
|
|
77
|
+
self.source = source
|
|
78
|
+
self.window = window
|
|
79
|
+
super().__init__(strategy, symbol)
|
|
80
|
+
|
|
81
|
+
def compute(self):
|
|
82
|
+
s = self.get_series(self.source)
|
|
83
|
+
delta = s.diff()
|
|
84
|
+
|
|
85
|
+
gain = delta.clip(lower=0)
|
|
86
|
+
loss = -delta.clip(upper=0)
|
|
87
|
+
|
|
88
|
+
avg_gain = gain.ewm(alpha=1 / self.window, adjust=False, min_periods=self.window).mean()
|
|
89
|
+
avg_loss = loss.ewm(alpha=1 / self.window, adjust=False, min_periods=self.window).mean()
|
|
90
|
+
|
|
91
|
+
rs = avg_gain / avg_loss
|
|
92
|
+
self._values = (100 - (100 / (1 + rs))).shift(1)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
class MACD(Indicator):
|
|
96
|
+
def __init__(
|
|
97
|
+
self,
|
|
98
|
+
strategy: Strategy,
|
|
99
|
+
symbol: str,
|
|
100
|
+
source: str = "close",
|
|
101
|
+
fast: int = 12,
|
|
102
|
+
slow: int = 26,
|
|
103
|
+
signal: int = 9,
|
|
104
|
+
):
|
|
105
|
+
self.source = source
|
|
106
|
+
self.fast = fast
|
|
107
|
+
self.slow = slow
|
|
108
|
+
self.signal = signal
|
|
109
|
+
super().__init__(strategy, symbol)
|
|
110
|
+
|
|
111
|
+
def compute(self):
|
|
112
|
+
s = self.get_series(self.source)
|
|
113
|
+
fast_ema = s.ewm(span=self.fast, adjust=False).mean()
|
|
114
|
+
slow_ema = s.ewm(span=self.slow, adjust=False).mean()
|
|
115
|
+
|
|
116
|
+
macd = fast_ema - slow_ema
|
|
117
|
+
signal = macd.ewm(span=self.signal, adjust=False).mean()
|
|
118
|
+
hist = macd - signal
|
|
119
|
+
|
|
120
|
+
self._values = pd.DataFrame({
|
|
121
|
+
"macd": macd.shift(1),
|
|
122
|
+
"signal": signal.shift(1),
|
|
123
|
+
"hist": hist.shift(1),
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
class Stochastic(Indicator):
|
|
128
|
+
def __init__(
|
|
129
|
+
self,
|
|
130
|
+
strategy: Strategy,
|
|
131
|
+
symbol: str,
|
|
132
|
+
k_window: int = 14,
|
|
133
|
+
d_window: int = 3,
|
|
134
|
+
smooth_k: int = 3,
|
|
135
|
+
):
|
|
136
|
+
self.k_window = k_window
|
|
137
|
+
self.d_window = d_window
|
|
138
|
+
self.smooth_k = smooth_k
|
|
139
|
+
super().__init__(strategy, symbol)
|
|
140
|
+
|
|
141
|
+
def compute(self):
|
|
142
|
+
d = self.strategy.broker._d(self.symbol)
|
|
143
|
+
high = d.high
|
|
144
|
+
low = d.low
|
|
145
|
+
close = d.close
|
|
146
|
+
|
|
147
|
+
lowest_low = low.rolling(self.k_window).min()
|
|
148
|
+
highest_high = high.rolling(self.k_window).max()
|
|
149
|
+
|
|
150
|
+
raw_k = 100 * (close - lowest_low) / (highest_high - lowest_low)
|
|
151
|
+
k = raw_k.rolling(self.smooth_k).mean()
|
|
152
|
+
d_line = k.rolling(self.d_window).mean()
|
|
153
|
+
|
|
154
|
+
self._values = pd.DataFrame({
|
|
155
|
+
"%K": k.shift(1),
|
|
156
|
+
"%D": d_line.shift(1),
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
class BollingerBands(Indicator):
|
|
161
|
+
def __init__(
|
|
162
|
+
self,
|
|
163
|
+
strategy: Strategy,
|
|
164
|
+
symbol: str,
|
|
165
|
+
source: str = "close",
|
|
166
|
+
window: int = 20,
|
|
167
|
+
num_std: float = 2.0,
|
|
168
|
+
):
|
|
169
|
+
self.source = source
|
|
170
|
+
self.window = window
|
|
171
|
+
self.num_std = num_std
|
|
172
|
+
super().__init__(strategy, symbol)
|
|
173
|
+
|
|
174
|
+
def compute(self):
|
|
175
|
+
s = self.get_series(self.source)
|
|
176
|
+
mid = s.rolling(self.window).mean()
|
|
177
|
+
std = s.rolling(self.window).std()
|
|
178
|
+
|
|
179
|
+
upper = mid + self.num_std * std
|
|
180
|
+
lower = mid - self.num_std * std
|
|
181
|
+
|
|
182
|
+
self._values = pd.DataFrame({
|
|
183
|
+
"middle": mid.shift(1),
|
|
184
|
+
"upper": upper.shift(1),
|
|
185
|
+
"lower": lower.shift(1),
|
|
186
|
+
"bandwidth": ((upper - lower) / mid).shift(1),
|
|
187
|
+
})
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
class ATR(Indicator):
|
|
191
|
+
def __init__(self, strategy: Strategy, symbol: str, window: int = 14):
|
|
192
|
+
self.window = window
|
|
193
|
+
super().__init__(strategy, symbol)
|
|
194
|
+
|
|
195
|
+
def compute(self):
|
|
196
|
+
d = self.strategy.broker._d(self.symbol)
|
|
197
|
+
prev_close = d.close.shift(1)
|
|
198
|
+
|
|
199
|
+
tr1 = d.high - d.low
|
|
200
|
+
tr2 = (d.high - prev_close).abs()
|
|
201
|
+
tr3 = (d.low - prev_close).abs()
|
|
202
|
+
|
|
203
|
+
tr = pd.concat([tr1, tr2, tr3], axis=1).max(axis=1)
|
|
204
|
+
atr = tr.ewm(alpha=1 / self.window, adjust=False, min_periods=self.window).mean()
|
|
205
|
+
|
|
206
|
+
self._values = atr.shift(1)
|
|
207
|
+
|
|
@@ -7,7 +7,7 @@ from overfitting.data import Data, MultiCurrency
|
|
|
7
7
|
from overfitting.broker import Broker
|
|
8
8
|
from overfitting.order import Order
|
|
9
9
|
from overfitting.position import Position
|
|
10
|
-
from overfitting.
|
|
10
|
+
from overfitting.visualization.report import Report
|
|
11
11
|
from overfitting.execution.slippage import SlippageModel
|
|
12
12
|
|
|
13
13
|
class Strategy:
|
|
@@ -200,7 +200,7 @@ class Strategy:
|
|
|
200
200
|
trades_list = self.broker.trades
|
|
201
201
|
captial = self.broker.initial_captial
|
|
202
202
|
|
|
203
|
-
p =
|
|
203
|
+
p = Report(
|
|
204
204
|
returns_series=returns,
|
|
205
205
|
trades_list=trades_list,
|
|
206
206
|
initial_capital=captial,
|
|
@@ -234,3 +234,5 @@ class Strategy:
|
|
|
234
234
|
trade_history_df = self.fetch_trades()
|
|
235
235
|
|
|
236
236
|
trade_history_df.to_csv(full_path, index=False)
|
|
237
|
+
|
|
238
|
+
|
|
@@ -2,13 +2,13 @@ import pandas as pd
|
|
|
2
2
|
import numpy as np
|
|
3
3
|
import matplotlib.pyplot as plt
|
|
4
4
|
import seaborn as sns
|
|
5
|
-
import overfitting.
|
|
5
|
+
import overfitting.visualization.graph as graph
|
|
6
6
|
from overfitting.order import Order
|
|
7
7
|
from typing import Optional, Sequence
|
|
8
8
|
from matplotlib.colors import LinearSegmentedColormap
|
|
9
9
|
from scipy.stats import skew, kurtosis
|
|
10
10
|
|
|
11
|
-
class
|
|
11
|
+
class Report:
|
|
12
12
|
def __init__(self,
|
|
13
13
|
returns_series: pd.Series,
|
|
14
14
|
trades_list: Sequence[Order],
|
|
@@ -3,22 +3,24 @@ README.md
|
|
|
3
3
|
pyproject.toml
|
|
4
4
|
overfitting/__init__.py
|
|
5
5
|
overfitting/broker.py
|
|
6
|
-
overfitting/
|
|
6
|
+
overfitting/error.py
|
|
7
|
+
overfitting/indicator.py
|
|
7
8
|
overfitting/order.py
|
|
8
9
|
overfitting/position.py
|
|
9
10
|
overfitting/strategy.py
|
|
10
|
-
overfitting/viewer.py
|
|
11
11
|
overfitting.egg-info/PKG-INFO
|
|
12
12
|
overfitting.egg-info/SOURCES.txt
|
|
13
13
|
overfitting.egg-info/dependency_links.txt
|
|
14
14
|
overfitting.egg-info/requires.txt
|
|
15
15
|
overfitting.egg-info/top_level.txt
|
|
16
|
+
overfitting/data/__init__.py
|
|
17
|
+
overfitting/data/data.py
|
|
18
|
+
overfitting/data/multi_currency.py
|
|
16
19
|
overfitting/entities/__init__.py
|
|
17
20
|
overfitting/entities/order.py
|
|
18
21
|
overfitting/execution/__init__.py
|
|
19
22
|
overfitting/execution/slippage.py
|
|
20
|
-
overfitting/
|
|
21
|
-
overfitting/
|
|
22
|
-
overfitting/
|
|
23
|
-
overfitting/
|
|
24
|
-
overfitting/utils/error.py
|
|
23
|
+
overfitting/visualization/__init__.py
|
|
24
|
+
overfitting/visualization/graph.py
|
|
25
|
+
overfitting/visualization/report.py
|
|
26
|
+
overfitting/visualization/viewer.py
|
|
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
|
{overfitting-0.3.0/overfitting/plot → overfitting-0.3.1/overfitting/visualization}/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|