-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbacktesting.py
93 lines (84 loc) · 3.26 KB
/
backtesting.py
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
#
# Event-Based Backtesting
# --Base Class (1)
#
# (c) Dr. Yves J. Hilpisch
# Artificial Intelligence in Finance
#
class BacktestingBase:
def __init__(self, env, model, amount, ptc, ftc, verbose=False):
self.env = env
self.model = model
self.initial_amount = amount
self.current_balance = amount
self.ptc = ptc
self.ftc = ftc
self.verbose = verbose
self.units = 0
self.trades = 0
def get_date_price(self, bar):
''' Returns date and price for a given bar.
'''
date = str(self.env.data.index[bar])[:10]
price = self.env.data[self.env.symbol].iloc[bar]
return date, price
def print_balance(self, bar):
''' Prints the current cash balance for a given bar.
'''
date, price = self.get_date_price(bar)
print(f'{date} | current balance = {self.current_balance:.2f}')
def calculate_net_wealth(self, price):
return self.current_balance + self.units * price
def print_net_wealth(self, bar):
''' Prints the net wealth for a given bar
(cash + position).
'''
date, price = self.get_date_price(bar)
net_wealth = self.calculate_net_wealth(price)
print(f'{date} | net wealth = {net_wealth:.2f}')
def place_buy_order(self, bar, amount=None, units=None):
''' Places a buy order for a given bar and for
a given amount or number of units.
'''
date, price = self.get_date_price(bar)
if units is None:
units = int(amount / price)
# units = amount / price # alternative handling
self.current_balance -= (1 + self.ptc) * \
units * price + self.ftc
self.units += units
self.trades += 1
if self.verbose:
print(f'{date} | buy {units} units for {price:.4f}')
self.print_balance(bar)
def place_sell_order(self, bar, amount=None, units=None):
''' Places a sell order for a given bar and for
a given amount or number of units.
'''
date, price = self.get_date_price(bar)
if units is None:
units = int(amount / price)
# units = amount / price # altermative handling
self.current_balance += (1 - self.ptc) * \
units * price - self.ftc
self.units -= units
self.trades += 1
if self.verbose:
print(f'{date} | sell {units} units for {price:.4f}')
self.print_balance(bar)
def close_out(self, bar):
''' Closes out any open position at a given bar.
'''
date, price = self.get_date_price(bar)
print(50 * '=')
print(f'{date} | *** CLOSING OUT ***')
if self.units < 0:
self.place_buy_order(bar, units=-self.units)
else:
self.place_sell_order(bar, units=self.units)
if not self.verbose:
print(f'{date} | current balance = {self.current_balance:.2f}')
perf = (self.current_balance / self.initial_amount - 1) * 100
print(f'{date} | net performance [%] = {perf:.4f}')
print(f'{date} | number of trades [#] = {self.trades}')
print(50 * '=')