How to Plot Renko
-
Hi everyone,
I have just started exploring Backtrader and I already got a question. How could I use Renko on strategy and to plot? I used the strategy from "Quickstart" as a starter but got an error (
'Plot_OldSync' object has no attribute 'mpyplot'
). Please let me know what I am doing wrong and how I could improve. Thanks!import backtrader as bt from datetime import datetime class quickStrategy(bt.Strategy): def __init__(self): self.dataclose = self.datas[0].close def next(self): if self.dataclose[0] < self.dataclose[-1]: if self.dataclose[-1] < self.dataclose[-2]: self.buy() # Variable for the starting cash startcash = 10000.0 # Create an instance of cerebro cerebro = bt.Cerebro() # Add the strategy cerebro.addstrategy(quickStrategy) # Get Apple data from Yahoo Finance data = bt.feeds.YahooFinanceData( dataname = 'AAPL', fromdate = datetime(2016,1,1), todate = datetime(2017,1,1), buffered = True ) # Convert to Renko data.addfilter(bt.filters.Renko, size=20, align=10) #Add the data to Cerebro cerebro.adddata(data) # Set desired cash start cerebro.broker.setcash(startcash) # Run over everything cerebro.run() #Plot cerebro.plot()
-
@vensaiten said in How to Plot Renko:
# Get Apple data from Yahoo Finance data = bt.feeds.YahooFinanceData( dataname = 'AAPL', fromdate = datetime(2016,1,1), todate = datetime(2017,1,1), buffered = True )
If I had to bet that is returning no usable data. It is well-known that Yahoo has crippled the API (shut down the old and made the new one unusable)
@vensaiten said in How to Plot Renko:
def next(self): if self.dataclose[0] < self.dataclose[-1]: if self.dataclose[-1] < self.dataclose[-2]: self.buy()
You could try to print the closing values there to check what's happening.
Solution: use a different data file (a csv file is good enough)
-
This time, as been told, I used the csv file as data feed and also used the logging strategy from "Quickstart" to I find where the problem is.
import backtrader as bt from datetime import datetime # For datetime objects import os.path # To manage paths class logging(bt.Strategy): def log(self, txt, dt=None): ''' Logging function for 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 def next(self): # Simply log the closing price of the series from the reference self.log('Close, %.2f' % self.dataclose[0]) # Variable for the starting cash startcash = 10000 # Create an instance of cerebro cerebro = bt.Cerebro() # Add the strategy cerebro.addstrategy(logging) # Path to Data Feed File modpath = os.path.dirname(os.path.abspath('__file__')) datapath = os.path.join(modpath, 'USDJPY_D_BID_2016.12.31-2018.01.12.csv') # Data Feed data = bt.feeds.GenericCSVData( dataname=datapath, timeframe = bt.TimeFrame.Days, fromdate=datetime(2017, 1, 1), todate=datetime(2017, 12, 31), nullvalue=0.0, dtformat=('%d.%m.%Y %H:%M:%S.%f'), tmformat=('%d.%m.%Y %H:%M:%S.%f'), datetime=0, high=2, low=3, open=1, close=4, volume=5, openinterest=-1 ) #Add the data to Cerebro cerebro.adddata(data) # Set our desired cash start cerebro.broker.setcash(startcash) # 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())
Above code gave me expected logs of close price:
Starting Portfolio Value: 10000.00 2017-01-01, Close, 117.51 2017-01-02, Close, 117.74 . . . 2017-12-29, Close, 112.66 2017-12-30, Close, 112.66 Final Portfolio Value: 10000.00
But when I inserted the line of
data.addfilter(bt.filters.Renko, size=20, align=1)
beforecerebro.adddata(data)
, it showed just the portfolio values and no log:Starting Portfolio Value: 10000.00 Final Portfolio Value: 10000.00
So I am guessing I am not adding the Renko filter properly. How can I fix this?
-
Just by looking at the values it would seem that your data doesn't move 20 points altogether in an entire year. You need to fine tune the Renko filter to your data set. There is no one size fits all for it.
-
Thanks for the insight! That did the job. Happy coding :)