Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    Resample data and indicator

    Indicators/Strategies/Analyzers
    1
    1
    72
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • G
      Gerry last edited by

      I am currently resampling data, and then using that in a simple moving average indicato. The output then is a second chart with the resampled data and moving average, however I would like to have the moving average (based on weekly data) to be plotted on the main layout, is this possible?
      Code:
      """
      class MovingAverage(bt.Strategy):

      params = (
      		('ema5', 5),
      		('ema20', 8),
      		('ema50', 50),
      		('ema_200', 200),
      		('printlog', True),
      		('atr_mult', 2.0),
      		('rsi_period', 30),
      		('atr_period', 14),
      		)
      
      
      def log(self, txt, dt=None, doprint=False):
      	if self.p.printlog or doprint:
      		dt = dt or self.datas[0].datetime.date(0)
      		print('%s, %s' % (dt.isoformat(), txt))
      
      
      def __init__(self):
      	
      	self.dataclose = self.data.close
      	self.ema5 = bt.ind.EMA(self.data1, period=self.p.ema5)
      	self.ema20 = bt.ind.EMA(self.data2, period=self.p.ema20)
      	self.rsi = bt.ind.RSI_SMA(period=self.p.rsi_period, plot=True)
      	self.atr = bt.ind.ATR(period=self.p.atr_period, plot=False)
      	self.crossup = bt.ind.CrossUp(self.data0, self.ema5, plot=False)
      	
      	self.addminperiod(45)
      	
      	self.order = None
      

      if name=='main':
      cash = 10000
      position = cash
      cerebro = bt.Cerebro(optreturn=True, stdstats=True)

      data = bt.feeds.GenericCSVData(
      	dataname="litecoin_unix_daily.csv",
      	fromdate=datetime.datetime(2017, 1, 1),
          # todate=datetime.datetime(2022, 3, 30),
          timeframe=bt.TimeFrame.Minutes,
          compression=1,
          nullvalue=0.0,
          dtformat=2,
          datetime=0,
          open=1,
          high=2,
          low=3,
          close=4
          )
      
      cerebro.adddata(data, name='data')
      cerebro.resampledata(data, timeframe=bt.TimeFrame.Weeks, compression=1)
      cerebro.resampledata(data, timeframe=bt.TimeFrame.Months, compression=1)
      

      """
      216af55f-9ca8-43fd-8f7f-acec17259fd6-image.png

      1 Reply Last reply Reply Quote 0
      • 1 / 1
      • First post
        Last post
      Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors