overfitting 0.3.2__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.3.2 → overfitting-0.4.1}/PKG-INFO +4 -3
- {overfitting-0.3.2 → overfitting-0.4.1}/README.md +4 -3
- {overfitting-0.3.2 → overfitting-0.4.1}/overfitting/analysis/graph.py +8 -0
- {overfitting-0.3.2 → overfitting-0.4.1}/overfitting/analysis/report.py +29 -25
- {overfitting-0.3.2 → overfitting-0.4.1}/overfitting/analysis/viewer.py +3 -9
- {overfitting-0.3.2 → overfitting-0.4.1}/overfitting/broker.py +27 -25
- overfitting-0.4.1/overfitting/data.py +89 -0
- {overfitting-0.3.2 → overfitting-0.4.1}/overfitting/errors.py +3 -0
- overfitting-0.4.1/overfitting/order.py +63 -0
- overfitting-0.4.1/overfitting/position.py +113 -0
- {overfitting-0.3.2 → overfitting-0.4.1}/overfitting/strategy.py +6 -6
- {overfitting-0.3.2 → overfitting-0.4.1}/overfitting/types.py +2 -3
- {overfitting-0.3.2 → overfitting-0.4.1}/overfitting.egg-info/PKG-INFO +4 -3
- {overfitting-0.3.2 → overfitting-0.4.1}/pyproject.toml +1 -1
- overfitting-0.3.2/overfitting/data.py +0 -120
- overfitting-0.3.2/overfitting/order.py +0 -97
- overfitting-0.3.2/overfitting/position.py +0 -145
- {overfitting-0.3.2 → overfitting-0.4.1}/LICENSE +0 -0
- {overfitting-0.3.2 → overfitting-0.4.1}/overfitting/__init__.py +0 -0
- {overfitting-0.3.2 → overfitting-0.4.1}/overfitting/analysis/__init__.py +0 -0
- {overfitting-0.3.2 → overfitting-0.4.1}/overfitting/indicators.py +0 -0
- {overfitting-0.3.2 → overfitting-0.4.1}/overfitting/slippage.py +0 -0
- {overfitting-0.3.2 → overfitting-0.4.1}/overfitting.egg-info/SOURCES.txt +0 -0
- {overfitting-0.3.2 → overfitting-0.4.1}/overfitting.egg-info/dependency_links.txt +0 -0
- {overfitting-0.3.2 → overfitting-0.4.1}/overfitting.egg-info/requires.txt +0 -0
- {overfitting-0.3.2 → overfitting-0.4.1}/overfitting.egg-info/top_level.txt +0 -0
- {overfitting-0.3.2 → 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.
|
|
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
|
|
@@ -50,6 +50,7 @@ Overfitting simulates a realistic crypto futures trading environment including
|
|
|
50
50
|
```python
|
|
51
51
|
import pandas as pd
|
|
52
52
|
from overfitting import Strategy, BacktestViewer
|
|
53
|
+
from overfitting.indicators import SMA
|
|
53
54
|
|
|
54
55
|
def load_data():
|
|
55
56
|
df = pd.read_csv('./data/BTCUSDT.csv')
|
|
@@ -97,9 +98,9 @@ class MyStrategy(Strategy):
|
|
|
97
98
|
if previous_sma_short >= previous_sma_long and sma_short < sma_long and position.qty > 0:
|
|
98
99
|
self.market_order(self.asset, -position.qty)
|
|
99
100
|
|
|
100
|
-
|
|
101
|
+
price_df, benchmark_data = load_data()
|
|
101
102
|
strategy = MyStrategy(
|
|
102
|
-
data=
|
|
103
|
+
data={"BTC": price_df},
|
|
103
104
|
benchmark=benchmark_data, # Default = None Optional
|
|
104
105
|
initial_capital=100_000, # Default Optional
|
|
105
106
|
commission_rate=0.0002, # Default Optional
|
|
@@ -15,6 +15,7 @@ Overfitting simulates a realistic crypto futures trading environment including
|
|
|
15
15
|
```python
|
|
16
16
|
import pandas as pd
|
|
17
17
|
from overfitting import Strategy, BacktestViewer
|
|
18
|
+
from overfitting.indicators import SMA
|
|
18
19
|
|
|
19
20
|
def load_data():
|
|
20
21
|
df = pd.read_csv('./data/BTCUSDT.csv')
|
|
@@ -62,9 +63,9 @@ class MyStrategy(Strategy):
|
|
|
62
63
|
if previous_sma_short >= previous_sma_long and sma_short < sma_long and position.qty > 0:
|
|
63
64
|
self.market_order(self.asset, -position.qty)
|
|
64
65
|
|
|
65
|
-
|
|
66
|
+
price_df, benchmark_data = load_data()
|
|
66
67
|
strategy = MyStrategy(
|
|
67
|
-
data=
|
|
68
|
+
data={"BTC": price_df},
|
|
68
69
|
benchmark=benchmark_data, # Default = None Optional
|
|
69
70
|
initial_capital=100_000, # Default Optional
|
|
70
71
|
commission_rate=0.0002, # Default Optional
|
|
@@ -162,4 +163,4 @@ You can simply test multiple currencies by passing data as dict[str, pd.DataFram
|
|
|
162
163
|
- **Parameter Optimizer**
|
|
163
164
|
A simple optimizer to help find the best-performing strategy parameters (like SMA windows, thresholds, etc.) based on backtest results.
|
|
164
165
|
|
|
165
|
-
> 💡 Got feedback or suggestions? Feel free to open an issue or contribute via pull request.
|
|
166
|
+
> 💡 Got feedback or suggestions? Feel free to open an issue or contribute via pull request.
|
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
# This file includes code derived from Pyfolio
|
|
2
|
+
# https://github.com/quantopian/pyfolio
|
|
3
|
+
#
|
|
4
|
+
# Original Copyright 2018 Quantopian, Inc.
|
|
5
|
+
# Licensed under Apache License 2.0
|
|
6
|
+
#
|
|
7
|
+
# Modifications made by Dohyun, 2025
|
|
8
|
+
|
|
1
9
|
import pandas as pd
|
|
2
10
|
import numpy as np
|
|
3
11
|
from numpy.lib.stride_tricks import as_strided
|
|
@@ -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,16 +1,17 @@
|
|
|
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
|
|
7
8
|
from overfitting.types import OrderType
|
|
8
|
-
from overfitting.errors import EmptyOrderParameters, InvalidOrderParameters, LiquidationError
|
|
9
|
+
from overfitting.errors import EmptyOrderParameters, InvalidOrderParameters, LiquidationError, StopOrderRejected
|
|
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)
|
|
@@ -113,16 +114,15 @@ class Broker:
|
|
|
113
114
|
open = self._open(symbol, self._i)
|
|
114
115
|
if ((order.qty > 0 and order.stop_price < open) or
|
|
115
116
|
(order.qty < 0 and order.stop_price > open)):
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
return order
|
|
117
|
+
raise StopOrderRejected(
|
|
118
|
+
f"STOP order rejected: stop price {order.stop_price} would trigger immediately at current price {open}."
|
|
119
|
+
)
|
|
120
120
|
|
|
121
121
|
# Put new order in the open_orders list if not rejected
|
|
122
122
|
self.open_orders[symbol][order.id] = order
|
|
123
123
|
return order
|
|
124
124
|
|
|
125
|
-
def cancel_order(self, symbol, order_id: str
|
|
125
|
+
def cancel_order(self, symbol, order_id: str) -> Optional[Order]:
|
|
126
126
|
if symbol not in self.open_orders:
|
|
127
127
|
return None
|
|
128
128
|
|
|
@@ -130,19 +130,16 @@ class Broker:
|
|
|
130
130
|
return None
|
|
131
131
|
|
|
132
132
|
order = self.open_orders[symbol][order_id]
|
|
133
|
-
order.cancel(reason)
|
|
134
133
|
del self.open_orders[symbol][order_id]
|
|
135
134
|
|
|
136
135
|
return order
|
|
137
136
|
|
|
138
|
-
def cancel_all_orders(self, symbol: str
|
|
137
|
+
def cancel_all_orders(self, symbol: str):
|
|
139
138
|
if symbol not in self.open_orders:
|
|
140
139
|
return None
|
|
141
140
|
|
|
142
141
|
orders = self.open_orders[symbol]
|
|
143
142
|
for order_id in list(orders.keys()):
|
|
144
|
-
order = orders[order_id]
|
|
145
|
-
order.cancel(reason)
|
|
146
143
|
del orders[order_id]
|
|
147
144
|
|
|
148
145
|
def get_position(self, symbol: str) -> Position:
|
|
@@ -184,14 +181,19 @@ class Broker:
|
|
|
184
181
|
if not order.theoretical_price:
|
|
185
182
|
raise EmptyOrderParameters("Cannot Exeucte Orders without Price. Please check the order")
|
|
186
183
|
|
|
187
|
-
notional = abs(order.qty) * order.executed_price
|
|
188
|
-
commission = notional * self.commission_rate
|
|
189
184
|
position = self.position[symbol]
|
|
190
|
-
pnl = position.update(order, liquidation)
|
|
191
185
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
186
|
+
notional = float(abs(order.qty)) * order.executed_price
|
|
187
|
+
commission = notional * self.commission_rate
|
|
188
|
+
pnl = position.process_trade(order, liquidation)
|
|
189
|
+
executed_time = pd.to_datetime(self.data.index[self._i])
|
|
190
|
+
|
|
191
|
+
# Update Order fields as it has been filled
|
|
192
|
+
order.pnl = pnl
|
|
193
|
+
order.executed_at = executed_time
|
|
194
|
+
order.commission = commission
|
|
195
|
+
order.realized_pnl = pnl - commission
|
|
196
|
+
self.is_liquidation_order = True if liquidation else False
|
|
195
197
|
|
|
196
198
|
# Update trades and balance
|
|
197
199
|
self.trades.append(order.to_dict())
|
|
@@ -244,7 +246,7 @@ class Broker:
|
|
|
244
246
|
# Check for the STOP order Trigger Condition
|
|
245
247
|
if ((order.qty > 0 and high >= order.stop_price) or
|
|
246
248
|
(order.qty < 0 and low <= order.stop_price)):
|
|
247
|
-
order.
|
|
249
|
+
order.is_triggered = True # Trigger the Order
|
|
248
250
|
|
|
249
251
|
if order.is_triggered == True:
|
|
250
252
|
if order.price is None: # STOP MARKET ORDER
|
|
@@ -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
|
+
)
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import uuid
|
|
2
|
+
from decimal import Decimal
|
|
3
|
+
import pandas as pd
|
|
4
|
+
from overfitting.types import OrderType
|
|
5
|
+
|
|
6
|
+
class Order:
|
|
7
|
+
def __init__(self,
|
|
8
|
+
time: pd.Timestamp,
|
|
9
|
+
symbol: str,
|
|
10
|
+
qty: float,
|
|
11
|
+
price:float,
|
|
12
|
+
type: OrderType,
|
|
13
|
+
stop_price: float = None,
|
|
14
|
+
label: str= None):
|
|
15
|
+
|
|
16
|
+
self.id = uuid.uuid4().hex[:16]
|
|
17
|
+
self.created_at = time
|
|
18
|
+
self.executed_at = None
|
|
19
|
+
self.symbol = symbol
|
|
20
|
+
# Decimal qty so Position bookkeeping is exact
|
|
21
|
+
self.qty = Decimal(str(qty))
|
|
22
|
+
self.side = "LONG" if self.qty > 0 else "SHORT"
|
|
23
|
+
self.price = price
|
|
24
|
+
self.type = type
|
|
25
|
+
self.stop_price = stop_price
|
|
26
|
+
self.is_triggered = False
|
|
27
|
+
self.is_liquidation_order = False
|
|
28
|
+
self.theoretical_price = 0
|
|
29
|
+
self.executed_price = 0
|
|
30
|
+
self.commission = 0
|
|
31
|
+
self.pnl = 0
|
|
32
|
+
self.realized_pnl = 0
|
|
33
|
+
self.label = label
|
|
34
|
+
|
|
35
|
+
def __repr__(self):
|
|
36
|
+
return (f"Order(id={self.id}"
|
|
37
|
+
f"created_at={self.created_at}, "
|
|
38
|
+
f"exeucted_at={self.executed_at}, "
|
|
39
|
+
f"symbol={self.symbol}, "
|
|
40
|
+
f"side={self.side}, "
|
|
41
|
+
f"qty={self.qty}, "
|
|
42
|
+
f"price={self.price}, "
|
|
43
|
+
f"type={self.type}, "
|
|
44
|
+
f"stop_price={self.stop_price}, "
|
|
45
|
+
f"is_triggered={self.is_triggered}, "
|
|
46
|
+
f"theoretical_price={self.theoretical_price}, "
|
|
47
|
+
f"executed_price={self.executed_price}, "
|
|
48
|
+
f"commission={self.commission}, "
|
|
49
|
+
f"pnl={self.pnl}, "
|
|
50
|
+
f"realized_pnl={self.realized_pnl}, "
|
|
51
|
+
f"label={self.label})")
|
|
52
|
+
|
|
53
|
+
def to_dict(self):
|
|
54
|
+
return {
|
|
55
|
+
k: (
|
|
56
|
+
v.name if isinstance(v, OrderType)
|
|
57
|
+
else float(v) if isinstance(v, Decimal)
|
|
58
|
+
else v
|
|
59
|
+
)
|
|
60
|
+
for k, v in self.__dict__.items()
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
|
|
@@ -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.
|
|
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
|
|
@@ -50,6 +50,7 @@ Overfitting simulates a realistic crypto futures trading environment including
|
|
|
50
50
|
```python
|
|
51
51
|
import pandas as pd
|
|
52
52
|
from overfitting import Strategy, BacktestViewer
|
|
53
|
+
from overfitting.indicators import SMA
|
|
53
54
|
|
|
54
55
|
def load_data():
|
|
55
56
|
df = pd.read_csv('./data/BTCUSDT.csv')
|
|
@@ -97,9 +98,9 @@ class MyStrategy(Strategy):
|
|
|
97
98
|
if previous_sma_short >= previous_sma_long and sma_short < sma_long and position.qty > 0:
|
|
98
99
|
self.market_order(self.asset, -position.qty)
|
|
99
100
|
|
|
100
|
-
|
|
101
|
+
price_df, benchmark_data = load_data()
|
|
101
102
|
strategy = MyStrategy(
|
|
102
|
-
data=
|
|
103
|
+
data={"BTC": price_df},
|
|
103
104
|
benchmark=benchmark_data, # Default = None Optional
|
|
104
105
|
initial_capital=100_000, # Default Optional
|
|
105
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,97 +0,0 @@
|
|
|
1
|
-
import uuid
|
|
2
|
-
import pandas as pd
|
|
3
|
-
from overfitting.types import OrderType, Status
|
|
4
|
-
|
|
5
|
-
class Order:
|
|
6
|
-
def __init__(self,
|
|
7
|
-
time: pd.Timestamp,
|
|
8
|
-
symbol: str,
|
|
9
|
-
qty: float,
|
|
10
|
-
price:float,
|
|
11
|
-
type: OrderType,
|
|
12
|
-
stop_price: float = None,
|
|
13
|
-
label: str= None):
|
|
14
|
-
|
|
15
|
-
self.id = self.make_id()
|
|
16
|
-
self.created_at = time
|
|
17
|
-
self.executed_at = None
|
|
18
|
-
self.symbol = symbol
|
|
19
|
-
self.side = "LONG" if qty > 0 else "SHORT"
|
|
20
|
-
self.qty = qty
|
|
21
|
-
self.price = price
|
|
22
|
-
self.type = type
|
|
23
|
-
self._status = Status.OPEN
|
|
24
|
-
self.stop_price = stop_price
|
|
25
|
-
self.is_triggered = False
|
|
26
|
-
self.reason = None
|
|
27
|
-
self.theoretical_price = 0
|
|
28
|
-
self.executed_price = 0
|
|
29
|
-
self.commission = 0
|
|
30
|
-
self.pnl = 0
|
|
31
|
-
self.realized_pnl = 0
|
|
32
|
-
self.label = label
|
|
33
|
-
|
|
34
|
-
def __repr__(self):
|
|
35
|
-
return (f"Order(id={self.id}, "
|
|
36
|
-
f"created_at={self.created_at}, "
|
|
37
|
-
f"exeucted_at={self.executed_at}, "
|
|
38
|
-
f"symbol={self.symbol}, "
|
|
39
|
-
f"side={self.side}, "
|
|
40
|
-
f"qty={self.qty}, "
|
|
41
|
-
f"price={self.price}, "
|
|
42
|
-
f"type={self.type}, "
|
|
43
|
-
f"status={self._status}, "
|
|
44
|
-
f"stop_price={self.stop_price}, "
|
|
45
|
-
f"is_triggered={self.is_triggered}, "
|
|
46
|
-
f"reason={self.reason}, "
|
|
47
|
-
f"theoretical_price={self.theoretical_price}, "
|
|
48
|
-
f"executed_price={self.executed_price}, "
|
|
49
|
-
f"commission={self.commission}, "
|
|
50
|
-
f"pnl={self.pnl}, "
|
|
51
|
-
f"realized_pnl={self.realized_pnl}, "
|
|
52
|
-
f"label={self.label})")
|
|
53
|
-
|
|
54
|
-
def to_dict(self):
|
|
55
|
-
out = {}
|
|
56
|
-
for k, v in self.__dict__.items():
|
|
57
|
-
if k == "_status":
|
|
58
|
-
out["status"] = v.name
|
|
59
|
-
elif k == "type":
|
|
60
|
-
out["type"] = v.name
|
|
61
|
-
else:
|
|
62
|
-
out[k] = v
|
|
63
|
-
return out
|
|
64
|
-
|
|
65
|
-
@staticmethod
|
|
66
|
-
def make_id():
|
|
67
|
-
return uuid.uuid4().hex[:16]
|
|
68
|
-
|
|
69
|
-
@property
|
|
70
|
-
def status(self):
|
|
71
|
-
return self._status
|
|
72
|
-
|
|
73
|
-
@status.setter
|
|
74
|
-
def status(self, status):
|
|
75
|
-
self._status = status
|
|
76
|
-
|
|
77
|
-
def trigger(self):
|
|
78
|
-
self.is_triggered = True
|
|
79
|
-
|
|
80
|
-
def cancel(self, reason=None):
|
|
81
|
-
self.status = Status.CANCELLED
|
|
82
|
-
self.reason = reason
|
|
83
|
-
|
|
84
|
-
def reject(self, reason=None):
|
|
85
|
-
self.status = Status.REJECTED
|
|
86
|
-
self.reason = reason
|
|
87
|
-
|
|
88
|
-
def fill(self, commission, pnl, executed_price, time, reason=None):
|
|
89
|
-
self.status = Status.FILLED
|
|
90
|
-
self.executed_at = time
|
|
91
|
-
self.commission = commission
|
|
92
|
-
self.pnl = pnl
|
|
93
|
-
realized = pnl - commission
|
|
94
|
-
self.realized_pnl = realized
|
|
95
|
-
self.executed_price = executed_price
|
|
96
|
-
self.reason = reason
|
|
97
|
-
|
|
@@ -1,145 +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 __getattr__(self, attr):
|
|
31
|
-
return object.__getattribute__(self, attr)
|
|
32
|
-
|
|
33
|
-
def __setattr__(self, attr, value):
|
|
34
|
-
object.__setattr__(self, attr, value)
|
|
35
|
-
|
|
36
|
-
def _update_liquid_price(self):
|
|
37
|
-
"""
|
|
38
|
-
NOTE: liquidation price is calculated based on ISOLATED mode.
|
|
39
|
-
|
|
40
|
-
Liquidation Price Calculation:
|
|
41
|
-
* Initial Margin = price * size / leverage
|
|
42
|
-
* Maint Margin = price * size * margin rate - margin amount
|
|
43
|
-
[LONG] LP = Entry Price - (Initial Margin - Maintenance Margin)
|
|
44
|
-
[SHORT] LP = Entry Price + (Initial Margin - Maintenance Margin)
|
|
45
|
-
"""
|
|
46
|
-
q = abs(self.qty)
|
|
47
|
-
if q == 0:
|
|
48
|
-
self.liquid_price = 0.0
|
|
49
|
-
self.margin = 0.0
|
|
50
|
-
return
|
|
51
|
-
|
|
52
|
-
notional = self.price * q
|
|
53
|
-
im = notional / self.leverage
|
|
54
|
-
mm = max(0.0, notional * self.maint_margin_rate - self.maint_amount)
|
|
55
|
-
|
|
56
|
-
self.margin = im + mm
|
|
57
|
-
delta_p = (im - mm) / q
|
|
58
|
-
|
|
59
|
-
if self.qty > 0: # long
|
|
60
|
-
self.liquid_price = self.price - delta_p
|
|
61
|
-
else: # short
|
|
62
|
-
self.liquid_price = self.price + delta_p
|
|
63
|
-
|
|
64
|
-
def _liquidate(self):
|
|
65
|
-
"""Returns PNL which is position Margin * -1"""
|
|
66
|
-
l = self.margin
|
|
67
|
-
self.qty = 0.0
|
|
68
|
-
self.price = 0.0
|
|
69
|
-
self.liquid_price = 0.0
|
|
70
|
-
self.margin = 0.0
|
|
71
|
-
return -l
|
|
72
|
-
|
|
73
|
-
def set_leverage(self, leverage):
|
|
74
|
-
if leverage <= 0 or leverage > 100:
|
|
75
|
-
raise Exception("set_leverage() Invalid Leverage. Please Choose Between 0 and 100")
|
|
76
|
-
|
|
77
|
-
self.leverage = leverage
|
|
78
|
-
self._update_liquid_price()
|
|
79
|
-
|
|
80
|
-
def _calculate_pnl(self, txn: Order):
|
|
81
|
-
"""Assumes this is only called during reducing/closing trades"""
|
|
82
|
-
closing_qty = txn.qty
|
|
83
|
-
trade_size = abs(closing_qty)
|
|
84
|
-
|
|
85
|
-
# Closing long position (selling)
|
|
86
|
-
if closing_qty < 0:
|
|
87
|
-
pnl_per_unit = txn.executed_price - self.price
|
|
88
|
-
# Closing short position (buying to cover)
|
|
89
|
-
else:
|
|
90
|
-
pnl_per_unit = self.price - txn.executed_price
|
|
91
|
-
|
|
92
|
-
return pnl_per_unit * trade_size
|
|
93
|
-
|
|
94
|
-
def update(self, txn: Order, liquidation = False):
|
|
95
|
-
if self.symbol != txn.symbol:
|
|
96
|
-
raise ValueError("Cannot update with a different symbol.")
|
|
97
|
-
|
|
98
|
-
if txn.qty == 0:
|
|
99
|
-
raise ValueError("Transaction quantity cannot be zero.")
|
|
100
|
-
|
|
101
|
-
if liquidation:
|
|
102
|
-
return self._liquidate()
|
|
103
|
-
|
|
104
|
-
pnl = 0.0
|
|
105
|
-
total_qty = self.qty + txn.qty
|
|
106
|
-
|
|
107
|
-
# Case 1: Position fully closed
|
|
108
|
-
if total_qty == 0:
|
|
109
|
-
pnl = self._calculate_pnl(txn)
|
|
110
|
-
self.qty = 0
|
|
111
|
-
self.price = 0.0
|
|
112
|
-
self.margin = 0.0
|
|
113
|
-
self.liquid_price = 0.0
|
|
114
|
-
|
|
115
|
-
else:
|
|
116
|
-
txn_side = copysign(1, txn.qty)
|
|
117
|
-
current_side = copysign(1, self.qty) if self.qty != 0 else txn_side
|
|
118
|
-
|
|
119
|
-
# Case 2: Partially closing or flipping position
|
|
120
|
-
if txn_side != current_side:
|
|
121
|
-
pnl = self._calculate_pnl(txn)
|
|
122
|
-
|
|
123
|
-
# If position flips (e.g., long → short or vice versa)
|
|
124
|
-
if abs(txn.qty) > abs(self.qty):
|
|
125
|
-
self.price = txn.executed_price # new position starts at txn price
|
|
126
|
-
|
|
127
|
-
else:
|
|
128
|
-
# Case 3: Adding to an existing position
|
|
129
|
-
weighted_cost = (self.price * self.qty) + (txn.executed_price * txn.qty)
|
|
130
|
-
self.price = weighted_cost / total_qty
|
|
131
|
-
|
|
132
|
-
self.qty = total_qty
|
|
133
|
-
self._update_liquid_price()
|
|
134
|
-
|
|
135
|
-
return pnl
|
|
136
|
-
|
|
137
|
-
def to_dict(self):
|
|
138
|
-
return{
|
|
139
|
-
'symbol': self.symbol,
|
|
140
|
-
'qty': self.qty,
|
|
141
|
-
'price': self.price,
|
|
142
|
-
'liquid_price': self.liquid_price,
|
|
143
|
-
'margin': self.margin,
|
|
144
|
-
'leverage': self.leverage
|
|
145
|
-
}
|
|
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
|