금융공학/BackTrader 공부

BackTrader 1 : Concepts

냥냥펀치데스 2021. 12. 24. 01:19
728x90

Platform Concepts

Data Feeds - Passing them around

Strategy 안에 Data 를 어떻게 집어넣어야 할지 고민할 이유 없음

Lines

모든 object 는 lines object 로 통일되어진다

  • Lines declaration
    • indicator 개발시 lines 추가로 간단히 indicator 만들기 가능
      •  
      • class SimpleMovingAverage(Indicator): lines = ('sma',) ...
  • Indexing: 0 and -1
    • lines 라 불리우는 것들은 line series 이기 때문에 현재를 보고 싶다면 0 으로 해서 볼수 있다.
      • def next(self):
            if self.movav.lines.sma[0] > self.data.lines.close[0]:
                print('Simple Moving Average is greater than the closing price')
    • 전에 set points 로 indicating 을 한다면 -1 로 과거를 볼수 있다
  • Lines Coupling
    • 다른 데이터로 조합된 indicator 를 비교할때 line coupling 을 통해 해결할수 있다.
    • Ex)
      • A daily data feed has around 250 bars per year
      • A weekly data feed has 52 bars per year
      • class MyStrategy(bt.Strategy):
            params = dict(period=20)
        
            def __init__(self):
        
                # data0 is a daily data
                sma0 = btind.SMA(self.data0, period=15)  # 15 days sma
                # data1 is a weekly data
                sma1 = btind.SMA(self.data1, period=5)  # 5 weeks sma
        
                self.buysig = sma0 > sma1()
        
            def next(self):
                if self.buysig[0]:
                    print('daily sma is greater than weekly sma1')

Operating the platform

Line Iterators

    • The key to iteration, just like with regular Python iterators
      • The next method
        • It will be called for each iteration
        • Called when the minimum period for the line iterator has been met
      • But because they are not regular iterators, two additional methods exist:
        • prenext - Called before the minimum period for the line iterator` has been met.
        • nextstart - Called exactly ONCE when the minimum period for the line iterator` has been met.

Extra methods for Indicators

  • 스피드를 빠르게 하기 위해서 runonce 기능을 사용할수 있다.
  • runonce 는 data 에 직접 접근해서 한번에 한번에 처리한다
  • The defined methods
    • once(self, start, end)
    • preonce(self, start, end)
    • oncestart(self, start, end)

Minimum Period

  • sma = btind.SimpleMovingAverage(self.data, period=25)
  • “period=25” instantiated moving average would have its methods called as follows:
    • prenext  - 24 times
    • nextstart - 1 time (in turn calling next)
    • next -  n additional times until the data feed has been exhausted
  • sma1 = btind.SimpleMovingAverage(self.data, period=25)
    
    sma2 = btind.SimpleMovingAverage(sma1, period=20)
  • What now goes on:
    • The same as above for sma1
    • sma2 is receiving a data feed which has a minimum period of 25 which is our sma1 and therefore
    • The sma2 methods are called as indicated:
      • prenext the first 25 + 18 times for a total of 43 times
      • 25 times to let sma1 produce its 1st sensible value
      • 18 times to accumulate extra sma1 values
      • For a total of 19 values (1 after 25 calls and then 18 more)
      • nextstart then 1 time (in turn calling next)
      • next the n additional times until the data feed has been exhausted
    • The platform is calling next when the system has already processed 44 bars.

Data Feeds

The platform provides several data feeds:

  • Several CSV Format and a Generic CSV reader
  • Yahoo online fetcher
  • Support for receiving Pandas DataFrames and blaze objects
  • Live Data Feeds with Interacive Brokers, Visual Chart and Oanda

A Strategy (derived) class

There are 2 methods which at least need customization:

  • __init__
  • next

A Cerebro

a Cerebro instance is what brings everything together and execute the actions

 

Defaults are taking care of if nothing special is wished.

  • A default broker is created
  • No commission for the operations
  • Data Feeds will be preloaded
  • The default execution mode will be runonce (batch operation) which is the fasterCustom indicators do not need to implement the runonce functionality. Cerebro will simulate it, which means those non-runonce compatible indicators will run slower. But still most of the system will run in batch mode.
  • All indicators must support the runonce mode for full speed. The ones included in the platform do.