For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

Do we need any other method than next in our strategy class for rapid prototyping?



  • For the past few days I am experimenting with backtrader. Initially it was hard but now I am getting the hang of it. I am planning to build a strategy encompassing of different technical indicators and I want to backtest using different combinations of these technical indicators. The important thing I wanted in my backtester is a buy/sell signals, stop losses and reports. The report should include sharpe, drawdown, number of win/loss trades and win/loss ratio. My question is for rapid prototyping do we need any other methods in bt.strategy than next?

    This is a simple strategy with next method alone

    class MyStrategy(bt.Strategy):
        def __init__(self):
            self.sma= bt.ind.EMA(period=10)
        def next(self):
            if not self.position: # check if open position exists
                if self.sma<self.data.close:
                    print('BUY CREATE, %.2f' % self.data.close[0])
                    self.buy(size=100)
            if self.position:
                if self.sma>self.data.close:
                    print('SELL CREATE, %.2f' % self.data.close[0])
                    self.sell(size=100)
    

    This is an another format of the same strategy with logs and order status reporting added to it (code copied and modified from someone else's strategy)

    class MyStrategy(bt.Strategy):
        def __init__(self):
            self.sma= bt.ind.EMA(period=10)
        def log(self, txt, dt=None):
            ''' Logging function fot this strategy'''
            dt = dt or self.datas[0].datetime.date(0)
            print('%s, %s' % (dt.isoformat(), txt))
        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 enougth cash
            if order.status in [order.Completed, order.Canceled, order.Margin]:
                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))
                self.bar_executed = len(self)
                    
            # Write down: no pending order
            self.order = None
        def notify_trade(self, trade):
            if not trade.isclosed:
                return        
            
        def next(self):
            if not self.position: # check if open position exists
                if self.sma<self.data.close:
                    print('BUY CREATE, %.2f' % self.data.close[0])
                    self.order=self.buy()
            if self.position:
                if self.sma>self.data.close:
                    print('SELL CREATE, %.2f' % self.data.close[0])
                    self.order=self.sell()
    

    Do we really need log, notify_trade and notify_order in our strategy class, if we want rapid prototyping and only care about the end statistics of a trade that is sharpe, drawdown, number of win/loss trades and win/loss ratio ? If yes why we required it?


Log in to reply
 

});