Backtrader Community

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

    mirono

    @mirono

    1
    Reputation
    423
    Profile views
    7
    Posts
    0
    Followers
    0
    Following
    Joined Last Online

    mirono Unfollow Follow

    Best posts made by mirono

    • Commission dependent on direction (buy or sell)

      My commissions for stocks are 1$ up to 100 units, or 0.01$ per unit if over 100 units.
      In addition exchange charges are 0.05$ on buy, 0.06$ on sell.
      I was able to code the 1st part:

      class CommInfo_My(bt.CommInfoBase):
          params = (
            ('stocklike', True),  # Stocks
            ('commtype', bt.CommInfoBase.COMM_FIXED),  # Apply fixed Commission
          # ('percabs', False),  # pass perc as xx% which is the default
            ('interest_long', True),
          )
      
          def _getcommission(self, size, price, pseudoexec):
              if size <= 100:
                  return 1.00
              else:
                  return size * self.p.commission
      

      (where I pass commission=0.01)

      But how do I code the 2nd part? Is there a direction indication in the CommInfoBase class?

      posted in General Code/Help
      M
      mirono

    Latest posts made by mirono

    • RE: Commission dependent on direction (buy or sell)

      Thanks. So size should be positive for buy and negative for sell (not short)?
      As for the 1c, I suspect it has to do with the time the position is held. I assume it increases with time (never had long lived positions in real life, to verify this). I'll take our advice and add 0.06 to the commissions.

      posted in General Code/Help
      M
      mirono
    • Commission dependent on direction (buy or sell)

      My commissions for stocks are 1$ up to 100 units, or 0.01$ per unit if over 100 units.
      In addition exchange charges are 0.05$ on buy, 0.06$ on sell.
      I was able to code the 1st part:

      class CommInfo_My(bt.CommInfoBase):
          params = (
            ('stocklike', True),  # Stocks
            ('commtype', bt.CommInfoBase.COMM_FIXED),  # Apply fixed Commission
          # ('percabs', False),  # pass perc as xx% which is the default
            ('interest_long', True),
          )
      
          def _getcommission(self, size, price, pseudoexec):
              if size <= 100:
                  return 1.00
              else:
                  return size * self.p.commission
      

      (where I pass commission=0.01)

      But how do I code the 2nd part? Is there a direction indication in the CommInfoBase class?

      posted in General Code/Help
      M
      mirono
    • RE: ATR issue

      You are right!
      When switching to a verified good data:

      from __future__ import (absolute_import, division, print_function,
                              unicode_literals)
      
      import datetime  # For datetime objects
      import os.path  # To manage paths
      import sys  # To find out the script name (in argv[0])
      
      # Import the backtrader platform
      import backtrader as bt
      import backtrader.indicators as btind
      import backtrader.feeds as btfeeds
      
         
      # Create a Stratey
      class NoStrategy(bt.Strategy):
      
          def __init__(self):
              btind.ATR(self.datas[0])
      
      
      if __name__ == '__main__':
          # Create a cerebro entity
          cerebro = bt.Cerebro()
          # Add a strategy
          cerebro.addstrategy(NoStrategy)
      
          # Datas are in a subfolder of the samples. Need to find where the script is
          # because it could have been called from anywhere
          #modpath = os.path.dirname(os.path.abspath(sys.argv[0]))
          #datapath = os.path.join(modpath, '../../datas/orcl-1995-2014.txt')
      
          # Create a Data Feed
      #    data1 = bt.feeds.YahooFinanceData(
      #        dataname="SPY",
      #        # Do not pass values before this date
      #        fromdate=datetime.datetime(2000, 1, 1),
      #        # Do not pass values before this date
      #        todate=datetime.datetime(2017, 10, 31),
      #        # Do not pass values after this date
      #        reverse=True)
      
          # Create a Data Feed
          data1 = btfeeds.GenericCSVData(dataname = '../data/spy-2000-2017.txt',
              fromdate = datetime.datetime(2000, 1, 1),
              todate = datetime.datetime(2017, 10, 31),
              dtformat=('%m/%d/%Y'),
              nullvalue = 0.0,
              datetime = 0,
              high = 3,
              low = 4,
              open = 2,
              close = 5,
              volume = 6,
              openinterest = 7)
          
          # Add the Data Feed to Cerebro
          cerebro.adddata(data1)
      
          # Run over everything
          cerebro.run()
      
          # Plot the result
          cerebro.plot()
      

      ATR seems to be fine!
      0_1511542021885_3.png

      posted in Indicators/Strategies/Analyzers
      M
      mirono
    • RE: ATR issue

      I've created a simple test to demonstrate the problem:

      from __future__ import (absolute_import, division, print_function,
                              unicode_literals)
      
      import datetime  # For datetime objects
      import os.path  # To manage paths
      import sys  # To find out the script name (in argv[0])
      
      # Import the backtrader platform
      import backtrader as bt
      import backtrader.indicators as btind
      
         
      # Create a Stratey
      class NoStrategy(bt.Strategy):
      
          def __init__(self):
              bt.indicators.ATR(self.datas[0])
      
      
      if __name__ == '__main__':
          # Create a cerebro entity
          cerebro = bt.Cerebro()
          # Add a strategy
          cerebro.addstrategy(NoStrategy)
      
          # Datas are in a subfolder of the samples. Need to find where the script is
          # because it could have been called from anywhere
          #modpath = os.path.dirname(os.path.abspath(sys.argv[0]))
          #datapath = os.path.join(modpath, '../../datas/orcl-1995-2014.txt')
      
          # Create a Data Feed
          data1 = bt.feeds.YahooFinanceData(
              dataname="SPY",
              # Do not pass values before this date
              fromdate=datetime.datetime(2000, 1, 1),
              # Do not pass values before this date
              todate=datetime.datetime(2017, 10, 31),
              # Do not pass values after this date
              reverse=True)
          
          # Add the Data Feed to Cerebro
          cerebro.adddata(data1)
      
          # Run over everything
          cerebro.run()
      
          # Plot the result
          cerebro.plot()
      

      The results for the ATR are pretty much the same (attached). What could be wrong?0_1511510854409_2.png

      posted in Indicators/Strategies/Analyzers
      M
      mirono
    • ATR issue

      I'm doing a simple strategy plot for SPY with ATR indicator.
      The ATR values for recent dates seems reasonable. However, as we go back in time the values increase to unreasonable values:![alt text](0_1511029731266_atr-issue.png image url)

      Checking this in other stock charting software make it clear that something is wrong with the ATR values.
      Any suggestions?

      posted in Indicators/Strategies/Analyzers
      M
      mirono
    • RE: Problem with the simple sample

      Thanks! I missed that. should have been reverse=True

      posted in General Code/Help
      M
      mirono
    • Problem with the simple sample

      Using the sample code:

       from __future__ import (absolute_import, division, print_function,
                              unicode_literals)
      
       import datetime  # For datetime objects
       import os.path  # To manage paths
       import sys  # To find out the script name (in argv[0])
      
      # Import the backtrader platform
      import backtrader as bt
      
      # Create a Stratey
      class TestStrategy(bt.Strategy):
      
          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
      
          def next(self):
              # Simply log the closing price of the series from the reference
              self.log('Close, %.2f' % self.dataclose[0])
      
              if self.dataclose[0] < self.dataclose[-1]:
                  # current close less than previous close
      
                  if self.dataclose[-1] < self.dataclose[-2]:
                      # previous close less than the previous close
      
                      # BUY, BUY, BUY!!! (with all possible default parameters)
                      self.log('BUY CREATE, %.2f' % self.dataclose[0])
                      self.buy()
      
      
      if __name__ == '__main__':
          # Create a cerebro entity
          cerebro = bt.Cerebro()
      
          # Add a strategy
          cerebro.addstrategy(TestStrategy)
      
          # Datas are in a subfolder of the samples. Need to find where the script is
          # because it could have been called from anywhere
          modpath = os.path.dirname(os.path.abspath(sys.argv[0]))
          datapath = os.path.join(modpath, '../../datas/orcl-1995-2014.txt')
      
          # Create a Data Feed
          data = bt.feeds.YahooFinanceData(
              dataname="AAPL",
              # Do not pass values before this date
              fromdate=datetime.datetime(2000, 1, 1),
              # Do not pass values before this date
              todate=datetime.datetime(2000, 12, 31),
              # Do not pass values after this date
              reverse=False)
      
          # Add the Data Feed to Cerebro
          cerebro.adddata(data)
      
          # Set our desired cash start
          cerebro.broker.setcash(100000.0)
      
          # Print out the starting conditions
          print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
      
          # Run over everything
          cerebro.run()
      
          # Print out the final result
          print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
      

      I get the following output:

      Starting Portfolio Value: 100000.00
      2000-12-29, Close, 0.97
      2000-12-28, Close, 0.96
      2000-12-28, BUY CREATE, 0.96
      2000-12-27, Close, 0.96
      2000-12-26, Close, 0.96
      2000-12-22, Close, 0.98
      2000-12-21, Close, 0.91
      2000-12-20, Close, 0.94
      2000-12-19, Close, 0.91
      2000-12-18, Close, 0.93
      2000-12-15, Close, 0.91
      2000-12-14, Close, 0.94
      2000-12-13, Close, 0.98
      2000-12-12, Close, 1.00
      2000-12-11, Close, 0.99
      2000-12-08, Close, 0.98
      2000-12-08, BUY CREATE, 0.98
      2000-12-07, Close, 0.93
      2000-12-07, BUY CREATE, 0.93
      2000-12-06, Close, 0.93
      2000-12-05, Close, 1.11
      2000-12-04, Close, 1.09
      2000-12-01, Close, 1.11
      2000-11-30, Close, 1.07
      2000-11-29, Close, 1.14
      2000-11-28, Close, 1.17
      2000-11-27, Close, 1.22
      2000-11-24, Close, 1.26
      2000-11-22, Close, 1.20
      2000-11-21, Close, 1.22
      2000-11-20, Close, 1.23
      2000-11-17, Close, 1.20
      2000-11-16, Close, 1.24
      2000-11-15, Close, 1.29
      2000-11-14, Close, 1.32
      2000-11-13, Close, 1.26
      2000-11-10, Close, 1.24
      2000-11-10, BUY CREATE, 1.24
      2000-11-09, Close, 1.31
      2000-11-08, Close, 1.31
      2000-11-07, Close, 1.39
      2000-11-06, Close, 1.39
      2000-11-03, Close, 1.45
      2000-11-02, Close, 1.45
      2000-11-01, Close, 1.33
      2000-10-31, Close, 1.27
      2000-10-31, BUY CREATE, 1.27
      2000-10-30, Close, 1.26
      2000-10-30, BUY CREATE, 1.26
      2000-10-27, Close, 1.21
      2000-10-27, BUY CREATE, 1.21
      2000-10-26, Close, 1.20
      2000-10-26, BUY CREATE, 1.20
      2000-10-25, Close, 1.20
      2000-10-24, Close, 1.23
      2000-10-23, Close, 1.33
      2000-10-20, Close, 1.27
      2000-10-19, Close, 1.23
      2000-10-19, BUY CREATE, 1.23
      2000-10-18, Close, 1.31
      2000-10-17, Close, 1.31
      2000-10-16, Close, 1.40
      2000-10-13, Close, 1.44
      2000-10-12, Close, 1.30
      2000-10-11, Close, 1.28
      2000-10-11, BUY CREATE, 1.28
      2000-10-10, Close, 1.36
      2000-10-09, Close, 1.42
      2000-10-06, Close, 1.44
      2000-10-05, Close, 1.44
      2000-10-04, Close, 1.54
      2000-10-03, Close, 1.45
      2000-10-02, Close, 1.58
      2000-09-29, Close, 1.68
      2000-09-28, Close, 3.48
      2000-09-27, Close, 3.18
      2000-09-26, Close, 3.35
      2000-09-25, Close, 3.48
      2000-09-22, Close, 3.40
      2000-09-21, Close, 3.69
      2000-09-20, Close, 3.97
      2000-09-19, Close, 3.90
      2000-09-18, Close, 3.95
      2000-09-15, Close, 3.59
      2000-09-14, Close, 3.70
      2000-09-13, Close, 3.77
      2000-09-12, Close, 3.76
      2000-09-11, Close, 3.80
      2000-09-08, Close, 3.83
      2000-09-07, Close, 4.03
      2000-09-06, Close, 3.80
      2000-09-05, Close, 4.06
      2000-09-01, Close, 4.13
      2000-08-31, Close, 3.96
      2000-08-30, Close, 3.87
      2000-08-30, BUY CREATE, 3.87
      2000-08-29, Close, 3.85
      2000-08-29, BUY CREATE, 3.85
      2000-08-28, Close, 3.78
      2000-08-28, BUY CREATE, 3.78
      2000-08-25, Close, 3.70
      2000-08-25, BUY CREATE, 3.70
      2000-08-24, Close, 3.65
      2000-08-24, BUY CREATE, 3.65
      2000-08-23, Close, 3.53
      2000-08-23, BUY CREATE, 3.53
      2000-08-22, Close, 3.36
      2000-08-22, BUY CREATE, 3.36
      2000-08-21, Close, 3.29
      2000-08-21, BUY CREATE, 3.29
      2000-08-18, Close, 3.25
      2000-08-18, BUY CREATE, 3.25
      2000-08-17, Close, 3.35
      2000-08-16, Close, 3.16
      2000-08-15, Close, 3.04
      2000-08-15, BUY CREATE, 3.04
      2000-08-14, Close, 3.06
      2000-08-11, Close, 3.10
      2000-08-10, Close, 3.09
      2000-08-09, Close, 3.09
      2000-08-08, Close, 3.04
      2000-08-07, Close, 3.12
      2000-08-04, Close, 3.08
      2000-08-03, Close, 3.12
      2000-08-02, Close, 3.07
      2000-08-01, Close, 3.21
      2000-07-31, Close, 3.31
      2000-07-28, Close, 3.14
      2000-07-27, Close, 3.38
      2000-07-26, Close, 3.26
      2000-07-25, Close, 3.26
      2000-07-24, Close, 3.17
      2000-07-21, Close, 3.48
      2000-07-20, Close, 3.59
      2000-07-19, Close, 3.43
      2000-07-18, Close, 3.72
      2000-07-17, Close, 3.79
      2000-07-14, Close, 3.75
      2000-07-13, Close, 3.68
      2000-07-13, BUY CREATE, 3.68
      2000-07-12, Close, 3.83
      2000-07-11, Close, 3.70
      2000-07-10, Close, 3.72
      2000-07-07, Close, 3.54
      2000-07-06, Close, 3.37
      2000-07-06, BUY CREATE, 3.37
      2000-07-05, Close, 3.36
      2000-07-05, BUY CREATE, 3.36
      2000-07-03, Close, 3.47
      2000-06-30, Close, 3.41
      2000-06-29, Close, 3.33
      2000-06-29, BUY CREATE, 3.33
      2000-06-28, Close, 3.54
      2000-06-27, Close, 3.37
      2000-06-26, Close, 3.52
      2000-06-23, Close, 3.36
      2000-06-22, Close, 3.50
      2000-06-21, Close, 3.62
      2000-06-20, Close, 3.29
      2000-06-19, Close, 3.14
      2000-06-19, BUY CREATE, 3.14
      2000-06-16, Close, 2.97
      2000-06-16, BUY CREATE, 2.97
      2000-06-15, Close, 3.00
      2000-06-14, Close, 2.94
      2000-06-13, Close, 3.07
      2000-06-12, Close, 2.97
      2000-06-09, Close, 3.11
      2000-06-08, Close, 3.08
      2000-06-07, Close, 3.14
      2000-06-06, Close, 3.02
      2000-06-05, Close, 2.97
      2000-06-05, BUY CREATE, 2.97
      2000-06-02, Close, 3.01
      2000-06-01, Close, 2.90
      2000-05-31, Close, 2.73
      2000-05-31, BUY CREATE, 2.73
      2000-05-30, Close, 2.85
      2000-05-26, Close, 2.81
      2000-05-25, Close, 2.84
      2000-05-24, Close, 2.85
      2000-05-23, Close, 2.79
      2000-05-22, Close, 2.93
      2000-05-19, Close, 3.06
      2000-05-18, Close, 3.28
      2000-05-17, Close, 3.30
      2000-05-16, Close, 3.44
      2000-05-15, Close, 3.29
      2000-05-12, Close, 3.50
      2000-05-11, Close, 3.34
      2000-05-10, Close, 3.23
      2000-05-10, BUY CREATE, 3.23
      2000-05-09, Close, 3.43
      2000-05-08, Close, 3.58
      2000-05-05, Close, 3.68
      2000-05-04, Close, 3.60
      2000-05-03, Close, 3.74
      2000-05-02, Close, 3.83
      2000-05-01, Close, 4.04
      2000-04-28, Close, 4.04
      2000-04-27, Close, 4.12
      2000-04-26, Close, 3.95
      2000-04-25, Close, 4.17
      2000-04-24, Close, 3.92
      2000-04-20, Close, 3.87
      2000-04-20, BUY CREATE, 3.87
      2000-04-19, Close, 3.94
      2000-04-18, Close, 4.13
      2000-04-17, Close, 4.03
      2000-04-14, Close, 3.64
      2000-04-14, BUY CREATE, 3.64
      2000-04-13, Close, 3.70
      2000-04-12, Close, 3.55
      2000-04-11, Close, 3.89
      2000-04-10, Close, 4.07
      2000-04-07, Close, 4.29
      2000-04-06, Close, 4.07
      2000-04-05, Close, 4.24
      2000-04-04, Close, 4.14
      2000-04-03, Close, 4.34
      2000-03-31, Close, 4.42
      2000-03-30, Close, 4.09
      2000-03-29, Close, 4.42
      2000-03-28, Close, 4.53
      2000-03-27, Close, 4.54
      2000-03-24, Close, 4.51
      2000-03-23, Close, 4.60
      2000-03-22, Close, 4.69
      2000-03-21, Close, 4.39
      2000-03-20, Close, 4.00
      2000-03-20, BUY CREATE, 4.00
      2000-03-17, Close, 4.07
      2000-03-16, Close, 3.95
      2000-03-15, Close, 3.78
      2000-03-15, BUY CREATE, 3.78
      2000-03-14, Close, 3.72
      2000-03-14, BUY CREATE, 3.72
      2000-03-13, Close, 3.95
      2000-03-10, Close, 4.09
      2000-03-09, Close, 3.98
      2000-03-08, Close, 3.97
      2000-03-08, BUY CREATE, 3.97
      2000-03-07, Close, 4.00
      2000-03-06, Close, 4.09
      2000-03-03, Close, 4.16
      2000-03-02, Close, 3.97
      2000-03-01, Close, 4.24
      2000-02-29, Close, 3.73
      2000-02-28, Close, 3.68
      2000-02-28, BUY CREATE, 3.68
      2000-02-25, Close, 3.59
      2000-02-25, BUY CREATE, 3.59
      2000-02-24, Close, 3.75
      2000-02-23, Close, 3.78
      2000-02-22, Close, 3.70
      2000-02-18, Close, 3.62
      2000-02-18, BUY CREATE, 3.62
      2000-02-17, Close, 3.74
      2000-02-16, Close, 3.71
      2000-02-15, Close, 3.87
      2000-02-14, Close, 3.77
      2000-02-11, Close, 3.54
      2000-02-11, BUY CREATE, 3.54
      2000-02-10, Close, 3.69
      2000-02-09, Close, 3.66
      2000-02-08, Close, 3.74
      2000-02-07, Close, 3.71
      2000-02-04, Close, 3.51
      2000-02-04, BUY CREATE, 3.51
      2000-02-03, Close, 3.36
      2000-02-03, BUY CREATE, 3.36
      2000-02-02, Close, 3.21
      2000-02-02, BUY CREATE, 3.21
      2000-02-01, Close, 3.26
      2000-01-31, Close, 3.38
      2000-01-28, Close, 3.31
      2000-01-27, Close, 3.58
      2000-01-26, Close, 3.58
      2000-01-25, Close, 3.65
      2000-01-24, Close, 3.46
      2000-01-21, Close, 3.62
      2000-01-20, Close, 3.69
      2000-01-19, Close, 3.47
      2000-01-18, Close, 3.38
      2000-01-18, BUY CREATE, 3.38
      2000-01-14, Close, 3.27
      2000-01-14, BUY CREATE, 3.27
      2000-01-13, Close, 3.15
      2000-01-13, BUY CREATE, 3.15
      2000-01-12, Close, 2.84
      2000-01-12, BUY CREATE, 2.84
      2000-01-11, Close, 3.02
      2000-01-10, Close, 3.18
      2000-01-07, Close, 3.24
      2000-01-06, Close, 3.09
      2000-01-05, Close, 3.38
      2000-01-04, Close, 3.33
      2000-01-03, Close, 3.64
      Final Portfolio Value: 100000.00
      

      Why the cash is not reducing?

      posted in General Code/Help
      M
      mirono