-
Notifications
You must be signed in to change notification settings - Fork 5
/
bh.py
56 lines (43 loc) · 1.75 KB
/
bh.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
"""Module for Buy and Hold strategy implementation.
The author is Zmicier Gotowka
Distributed under Fcore License 1.1 (see license.md)
"""
from backtest.base import BackTest
class BuyAndHold(BackTest):
"""
Buy and hold strategy implementation.
"""
def skip_criteria(self, index):
"""
Abstract method placeholder. Check if this cycle should be skipped. Not relevant to B&H strategy.
"""
return False
def do_tech_calculation(self, ex):
"""
Abstract method placeholder. B&H strategy does not involve any technical calculation.
"""
def do_calculation(self):
"""
Main strategy calculation method.
"""
rows = self.get_main_data().get_rows()
######################################
# Perform the global calculation setup
######################################
self.setup()
# Iterate through all rows and calculate the required values
for row in rows:
####################################################################################################
# Setup cycle calculations if current cycle shouldn't be skipped (because of offset or lack of data)
####################################################################################################
if self.do_cycle(row) == False:
continue
########################
# Open positions
########################
# Open a long position if we have enough cash
self.exec().open_long_max()
##############################
# Teardown the cycle
##############################
self.tear_down()