Backtrader Example of Moving-Average Entries with OCO Limit Orders
Summary
This Backtrader example combines a moving-average crossover entry signal with three buy limit orders placed at progressively lower prices after an upward crossover. The orders can be linked using one-cancels-other behavior, so execution of a linked order cancels its siblings; parameters let the user change order relationships, validity periods, and whether target-size order methods are used. The strategy tracks outstanding order references, waits while orders remain pending, records when an order completes, and closes a position after a configurable holding interval.
The script includes configurable moving-average periods, limit offsets, order validity, data dates, broker and sizing settings, and optional plotting. It is an implementation example, not a performance study: it provides no results or risk analysis. Its behavior depends on Backtrader’s broker and order-simulation settings, and the example’s parameter choices and price calculations should be reviewed before interpreting any backtest as evidence of a viable strategy.
Key ideas
- An upward moving-average crossover triggers a set of buy limit orders at successively lower prices.
- One-cancels-other links can cancel related orders after one is executed.
- The strategy avoids placing new orders while prior orders remain active and closes after a set holding period.
- Broker simulation, order validity, and the code’s price offsets affect backtest interpretation.
Tags
Full text
# oco.py
```py
#!/usr/bin/env python
# -*- coding: utf-8; py-indent-offset:4 -*-
###############################################################################
#
# Copyright (C) 2015-2023 Daniel Rodriguez
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
###############################################################################
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import argparse
import datetime
import backtrader as bt
class St(bt.Strategy):
params = dict(
ma=bt.ind.SMA,
p1=5,
p2=15,
limit=0.005,
limdays=3,
limdays2=1000,
hold=10,
usetarget=False, # use order_target_size
switchp1p2=False, # switch prices of order1 and order2
oco1oco2=False, # False - use order1 as oco for order3, else order2
do_oco=True, # use oco or not
)
def notify_order(self, order):
print('{}: Order ref: {} / Type {} / Status {}'.format(
self.data.datetime.date(0),
order.ref, 'Buy' * order.isbuy() or 'Sell',
order.getstatusname()))
if order.status == order.Completed:
self.holdstart = len(self)
if not order.alive() and order.ref in self.orefs:
self.orefs.remove(order.ref)
def __init__(self):
ma1, ma2 = self.p.ma(period=self.p.p1), self.p.ma(period=self.p.p2)
self.cross = bt.ind.CrossOver(ma1, ma2)
self.orefs = list()
if self.p.usetarget:
print('-' * 5, 'Using order_target_size')
self._dobuy = self.order_target_size
self._doclose = self.order_target_size
else:
self._dobuy = self.buy
self._doclose = self.close
def next(self):
if self.orefs:
return # pending orders do nothing
if not self.position:
if self.cross > 0.0: # crossing up
p1 = self.data.close[0] * (1.0 - self.p.limit)
p2 = self.data.close[0] * (1.0 - 2 * 2 * self.p.limit)
p3 = self.data.close[0] * (1.0 - 3 * 3 * self.p.limit)
valid1 = datetime.timedelta(self.p.limdays)
valid2 = valid3 = datetime.timedelta(self.p.limdays2)
if self.p.switchp1p2:
p1, p2 = p2, p1
valid1, valid2 = valid2, valid1
print('valid1 is:', valid1)
kargs = dict(exectype=bt.Order.Limit)
kargs[('target' * self.p.usetarget) or 'size'] = 1
o1 = self._dobuy(price=p1, valid=valid1, **kargs)
print('{}: Oref {} / Buy at {}'.format(
self.datetime.date(), o1.ref, p1))
oco2 = o1 if self.p.do_oco else None
o2 = self._dobuy(price=p2, valid=valid2, oco=oco2, **kargs)
print('{}: Oref {} / Buy at {}'.format(
self.datetime.date(), o2.ref, p2))
if self.p.do_oco:
oco3 = o1 if not self.p.oco1oco2 else oco2
else:
oco3 = None
o3 = self._dobuy(price=p3, valid=valid3, oco=oco3, **kargs)
print('{}: Oref {} / Buy at {}'.format(
self.datetime.date(), o3.ref, p3))
self.orefs = [o1.ref, o2.ref, o3.ref]
else: # in the market
if (len(self) - self.holdstart) >= self.p.hold:
self._doclose()
def runstrat(args=None):
args = parse_args(args)
cerebro = bt.Cerebro()
# Data feed kwargs
kwargs = dict()
# Parse from/to-date
dtfmt, tmfmt = '%Y-%m-%d', 'T%H:%M:%S'
for a, d in ((getattr(args, x), x) for x in ['fromdate', 'todate']):
if a:
strpfmt = dtfmt + tmfmt * ('T' in a)
kwargs[d] = datetime.datetime.strptime(a, strpfmt)
# Data feed
data0 = bt.feeds.BacktraderCSVData(dataname=args.data0, **kwargs)
cerebro.adddata(data0)
# Broker
cerebro.broker = bt.brokers.BackBroker(**eval('dict(' + args.broker + ')'))
# Sizer
cerebro.addsizer(bt.sizers.FixedSize, **eval('dict(' + args.sizer + ')'))
# Strategy
cerebro.addstrategy(St, **eval('dict(' + args.strat + ')'))
# Execute
cerebro.run(**eval('dict(' + args.cerebro + ')'))
if args.plot: # Plot if requested to
cerebro.plot(**eval('dict(' + args.plot + ')'))
def parse_args(pargs=None):
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description=(
'Sample Skeleton'
)
)
parser.add_argument('--data0', default='../../datas/2005-2006-day-001.txt',
required=False, help='Data to read in')
# Defaults for dates
parser.add_argument('--fromdate', required=False, default='',
help='Date[time] in YYYY-MM-DD[THH:MM:SS] format')
parser.add_argument('--todate', required=False, default='',
help='Date[time] in YYYY-MM-DD[THH:MM:SS] format')
parser.add_argument('--cerebro', required=False, default='',
metavar='kwargs', help='kwargs in key=value format')
parser.add_argument('--broker', required=False, default='',
metavar='kwargs', help='kwargs in key=value format')
parser.add_argument('--sizer', required=False, default='',
metavar='kwargs', help='kwargs in key=value format')
parser.add_argument('--strat', required=False, default='',
metavar='kwargs', help='kwargs in key=value format')
parser.add_argument('--plot', required=False, default='',
nargs='?', const='{}',
metavar='kwargs', help='kwargs in key=value format')
return parser.parse_args(pargs)
if __name__ == '__main__':
runstrat()
```Shown in full with attribution under the source's licence. Licence: GPL-3.0
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.