Skip to content

Commit 1c6fd18

Browse files
committed
Meta Strategy
1 parent f52b88b commit 1c6fd18

File tree

5 files changed

+45
-12
lines changed

5 files changed

+45
-12
lines changed

images/overview.png

117 KB
Loading

images/overview2.png

96.5 KB
Loading

strategies/ichimokuStrat1.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
import sys
2020
import backtrader as bt
2121

22+
import metaStrategy as mt
23+
2224
# Create a subclass of Strategy to define the indicators and logic
23-
class ichimokuStrat1(bt.Strategy):
25+
class ichimokuStrat1(mt.MetaStrategy):
2426

2527
params = (
2628
('atrperiod', 14), # ATR Period (standard)
@@ -41,12 +43,10 @@ def notify_order(self, order):
4143
if not order.alive():
4244
self.order = None # indicate no order is pending
4345

44-
def __init__(self, parameters = None):
46+
def __init__(self, *argv):
4547

46-
# Set UI modified parameters
47-
if parameters != None:
48-
for parameterName, parameterValue in parameters.items():
49-
setattr(self.params, parameterName, parameterValue)
48+
# used to modify parameters
49+
super().__init__(argv[0])
5050

5151
# Ichi indicator
5252
self.ichi = bt.indicators.Ichimoku(self.datas[0],

strategies/metaStrategy.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
###############################################################################
2+
#
3+
# Copyright (C) 2021 - Skinok
4+
#
5+
# This program is free software: you can redistribute it and/or modify
6+
# it under the terms of the GNU General Public License as published by
7+
# the Free Software Foundation, either version 3 of the License, or
8+
# (at your option) any later version.
9+
#
10+
# This program is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU General Public License
16+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
#
18+
###############################################################################
19+
import backtrader as bt
20+
21+
class MetaStrategy(bt.Strategy):
22+
23+
def __init__(self, parameters = None):
24+
25+
# Set UI modified parameters
26+
if parameters != None:
27+
for parameterName, parameterValue in parameters.items():
28+
setattr(self.params, parameterName, parameterValue)
29+
30+
pass

strategies/sma_crossover.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@
2525
import backtrader as bt
2626
import backtrader.indicators as btind
2727

28+
import metaStrategy as mt
2829

29-
class sma_crossover(bt.Strategy):
30+
class sma_crossover(mt.MetaStrategy):
3031
'''This is a long-only strategy which operates on a moving average cross
3132
3233
Note:
@@ -55,15 +56,17 @@ class sma_crossover(bt.Strategy):
5556
('fast', 15),
5657
# period for the slow moving average
5758
('slow', 30),
58-
# moving average to use
59-
('_movav', btind.MovAv.SMA),
6059
# Trade size
6160
('tradeSize', 2000)
6261
)
6362

64-
def __init__(self):
65-
sma_fast = self.p._movav(period=self.p.fast)
66-
sma_slow = self.p._movav(period=self.p.slow)
63+
def __init__(self, *argv):
64+
65+
# used to modify parameters
66+
super().__init__(argv[0])
67+
68+
sma_fast = btind.MovAv.SMA(period=self.p.fast)
69+
sma_slow = btind.MovAv.SMA(period=self.p.slow)
6770

6871
self.buysig = btind.CrossOver(sma_fast, sma_slow)
6972

0 commit comments

Comments
 (0)