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/

    Quickstart guide code not working

    General Code/Help
    2
    3
    55
    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.
    • C
      choc2015 last edited by

      I'm trying to get the backtester quickstart guide code working however currently all it does is nothing is print the portfolio value and some line buffer object:
      Starting Portfolio Value: 100000.00
      <backtrader.linebuffer.LineBuffer object at 0x0000019BB31D1670> dataclose
      Here is my code:

      from __future__ import(absolute_import, division, print_function, unicode_literals)
      
      import datetime
      import os.path
      import sys
      import btoandav20
      import backtrader as bt
      below50 = False
      above150 = False
      class TestStrategy(bt.Strategy):
          params50 = 50
          params100 = 100
          params150 = 150
      
          
          def __init__(self):
              self.dataclose = self.datas[0].close
              print(self.dataclose, "dataclose")
              self.ema50 = bt.indicators.EMA(self.data[0], period = self.params50)
              self.ema100 = bt.indicators.EMA(self.data[0], period = self.params100)
              self.ema150 = bt.indicators.EMA(self.data[0], period = self.params150)
      
          def log(self, txt, dt=None):
              dt = dt or self.datas[0].datetime.date(0)
              print(f'{dt.isoformat()}{txt}')
      
          def next(self):
              self.log('Close:' , self.dataclose[0])
              print('Close:', self.dataclose[0])
              if self.data[0] <= self.ema50 +0.0003 and below50 == False:
                  below50 = True
      
              if self.data[0] < self.ema150 and below50 == True:
                  below50 = True
      
              if self.data[0] == self.ema150 -0.0003:
                  above150 = True
      
              if self.data[0] >= self.ema50 and above150 == True:
                   above150 = False
      
              if below50 == True and self.data[0] >= self.ema50:
                  self.log('BUY CREATE, %.2F' % self.data[0])
                  self.buy()
      
                  
                  
                  
                  
              
              
      
      if __name__ == '__main__':
          cerebro = bt.Cerebro()
      
          #modpath = os.path.dirname(os.path.abspath(sys.argv[0]))
          #datapath = ('C:\\Users\\admin1\\anaconda3\\Menu\\csv_data\\1m-2021-02-01')
      
      
          data = bt.feeds.YahooFinanceCSVData(dataname='C:\\Users\\admin1\\anaconda3\\EURUSD.csv')
          #data_resampled = data.resample(timeframe=bt.TimeFrame.Minutes, compression=1)
          
          cerebro.adddata(data)
      
          cerebro.broker.setcash(100000.0)
          
      
          print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
          cerebro.addstrategy(TestStrategy)
          cerebro.run()
      
          print('Final Portfolio Value: %.2f' %cerebro.broker.getvalue())
      
      run-out 1 Reply Last reply Reply Quote 0
      • run-out
        run-out @choc2015 last edited by

        @choc2015 I'm pretty confident this is not from the quickstart guide. :) You have a lot of things going wrong in your code. I would suggest you spend some time on the code examples without trying too much at first, and then you'll find a much easier path to your algos. Let us know how it goes.

        RunBacktest.com

        1 Reply Last reply Reply Quote 1
        • C
          choc2015 last edited by

          nevermind, I figured it out

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