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/

    TypeError

    General Code/Help
    2
    6
    220
    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.
    • Abhishek Agrawal
      Abhishek Agrawal last edited by

      Getting this error while adding my Strategy.
      Can anyone please help?
      code_text

        File "C:/Users/Abhishek/BT/main.py", line 78, in <module>
          cerebro.addstrategy(TestStrategy)
      TypeError: addstrategy() missing 1 required positional argument: 'strategy'
      Abhishek Agrawal 1 Reply Last reply Reply Quote 0
      • Abhishek Agrawal
        Abhishek Agrawal @Abhishek Agrawal last edited by

        @Abhishek-Agrawal @backtrader @ab_trader

        1 Reply Last reply Reply Quote 0
        • run-out
          run-out last edited by

          Please share your code that caused the error.

          RunBacktest.com

          Abhishek Agrawal 1 Reply Last reply Reply Quote 0
          • Abhishek Agrawal
            Abhishek Agrawal @run-out last edited by

            @run-out Hey that error has been solved. I want to code my program that is starts taking trade after 9:30(Market opens at 9:30). Timeframe = 1 minute
            Condition is if close of any 1 minute bar after 9:30 is above the high range then buy at market price.
            If close of any 1 minute bar after 9:30 is below the low range then sell at market price.

            from __future__ import (absolute_import, division, print_function,
                                    unicode_literals)
            import backtrader as bt
            import datetime
            from datetime import datetime
            import os
            import sys
            import backtrader.feeds as btfeed
            
            import pandas as pd
            
            
            class TestStrategy(bt.Strategy):
                def log(self, txt, dt=None):
                    dt = dt or self.data.datetime.datetime(0)
                    print('%s, %s' % (dt.isoformat(), txt))
            
                def __init__(self):
                    self.buysignal = self.data.close > self.data.high
                    self.sellsignal = self.data.close > self.data.low
                    self.dataclose = self.datas[0].close
                    self.order = None
                    self.buyprice = None
                    self.buycomm = None
                    self.starttime = datetime.now().replace(hour=9, minute=30, second=0)
                    self.t = self.datas[0].datetime.time(0)
            
                def notify_order(self, order):
                    if order.status in [order.Submitted, order.Accepted]:
                        # Buy/Sell order submitted/accepted to/by broker - Nothing to do
                        return
            
                    # Check if an order has been completed
                    # Attention: broker could reject order if not enough cash
                    if order.status in [order.Completed]:
                        if order.isbuy():
                            self.log(
                                'BUY EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' %
                                (order.executed.price,
                                 order.executed.value,
                                 order.executed.comm))
            
                            self.buyprice = order.executed.price
                            self.buycomm = order.executed.comm
                        else:  # Sell
                            self.log('SELL EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' %
                                     (order.executed.price,
                                      order.executed.value,
                                      order.executed.comm))
            
                    elif order.status in [order.Canceled, order.Margin, order.Rejected]:
                        self.log('Order Canceled/Margin/Rejected')
            
                    self.order = None
            
                def next(self):
            
                    if self.order:
                        return
                    if not self.position:
                        if self.t > self.starttime.time():
                            if self.buysignal:
                                self.log('BUY CREATED, %.2f' % self.dataclose[0])
            
                                # Keep track of the created order to avoid a 2nd order
                                self.order = self.buy()
            
                            else:
                                if self.sellsignal:
                                    self.log('SELL CREATED, %.2f' % self.dataclose[0])
            
                                    # Keep track of the created order to avoid a 2nd order
                                    self.order = self.sell()
            
                def stop(self):
                    self.log(' Ending Value %.2f' % (self.broker.getvalue()))
            
            
            if __name__ == '__main__':
                cerebro = bt.Cerebro()
            
            
                class DataFeed(btfeed.GenericCSVData):
                    params = (
                        ('dtformat', '%Y%m%d'),
                        ('tmformat', '%H:%M:%S'),
                        ('datetime', 0),
                        ('time', 1),
                        ('open', 2),
                        ('high', 3),
                        ('low', 4),
                        ('close', 5),
                        ('volume', 6),
                        ('openinterest', 7)
                    )
            
            
                data = DataFeed(dataname='BANKNIFTYFEB18.csv', timeframe=bt.TimeFrame.Minutes, compression=1)
                cerebro.adddata(data)
                cerebro.addstrategy(TestStrategy)
            
                cerebro.run()
            
                # why declare a class for generating data
                # my data fromat is diff than the yahoo data. so have to modify
                # letme show u the plott file
            
            
            Abhishek Agrawal 1 Reply Last reply Reply Quote 0
            • Abhishek Agrawal
              Abhishek Agrawal @Abhishek Agrawal last edited by

              @run-out Sorry the mkt starts at 9:15

              1 Reply Last reply Reply Quote 0
              • run-out
                run-out last edited by

                If you've solved the problem do you still have another question?

                RunBacktest.com

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