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



  • 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'




  • Please share your code that caused the error.



  • @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
    
    


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



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


Log in to reply
 

});