Backtrader Community

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

    BMccoy

    @BMccoy

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

    BMccoy Unfollow Follow

    Latest posts made by BMccoy

    • trades not registering, but strategy is plotting

      For a buy only and exit strategy, when I run the following code I am able to see the chart and data and the indicator. However it doesnt appear to run any trades. Is this because I need trade analyzers. I did this with a strategy before and it worked as designed. But when I changed the data to intraday it gave me loads of problems.

      I end up with the following.

      SMA.JPG

      import backtrader as bt
      from datetime import datetime

      class SmaCross(bt.Strategy):

      params = dict(
          pfast=10,  
          pslow=30
      )
      
      def __init__(self):
          sma1 = bt.ind.SMA(period=self.p.pfast)
          sma2 = bt.ind.SMA(period=self.p.pslow)  
          self.crossover = bt.ind.CrossOver(sma1, sma2)  
      
      def next(self):
          if not self.position:  
              if self.crossover > 0:
                  self.buy() 
      
              elif self.crossover < 0:  
                  self.close() 
      

      In[8]:

      data = bt.feeds.GenericCSVData(
      dataname="test.csv",
      dtformat='%Y-%m-%d %H:%M:%S',
      open=1,
      high=2,
      low=3,
      close=4,
      volume=5,
      fromdate=datetime(2020, 1, 1),
      todate=datetime(2021, 11, 4)

      )
      

      In[9]:

      cerebro = bt.Cerebro()

      cerebro.adddata(data)

      cerebro.addstrategy(SmaCross)
      cerebro.run()
      cerebro.plot(style='Candlestick')

      posted in General Code/Help
      B
      BMccoy
    • Keep getting error module 'backtrader' has no attribute 'Timeframe'

      Hello

      I keep getting the following error module 'backtrader' has no attribute 'Timeframe'

      I am trying to plot using intraday data and not many really good samples out there. Plotting daily data is fine
      Code is as follows

      import os, sys, argparse
      import backtrader as bt
      import backtrader.analyzers as btanalyzers
      import matplotlib
      from datetime import datetime
      import backtrader.feeds as btfeeds

      data = bt.feeds.GenericCSVData(
      dataname='C:/Users/brand/NQ21.csv',
      datetime=0,
      fromdate=datetime(2021, 1, 3),
      timeframe=bt.TimeFrame.Minutes,
      dtformat=('%Y-%m-%d %H:%M:%S'),
      open=1,
      high=2,
      low=3,
      close=4,
      )
      class Mystrat(bt.Strategy):

      def __init__(self):
          self.the_highest_high_4 = bt.ind.Highest(self.data.high, period=4)
          self.the_lowest_low_4 = bt.ind.Lowest(self.data.low, period=4)
      

      cerebro = bt.Cerebro()

      data = bt.feeds.YahooFinanceData(dataname = 'NQ20.csv')
      cerebro.adddata(data)

      cerebro = bt.Cerebro()
      cerebro.adddata(data, timeframe=bt.Timeframe.Minutes, compression=5)
      cerebro.addstrategy(Mystrat)
      cerebro.run()
      cerebro.plot(style='bar')

      posted in General Code/Help
      B
      BMccoy
    • RE: ImportError: cannot import name 'warnings' from 'matplotlib.dates' when trying to plot using cerebro.plot

      @davidavr Thanks but unfortunately I get a different error when putting that into cmd...
      ERROR: Error [WinError 2] The system cannot find the file specified while executing command git clone -q https://github.com/mementum/backtrader.git 'C:\Users\brand\AppData\Local\Temp\pip-install-g_gi_wmv\backtrader_61ddfe02b5664377a282d1e46e60fea0'
      ERROR: Cannot find command 'git' - do you have 'git' installed and in your PATH?

      posted in General Discussion
      B
      BMccoy
    • ImportError: cannot import name 'warnings' from 'matplotlib.dates' when trying to plot using cerebro.plot

      Hello, I am trying to run the code below and keep getting the
      ImportError: cannot import name 'warnings' from 'matplotlib.dates'
      error message. I have read around multiple guthub responses and they all say uninstall matplotlib and perform the following
      pip uninstall matplotlib
      pip install matplotlib==3.2.2
      I do this from the cmd and nothing gets resolved.
      Just note I am trying to run this from Jupyter notebook, so not sure id the dates.py in there is getting updated properly.
      Thank you

      import backtrader as bt
      import backtrader.analyzers as btanalyzers
      import matplotlib
      from datetime import datetime

      class MaCrossStrategy(bt.Strategy):

      def __init__(self):
          ma_fast = bt.ind.SMA(period = 10)
          ma_slow = bt.ind.SMA(period = 50)
           
          self.crossover = bt.ind.CrossOver(ma_fast, ma_slow)
      
      def next(self):
          if not self.position:
              if self.crossover > 0: 
                  self.buy()
          elif self.crossover < 0: 
              self.close()
      

      cerebro = bt.Cerebro()

      data = bt.feeds.YahooFinanceData(dataname = 'AAPL', fromdate = datetime(2010, 1, 1), todate = datetime(2020, 1, 1))
      cerebro.adddata(data)

      cerebro.addstrategy(MaCrossStrategy)

      cerebro.broker.setcash(1000000.0)

      cerebro.addsizer(bt.sizers.PercentSizer, percents = 10)

      cerebro.addanalyzer(btanalyzers.SharpeRatio, _name = "sharpe")
      cerebro.addanalyzer(btanalyzers.Transactions, _name = "trans")
      cerebro.addanalyzer(btanalyzers.TradeAnalyzer, _name = "trades")

      back = cerebro.run()

      cerebro.broker.getvalue()

      back[0].analyzers.sharpe.get_analysis()

      back[0].analyzers.trans.get_analysis()

      back[0].analyzers.trades.get_analysis()

      cerebro.plot()

      posted in General Discussion
      B
      BMccoy