For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

# BBand Strategy

• Hello, I am excited to see that Backtrader now has a community section and I want to share a simple strategy with you all. This strategy uses Backtrader's BBand indicator and buys after the market dips into the lower band and sells on the moving average after the market hits the top band. This works great in sideways/bull markets. The idea is to buy during a low period and sell if the market dips below a moving average. Also note I am new to algotrading and programming in general so don't laugh to hard at this idea/strategy

I actually use this strategy in the Bitcoin world and have had great results trading it live in the past 3 months. I use Tradewave to run it live with a small porting of my holdings.

You can use talib to create a BBands in Tradewave or you can use a simple math formula to recreate the bbands used in Backtrader....

``````std = data(interval=INTERVAL).btc_usd.std(SETTING1)

ma = data(interval=INTERVAL).btc_usd.ma(SETTING1)

upperband = ma + (Decimal(2) * std)

lowerband = ma - (Decimal(2) * std)
``````

Note, I changed a lot of the code to work better such as not trading in low volatile markets, using a stop when the market turns and other things you can use to help make better, safer decisions. :wink:

So here is the code you can copy and paste....

``````import datetime  # For datetime objects
import os.path  # To manage paths
import sys  # To find out the script name (in argv[0])

# Create a Stratey
class TestStrategy(bt.Strategy):
params = (('BBandsperiod', 20),)

def log(self, txt, dt=None):
''' Logging function fot this strategy'''
dt = dt or self.datas[0].datetime.date(0)
print('%s, %s' % (dt.isoformat(), txt))

def __init__(self):
# Keep a reference to the "close" line in the data[0] dataseries
self.dataclose = self.datas[0].close

# To keep track of pending orders and buy price/commission
self.order = None
self.redline = None
self.blueline = None

self.bband = bt.indicators.BBands(self.datas[0], period=self.params.BBandsperiod)

def notify_order(self, order):
if order.status in [order.Submitted, order.Accepted]:
# Buy/Sell order submitted/accepted to/by broker - Nothing to do
return

# Check if an order has been completed
# Attention: broker could reject order if not enougth cash
if order.status in [order.Completed, order.Canceled, order.Margin]:
self.log(
'BUY EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' %
(order.executed.price,
order.executed.value,
order.executed.comm))

else:  # Sell
self.log('SELL EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' %
(order.executed.price,
order.executed.value,
order.executed.comm))

self.bar_executed = len(self)

# Write down: no pending order
self.order = None

return

self.log('OPERATION PROFIT, GROSS %.2f, NET %.2f' %

def next(self):
# Simply log the closing price of the series from the reference
self.log('Close, %.2f' % self.dataclose[0])

# Check if an order is pending ... if yes, we cannot send a 2nd one
if self.order:
return

if self.dataclose < self.bband.lines.bot and not self.position:
self.redline = True

if self.dataclose > self.bband.lines.top and self.position:
self.blueline = True

if self.dataclose > self.bband.lines.mid and not self.position and self.redline:
# Keep track of the created order to avoid a 2nd order

if self.dataclose > self.bband.lines.top and not self.position:
# Keep track of the created order to avoid a 2nd order

if self.dataclose < self.bband.lines.mid and self.position and self.blueline:
# SELL, SELL, SELL!!! (with all possible default parameters)
self.log('SELL CREATE, %.2f' % self.dataclose[0])
self.blueline = False
self.redline = False
# Keep track of the created order to avoid a 2nd order
self.order = self.sell()

if __name__ == '__main__':
# Create a cerebro entity
cerebro = bt.Cerebro()

# Datas are in a subfolder of the samples. Need to find where the script is
# because it could have been called from anywhere
modpath = os.path.dirname(os.path.abspath(sys.argv[0]))
datapath = os.path.join(modpath, 'TSLA-USD.csv')

# Create a Data Feed
data = bt.feeds.GenericCSVData(
dataname=datapath,
# Do not pass values before this date
fromdate=datetime.datetime(2008, 4, 4),
# Do not pass values before this date
todate=datetime.datetime(2016, 12, 2),

nullvalue=0.0,

dtformat=('%m/%d/%Y'),

datetime=0,
high=2,
low=3,
open=1,
close=4,
volume=5,
openinterest=-1)

# Add the Data Feed to Cerebro

# Set our desired cash start
cerebro.broker.setcash(10000.0)

# Add a FixedSize sizer according to the stake

# Set the commission
cerebro.broker.setcommission(commission=0.002)

# Print out the starting conditions
print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())

# Run over everything
cerebro.run()

# Print out the final result
print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())

# Plot the result
cerebro.plot()
``````

Also, you can just use this script and paste it into your browsers to grab quick market cvs data from yahoo.

``````http://ichart.finance.yahoo.com/table.csv?s=ENTERSTOCKHERE
``````

I will be posting more simple examples and hope others do as well. I found backtesting ideas with Backtrader can be quick and when compared to using online backtesters and more powerful. Using the right parameters without oversampling is still a difficult thing to master. Hope to keep learning and trying new things!

• Thx for sharing. Really impressive.

• Thanks for the post. You used period of 20 for Bband indicator. How did you come to this value?

• @ab_trader I just wanted to take the past 30 days of data and use that to find the average low and highs for the BBand and trade on 1 day ticks. When backtesting, 20 days of data seemed to produce better results. I also found this to be true on Quantopian when I added the strategy there and tried many different stocks on 1 day ticks with 20 days of data.

• Thanks for sharing,
I tried executing this, the Buy orders are getting created but not executed.
The log says
2016-09-08, Close, 8952.50
2016-09-09, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00
2016-09-09, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00
2016-09-09, Close, 8866.70

Any idea why this might be happening

• The code from @tim3lord will print `EXECUTED` even when the order has been canceled (or rejected because it cannot meet cash requirements). See the following lines

``````        if order.status in [order.Completed, order.Canceled, order.Margin]:
self.log(
'BUY EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' %
(order.executed.price,
order.executed.value,
order.executed.comm))
``````

Of course this is purely cosmetic.

The code also contains

``````# Set our desired cash start
cerebro.broker.setcash(10000.0)

# Add a FixedSize sizer according to the stake
``````

Your asset is in the `8000`s range and for a `stocklike` behavior you would need at least `5 * 8000 = 40,000` monetary units.

The orders are obviously being rejected due to lack of enough cash.

• Awesome, I changed the setcash parameter and it works now, in fact showing some profits by changing the BB parameter.
I will be sharing something soon, still trying to get my strategy coded in backtrader framework (as I am new to python)

• Thanks for sharing but I'm confused with a following.

``````if self.dataclose > self.bband.lines.mid and not self.position and self.redline
``````

If close is bigger than mid (higher visually) and at the same time lower than the bottom we are buying.
How is that possible? Please, clarify how it works, looks like I didn't get the concept of the indicator.

• `redline` is a flag which is set when a condition is met. But it doesn't mean that when is later checked together with other conditions, that the original condition is still true. But the flag is set.

• Thanks, I see. I didn't get the strategy right.

• @tim3lord

Hello, I am trying to run the code but gettin an error:

File "C:/Users/Sam/PycharmProjects/Test/.ipynb_checkpoints/Backtrader BB Strategy.py", line 108
cerebro = bt.Cerebro()
^
IndentationError: expected an indented block

I do not see where the indent is off as it looks lined up to me. Can you take a quick look if you have the chance please? Thanks.

``````import datetime  # For datetime objects
import os.path  # To manage paths
import sys  # To find out the script name (in argv[0])

# Also, you can just use this script and paste it into your browsers to grab quick market cvs data from yahoo.
# http://ichart.finance.yahoo.com/table.csv?s=ENTERSTOCKHERE

# Create a Stratey
class TestStrategy(bt.Strategy):
params = (('BBandsperiod', 20),)

def log(self, txt, dt=None):
''' Logging function fot this strategy'''
dt = dt or self.datas[0].datetime.date(0)
print('%s, %s' % (dt.isoformat(), txt))

def __init__(self):
# Keep a reference to the "close" line in the data[0] dataseries
self.dataclose = self.datas[0].close

# To keep track of pending orders and buy price/commission
self.order = None
self.redline = None
self.blueline = None

self.bband = bt.indicators.BBands(self.datas[0], period=self.params.BBandsperiod)

def notify_order(self, order):
if order.status in [order.Submitted, order.Accepted]:
# Buy/Sell order submitted/accepted to/by broker - Nothing to do
return

# Check if an order has been completed
# Attention: broker could reject order if not enougth cash
if order.status in [order.Completed, order.Canceled, order.Margin]:
self.log(
'BUY EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' %
(order.executed.price,
order.executed.value,
order.executed.comm))

else:  # Sell
self.log('SELL EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' %
(order.executed.price,
order.executed.value,
order.executed.comm))

self.bar_executed = len(self)

# Write down: no pending order
self.order = None

return

self.log('OPERATION PROFIT, GROSS %.2f, NET %.2f' %

def next(self):
# Simply log the closing price of the series from the reference
self.log('Close, %.2f' % self.dataclose[0])

# Check if an order is pending ... if yes, we cannot send a 2nd one
if self.order:
return

if self.dataclose < self.bband.lines.bot and not self.position:
self.redline = True

if self.dataclose > self.bband.lines.top and self.position:
self.blueline = True

if self.dataclose > self.bband.lines.mid and not self.position and self.redline:
# Keep track of the created order to avoid a 2nd order

if self.dataclose > self.bband.lines.top and not self.position:
# Keep track of the created order to avoid a 2nd order

if self.dataclose < self.bband.lines.mid and self.position and self.blueline:
# SELL, SELL, SELL!!! (with all possible default parameters)
self.log('SELL CREATE, %.2f' % self.dataclose[0])
self.blueline = False
self.redline = False
# Keep track of the created order to avoid a 2nd order
self.order = self.sell()

if __name__ == '__main__':
# Create a cerebro entity
cerebro = bt.Cerebro()

# data
data = btfeeds.GenericCSVData(
dataname='C:\\Users\\Sam\\PycharmProjects\\Test\\.ipynb_checkpoints\\orcl-1995-2014.csv',

fromdate=datetime.datetime.strptime("1/1/2000", "%m/%d/%Y"),
todate=datetime.datetime.strptime("12/31/2000", "%m/%d/%Y"),

nullvalue=0.0,  # missing values to be replaced with 0

dtformat=('%m/%d/%Y'),

datetime=0,
time=-1,
open=1,
high=2,
low=3,
close=4,
volume=6,
openinterest=-1,

)

# Add the Data Feed to Cerebro

# Set our desired cash start
cerebro.broker.setcash(10000.0)

# Add a FixedSize sizer according to the stake

# Set the commission
cerebro.broker.setcommission(commission=0.002)

# Print out the starting conditions
print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())

# Run over everything
cerebro.run()

# Print out the final result
print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())

# Plot the result
cerebro.plot()
``````

• @tim3lord

Lines where it is occuring:

``````if __name__ == '__main__':
# Create a cerebro entity
cerebro = bt.Cerebro()
``````

• @tim3lord

Nevermind silly error! Figured it out thanks

• @tim3lord

If the close is above the top of the bollinger bands and we are not in a trade, why would we be buying also? If we are looking for mean reversion, wouldnt we not buy in this instance. This is from the 5th if statement shown below:

`````` if self.dataclose > self.bband.lines.top and not self.position: