Hi! I would like to plot an indicator on hourly data but the indicator is calculated based on daily data.
I tried using the following code but I am unable to see the indicator
self.indicatorname.plotinfo.plotmaster =True
self.indicatorname.plotinfo.plotylimited = False
This is the full code.
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import os.path
import argparse
import datetime
import math
import backtrader as bt
import backtrader.feeds as btfeeds
import backtrader.plot as plt
import backtrader.indicators as btinds
class Donchian(btinds.PeriodN):
lines = ('maxi','mini',)
params = (('period',6),)
def __init__(self):
self.lines.maxi = btinds.Highest(self.data.high, period=self.p.period)
self.lines.mini = btinds.Lowest(self.data.low, period=self.p.period)
class St(bt.Strategy):
def log(self, txt, dt=None):
''' Logging function fot this strategy'''
dt = dt or self.data0.datetime.date(0)
dt1 = self.data0.datetime.time(0)
print('%s,%s, %s' % (dt.isoformat(),dt1.isoformat(), txt))
def __init__(self):
self.channel = Donchian(self.data1)
self.channel.plotinfo.subplot=False
self.channel.plotinfo.plotmaster = True
self.buysignal = 0
self.sellsignal = 0
self.dayhigh = 0
self.daylow = 0
def notify_order(self, order):
if order.status in [order.Submitted, order.Accepted]:
return
if order.status in [order.Completed]:
if order.isbuy():
self.log(
'BUY EXECUTED, Price: %.5f, Cost: %.5f' %
(order.executed.price,
order.executed.value))
self.buyprice = order.executed.price
else: # Sell
self.log('SELL EXECUTED, Price: %.5f, Cost: %.5f' %
(order.executed.price,
order.executed.value))
self.bar_executed = len(self)
elif order.status in [order.Canceled, order.Margin, order.Rejected]:
self.log('Order Canceled/Margin/Rejected')
self.order = None
def notify_trade(self, trade):
if not trade.isclosed:
return
self.log('OPERATION PROFIT, GROSS %.5f, NET %.5f' %
(trade.pnl, trade.pnlcomm))
def next(self):
txt = ','.join(
['%04d' % len(self.data0),
'%04d' % len(self.data1),
self.data0.datetime.date(0).isoformat(),
self.data0.datetime.time(0).isoformat(),
self.data1.datetime.date(0).isoformat(),
'Open : %.5f' % self.data0.open[0],
'Close : %.5f' % self.data0.close[0],
'Buysignal = %d' % self.buysignal,
'Sellsignal = %d' % self.sellsignal,
'Current Low = %.5f' % self.daylow,
'Current High = %.5f' % self.dayhigh])
print(txt)
if self.buysignal >0 :
if self.data0.datetime.time(0)==datetime.time(0,0,0):
self.buysignal = 0
self.sellsignal = 0
if self.sellsignal >0 :
if self.data0.datetime.time(0)==datetime.time(0,0,0):
self.buysignal = 0
self.sellsignal = 0
if self.buysignal == 0 :
if self.data0.close[0] < self.channel.lines.mini[0]:
self.buysignal = 1
self.daylow = self.channel.lines.mini[0]
print("-----------breaking low----------------")
print(self.data0.datetime.time(0),"Current Low : %.5f" % self.channel.mini[0])
elif self.buysignal == 1:
if self.data0.close[0] > self.daylow:
print('Current Low : %5f' % self.daylow)
self.log('BUY CREATE, %.5f' % self.data0.close[0])
self.order=self.buy(data=self.data0)
self.buysignal = 0
if self.sellsignal==0:
if self.data0.close[0] > self.channel.lines.maxi[0]:
self.sellsignal=1
self.dayhigh = self.channel.lines.maxi[0]
print("------------breaking high----------------")
print(self.data0.datetime.time(0),"Current High : %.5f" % self.channel.lines.maxi[0])
elif self.sellsignal ==1:
if self.data0.close[0] < self.dayhigh:
print('Current High : %5f' % self.dayhigh)
self.log('SELL CREATE, %.5f' % self.data0.close[0])
self.order = self.sell(data=self.data0)
self.sellsignal=0
def runstrat():
args = parse_args()
cerebro = bt.Cerebro()
modpath = 'd:\\I - TradersGPS\\'
datapath0 = os.path.join(modpath, 'GBPUSD_H1_UTC+2_00.csv')
datapath1 = os.path.join(modpath, 'GBPUSD_D1_UTC+2_00.csv')
data0 = btfeeds.GenericCSVData(dataname=datapath0,
timeframe=bt.TimeFrame.Minutes,
compression = 60,
fromdate = datetime.datetime(2016,10,21),
todate=datetime.datetime(2016,11,15),
nullvalue=0.0,
dtformat=('%Y.%m.%d'),
tmformat=('%H:%M'),
datetime=0,
time=1,
open=2,
high=3,
low=4,
close=5,
volume=6,
openinterest=-1)
data1 = btfeeds.GenericCSVData(dataname=datapath1,
timeframe = bt.TimeFrame.Days,
fromdate =datetime.datetime(2016,10,21),
todate= datetime.datetime(2016,11,15),
nullvalue=0.0,
dtformat=('%Y.%m.%d'),
tmformat=('%H:%M'),
datetime=0,
time=1,
open=2,
high=3,
low=4,
close=5,
volume=6,
openinterest=-1)
cerebro.adddata(data0,name ="Hourly Data")
cerebro.adddata(data1,name="Daily Data")
cerebro.addstrategy(St)
cerebro.broker.setcash(100000.0)
print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
cerebro.run(stdstats=False)
print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
cerebro.plot()