Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    1. Home
    2. pizza
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
    P
    • Profile
    • Following 0
    • Followers 0
    • Topics 2
    • Posts 4
    • Best 0
    • Groups 0

    pizza

    @pizza

    0
    Reputation
    1
    Profile views
    4
    Posts
    0
    Followers
    0
    Following
    Joined Last Online

    pizza Unfollow Follow

    Latest posts made by pizza

    • backtrader_plotting add many data feeds exception

      Hi, I'm trying to add a lot of data feed to backtrader, using cerebro.plot() it displays fine, but using backtrader_plotting to output html, I'm getting some performance exceptions, is there a way to fix it?

      Thank you!

      except:

      /backtrader_demo/conda37/lib/python3.7/site-packages/backtrader_plotting/bokeh/bokeh.py:468: PerformanceWarning: DataFrame is highly fragmented.  This is usually the result of calling `frame.insert` many times, which has poor performance.  Consider joining all columns at once using pd.concat(axis=1) instead.  To get a de-fragmented frame, use `newframe = frame.copy()`
        strategydf[source_id] = dataline
      

      code:

      import datetime
      import time
      
      import backtrader as bt
      import backtrader.feeds as btfeed
      import pandas as pd
      from backtrader_plotting import Bokeh
      from backtrader_plotting.schemes import Tradimo
      
      tickers = ['AAPL', 'TSLA', 'ARDX', 'SNDL', 'PHUN', 'ABUS', 'AAL', 'AMD', 'NVDA', 'BABA', 'BAC', 'PBR', 'AMC']
      period1 = int(time.mktime(datetime.datetime(2020, 1, 1, 23, 59).timetuple()))
      period2 = int(time.mktime(datetime.datetime(2021, 12, 31, 23, 59).timetuple()))
      interval = '1d'  # 1d, 1m
      
      print('>> download data')
      for ticker in tickers:
          query_string = f'https://query1.finance.yahoo.com/v7/finance/download/{ticker}?period1={period1}&period2={period2}&interval={interval}&events=history&includeAdjustedClose=true'
      
          df = pd.read_csv(query_string)
          # print(df)
          df.to_csv('{name}.csv'.format(name=ticker))
          time.sleep(2)
          print(ticker)
      
      print('>> add data')
      cerebro = bt.Cerebro()
      
      for ticker in tickers:
          # data = btfeeds.YahooFinanceCSVData(dataname='AAPL.csv')
          data = btfeed.GenericCSVData(
              dataname='{name}.csv'.format(name=ticker),
      
              fromdate=datetime.datetime(2020, 1, 1),
              todate=datetime.datetime(2021, 12, 31),
      
              nullvalue=0.0,
      
              dtformat=('%Y-%m-%d'),
      
              # ,Date,Open,High,Low,Close,Adj Close,Volume
              datetime=1,
              high=3,
              low=4,
              open=2,
              close=5,
              volume=7,
              openinterest=-1
          )
      
          cerebro.adddata(data)  # a 'name' parameter can be passed for plotting purposes
      
      print('>> run')
      cerebro.run()
      # cerebro.plot()  # normal to plot
      
      # export to html
      b = Bokeh(style='bar', filename='plot.html', scheme=Tradimo(), output_mode='save')
      cerebro.plot(b)
      
      print('>> done')
      
      
      posted in General Discussion
      P
      pizza
    • RE: How to get the last value from line in next()?

      @run-out Thanks!

      posted in Indicators/Strategies/Analyzers
      P
      pizza
    • RE: How to get the last value from line in next()?

      correction:

      class TestIndicator(bt.Indicator):
      	...
      	def next(self):
      		dynamic_period = get_dynamic_period(self.data) 
      		sma = bt.indicators.SMA(self.data.close.get(0, self.p.data_period), period=dynamic_period)
      
      		last_value = sma.get(-1)  
      		self.lines.dynamic_sma[0] = last_value # not work
      
      posted in Indicators/Strategies/Analyzers
      P
      pizza
    • How to get the last value from line in next()?

      Hello! This is my indicator

      class TestIndicator(bt.Indicator):
      	...
      	def next(self):
      		dynamic_period = get_dynamic_period(self.data) 
      		sma = bt.indicators.SMA(self.data, period=dynamic_period)
      
      		last_value = sma.get(-1)  
      		self.lines.dynamic_sma[0] = last_value # not work
      		
      

      How to get the last value from line in next()?
      Thanks !

      posted in Indicators/Strategies/Analyzers
      P
      pizza