overfitting 0.4.0__tar.gz → 0.4.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.
- {overfitting-0.4.0 → overfitting-0.4.1}/PKG-INFO +3 -3
- {overfitting-0.4.0 → overfitting-0.4.1}/README.md +2 -2
- {overfitting-0.4.0 → overfitting-0.4.1}/overfitting/analysis/report.py +29 -25
- {overfitting-0.4.0 → overfitting-0.4.1}/overfitting/analysis/viewer.py +3 -9
- {overfitting-0.4.0 → overfitting-0.4.1}/overfitting/broker.py +10 -9
- overfitting-0.4.1/overfitting/data.py +89 -0
- {overfitting-0.4.0 → overfitting-0.4.1}/overfitting/order.py +16 -10
- overfitting-0.4.1/overfitting/position.py +113 -0
- {overfitting-0.4.0 → overfitting-0.4.1}/overfitting/strategy.py +6 -6
- {overfitting-0.4.0 → overfitting-0.4.1}/overfitting.egg-info/PKG-INFO +3 -3
- {overfitting-0.4.0 → overfitting-0.4.1}/pyproject.toml +1 -1
- overfitting-0.4.0/overfitting/data.py +0 -120
- overfitting-0.4.0/overfitting/position.py +0 -118
- {overfitting-0.4.0 → overfitting-0.4.1}/LICENSE +0 -0
- {overfitting-0.4.0 → overfitting-0.4.1}/overfitting/__init__.py +0 -0
- {overfitting-0.4.0 → overfitting-0.4.1}/overfitting/analysis/__init__.py +0 -0
- {overfitting-0.4.0 → overfitting-0.4.1}/overfitting/analysis/graph.py +0 -0
- {overfitting-0.4.0 → overfitting-0.4.1}/overfitting/errors.py +0 -0
- {overfitting-0.4.0 → overfitting-0.4.1}/overfitting/indicators.py +0 -0
- {overfitting-0.4.0 → overfitting-0.4.1}/overfitting/slippage.py +0 -0
- {overfitting-0.4.0 → overfitting-0.4.1}/overfitting/types.py +0 -0
- {overfitting-0.4.0 → overfitting-0.4.1}/overfitting.egg-info/SOURCES.txt +0 -0
- {overfitting-0.4.0 → overfitting-0.4.1}/overfitting.egg-info/dependency_links.txt +0 -0
- {overfitting-0.4.0 → overfitting-0.4.1}/overfitting.egg-info/requires.txt +0 -0
- {overfitting-0.4.0 → overfitting-0.4.1}/overfitting.egg-info/top_level.txt +0 -0
- {overfitting-0.4.0 → overfitting-0.4.1}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: overfitting
|
|
3
|
-
Version: 0.4.
|
|
3
|
+
Version: 0.4.1
|
|
4
4
|
Summary: A Robust Futures CryptoCurrency Backtesting Library.
|
|
5
5
|
Author-email: Dohyun Kim <dohyun.k.july@gmail.com>
|
|
6
6
|
License: MIT
|
|
@@ -98,9 +98,9 @@ class MyStrategy(Strategy):
|
|
|
98
98
|
if previous_sma_short >= previous_sma_long and sma_short < sma_long and position.qty > 0:
|
|
99
99
|
self.market_order(self.asset, -position.qty)
|
|
100
100
|
|
|
101
|
-
|
|
101
|
+
price_df, benchmark_data = load_data()
|
|
102
102
|
strategy = MyStrategy(
|
|
103
|
-
data=
|
|
103
|
+
data={"BTC": price_df},
|
|
104
104
|
benchmark=benchmark_data, # Default = None Optional
|
|
105
105
|
initial_capital=100_000, # Default Optional
|
|
106
106
|
commission_rate=0.0002, # Default Optional
|
|
@@ -63,9 +63,9 @@ class MyStrategy(Strategy):
|
|
|
63
63
|
if previous_sma_short >= previous_sma_long and sma_short < sma_long and position.qty > 0:
|
|
64
64
|
self.market_order(self.asset, -position.qty)
|
|
65
65
|
|
|
66
|
-
|
|
66
|
+
price_df, benchmark_data = load_data()
|
|
67
67
|
strategy = MyStrategy(
|
|
68
|
-
data=
|
|
68
|
+
data={"BTC": price_df},
|
|
69
69
|
benchmark=benchmark_data, # Default = None Optional
|
|
70
70
|
initial_capital=100_000, # Default Optional
|
|
71
71
|
commission_rate=0.0002, # Default Optional
|
|
@@ -41,6 +41,7 @@ class Report:
|
|
|
41
41
|
self.summary_df, self.drawdown_series, self.drawdown_table = self._compute_summary()
|
|
42
42
|
|
|
43
43
|
def show(self):
|
|
44
|
+
self._print_summary(self.summary_df, self.drawdown_table)
|
|
44
45
|
self.plot_cumulative() # Plot cumulative returns vs benchmark
|
|
45
46
|
self.plot_cumulative_log() # Plot cumulative returns on a log scale vs benchmark
|
|
46
47
|
self.plot_daily_returns() # Plot daily returns over time
|
|
@@ -225,24 +226,29 @@ class Report:
|
|
|
225
226
|
return gross_returns, return_percents
|
|
226
227
|
|
|
227
228
|
def _compute_summary(self):
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
229
|
+
drawdown = self._compute_drawdown()
|
|
230
|
+
return_stats = self._compute_return_stats(drawdown)
|
|
231
|
+
trade_stats = self._compute_trade_stats()
|
|
231
232
|
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
233
|
+
summary_df = self._format_summary({**return_stats, **trade_stats})
|
|
234
|
+
dd_table = graph.show_worst_drawdown_periods(self.daily_returns)
|
|
235
|
+
return summary_df, drawdown, dd_table
|
|
236
|
+
|
|
237
|
+
def _compute_drawdown(self):
|
|
238
|
+
peak = self.cum.expanding(min_periods=1).max()
|
|
239
|
+
return (self.cum / peak) - 1
|
|
237
240
|
|
|
241
|
+
def _compute_return_stats(self, drawdown):
|
|
238
242
|
years = max(1e-9, (pd.to_datetime(self.end) - pd.to_datetime(self.start)).days / 365)
|
|
239
243
|
cum_return = float(self.cum.iloc[-1])
|
|
240
244
|
cagr = (cum_return ** (1 / years)) - 1 if cum_return > 0 else np.nan
|
|
241
245
|
|
|
242
|
-
|
|
243
|
-
|
|
246
|
+
monthly = (1 + self.daily_returns).resample("ME").prod().sub(1)
|
|
247
|
+
has_monthly = len(monthly) > 2
|
|
248
|
+
skew_val = float(skew(monthly)) if has_monthly else np.nan
|
|
249
|
+
kurt_val = float(kurtosis(monthly, fisher=False)) if has_monthly else np.nan
|
|
244
250
|
|
|
245
|
-
|
|
251
|
+
return {
|
|
246
252
|
"Number of Years": round(years, 2),
|
|
247
253
|
"Start Date": self.start,
|
|
248
254
|
"End Date": self.end,
|
|
@@ -250,26 +256,24 @@ class Report:
|
|
|
250
256
|
"Final Balance": self.initial_capital * cum_return,
|
|
251
257
|
"CAGR": cagr,
|
|
252
258
|
"Cumulative Return": cum_return,
|
|
253
|
-
"Sharpe Ratio":
|
|
254
|
-
"Sortino Ratio":
|
|
259
|
+
"Sharpe Ratio": graph.sharpe_ratio(self.daily_returns, risk_free=0, period="daily"),
|
|
260
|
+
"Sortino Ratio": graph.sortino_ratio(self.daily_returns, required_return=0, period="daily"),
|
|
255
261
|
"Max Drawdown": float(drawdown.min()),
|
|
256
|
-
"Daily Value At Risk": float(
|
|
257
|
-
"Skew":
|
|
258
|
-
"Kurtosis":
|
|
262
|
+
"Daily Value At Risk": float(graph.value_at_risk(self.daily_returns, sigma=2, period=None)),
|
|
263
|
+
"Skew": skew_val,
|
|
264
|
+
"Kurtosis": kurt_val,
|
|
259
265
|
}
|
|
260
266
|
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
summary.update(stats.to_dict())
|
|
267
|
+
def _compute_trade_stats(self):
|
|
268
|
+
return graph.trade_summary(self.gross_returns, self.return_percents).to_dict()
|
|
264
269
|
|
|
270
|
+
def _format_summary(self, summary: dict):
|
|
265
271
|
df = pd.DataFrame.from_dict(summary, orient="index", columns=[""])
|
|
266
272
|
df[""] = df[""].apply(lambda x: f"{x:,.8f}" if isinstance(x, float) else x)
|
|
273
|
+
return df
|
|
267
274
|
|
|
275
|
+
def _print_summary(self, summary_df, dd_table):
|
|
268
276
|
print("Performance Summary")
|
|
269
277
|
with pd.option_context("display.colheader_justify", "left", "display.width", None):
|
|
270
|
-
print(
|
|
271
|
-
|
|
272
|
-
dd_table = graph.show_worst_drawdown_periods(self.daily_returns)
|
|
273
|
-
print(dd_table)
|
|
274
|
-
|
|
275
|
-
return df, drawdown, dd_table
|
|
278
|
+
print(summary_df.to_string(header=False))
|
|
279
|
+
print(dd_table)
|
|
@@ -4,7 +4,6 @@ import numpy as np
|
|
|
4
4
|
import plotly.graph_objects as go
|
|
5
5
|
|
|
6
6
|
from overfitting import Strategy
|
|
7
|
-
from overfitting.data import MultiCurrency
|
|
8
7
|
|
|
9
8
|
from PySide6.QtWidgets import (
|
|
10
9
|
QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout,
|
|
@@ -40,9 +39,7 @@ class StrategyAdapter:
|
|
|
40
39
|
self._strategy = strategy
|
|
41
40
|
|
|
42
41
|
def get_symbols(self) -> list[str]:
|
|
43
|
-
|
|
44
|
-
return list(self._strategy.data.symbols)
|
|
45
|
-
return ["DEFAULT"]
|
|
42
|
+
return list(self._strategy.data.symbols)
|
|
46
43
|
|
|
47
44
|
|
|
48
45
|
class OHLCBuilder:
|
|
@@ -78,10 +75,7 @@ class OHLCBuilder:
|
|
|
78
75
|
@classmethod
|
|
79
76
|
def from_strategy(cls, strategy, symbol: str) -> pd.DataFrame:
|
|
80
77
|
"""Extract a tidy OHLC DataFrame for *symbol* from a strategy instance."""
|
|
81
|
-
|
|
82
|
-
d = strategy.data[symbol] if isinstance(strategy.data, MultiCurrency) else strategy.data
|
|
83
|
-
except KeyError:
|
|
84
|
-
d = strategy.data
|
|
78
|
+
d = strategy.data[symbol]
|
|
85
79
|
|
|
86
80
|
return pd.DataFrame({
|
|
87
81
|
"timestamp": pd.to_datetime(d.timestamp),
|
|
@@ -114,7 +108,7 @@ class TradeBuilder:
|
|
|
114
108
|
if col in df.columns:
|
|
115
109
|
df[col] = pd.to_datetime(df[col], errors="coerce")
|
|
116
110
|
|
|
117
|
-
if
|
|
111
|
+
if symbol and "symbol" in df.columns:
|
|
118
112
|
df = df[df["symbol"] == symbol]
|
|
119
113
|
|
|
120
114
|
for c in cls.COLUMNS:
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import pandas as pd
|
|
2
|
-
from
|
|
3
|
-
from
|
|
2
|
+
from decimal import Decimal
|
|
3
|
+
from typing import List, Dict, Optional, Tuple
|
|
4
|
+
from overfitting.data import Data
|
|
4
5
|
from overfitting.order import Order
|
|
5
6
|
from overfitting.position import Position
|
|
6
7
|
from overfitting.slippage import SlippageModel
|
|
@@ -9,8 +10,8 @@ from overfitting.errors import EmptyOrderParameters, InvalidOrderParameters, Liq
|
|
|
9
10
|
|
|
10
11
|
class Broker:
|
|
11
12
|
def __init__(self,
|
|
12
|
-
data:
|
|
13
|
-
cash: float,
|
|
13
|
+
data: Data,
|
|
14
|
+
cash: float,
|
|
14
15
|
commission_rate: float,
|
|
15
16
|
maint_margin_rate: float,
|
|
16
17
|
maint_amount:float,
|
|
@@ -45,8 +46,8 @@ class Broker:
|
|
|
45
46
|
f"positions={list(self.position.keys())}, "
|
|
46
47
|
f"trades={len(self.trades)})")
|
|
47
48
|
|
|
48
|
-
def _d(self, symbol: str)
|
|
49
|
-
return self.data[symbol]
|
|
49
|
+
def _d(self, symbol: str):
|
|
50
|
+
return self.data[symbol]
|
|
50
51
|
|
|
51
52
|
def _bars(self, symbol: str, i: int) -> Tuple:
|
|
52
53
|
d = self._d(symbol)
|
|
@@ -82,8 +83,8 @@ class Broker:
|
|
|
82
83
|
if not symbol or not isinstance(symbol, str):
|
|
83
84
|
raise InvalidOrderParameters(f"symbol must be a non-empty string. - {symbol}")
|
|
84
85
|
|
|
85
|
-
if not qty or not isinstance(qty, (int, float)):
|
|
86
|
-
raise InvalidOrderParameters(f"qty must be a non-empty
|
|
86
|
+
if not qty or not isinstance(qty, (int, float, Decimal)):
|
|
87
|
+
raise InvalidOrderParameters(f"qty must be a non-empty number. - {qty}")
|
|
87
88
|
|
|
88
89
|
if symbol not in self.position:
|
|
89
90
|
self.position[symbol] = Position(symbol, self.maint_margin_rate, self.maint_amount)
|
|
@@ -182,7 +183,7 @@ class Broker:
|
|
|
182
183
|
|
|
183
184
|
position = self.position[symbol]
|
|
184
185
|
|
|
185
|
-
notional = abs(order.qty) * order.executed_price
|
|
186
|
+
notional = float(abs(order.qty)) * order.executed_price
|
|
186
187
|
commission = notional * self.commission_rate
|
|
187
188
|
pnl = position.process_trade(order, liquidation)
|
|
188
189
|
executed_time = pd.to_datetime(self.data.index[self._i])
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import pandas as pd
|
|
2
|
+
import numpy as np
|
|
3
|
+
from types import SimpleNamespace
|
|
4
|
+
from pandas.api.types import is_datetime64_any_dtype, is_integer_dtype, is_float_dtype
|
|
5
|
+
from overfitting.errors import InitializationError
|
|
6
|
+
|
|
7
|
+
REQUIRED_OHLC = ("open", "high", "low", "close")
|
|
8
|
+
|
|
9
|
+
class Data(dict):
|
|
10
|
+
"""
|
|
11
|
+
data = Data({"BTCUSDT": df_btc, "ETHUSDT": df_eth})
|
|
12
|
+
data["BTCUSDT"].close[i] # close price of BTC at step i
|
|
13
|
+
data.index[i] # the timestamp at step i
|
|
14
|
+
data.symbols # ("BTCUSDT", "ETHUSDT")
|
|
15
|
+
len(data) # how many bars there are
|
|
16
|
+
"""
|
|
17
|
+
def __init__(self, frames: dict[str, pd.DataFrame]):
|
|
18
|
+
if not isinstance(frames, dict) or not frames:
|
|
19
|
+
raise InitializationError("Pass a non-empty dict mapping symbol -> DataFrame.")
|
|
20
|
+
|
|
21
|
+
bars = {symbol: _bars_from_frame(df, symbol) for symbol, df in frames.items()}
|
|
22
|
+
_check_aligned_timelines(bars)
|
|
23
|
+
|
|
24
|
+
super().__init__(bars)
|
|
25
|
+
self.symbols = tuple(bars)
|
|
26
|
+
self.index = next(iter(bars.values())).timestamp
|
|
27
|
+
|
|
28
|
+
def __len__(self) -> int:
|
|
29
|
+
return len(self.index)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _bars_from_frame(df: pd.DataFrame, symbol: str) -> SimpleNamespace:
|
|
33
|
+
"""Turn one symbol's DataFrame into a namespace of numpy arrays."""
|
|
34
|
+
if not isinstance(df, pd.DataFrame) or df.empty:
|
|
35
|
+
raise InitializationError(f"'{symbol}': expected a non-empty DataFrame.")
|
|
36
|
+
|
|
37
|
+
missing = [c for c in REQUIRED_OHLC if c not in df.columns]
|
|
38
|
+
if missing:
|
|
39
|
+
raise InitializationError(
|
|
40
|
+
f"'{symbol}': missing required columns {missing}. Got: {list(df.columns)}"
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
timestamps = _pull_timestamps(df, symbol)
|
|
44
|
+
|
|
45
|
+
arrays = {col: df[col].to_numpy() for col in df.columns}
|
|
46
|
+
arrays["timestamp"] = timestamps
|
|
47
|
+
return SimpleNamespace(**arrays)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _pull_timestamps(df: pd.DataFrame, symbol: str) -> np.ndarray:
|
|
51
|
+
"""Find the timestamp series — either a column or the DatetimeIndex — and normalise it."""
|
|
52
|
+
if "timestamp" in df.columns:
|
|
53
|
+
ts = df["timestamp"]
|
|
54
|
+
elif isinstance(df.index, pd.DatetimeIndex) or df.index.name == "timestamp":
|
|
55
|
+
ts = pd.Series(df.index, name="timestamp")
|
|
56
|
+
else:
|
|
57
|
+
raise InitializationError(
|
|
58
|
+
f"'{symbol}': add a 'timestamp' column or use a DatetimeIndex."
|
|
59
|
+
)
|
|
60
|
+
return _to_datetime_ns(ts).to_numpy()
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def _to_datetime_ns(ts: pd.Series) -> pd.Series:
|
|
64
|
+
"""Coerce whatever the user gave us into tz-naive datetime64[ns]."""
|
|
65
|
+
if is_datetime64_any_dtype(ts):
|
|
66
|
+
dt = pd.to_datetime(ts)
|
|
67
|
+
try:
|
|
68
|
+
return dt.tz_localize(None)
|
|
69
|
+
except Exception:
|
|
70
|
+
return dt
|
|
71
|
+
|
|
72
|
+
if is_integer_dtype(ts) or is_float_dtype(ts):
|
|
73
|
+
# Big numbers look like milliseconds; smaller ones look like seconds.
|
|
74
|
+
unit = "ms" if pd.Series(ts).max() > 1e12 else "s"
|
|
75
|
+
return pd.to_datetime(ts, unit=unit)
|
|
76
|
+
|
|
77
|
+
return pd.to_datetime(ts, errors="raise")
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def _check_aligned_timelines(bars: dict[str, SimpleNamespace]) -> None:
|
|
81
|
+
"""All symbols must share the same timeline — for now."""
|
|
82
|
+
# TODO: auto-fill / reindex so symbols with different timelines can co-exist.
|
|
83
|
+
iterator = iter(bars.items())
|
|
84
|
+
_, first = next(iterator)
|
|
85
|
+
for symbol, b in iterator:
|
|
86
|
+
if len(b.timestamp) != len(first.timestamp) or not np.array_equal(b.timestamp, first.timestamp):
|
|
87
|
+
raise InitializationError(
|
|
88
|
+
f"'{symbol}' timeline doesn't match the other symbols."
|
|
89
|
+
)
|
|
@@ -1,23 +1,25 @@
|
|
|
1
1
|
import uuid
|
|
2
|
+
from decimal import Decimal
|
|
2
3
|
import pandas as pd
|
|
3
4
|
from overfitting.types import OrderType
|
|
4
5
|
|
|
5
6
|
class Order:
|
|
6
|
-
def __init__(self,
|
|
7
|
-
time: pd.Timestamp,
|
|
8
|
-
symbol: str,
|
|
9
|
-
qty: float,
|
|
10
|
-
price:float,
|
|
11
|
-
type: OrderType,
|
|
7
|
+
def __init__(self,
|
|
8
|
+
time: pd.Timestamp,
|
|
9
|
+
symbol: str,
|
|
10
|
+
qty: float,
|
|
11
|
+
price:float,
|
|
12
|
+
type: OrderType,
|
|
12
13
|
stop_price: float = None,
|
|
13
14
|
label: str= None):
|
|
14
|
-
|
|
15
|
+
|
|
15
16
|
self.id = uuid.uuid4().hex[:16]
|
|
16
17
|
self.created_at = time
|
|
17
18
|
self.executed_at = None
|
|
18
19
|
self.symbol = symbol
|
|
19
|
-
|
|
20
|
-
self.qty = qty
|
|
20
|
+
# Decimal qty so Position bookkeeping is exact
|
|
21
|
+
self.qty = Decimal(str(qty))
|
|
22
|
+
self.side = "LONG" if self.qty > 0 else "SHORT"
|
|
21
23
|
self.price = price
|
|
22
24
|
self.type = type
|
|
23
25
|
self.stop_price = stop_price
|
|
@@ -50,7 +52,11 @@ class Order:
|
|
|
50
52
|
|
|
51
53
|
def to_dict(self):
|
|
52
54
|
return {
|
|
53
|
-
k: (
|
|
55
|
+
k: (
|
|
56
|
+
v.name if isinstance(v, OrderType)
|
|
57
|
+
else float(v) if isinstance(v, Decimal)
|
|
58
|
+
else v
|
|
59
|
+
)
|
|
54
60
|
for k, v in self.__dict__.items()
|
|
55
61
|
}
|
|
56
62
|
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
from decimal import Decimal
|
|
2
|
+
from overfitting.order import Order
|
|
3
|
+
|
|
4
|
+
ZERO = Decimal("0")
|
|
5
|
+
|
|
6
|
+
class Position:
|
|
7
|
+
def __init__(self, symbol: str, maint_margin_rate: float = 0.005, maint_amount: float = 0):
|
|
8
|
+
self.symbol = symbol
|
|
9
|
+
self.maint_margin_rate = maint_margin_rate
|
|
10
|
+
self.maint_amount = maint_amount
|
|
11
|
+
self.leverage = 1
|
|
12
|
+
|
|
13
|
+
# Open-position state. All zero when flat.
|
|
14
|
+
self.qty = ZERO
|
|
15
|
+
self.price = 0.0
|
|
16
|
+
self.margin = 0.0
|
|
17
|
+
self.liquid_price = 0.0
|
|
18
|
+
|
|
19
|
+
def __repr__(self):
|
|
20
|
+
return (f"Position(symbol={self.symbol}, qty={self.qty}, price={self.price}, "
|
|
21
|
+
f"liquid_price={self.liquid_price}, margin={self.margin}, "
|
|
22
|
+
f"leverage={self.leverage})")
|
|
23
|
+
|
|
24
|
+
def _recalc_liquidation(self) -> None:
|
|
25
|
+
"""
|
|
26
|
+
Liquidation (isolated margin):
|
|
27
|
+
notional = abs(qty) * entry_price
|
|
28
|
+
initial_margin = notional / leverage
|
|
29
|
+
maint_margin = max(0, notional * maint_margin_rate - maint_amount)
|
|
30
|
+
|
|
31
|
+
long liquidation = entry - (initial_margin - maint_margin) / abs(qty)
|
|
32
|
+
short liquidation = entry + (initial_margin - maint_margin) / abs(qty)
|
|
33
|
+
"""
|
|
34
|
+
size = abs(self.qty)
|
|
35
|
+
if size == 0:
|
|
36
|
+
# Flat — no margin and no liquidation price to track.
|
|
37
|
+
self.margin = 0.0
|
|
38
|
+
self.liquid_price = 0.0
|
|
39
|
+
return
|
|
40
|
+
|
|
41
|
+
# qty is Decimal; cast to float for the price-side math.
|
|
42
|
+
size_f = float(size)
|
|
43
|
+
|
|
44
|
+
# Isolated-margin model.
|
|
45
|
+
# initial_margin: what we lock up to open the position at this leverage.
|
|
46
|
+
# maint_margin: cushion the exchange requires before forcing liquidation.
|
|
47
|
+
notional = self.price * size_f
|
|
48
|
+
initial_margin = notional / self.leverage
|
|
49
|
+
maint_margin = max(0.0, notional * self.maint_margin_rate - self.maint_amount)
|
|
50
|
+
|
|
51
|
+
self.margin = initial_margin + maint_margin
|
|
52
|
+
|
|
53
|
+
# How far the price can move against us before we get liquidated.
|
|
54
|
+
# Long: liquidates when price drops by `offset`.
|
|
55
|
+
# Short: liquidates when price rises by `offset`.
|
|
56
|
+
offset = (initial_margin - maint_margin) / size_f
|
|
57
|
+
self.liquid_price = self.price - offset if self.qty > 0 else self.price + offset
|
|
58
|
+
|
|
59
|
+
def _add_to_position(self, order: Order) -> None:
|
|
60
|
+
new_qty = self.qty + order.qty
|
|
61
|
+
# Weighted-average entry price
|
|
62
|
+
self.price = (
|
|
63
|
+
self.price * float(self.qty) + order.executed_price * float(order.qty)
|
|
64
|
+
) / float(new_qty)
|
|
65
|
+
self.qty = new_qty
|
|
66
|
+
self._recalc_liquidation()
|
|
67
|
+
|
|
68
|
+
def _reset(self) -> None:
|
|
69
|
+
self.qty = ZERO
|
|
70
|
+
self.price = 0.0
|
|
71
|
+
self.margin = 0.0
|
|
72
|
+
self.liquid_price = 0.0
|
|
73
|
+
|
|
74
|
+
def set_leverage(self, leverage: int) -> None:
|
|
75
|
+
if leverage <= 0 or leverage > 100:
|
|
76
|
+
raise Exception("Leverage must be between 1 and 100.")
|
|
77
|
+
self.leverage = leverage
|
|
78
|
+
self._recalc_liquidation()
|
|
79
|
+
|
|
80
|
+
def process_trade(self, order: Order, liquidation: bool = False) -> float:
|
|
81
|
+
"""Apply an executed order. Returns realized PnL (commission excluded)."""
|
|
82
|
+
if order.symbol != self.symbol:
|
|
83
|
+
raise ValueError(f"Order symbol {order.symbol!r} doesn't match position {self.symbol!r}.")
|
|
84
|
+
if order.qty == 0:
|
|
85
|
+
raise ValueError("Order quantity cannot be zero.")
|
|
86
|
+
|
|
87
|
+
if liquidation: # Force liquidation
|
|
88
|
+
loss = -self.margin
|
|
89
|
+
self._reset()
|
|
90
|
+
return loss
|
|
91
|
+
|
|
92
|
+
# Flat or same-side add: update entry price and increase position size.
|
|
93
|
+
if self.qty == 0 or self.qty * order.qty > 0:
|
|
94
|
+
self._add_to_position(order)
|
|
95
|
+
return 0.0
|
|
96
|
+
else:
|
|
97
|
+
closed_qty = min(abs(self.qty), abs(order.qty))
|
|
98
|
+
direction = 1 if self.qty > 0 else -1
|
|
99
|
+
# Long earns (exit - entry) and short earns (entry - exit). Same formula with sign flip.
|
|
100
|
+
pnl = direction * (order.executed_price - self.price) * float(closed_qty)
|
|
101
|
+
|
|
102
|
+
self.qty += order.qty
|
|
103
|
+
|
|
104
|
+
if self.qty == 0:
|
|
105
|
+
self._reset()
|
|
106
|
+
else:
|
|
107
|
+
# If we flipped sides, the new entry price is the executed price.
|
|
108
|
+
flipped = self.qty * order.qty > 0
|
|
109
|
+
if flipped:
|
|
110
|
+
self.price = order.executed_price
|
|
111
|
+
self._recalc_liquidation()
|
|
112
|
+
|
|
113
|
+
return pnl
|
|
@@ -2,8 +2,8 @@ import os
|
|
|
2
2
|
import pandas as pd
|
|
3
3
|
import numpy as np
|
|
4
4
|
from abc import abstractmethod
|
|
5
|
-
from typing import List, Optional,
|
|
6
|
-
from overfitting.data import Data
|
|
5
|
+
from typing import List, Optional, Dict
|
|
6
|
+
from overfitting.data import Data
|
|
7
7
|
from overfitting.broker import Broker
|
|
8
8
|
from overfitting.order import Order
|
|
9
9
|
from overfitting.position import Position
|
|
@@ -11,8 +11,8 @@ from overfitting.analysis.report import Report
|
|
|
11
11
|
from overfitting.slippage import SlippageModel
|
|
12
12
|
|
|
13
13
|
class Strategy:
|
|
14
|
-
def __init__(self,
|
|
15
|
-
data:
|
|
14
|
+
def __init__(self,
|
|
15
|
+
data: Dict[str, pd.DataFrame],
|
|
16
16
|
*,
|
|
17
17
|
benchmark: Optional[pd.DataFrame] = None,
|
|
18
18
|
initial_capital: float =100000,
|
|
@@ -20,9 +20,9 @@ class Strategy:
|
|
|
20
20
|
maint_margin_rate: float =0.005,
|
|
21
21
|
maint_amount: float=0,
|
|
22
22
|
slippage_model: Optional[SlippageModel] = None):
|
|
23
|
-
|
|
23
|
+
|
|
24
24
|
self.benchmark = benchmark
|
|
25
|
-
self.data =
|
|
25
|
+
self.data = Data(data)
|
|
26
26
|
self.broker = Broker(
|
|
27
27
|
data=self.data,
|
|
28
28
|
cash=initial_capital,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: overfitting
|
|
3
|
-
Version: 0.4.
|
|
3
|
+
Version: 0.4.1
|
|
4
4
|
Summary: A Robust Futures CryptoCurrency Backtesting Library.
|
|
5
5
|
Author-email: Dohyun Kim <dohyun.k.july@gmail.com>
|
|
6
6
|
License: MIT
|
|
@@ -98,9 +98,9 @@ class MyStrategy(Strategy):
|
|
|
98
98
|
if previous_sma_short >= previous_sma_long and sma_short < sma_long and position.qty > 0:
|
|
99
99
|
self.market_order(self.asset, -position.qty)
|
|
100
100
|
|
|
101
|
-
|
|
101
|
+
price_df, benchmark_data = load_data()
|
|
102
102
|
strategy = MyStrategy(
|
|
103
|
-
data=
|
|
103
|
+
data={"BTC": price_df},
|
|
104
104
|
benchmark=benchmark_data, # Default = None Optional
|
|
105
105
|
initial_capital=100_000, # Default Optional
|
|
106
106
|
commission_rate=0.0002, # Default Optional
|
|
@@ -1,120 +0,0 @@
|
|
|
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.errors import InitializationError
|
|
5
|
-
|
|
6
|
-
REQUIRED_OHLC = ("open", "high", "low", "close")
|
|
7
|
-
|
|
8
|
-
class Data(dict):
|
|
9
|
-
|
|
10
|
-
open: np.ndarray # [Required] Open prices
|
|
11
|
-
high: np.ndarray # [Required] High prices
|
|
12
|
-
low: np.ndarray # [Required] Low prices
|
|
13
|
-
close: np.ndarray # [Required] Close prices
|
|
14
|
-
timestamp: np.ndarray # Candle timestamps
|
|
15
|
-
index: np.ndarray # Fast datetime64[ns] index
|
|
16
|
-
columns: tuple[str, ...] # Available columns
|
|
17
|
-
n: int # Number of rows
|
|
18
|
-
|
|
19
|
-
"""
|
|
20
|
-
Usage: data.open[i], data.high[i], data.timestamp[i]
|
|
21
|
-
"""
|
|
22
|
-
|
|
23
|
-
def __init__(self, df: pd.DataFrame):
|
|
24
|
-
if not isinstance(df, pd.DataFrame) or df.empty:
|
|
25
|
-
raise InitializationError("Data must be a non-empty pandas DataFrame.")
|
|
26
|
-
|
|
27
|
-
# Validate OHLC
|
|
28
|
-
missing = [c for c in REQUIRED_OHLC if c not in df.columns]
|
|
29
|
-
if missing:
|
|
30
|
-
raise InitializationError(f"Missing required columns: {missing}. Available: {list(df.columns)}")
|
|
31
|
-
|
|
32
|
-
# Resolve timestamp: column or index
|
|
33
|
-
if "timestamp" in df.columns:
|
|
34
|
-
ts = df["timestamp"]
|
|
35
|
-
elif isinstance(df.index, pd.DatetimeIndex) or df.index.name == "timestamp":
|
|
36
|
-
# normalize to a Series so downstream is uniform
|
|
37
|
-
ts = pd.Series(df.index, index=df.index, name="timestamp")
|
|
38
|
-
else:
|
|
39
|
-
raise InitializationError("Provide a 'timestamp' column or use a DatetimeIndex.")
|
|
40
|
-
|
|
41
|
-
# Coerce timestamp to datetime64[ns]
|
|
42
|
-
ts = self._to_datetime_ns(ts)
|
|
43
|
-
|
|
44
|
-
payload = {c: df[c].to_numpy() for c in df.columns}
|
|
45
|
-
payload["timestamp"] = ts.to_numpy()
|
|
46
|
-
|
|
47
|
-
super().__init__(payload)
|
|
48
|
-
object.__setattr__(self, "columns", tuple(sorted(payload.keys(), key=lambda x: (x!='timestamp', x))))
|
|
49
|
-
object.__setattr__(self, "index", ts.to_numpy())
|
|
50
|
-
object.__setattr__(self, "n", len(df))
|
|
51
|
-
|
|
52
|
-
@staticmethod
|
|
53
|
-
def _to_datetime_ns(s: pd.Series) -> pd.Series:
|
|
54
|
-
if is_datetime64_any_dtype(s):
|
|
55
|
-
dt = pd.to_datetime(s)
|
|
56
|
-
# drop tz if present
|
|
57
|
-
try:
|
|
58
|
-
dt = dt.tz_localize(None)
|
|
59
|
-
except Exception:
|
|
60
|
-
pass
|
|
61
|
-
return dt
|
|
62
|
-
|
|
63
|
-
if is_integer_dtype(s) or is_float_dtype(s):
|
|
64
|
-
# Heuristic: ms since epoch vs seconds
|
|
65
|
-
mx = pd.Series(s).max()
|
|
66
|
-
unit = "ms" if mx > 1e12 else "s"
|
|
67
|
-
return pd.to_datetime(s, unit=unit)
|
|
68
|
-
|
|
69
|
-
# Strings or mixed -> let pandas parse
|
|
70
|
-
return pd.to_datetime(s, errors="raise")
|
|
71
|
-
|
|
72
|
-
def __getattr__(self, key):
|
|
73
|
-
# attribute-style access for columns
|
|
74
|
-
try:
|
|
75
|
-
return self[key]
|
|
76
|
-
except KeyError:
|
|
77
|
-
raise AttributeError( f"'Data' has no field '{key}'. Available: {', '.join(self.columns)}")
|
|
78
|
-
|
|
79
|
-
def __len__(self):
|
|
80
|
-
return self.n
|
|
81
|
-
|
|
82
|
-
def __setattr__(self, key, value):
|
|
83
|
-
# keep the container read-only (avoids accidental mistakes)
|
|
84
|
-
if key in {"index", "columns", "n"}:
|
|
85
|
-
object.__setattr__(self, key, value)
|
|
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
|
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
from math import copysign
|
|
2
|
-
from overfitting.order import Order
|
|
3
|
-
|
|
4
|
-
class Position:
|
|
5
|
-
def __init__(self,
|
|
6
|
-
symbol:str =None,
|
|
7
|
-
maint_margin_rate:float=0.005,
|
|
8
|
-
maint_amount:float=0):
|
|
9
|
-
|
|
10
|
-
self.symbol = symbol
|
|
11
|
-
self.qty = 0.0
|
|
12
|
-
self.price = 0.0
|
|
13
|
-
self.liquid_price = 0.0
|
|
14
|
-
self.margin = 0.0
|
|
15
|
-
self.leverage = 1 # Default Leverage
|
|
16
|
-
self.maint_margin_rate = maint_margin_rate
|
|
17
|
-
self.maint_amount = maint_amount
|
|
18
|
-
|
|
19
|
-
def __repr__(self):
|
|
20
|
-
return (f"Position("
|
|
21
|
-
f"symbol={self.symbol}, "
|
|
22
|
-
f"qty={self.qty}, "
|
|
23
|
-
f"price={self.price}, "
|
|
24
|
-
f"liquid_price={self.liquid_price}, "
|
|
25
|
-
f"margin={self.margin}, "
|
|
26
|
-
f"leverage={self.leverage}, "
|
|
27
|
-
f"maint_margin_rate={self.maint_margin_rate}, "
|
|
28
|
-
f"maint_amount={self.maint_amount})")
|
|
29
|
-
|
|
30
|
-
def _update_liquid_price(self):
|
|
31
|
-
"""
|
|
32
|
-
NOTE: liquidation price is calculated based on ISOLATED mode.
|
|
33
|
-
|
|
34
|
-
Liquidation Price Calculation:
|
|
35
|
-
* Initial Margin = price * size / leverage
|
|
36
|
-
* Maint Margin = price * size * margin rate - margin amount
|
|
37
|
-
[LONG] LP = Entry Price - (Initial Margin - Maintenance Margin)
|
|
38
|
-
[SHORT] LP = Entry Price + (Initial Margin - Maintenance Margin)
|
|
39
|
-
"""
|
|
40
|
-
q = abs(self.qty)
|
|
41
|
-
if q == 0:
|
|
42
|
-
self.liquid_price = 0.0
|
|
43
|
-
self.margin = 0.0
|
|
44
|
-
return
|
|
45
|
-
|
|
46
|
-
notional = self.price * q
|
|
47
|
-
im = notional / self.leverage
|
|
48
|
-
mm = max(0.0, notional * self.maint_margin_rate - self.maint_amount)
|
|
49
|
-
|
|
50
|
-
self.margin = im + mm
|
|
51
|
-
delta_p = (im - mm) / q
|
|
52
|
-
|
|
53
|
-
if self.qty > 0: # long
|
|
54
|
-
self.liquid_price = self.price - delta_p
|
|
55
|
-
else: # short
|
|
56
|
-
self.liquid_price = self.price + delta_p
|
|
57
|
-
|
|
58
|
-
def _liquidate(self):
|
|
59
|
-
"""Returns PNL which is position margin * -1"""
|
|
60
|
-
pnl = -self.margin
|
|
61
|
-
self._reset()
|
|
62
|
-
return pnl
|
|
63
|
-
|
|
64
|
-
def _reset(self):
|
|
65
|
-
self.qty = 0.0
|
|
66
|
-
self.price = 0.0
|
|
67
|
-
self.liquid_price = 0.0
|
|
68
|
-
self.margin = 0.0
|
|
69
|
-
|
|
70
|
-
def set_leverage(self, leverage):
|
|
71
|
-
if leverage <= 0 or leverage > 100:
|
|
72
|
-
raise Exception("set_leverage() Invalid Leverage. Please Choose Between 0 and 100")
|
|
73
|
-
|
|
74
|
-
self.leverage = leverage
|
|
75
|
-
self._update_liquid_price()
|
|
76
|
-
|
|
77
|
-
def _calculate_realized_pnl(self, entry_price: float, exit_price: float, qty: float, side: float):
|
|
78
|
-
"""
|
|
79
|
-
side > 0 : position is long
|
|
80
|
-
side < 0 : position is short
|
|
81
|
-
"""
|
|
82
|
-
if side > 0:
|
|
83
|
-
return (exit_price - entry_price) * qty
|
|
84
|
-
return (entry_price - exit_price) * qty
|
|
85
|
-
|
|
86
|
-
def process_trade(self, order: Order, liquidation: bool = False) -> float:
|
|
87
|
-
if self.symbol != order.symbol:
|
|
88
|
-
raise ValueError("Cannot process trade with a different symbol.")
|
|
89
|
-
if order.qty == 0:
|
|
90
|
-
raise ValueError("Transaction quantity cannot be zero.")
|
|
91
|
-
|
|
92
|
-
if liquidation:
|
|
93
|
-
return self._liquidate()
|
|
94
|
-
|
|
95
|
-
same_side = self.qty == 0 or (self.qty * order.qty > 0)
|
|
96
|
-
|
|
97
|
-
if same_side:
|
|
98
|
-
total = self.qty + order.qty
|
|
99
|
-
self.price = (self.price * self.qty + order.executed_price * order.qty) / total
|
|
100
|
-
self.qty = total
|
|
101
|
-
self._update_liquid_price()
|
|
102
|
-
return 0.0
|
|
103
|
-
|
|
104
|
-
# Opposite side — reduce, close, or flip
|
|
105
|
-
close_qty = min(abs(self.qty), abs(order.qty))
|
|
106
|
-
side = 1 if self.qty > 0 else -1
|
|
107
|
-
pnl = self._calculate_realized_pnl(self.price, order.executed_price, close_qty, side)
|
|
108
|
-
|
|
109
|
-
self.qty += order.qty
|
|
110
|
-
|
|
111
|
-
if self.qty == 0:
|
|
112
|
-
self._reset()
|
|
113
|
-
else: # flip or reduce
|
|
114
|
-
if self.qty * order.qty > 0: # flipped
|
|
115
|
-
self.price = order.executed_price
|
|
116
|
-
self._update_liquid_price()
|
|
117
|
-
|
|
118
|
-
return pnl
|
|
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
|