Skip to content

Commit 7e7b523

Browse files
committed
backtest doc
1 parent b31aad1 commit 7e7b523

1 file changed

Lines changed: 24 additions & 1 deletion

File tree

lib/backtest.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,26 @@
11
class BackTest(object):
2+
""" Callable object running back tests for a strategy over a stock
3+
4+
>>> backtest = BackTest()
5+
6+
If goog is a Stock and bollinger a strategy (cf. strategy/__init__.py)
7+
8+
>>> backtest(goog, bollinger)
9+
BackTest(trades=[99], position=short, gross=1253.63, net=662.1, passive=491.19)
10+
11+
Current position is short, gross PNL is 1253.63, net PNL taking into account
12+
the closing of the position and the trading costs if applicable is 662.1.
13+
14+
A passive strategy (buy on day 1 sell on last day) would have returned 491.19
15+
16+
Trading costs are 0 by default. To change that set the cost attribute of
17+
the backtest object to a function taking a trade as an argument and returning
18+
the cost.
19+
20+
>>> backtest.cost = lambda trade: 0.5 * trade / 100
21+
>>> backtest
22+
BackTest(trades=[99], position=short, gross=1253.63, net=435.78005, passive=491.19)
23+
"""
224

325
sell = {'long': None, None: 'short'}
426
buy = {None: 'long', 'short': None}
@@ -26,6 +48,7 @@ def __repr__(self):
2648

2749
@property
2850
def trade_cost(self):
51+
""" return the cost trading cost over the backtesting period """
2952
return sum(self.cost(abs(trade)) for trade in self.trades)
3053

3154
@property
@@ -45,5 +68,5 @@ def net(self):
4568

4669
@property
4770
def passive(self):
71+
""" buy on day 1 sell on last day """
4872
return self.last.close - self.first.close
49-

0 commit comments

Comments
 (0)