Problems on using renko bricks with forex, Oanda EUR_USD
-
Hello!
I want to try to use renko bricks in a strategy, and also test it, but I see that the renko plot is not working fine.
One brick up and one brick down.
I am trying to use it with EUR_USD. I got the data from Oanda and stored it into a CSV.
I think the problem is because the price contains 5 decimals.
Any help will be really appreciated.My code looks like this:
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, %.4f' % self.dataclose[0]) class OandaCSVData(bt.feeds.GenericCSVData): params = ( ('nullvalue', 0.0), ('dtformat', '%Y-%m-%dT%H:%M:%S'), ('datetime', 0), ('time', -1), ('open', 1), ('high', 2), ('low', 3), ('close', 4), ('volume', 5), ('openinterest', -1), ) #Variable for our starting cash startcash = 10000 #Create an instance of cerebro cerebro = bt.Cerebro() #Add our strategy cerebro.broker.setcash(startcash) #create our data list datalist = [ ('EUR_USD.M1.csv', 'CADCHF'), #[0] = Data file, [1] = Data name ] data = OandaCSVData(dataname='EUR_USD.M1.csv', fromdate=datetime(2017, 7, 1), todate=datetime(2017, 7, 5), timeframe=bt.TimeFrame.Minutes) data.addfilter(bt.filters.Renko, **dict(size=0.0008)) cerebro.adddata(data, name='EUR_USD') # Set our desired cash start cerebro.addstrategy(logging) # Run over everything cerebro.run(**dict(stdstats=False)) #Get final portfolio Value portvalue = cerebro.broker.getvalue() pnl = portvalue - startcash #Print out the final result print('Final Portfolio Value: ${}'.format(portvalue)) print('P/L: ${}'.format(pnl)) #Finally plot the end results cerebro.plot(style='candle', barup='green', bardown='red')
My logger prints this:
... 2017-07-04, Close, 1.1352 2017-07-04, Close, 1.1360 2017-07-04, Close, 1.1344 2017-07-04, Close, 1.1360 2017-07-04, Close, 1.1344 2017-07-04, Close, 1.1360 2017-07-04, Close, 1.1344 2017-07-04, Close, 1.1336 2017-07-04, Close, 1.1352 2017-07-04, Close, 1.1360 2017-07-04, Close, 1.1344 2017-07-04, Close, 1.1360 2017-07-04, Close, 1.1344 2017-07-04, Close, 1.1360 ...
And it plots like this:
-
There was a very similar question a couple of days ago
A quick glance would seem to indicate that your problem is similar but in the other direction. You have
size=0.0008
and the price moves in that range, triggering the up/downs (you have instances of more than one in the same direction)... 2017-07-04, Close, 1.1352 2017-07-04, Close, 1.1360 2017-07-04, Close, 1.1344 2017-07-04, Close, 1.1360 2017-07-04, Close, 1.1344 2017-07-04, Close, 1.1360 2017-07-04, Close, 1.1344 ...
1.1360 - 1.1352 = 0.0008
which is the size and the rest are above thesize
(in absolute value), each time in an opposite directions.Play with a different size and you will see different results.