-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbacktesting.py
More file actions
233 lines (188 loc) · 9.08 KB
/
backtesting.py
File metadata and controls
233 lines (188 loc) · 9.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
"""
backtesting engine - does the heavy lifting for trade simulation
probably has some bugs but seems to work
"""
import pandas as pd
import numpy as np
class BtEngine:
def __init__(self, initialCapital=100000, commission=0.001):
# setup bt engine with some defaults
self.initialCapital = initialCapital
self.commission = commission
def runBt(self, signals, regimeData, capital=None):
# main backtesting function - this is where the magic happens
if capital is None:
capital = self.initialCapital
# prepare the data for backtesting
btData = signals.copy()
btData['regime'] = regimeData
# initialize portfolio tracking variables
btData = self._initPortfolioTracking(btData, capital)
# execute trades and track the portfolio - TODO: optimize this loop
tradeHistory = []
currentPosition = 0
availableCash = capital
for i in range(1, len(btData)):
# get current market state
currentPrice = btData['price'].iloc[i]
currentRegime = btData['regime'].iloc[i]
# check for position changes
if 'positions' in btData.columns:
positionChange = btData['positions'].iloc[i]
if not pd.isna(positionChange) and positionChange != 0:
# execute the trade
tradeResult = self._executeTrade(
positionChange, currentPrice, currentPosition, availableCash,
btData.index[i], currentRegime
)
if tradeResult:
currentPosition = tradeResult['new_position']
availableCash = tradeResult['new_cash']
tradeHistory.append(tradeResult['trade_record'])
# update portfolio values
holdingsValue = currentPosition * currentPrice
totalValue = availableCash + holdingsValue
btData.loc[btData.index[i], 'holdings'] = holdingsValue
btData.loc[btData.index[i], 'cash'] = availableCash
btData.loc[btData.index[i], 'total'] = totalValue
# calculate returns and performance metrics
btData = self._calculatePortfolioReturns(btData)
return btData, tradeHistory
def _initPortfolioTracking(self, data, initialCapital):
# setup tracking columns for portfolio
data['holdings'] = 0.0
data['cash'] = initialCapital
data['total'] = initialCapital
data['returns'] = 0.0
data['portfolio_returns'] = 0.0
data['cumulative_returns'] = 1.0
return data
def _executeTrade(self, positionChange, price, currentPos, currentCash, timestamp, regime):
# execute a single trade with commission costs
try:
if positionChange > 0: # buying
if currentPos <= 0: # not currently long
# calculate how many shares we can afford
maxShares = currentCash / (price * (1 + self.commission))
sharesToBuy = int(maxShares)
if sharesToBuy > 0:
tradeCost = sharesToBuy * price * (1 + self.commission)
newCash = currentCash - tradeCost
newPosition = currentPos + sharesToBuy
return {
'new_position': newPosition,
'new_cash': newCash,
'trade_record': {
'date': timestamp,
'action': 'BUY',
'price': price,
'shares': sharesToBuy,
'commission': sharesToBuy * price * self.commission,
'regime': regime,
'portfolio_value': newCash + newPosition * price
}
}
elif positionChange < 0: # selling
if currentPos > 0: # currently holding shares
# sell all shares
saleProceeds = currentPos * price * (1 - self.commission)
newCash = currentCash + saleProceeds
return {
'new_position': 0,
'new_cash': newCash,
'trade_record': {
'date': timestamp,
'action': 'SELL',
'price': price,
'shares': currentPos,
'commission': currentPos * price * self.commission,
'regime': regime,
'portfolio_value': newCash
}
}
return None
except Exception as e:
print(f"error executing trade: {e}")
return None
def _calculatePortfolioReturns(self, btData):
# calculate portfolio returns and benchmark comparison
# portfolio returns
btData['portfolio_returns'] = btData['total'].pct_change()
btData['cumulative_returns'] = (1 + btData['portfolio_returns']).cumprod()
# benchmark returns (simple buy and hold)
btData['benchmark_returns'] = btData['price'].pct_change()
btData['benchmark_cumulative'] = (1 + btData['benchmark_returns']).cumprod()
# excess returns over benchmark
btData['excess_returns'] = (
btData['portfolio_returns'] - btData['benchmark_returns']
)
return btData
def calculateTradeStatistics(self, trades):
# calculate detailed statistics about the trades
if not trades:
return {'total_trades': 0, 'message': 'no trades executed'}
tradeDf = pd.DataFrame(trades)
# separate buy and sell trades for analysis
buyTrades = tradeDf[tradeDf['action'] == 'BUY'].reset_index(drop=True)
sellTrades = tradeDf[tradeDf['action'] == 'SELL'].reset_index(drop=True)
# calculate round-trip trade results
roundTripTrades = []
minTradeCount = min(len(buyTrades), len(sellTrades))
for i in range(minTradeCount):
entryPrice = buyTrades.iloc[i]['price']
exitPrice = sellTrades.iloc[i]['price']
shareCount = buyTrades.iloc[i]['shares']
entryCommission = buyTrades.iloc[i]['commission']
exitCommission = sellTrades.iloc[i]['commission']
pnl = (exitPrice - entryPrice) * shareCount - entryCommission - exitCommission
returnPct = pnl / (entryPrice * shareCount)
holdingPeriod = (sellTrades.iloc[i]['date'] - buyTrades.iloc[i]['date']).days
roundTripTrades.append({
'entry_date': buyTrades.iloc[i]['date'],
'exit_date': sellTrades.iloc[i]['date'],
'entry_price': entryPrice,
'exit_price': exitPrice,
'shares': shareCount,
'pnl': pnl,
'return_percent': returnPct,
'holding_period_days': holdingPeriod,
'entry_regime': buyTrades.iloc[i]['regime'],
'exit_regime': sellTrades.iloc[i]['regime']
})
if roundTripTrades:
rtDf = pd.DataFrame(roundTripTrades)
# calculate key statistics
winningTrades = rtDf[rtDf['pnl'] > 0]
losingTrades = rtDf[rtDf['pnl'] <= 0]
stats = {
'total_round_trips': len(roundTripTrades),
'winning_trades': len(winningTrades),
'losing_trades': len(losingTrades),
'win_rate': len(winningTrades) / len(roundTripTrades) * 100,
'total_pnl': rtDf['pnl'].sum(),
'average_pnl_per_trade': rtDf['pnl'].mean(),
'average_return_per_trade': rtDf['return_percent'].mean() * 100,
'best_trade': rtDf['pnl'].max(),
'worst_trade': rtDf['pnl'].min(),
'average_holding_period': rtDf['holding_period_days'].mean(),
'total_commissions_paid': tradeDf['commission'].sum(),
'profit_factor': abs(winningTrades['pnl'].sum() / losingTrades['pnl'].sum()) if len(losingTrades) > 0 else np.inf
}
# regime-specific performance
regimeStats = {}
for regime in rtDf['entry_regime'].unique():
regimeTrades = rtDf[rtDf['entry_regime'] == regime]
if len(regimeTrades) > 0:
regimeStats[regime] = {
'trade_count': len(regimeTrades),
'win_rate': (regimeTrades['pnl'] > 0).mean() * 100,
'average_pnl': regimeTrades['pnl'].mean(),
'average_return': regimeTrades['return_percent'].mean() * 100
}
stats['regime_breakdown'] = regimeStats
else:
stats = {
'total_round_trips': 0,
'message': 'no completed round-trip trades found'
}
return stats