Backtrader Community

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

    Andrew M

    @Andrew M

    0
    Reputation
    28
    Profile views
    7
    Posts
    0
    Followers
    0
    Following
    Joined Last Online

    Andrew M Unfollow Follow

    Latest posts made by Andrew M

    • RE: Evaluate Weekly Trend, Execute Trades on Minute basis

      (I cut out a few days in results for brevity)

      posted in General Code/Help
      Andrew M
      Andrew M
    • RE: Evaluate Weekly Trend, Execute Trades on Minute basis

      length.append(self.data[0]) - this will return huge list of data feeds, I don't think you need it; if you implement your indicator in the __init()__, you don't need to check this. bt will do it for you.

      @ab_trader Did all the above, but still having trouble with this last part. Made a dummy indicator class but I get an "array index out of bound exception" on the " self.dayHigh[0] = self.data1.high[-5]" line if I don't give a massive self.addminperiod. Data1 is days, but it seems self.addminperiod only cares about data0 (minutes). If I switch data1 to data0, then having self.addminperiod(5) works "correctly" since I need data from 5 previous bars ago (although of course the indicator should only care about days)

      How can I sync up self.addminperiod to days?

      Once I have this basic script set up I have the foundation to build on my own. Just need to fix this! Thanks again in advance.

      Below is the simple code and the output.

      class dummyIndicator(bt.Indicator,):
          lines = ('dayHigh',)
          params = (
              ('period', 5),
          )
      
          def __init__(self):
              self.addminperiod(1000)
      
          def next(self):
              self.dayHigh[0] = self.data1.high[-5]
      
      class TestStrategy(bt.Strategy):
          params = (
              # changes the starting indicator value
              ('maperiod', 5),
      
          )
      
          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 __init__(self):
              # Keep a reference to the "close" line in the data[0] dataseries
              self.dataclose = self.datas[0].close
      
              # To keep track of pending orders and buy price/commission
              self.order = None
              self.buyprice = None
              self.buycomm = None
      
              #"Unexpected argument" but code still runs
              self.dayHigh = dummyIndicator(self.data0, self.data1)
      
          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))
      
                  self.bar_executed = len(self)
      
              elif order.status in [order.Canceled, order.Margin, order.Rejected]:
                  self.log('Order Canceled/Margin/Rejected')
      
              self.order = None
      
          def notify_trade(self, trade):
              if not trade.isclosed:
                  return
      
              self.log('OPERATION PROFIT, GROSS %.2f, NET %.2f' %
                       (trade.pnl, trade.pnlcomm))
      
          def next(self):
      
              self.log('Current Date')
              print("Indicator Value", self.dayHigh[0])
      
              # Check if an order is pending ... if yes, we cannot send a 2nd one
              if self.order:
                  return
      
              # Check if we are in the market
              if not self.position:
      
                  # Not yet ... we MIGHT BUY if ...
                  if self.data0.close[0] > self.dayHigh[0]:
      
                      # BUY, BUY, BUY!!! (with all possible default parameters)
                      self.log('BUY CREATE, %.2f' % self.dataclose[0])
      
                      # Keep track of the created order to avoid a 2nd order
                      self.order = self.buy()
      
              else:
      
                  if self.data0.close[0] < self.dayHigh[0]:
                      # SELL, SELL, SELL!!! (with all possible default parameters)
                      self.log('SELL CREATE, %.2f' % self.dataclose[0])
      
                      # Keep track of the created order to avoid a 2nd order
                      self.order = self.sell()
      
      if __name__ == '__main__':
          DataFactory = alpaca_backtrader_api.AlpacaData
          data0 = DataFactory(dataname='DSX',
                              historical=True,
                              fromdate=datetime(2020, 1, 20),
                              timeframe=bt.TimeFrame.Minutes,
                              reversed=True)
          cerebro.adddata(data0)
          data1 = cerebro.resampledata(data0, timeframe=bt.TimeFrame.Days)
          cerebro.adddata(data1)
          cerebro.addstrategy(TestStrategy)
          cerebro.broker.setcash(1000.0)
          print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
          cerebro.run()
          print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
      

      OUTPUT:

      Starting Portfolio Value: 1000.00
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-28, Current Date
      Indicator Value 2.8
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, BUY CREATE, 2.86
      2020-01-29, BUY EXECUTED, Price: 2.80, Cost: 2.80, Comm 0.00
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, SELL CREATE, 2.76
      2020-01-29, SELL EXECUTED, Price: 2.76, Cost: 2.80, Comm 0.00
      2020-01-29, OPERATION PROFIT, GROSS -0.04, NET -0.04
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      2020-01-29, Current Date
      Indicator Value 2.78
      
      Final Portfolio Value: 999.90
      
      Process finished with exit code 0
      
      
      posted in General Code/Help
      Andrew M
      Andrew M
    • RE: Evaluate Weekly Trend, Execute Trades on Minute basis

      I know my question might seem simple, but I am new to python (but an experienced trader). The documentation is helpful but the examples are rather complicated and confusing for a beginner.

      I've marked up the code for extra clarity. Again, it's calculating a simple "baseline" number that should be from Daily Bars and the trades should be coming from Minute bars. Example: The previous 5 day's highs average to 5 dollars, the lows average to 3 dollars, so the "baseline" indicator on day 1 should be 4 dollars. If the stock price, at any minute is below 4, buy order is placed, and if price is above 4, it sells.

      To help me learn, I would greatly appreciate if you could point out where exactly, specifically, the separate data feeds would be referenced in the simple code below to achieve the desired result. If we can work this small example out I'd be able to run and expand on it. Thank you again for your help.

      #setting cerebro
      cerebro = bt.Cerebro()
      
      #using alpaca
      store = alpaca_backtrader_api.AlpacaStore(
          key_id=ALPACA_API_KEY,
          secret_key=ALPACA_SECRET_KEY,
          paper=ALPACA_PAPER
      )
      if not ALPACA_PAPER:
          broker = alpaca_backtrader_api.AlpacaBroker()
          cerebro.setbroker(broker)
      
      length = []
      
      class TestStrategy(bt.Strategy):
      
          #this is the logging function
          def log(self, txt, dt=None):
              dt = dt or self.datas[0].datetime.date(0)
              print('%s, %s' % (dt.isoformat(), txt))
      
          #keeping references to certain data
          def __init__(self):
              self.datalow = self.datas[0].low
              self.datahigh = self.datas[0].high
              self.order = None
              self.buyprice = None
              self.buycomm = None
      
          #order notifications
          def notify_order(self, order):
              if order.status in [order.Submitted, order.Accepted]:
                  return
              if order.status in [order.Completed]:
                  if order.isbuy():
                      self.log('BUY EXECUTED, %.8f' % order.executed.price)
                      self.log('Current Shares: %.8f' % self.getposition(data=dataMinutes).size)
                  elif order.issell():
                      self.log('SELL EXECUTED, %.8f' % order.executed.price)
                      self.log('Current Shares: %.8f' % self.getposition(data=dataMinutes).size)
      
              elif order.status in [order.Canceled, order.Margin, order.Rejected]:
                  self.log('Order Canceled/Margin/Rejected')
      
          #trade notifications
          def notify_trade(self, trade):
              if not trade.isclosed:
                  return
      
              self.log('OPERATION PROFIT, GROSS %.8f, NET %.f' %
                       (trade.pnl, trade.pnlcomm))
      
          #is called for each bar of data
          def next(self):
      
              daysAgo = 5
              length.append(self.data[0])
      
              #This is the indicator of sorts. It calculates "baseline" and decides
              #whether to start placing orders or not.
              #This should use Daily Data only.
              if len(length) >= daysAgo + 1:
                  last5highavg = (self.datahigh[-1] + self.datahigh[-2] + self.datahigh[-3] + self.datahigh[-4] + self.datahigh[-5]) / 5
                  last5lowavg = (self.datalow[-1] + self.datalow[-2] + self.datalow[-3] + self.datalow[-4] + self.datalow[-5]) / 5
      
                  baseline = (last5highavg + last5lowavg) / 2
      
                  #order logic. This should be based on Minute Bars
                  if self.order:
                      return
                  if not self.position:
                      if self.data[0] < baseline:
                          self.log('BUY CREATE, %.8f' % self.data[0])
                          self.order = self.buy(exectype=bt.Order.Limit, price=self.data[0], size=310)
                  else:
                      if self.data[0] > baseline:
                          self.log('SELL CREATE, %.8f' % self.data[0])
                          self.order = self.sell(exectype=bt.Order.Limit, price=self.data[0])
              else:
                  print("Fewer than 5 days")
      
      #adding the strategy and setting cash
      cerebro.addstrategy(TestStrategy)
      cerebro.broker.setcash(1000.0)
      
      #data feed resampling from minutes to days
      DataFactory = alpaca_backtrader_api.AlpacaData
      dataMinutes = DataFactory(dataname='DSX', historical=True, fromdate=datetime(2020, 1, 15), todate=datetime(2020, 1, 31), timeframe=bt.TimeFrame.Minutes)
      cerebro.resampledata(dataMinutes, timeframe=bt.TimeFrame.Days, compression=1)
      
      #dataMinutes = DataFactory(dataname='DSX', historical=True, fromdate=datetime(2020, 1, 15), todate=datetime(2020, 1, 31), timeframe=bt.TimeFrame.Days)
      
      #printing the results
      print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
      cerebro.run()
      print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
      
      posted in General Code/Help
      Andrew M
      Andrew M
    • RE: Evaluate Weekly Trend, Execute Trades on Minute basis

      Thank you again for your quick response and the heads up. I'll take a look at the indicator docs and get back to you soon!

      posted in General Code/Help
      Andrew M
      Andrew M
    • RE: Evaluate Weekly Trend, Execute Trades on Minute basis

      @ab_trader I regenerated them immediately after posting. The above keys are disabled. How can I delete/edit the post?

      posted in General Code/Help
      Andrew M
      Andrew M
    • RE: Evaluate Weekly Trend, Execute Trades on Minute basis

      @ab_trader Hi, thanks for your prompt response. Still very unclear though. Hoping you can still help me out with this.

      @ab_trader said in Evaluate Weekly Trend, Execute Trades on Minute basis:

      Add minute data feed, resample it to daily bars.

      In my above code snippet I already the minute feed. In the below code snipped I resample it to daily bars.

      You will have two data feeds. Check your indicator against second data feed and issue your orders using first data feed.

      How would you access the separate data feeds?

      Given my code snippet above, is there a practical way to access the Minute feed only when making orders and the Day feed only when checking the indicator? A passed argument, perhaps? Unsure on practical implementation.

      Below, modified the original code snippet with the minute feed resampled to daily bars. However, it only issues orders based on the incorrect Day feed.

      import alpaca_backtrader_api
      import backtrader as bt
      import pandas as pd
      from datetime import datetime
      
      ALPACA_API_KEY = MY_ALPACA_API_KEY
      ALPACA_SECRET_KEY = MY_ALPACA_SECRET_KEY
      ALPACA_PAPER = True
      
      cerebro = bt.Cerebro()
      
      store = alpaca_backtrader_api.AlpacaStore(
          key_id=ALPACA_API_KEY,
          secret_key=ALPACA_SECRET_KEY,
          paper=ALPACA_PAPER
      )
      if not ALPACA_PAPER:
          broker = alpaca_backtrader_api.AlpacaBroker()
          cerebro.setbroker(broker)
      
      length = []
      
      
      class TestStrategy(bt.Strategy):
      
          def log(self, txt, dt=None):
              ''' Logging function for this strategy'''
              dt = dt or self.datas[0].datetime.date(0)
              print('%s, %s' % (dt.isoformat(), txt))
      
          def __init__(self):
              self.datalow = self.datas[0].low
              self.datahigh = self.datas[0].high
              self.order = None
              self.buyprice = None
              self.buycomm = None
      
      
          def notify_order(self, order):
              if order.status in [order.Submitted, order.Accepted]:
      
                  return
      
              if order.status in [order.Completed]:
                  if order.isbuy():
                      self.log('BUY EXECUTED, %.8f' % order.executed.price)
                      self.log('Current Shares: %.8f' % self.getposition(data=dataMinutes).size)
                  elif order.issell():
                      self.log('SELL EXECUTED, %.8f' % order.executed.price)
                      self.log('Current Shares: %.8f' % self.getposition(data=dataMinutes).size)
      
              elif order.status in [order.Canceled, order.Margin, order.Rejected]:
                  self.log('Order Canceled/Margin/Rejected')
      
      
          def notify_trade(self, trade):
              if not trade.isclosed:
                  return
      
              self.log('OPERATION PROFIT, GROSS %.8f, NET %.f' %
                       (trade.pnl, trade.pnlcomm))
      
          def next(self):
      
              daysAgo = 5
              length.append(self.data[0])
      
              if len(length) >= daysAgo + 1:
                  self.log('Low, %.2f' % self.datahigh[0])
                  last5highavg = (self.datahigh[-1] + self.datahigh[-2] + self.datahigh[-3] + self.datahigh[-4] + self.datahigh[-5]) / 5
                  last5lowavg = (self.datalow[-1] + self.datalow[-2] + self.datalow[-3] + self.datalow[-4] + self.datalow[-5]) / 5
                  print(self.datas[0].datetime.date(0),  "---> Above trade was made today")
      
                  baseline = (last5highavg + last5lowavg) / 2
      
                  print(
                      "current price:",
                      self.data[0],
                      "baseline:", baseline, )
      
                  if self.order:
                      return
      
                  if not self.position:
      
                      if self.data[0] < baseline:
                          self.log('BUY CREATE, %.8f' % self.data[0])
                          self.order = self.buy(exectype=bt.Order.Limit, price=self.data[0], size=310)
      
                  else:
      
                      if self.data[0] > baseline:
                          self.log('SELL CREATE, %.8f' % self.data[0])
                          self.order = self.sell(exectype=bt.Order.Limit, price=self.data[0])
      
              else:
                  print("Fewer than 5 days")
      
      
      cerebro.addstrategy(TestStrategy)
      
      
      DataFactory = alpaca_backtrader_api.AlpacaData
      dataMinutes = DataFactory(dataname='DSX', historical=True, fromdate=datetime(2020, 1, 15), todate=datetime(2020, 1, 31), timeframe=bt.TimeFrame.Minutes)
      
      #Added the resampling code snippet here
      cerebro.resampledata(dataMinutes, timeframe=bt.TimeFrame.Days, compression=1)
      
      
      cerebro.broker.setcash(1000.0)
      
      print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
      cerebro.run()
      print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
      
      posted in General Code/Help
      Andrew M
      Andrew M
    • Evaluate Weekly Trend, Execute Trades on Minute basis

      Hi, noob here looking for help.

      Confused with datafeed timeframes. Hacked together a simple example from the tutorials to illustrate:

      The below program uses an "indicator," baseline, to determine trades. If current price is below baseline, it buys, if above, it sells.

      Baseline is average of the last 5 day's highs and last 5 day's lows, divided by 2.

      However, I can't seem to figure out how to get those daily highs and lows, while executing trades on a minute basis. I can get them if I set the datafeed to daily though. If I set it to minutes, it just takes the last 5 prices on a minute basis, instead of daily highs and lows...

      I know this may sound like a simple question, but if someone could give advice/amend the below code to work, I will be able to continue my backtrader journey. Thank you!

      #Making sure 5 days have passed
      length = []
      
      class TestStrategy(bt.Strategy):
      
          def log(self, txt, dt=None):
              ''' Logging function for this strategy'''
              dt = dt or self.datas[0].datetime.date(0)
              print('%s, %s' % (dt.isoformat(), txt))
      
          def __init__(self):
              self.datalow = self.datas[0].low
              self.datahigh = self.datas[0].high
              self.order = None
              self.buyprice = None
              self.buycomm = None
      
      
          def notify_order(self, order):
              if order.status in [order.Submitted, order.Accepted]:
      
                  return
      
              if order.status in [order.Completed]:
                  if order.isbuy():
                      self.log('BUY EXECUTED, %.8f' % order.executed.price)
                      self.log('Current Shares: %.8f' % self.getposition(data=dataMinutes).size)
                  elif order.issell():
                      self.log('SELL EXECUTED, %.8f' % order.executed.price)
                      self.log('Current Shares: %.8f' % self.getposition(data=dataMinutes).size)
      
              elif order.status in [order.Canceled, order.Margin, order.Rejected]:
                  self.log('Order Canceled/Margin/Rejected')
      
      
          def notify_trade(self, trade):
              if not trade.isclosed:
                  return
      
              self.log('OPERATION PROFIT, GROSS %.8f, NET %.f' %
                       (trade.pnl, trade.pnlcomm))
      
          def next(self):
              ################################RelevantCode
      
              #Needs 5 previous days of data for the indicator to work
              daysAgo = 5
              length.append(self.data[0])
      
              #If there's 5 days of data....
              if len(length) >= daysAgo + 1:
                  self.log('Low, %.2f' % self.datahigh[0])
      
                  #average the daily highs of past 5 days
                  last5highavg = (self.datahigh[-1] + self.datahigh[-2] + self.datahigh[-3] + self.datahigh[-4] + self.datahigh[-5]) / 5
      
                  #average the daily lows of past 5 days
                  last5lowavg = (self.datalow[-1] + self.datalow[-2] + self.datalow[-3] + self.datalow[-4] + self.datalow[-5]) / 5
                
                  #calculate indicator on a daily basis
                  baseline = (last5highavg + last5lowavg) / 2
      
                 ################################RelevantCode
      
                  if self.order:
                      return
      
                  if not self.position:
      
                      if self.data[0] < baseline:
                          self.log('BUY CREATE, %.8f' % self.data[0])
                          self.order = self.buy(exectype=bt.Order.Limit, price=self.data[0], size=310)
      
                  else:
      
                      if self.data[0] > baseline:
                          self.log('SELL CREATE, %.8f' % self.data[0])
                          self.order = self.sell(exectype=bt.Order.Limit, price=self.data[0])
      
              else:
                  print("Fewer than 5 days")
      
      
      cerebro.addstrategy(TestStrategy)
      
      
      #Alpaca data. I chose minutes for execution....
      DataFactory = alpaca_backtrader_api.AlpacaData
      dataMinutes = DataFactory(dataname='DSX', historical=True, fromdate=datetime(2020, 1, 15), todate=datetime(2020, 1, 31), timeframe=bt.TimeFrame.Minutes)
      cerebro.adddata(dataMinutes)
      
      cerebro.broker.setcash(1000.0)
      
      print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
      cerebro.run()
      print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
      
      posted in General Code/Help timeframe datafeed execution multiple data
      Andrew M
      Andrew M