Backtrader Community

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

    rorymack

    @rorymack

    0
    Reputation
    485
    Profile views
    3
    Posts
    0
    Followers
    0
    Following
    Joined Last Online

    rorymack Unfollow Follow

    Latest posts made by rorymack

    • RE: AttributeError: 'NoneType' object has no attribute 'close'

      Maxim

      I am getting the same type of error running anything that tries to pick up a Yahoo feed.

      Traceback (most recent call last):
        File "btfd.py", line 233, in <module>
          runstrat()
        File "btfd.py", line 177, in runstrat
          cerebro.run(**eval('dict(' + args.cerebro + ')'))
        File "/Users/rorymackay/anaconda/envs/backtrader/lib/python2.7/site-packages/backtrader/cerebro.py", line 1070, in run
          runstrat = self.runstrategies(iterstrat)
        File "/Users/rorymackay/anaconda/envs/backtrader/lib/python2.7/site-packages/backtrader/cerebro.py", line 1146, in runstrategies
          data.preload()
        File "/Users/rorymackay/anaconda/envs/backtrader/lib/python2.7/site-packages/backtrader/feed.py", line 689, in preload
          self.f.close()
      AttributeError: 'NoneType' object has no attribute 'close'
      

      I assume it is for the same reason, earlier I had an error message that pointed directly to the url you showed.

      However using pandas and accessing the same data (perhaps not in the same way ) I do get a response.
      I did have to directly install the pandas-Datareader from pip as the anaconda one was not working. (this on a linux box)
      Conda appears to have fixed this as the "latest" version is working on a mac...

      pandas-datareader         0.3.0.post0              py27_0    conda-forge
      
      

      This is the pip version that is now working on the linux box.

      Not sure if that sheds any light on the issue.

      this gets the data to a csv ...

      
      
      import datetime
      import numpy as np
      import pandas as pd
      #import pandas_datareader as pdr
      import pandas_datareader.data as web
      import os 
      import sys  # To find out the script name (in argv[0])
      start = datetime.datetime(2000, 1, 1)
      end = datetime.datetime(2015, 5, 9)
      
      data = web.DataReader('ORCL', 'google',start,end)
      
      os.chdir('/Users/rorymackay/Documents/backtrader_test')
      modpath = os.path.dirname(os.path.abspath(sys.argv[0]))
      datapath = os.path.join(modpath,'backtrader_test/datas/orcl.csv')
      
      data.to_csv('orcl.csv')
      
      posted in General Code/Help
      R
      rorymack
    • RE: Simple channel indicator

      Hi @ThatBlokeDave

      Thank you for your reply. I am blundering my way around here and appreciate you taking time to help me out.

      1. Interesting that is was the data feed, I hacked this attempt from the SMACrossover example.
        Looking at the DataFeeds reference https://www.backtrader.com/docu/dataautoref.html
        It appears that it has all the lines needed.
        Is it generally better to create csv's of the data ?
        I notice quickstart tutorial does that.

      I ran it again with your modifications and the yahoo data feed and it worked! Not sure if it is a version thing.

      1. AhHa ! Thank you my python fu is not what it should be...
      2. Ok, again poor Python skills on my side.
      3. Not sure if I follow you here, I had in mind that I would set up the indicator and then set up the signal in
       def next(self):
      

      with this logic :
      If previous period channel max is < current period high buy
      and
      If previous period channel low is > current period low sell
      Which would generate the long and short signals.

      I had it in my head that the indicator is separate from the signal, Is that conceptually correct?

      1. Again thanks bad python on my part.

      The solution

      1. Yes I do !

      Thank you for the solution, I will play around some more with it to get to grips with the platform.
      As an aside, how do you get the indicator to plot as an overlay on the data? As opposed to in a separate plot.

      Thanks again.
      RM

      posted in Indicators/Strategies/Analyzers
      R
      rorymack
    • Simple channel indicator

      Hi
      I have tried to write a simple channel indicator. The indicator has 2 lines. The maximum of the last 20 Highs and the minimum of the last 20 lows.
      Here is my attempt.

      class channel20(bt.Indicator):
          lines = ('maxi','mini',)
          params = (('period', 20),)
          
          def __init__(self):
              self.l.maxi= math.max(self.data.high(p.period))
              self.l.mini= math.min(self.data.low(p.period))
      

      putting it into a simple strategy to see if it works, I tried this:

      from datetime import datetime
      import backtrader as bt
      
      
      class channel20(bt.Indicator):
          lines = ('maxi','mini',)
          params = (('period', 20),)
          
          def __init__(self):
              self.l.maxi= math.max(self.data.high(p.period))
              self.l.mini= math.min(self.data.low(p.period))
      
      class test(bt.SignalStrategy):
          def __init__(self):
              channel = bt.indicator.channel20
      
      cerebro = bt.Cerebro()
      cerebro.addstrategy(test)
      
      data0 = bt.feeds.YahooFinanceData(dataname='YHOO', fromdate=datetime(2011, 1, 1),
                                        todate=datetime(2012, 12, 31))
      cerebro.adddata(data0)
      
      cerebro.run()
      cerebro.plot()
      

      to get the following error.

      ---------------------------------------------------------------------------
      AttributeError                            Traceback (most recent call last)
      <ipython-input-22-d93cd4afba9e> in <module>()
           23 cerebro.adddata(data0)
           24 
      ---> 25 cerebro.run()
           26 cerebro.plot()
      
      /home/rory/anaconda2/envs/backtrader/lib/python2.7/site-packages/backtrader/cerebro.pyc in run(self, **kwargs)
         1068             # let's skip process "spawning"
         1069             for iterstrat in iterstrats:
      -> 1070                 runstrat = self.runstrategies(iterstrat)
         1071                 self.runstrats.append(runstrat)
         1072         else:
      
      /home/rory/anaconda2/envs/backtrader/lib/python2.7/site-packages/backtrader/cerebro.pyc in runstrategies(self, iterstrat, predata)
         1144                 data._start()
         1145                 if self._dopreload:
      -> 1146                     data.preload()
         1147 
         1148         for stratcls, sargs, skwargs in iterstrat:
      
      /home/rory/anaconda2/envs/backtrader/lib/python2.7/site-packages/backtrader/feed.pyc in preload(self)
          687 
          688         # preloaded - no need to keep the object around - breaks multip in 3.x
      --> 689         self.f.close()
          690         self.f = None
          691 
      
      AttributeError: 'NoneType' object has no attribute 'close'
      
      

      I was hoping to simply get a printout of the indicator.
      I wonder if anyone could shed some light on what I have done wrong.
      Thanks

      RM

      posted in Indicators/Strategies/Analyzers
      R
      rorymack