Backtrader Community

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

    SShepherd

    @SShepherd

    0
    Reputation
    518
    Profile views
    10
    Posts
    0
    Followers
    0
    Following
    Joined Last Online

    SShepherd Unfollow Follow

    Latest posts made by SShepherd

    • RE: Executing Buy Sell Strategy for PivotPoint

      Great! Now the chart is working. Here's what it looks like with your simple tweak:

              self.signal_add(bt.SIGNAL_LONGEXIT, -1.0*self.sellsignal)
      

      LONGEXIT works

      And I'd like to add that the community behind this software is fantastic!

      My next step will be to figure out the date time. I believe I need to create a new Signal class with an if statement to check the date, correct? Since I won't be using a CrossUp, I'll simply compare the date and data0-pp1.p

      posted in General Code/Help
      S
      SShepherd
    • RE: Executing Buy Sell Strategy for PivotPoint

      OK, baby steps. I got the CrossUp and CrossDown signals generated but it doesn't do LONGEXIT

      See image: crossup and crossdown no LONGEXIT

      from __future__ import (absolute_import, division, print_function,
                              unicode_literals)
      
      import argparse
      
      import backtrader as bt
      import backtrader.feeds as btfeeds
      import backtrader.utils.flushfile
      
      
      class St(bt.SignalStrategy):
          params = (('usepp1', False),
                    ('plot_on_daily', False))
      
          def __init__(self):
              autoplot = self.p.plot_on_daily
              self.pp = pp = bt.ind.PivotPoint(self.data1, _autoplot=autoplot)
      
              ## Here is here I am trying to buy when the daily close is less than the
              ## pivot on the Xth day and sell whenever it crosses r1
              pp1 = self.pp()  # couple the entire indicators
              self.buysignal = bt.ind.CrossUp(self.data0.close, pp1)
              self.sellsignal = bt.ind.CrossDown(self.data0.close, pp1.r1)
      
              self.signal_add(bt.SIGNAL_LONG, self.buysignal)
              self.signal_add(bt.SIGNAL_LONGEXIT, self.sellsignal)
      
          def next(self):
              txt = ','.join(
                  ['%04d' % len(self),
                   '%04d' % len(self.data0),
                   '%04d' % len(self.data1),
                   self.data.datetime.date(0).isoformat(),
                   '%04d' % len(self.pp),
                   '%.2f' % self.pp[0]])
      
              print(txt)
      
      
      def runstrat():
          args = parse_args()
      
          cerebro = bt.Cerebro()
          data = btfeeds.BacktraderCSVData(dataname=args.data)
          cerebro.adddata(data)
          cerebro.resampledata(data, timeframe=bt.TimeFrame.Months)
      
          cerebro.broker.setcash(100000.0)
          #cerebro.signal_accumulate(True)
      
          cerebro.addstrategy(St,
                              plot_on_daily=args.plot_on_daily)
          cerebro.run(runonce=False)
          if args.plot:
              cerebro.plot(style='bar')
      
      
      def parse_args():
          parser = argparse.ArgumentParser(
              formatter_class=argparse.ArgumentDefaultsHelpFormatter,
              description='Sample for pivot point and cross plotting')
      
          parser.add_argument('--data', required=False,
                              default='../../datas/2005-2006-day-001.txt',
                              help='Data to be read in')
      
          parser.add_argument('--plot', required=False, action='store_true',
                              help=('Plot the result'))
      
          parser.add_argument('--plot-on-daily', required=False, action='store_true',
                              help=('Plot the indicator on the daily data'))
      
          return parser.parse_args()
      
      
      if __name__ == '__main__':
          runstrat()
      
      posted in General Code/Help
      S
      SShepherd
    • RE: Executing Buy Sell Strategy for PivotPoint

      @backtrader Let me give it a shot!

      Also, side question, how are dataX automagically created? I don't see them defined anywhere -- I assume this is in the indicator?

      posted in General Code/Help
      S
      SShepherd
    • RE: Executing Buy Sell Strategy for PivotPoint

      @backtrader said in Executing Buy Sell Strategy for PivotPoint:

      I ideally want to buy once a month when it crosses the pivot after a certain day

      The previous statement raises, at least, the following questions, which may help you into coding your strategy:

      • Which pivot?

      The first pivot, and R1

      • Crossing to the upside? To the downside?

      Crossing to the upside

      • Which certain day?

      The 15th of the month

      posted in General Code/Help
      S
      SShepherd
    • RE: Executing Buy Sell Strategy for PivotPoint

      @SShepherd said in Executing Buy Sell Strategy for PivotPoint:

      @backtrader Thanks for response, I uploaded the entire file and dataset

      Here is the full code: http://dpaste.com/0FYC29P

      Here is the dataset: http://dpaste.com/37EGH80 [same as 2005-2006-day-001.txt]

      I ideally want to buy once a month when it crosses the pivot after a certain day, but not sure how to do that

      I'm copying the code into this forum for archival purposes:

      from __future__ import (absolute_import, division, print_function,
                              unicode_literals)
      
      import argparse
      
      import backtrader as bt
      import backtrader.feeds as btfeeds
      import backtrader.utils.flushfile
      
      
      class St(bt.SignalStrategy):
          params = (('usepp1', False),
                    ('plot_on_daily', False))
      
          def __init__(self):
              autoplot = self.p.plot_on_daily
              self.pp = pp = bt.ind.PivotPoint(self.data1, _autoplot=autoplot)
      
      
              ## Here is here I am trying to buy when the daily close is less than the
              ## pivot on the Xth day and sell whenever it crosses r1
              pp1 = self.pp()  # couple the entire indicators
              self.buysignal = self.data0.close < pp1.p
              self.sellsignal = self.data0.close > pp1.r1
      
              self.signal_add(bt.SIGNAL_LONG, self.buysignal)
              self.signal_add(bt.SIGNAL_LONGEXIT, self.sellsignal)
      
      
          def next(self):
              txt = ','.join(
                  ['%04d' % len(self),
                   '%04d' % len(self.data0),
                   '%04d' % len(self.data1),
                   self.data.datetime.date(0).isoformat(),
                   '%04d' % len(self.pp),
                   '%.2f' % self.pp[0]])
      
              print(txt)
      
      
      def runstrat():
          args = parse_args()
      
          cerebro = bt.Cerebro()
          data = btfeeds.BacktraderCSVData(dataname=args.data)
          cerebro.adddata(data)
          cerebro.resampledata(data, timeframe=bt.TimeFrame.Months)
      
          cerebro.addstrategy(St,
                              plot_on_daily=args.plot_on_daily)
          cerebro.run(runonce=False)
          if args.plot:
              cerebro.plot(style='bar')
      
      
      def parse_args():
          parser = argparse.ArgumentParser(
              formatter_class=argparse.ArgumentDefaultsHelpFormatter,
              description='Sample for pivot point and cross plotting')
      
          parser.add_argument('--data', required=False,
                              default='../../datas/2005-2006-day-001.txt',
                              help='Data to be read in')
      
          parser.add_argument('--plot', required=False, action='store_true',
                              help=('Plot the result'))
      
          parser.add_argument('--plot-on-daily', required=False, action='store_true',
                              help=('Plot the indicator on the daily data'))
      
          return parser.parse_args()
      
      
      if __name__ == '__main__':
          runstrat()
      
      posted in General Code/Help
      S
      SShepherd
    • RE: Executing Buy Sell Strategy for PivotPoint

      @backtrader Thanks for response, I uploaded the entire file and dataset

      Here is the full code: http://dpaste.com/0FYC29P

      Here is the dataset: http://dpaste.com/37EGH80 [same as 2005-2006-day-001.txt]

      I ideally want to buy once a month when it crosses the pivot after a certain day, but not sure how to do that

      posted in General Code/Help
      S
      SShepherd
    • Executing Buy Sell Strategy for PivotPoint

      Trying to execute a strategy for buying at the first P and selling at the R1:

      class St(bt.SignalStrategy):
          params = ()
      
      def __init__(self):
          self.pp = bt.ind.PivotPoint(self.data1)
          pp1 = self.pp()  # couple the entire indicators
          self.buysignal = self.data0.close < pp1.p
          self.sellsignal = self.data0.close > pp1.r1
      
          self.signal_add(bt.SIGNAL_LONG, self.buysignal)
          self.signal_add(bt.SIGNAL_LONGEXIT, self.sellsignal)
      

      def runstrat():
      args = parse_args()

      cerebro = bt.Cerebro()
      data = use_clf_data()
      
      # Add commodity
      commCL= bt.CommissionInfo(commission=2.0, margin=1000.0, mult=1000.0)
      cerebro.broker.addcommissioninfo(commCL)
      cerebro.broker.setcash(100000.0)
      
      cerebro.signal_accumulate(True)
      
      # Add the data
      cerebro.adddata(data)
      cerebro.resampledata(data, timeframe=bt.TimeFrame.Months)
      
      cerebro.addstrategy(St)
      cerebro.run(runonce=False)
      if args.plot:
          #cerebro.plot(style='bar')
          cerebro.plot(style='line')
      

      However, I get this: plot

      I'm expecting a lot more buy and sell events. What did I do wrong?

      posted in General Code/Help
      S
      SShepherd
    • RE: Executing Buy Sell Strategy for PivotPoint

      @SShepherd

      This can initiate the buy but only for the first datapoint:

          self.pp = bt.ind.PivotPoint(self.data1)
          pp1 = self.pp()  # couple the entire indicators
          self.buysignal = self.data0.close < pp1.r1
          self.sellsignal = self.data0.close > pp1.p
      
          self.signal_add(bt.SIGNAL_LONG, self.buysignal)
          self.signal_add(bt.SIGNAL_LONGEXIT, self.sellsignal)
      

      And here's my runstrat():

      cerebro.signal_accumulate(True)
      
      # Add the data
      cerebro.adddata(data)
      cerebro.resampledata(data, timeframe=bt.TimeFrame.Months)
      
      cerebro.addstrategy(St)
      
      posted in General Code/Help
      S
      SShepherd
    • RE: Executing Buy Sell Strategy for PivotPoint

      How would I attach a Strategy to this to LONG when it crosses P and SHORT when it crosses R1?

      This does not work:

      class MySignal(bt.Indicator):
          lines = ('signal',)
      
      def __init__(self):
          self.lines.signal = self.data - bt.indicators.PivotPoint(self.data)
      
      posted in General Code/Help
      S
      SShepherd
    • RE: Any consideration to create Slack channel?

      I think, just set one up and see who joins. Then we'll use social darwinism to determine which chat room stays

      With that said, I'm interested

      posted in General Discussion
      S
      SShepherd