-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbacktest_python.py
More file actions
65 lines (56 loc) · 2.11 KB
/
Copy pathbacktest_python.py
File metadata and controls
65 lines (56 loc) · 2.11 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
from backtesting import Backtest, Strategy
from backtesting.lib import crossover
from backtesting.test import SMA
import yfinance as yf
import pandas as pd
import datetime as dt
import openpyxl
class MySmaStrategy(Strategy):
def init(self):
price = self.data.Close
self.sma1 = self.I(SMA, price, 10)
self.sma2 = self.I(SMA, price, 20)
def next(self):
if crossover(self.sma1, self.sma2):
self.buy()
elif crossover(self.sma2, self.sma1):
self.sell()
# Fetch historical data using yfinance
start = dt.datetime(2020, 1, 1)
end = dt.datetime(2022, 1, 1)
data = yf.download('TSLA', start=start, end=end)
# Prepare data for backtesting
data = data[['Open', 'High', 'Low', 'Close', 'Adj Close', 'Volume']]
data.columns = ['Open', 'High', 'Low', 'Close', 'Adj Close', 'Volume']
# Run the backtest
bt = Backtest(data, MySmaStrategy, commission=0.002, exclusive_orders=True)
stats = bt.run()
# Extract main metrics
metrics = {
"Start": [stats['Start']],
"End": [stats['End']],
"Duration": [stats['Duration']],
"Equity Final [$]": [stats['Equity Final [$]']],
"Equity Peak [$]": [stats['Equity Peak [$]']],
"Return [%]": [stats['Return [%]']],
"Buy & Hold Return [%]": [stats['Buy & Hold Return [%]']],
"Max. Drawdown [%]": [stats['Max. Drawdown [%]']],
"Avg. Drawdown [%]": [stats['Avg. Drawdown [%]']],
"Max. Drawdown Duration": [stats['Max. Drawdown Duration']],
"Trades": [stats['# Trades']],
"Win Rate [%]": [stats['Win Rate [%]']],
"Best Trade [%]": [stats['Best Trade [%]']],
"Worst Trade [%]": [stats['Worst Trade [%]']],
"Avg. Trade [%]": [stats['Avg. Trade [%]']],
"Max. Trade Duration": [stats['Max. Trade Duration']],
"Avg. Trade Duration": [stats['Avg. Trade Duration']],
"Profit Factor": [stats['Profit Factor']],
"Expectancy [%]": [stats['Expectancy [%]']],
"Sharpe Ratio": [stats['Sharpe Ratio']],
"Sortino Ratio": [stats['Sortino Ratio']],
}
# Convert to DataFrame
metrics_df = pd.DataFrame(metrics)
# Save to Excel
metrics_df.to_excel("strategy_metrics.xlsx", index=False)
print(stats)