Backtrader中文筆記之Operating the platform(操作平臺)。
Line Iterators
行 迭代器
To engage into operations, the plaftorm uses the notion of line iterators. They have been loosely modeled after Python’s iterators but have actually nothing to do with them.
為了進行操作,plaftorm使用了行迭代器的概念。它們鬆散地模仿了Python的迭代器,但實際上與它們沒有任何關係。
Strategies and Indicators are line iterators
策略與指標都是線迭代器
The line iterator concept tries to describe the following:
對行迭代器嘗試以下方式解釋:
A Line Iterator kicks slave line iterators telling them to iterate
一個行迭代器踢了行迭代器成員,告訴它們進行迭代
A Line Iterator then iterates over its own declared named lines setting values
然後行迭代器迭代自己宣告的命名行,並設定值
The key to iteration, just like with regular Python iterators, is
就像Python的迭代器一樣,迭代的關鍵是:
The next
method
next方法
It will be called for each iteration. The datas
array which the line iterator has and serve as basis for logic/calculations will have already been moved to the next index by the platform (barring data replay)
每次迭代都會呼叫它。迭代器作為邏輯/計算基礎,有能力移動資料陣列的下一個索引(禁止資料重放)
Called when the minimum period for the line iterator has been met. A bit more on this below.
當滿足行迭代器的最小週期時呼叫。下面還有一點。
But because they are not regular iterators, two additional methods exist:
但是因為它們不是常規的迭代器,所以存在兩個額外的方法:
prenext
Called before the minimum period for the line iterator` has been met
在滿足行迭代器的最小週期之前呼叫。(理解為在next的過程中,不滿足條件沒有輸出的,比如15日均線,前面沒有輸出的就在這裡輸出)
nextstart
Called exactly ONCE when the minimum period for the line iterator` has been met.
當滿足行迭代器的最小週期時,只調用一次。
The default behavior is to forward the call to next
, but can of course be overriden if needed.
預設的行為是將呼叫轉發到next,但是如果需要,當然可以被覆蓋。
Extra methods for Indicators
策略的額外方法
To speed up operations, Indicators support a batch operation mode which has been termed as runonce. It is not strictly needed (a next
method suffices) but it greatly reduces time.
為了加快操作速度,指標支援稱為runonce的批處理操作模式。它不是嚴格需要的(next方法就足夠了),但是它大大縮短了時間。
The runonce methods rules void the get/set point with index 0 and relies on direct access to the underlying arrays holding the data and being passed the right indices for each state.
runonce方法規定使用索引0來grt/set點,並依賴於對儲存資料的底層陣列的直接訪問,併為每個狀態傳遞正確的索引。
The defined methods follow the naming of the next family:
定義的方法遵循該規則的命名:
-
once(self, start, end)
Called when the minimum period has been met. The internal array must be processed between start and end which are zero based from the start of the internal array
- 當滿足最小週期時呼叫。內部陣列必須在開始和結束之間進行處理,從內部陣列的開始到結束都是零
-
preonce(self, start, end)
Called before the minimum period has been met.
- 在滿足最小期限之前呼叫。
-
oncestart(self, start, end)
-
Called exactlyONCEwhen the minimum period has been met.
The default behavior is to forward the call to
once
, but can of course be overriden if needed. -
恰好在滿足最小週期時呼叫一次。
預設的行為是將呼叫轉發到once,但是如果需要,當然可以被覆蓋。
Minimum Period
最小週期
A picture is worth a thousand words and in this case possibly an example too. A SimpleMovingAverage is capable of explaining it:
一張圖片勝過千言萬語,在這種情況下,也可能是一個例子。一個SimpleMovingAverage能夠解釋它:
class SimpleMovingAverage(Indicator): lines = ('sma',) params = dict(period=20) def __init__(self): ... # Not relevant for the explanation def prenext(self): print('prenext:: current period:', len(self)) def nextstart(self): print('nextstart:: current period:', len(self)) # emulate default behavior ... call next self.next() def next(self): print('next:: current period:', len(self))
And the instantiation could look like:
例項如下所示
sma = btind.SimpleMovingAverage(self.data, period=25)
Briefly explained:
簡略說明
-
Assuming the data passed to the moving average is a standard data feed its default period is
1
that is: the data feed produces a bar with no initial delay. -
Then the “period=25” instantiated moving average would have its methods called as follows:
-
prenext
24 times -
nextstart
1 time (in turn callingnext
) -
next
n additional times until the data feed has been exhausted
-
Let’s go for the killer indicator: a SimpleMovingAverage over another SimpleMovingAverage. The instantiation could look like:
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 oursma1
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 callingnext
) -
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.
The minimum period has been automatically adjusted to the incoming data.
Strategies and Indicators adhere to this behavior:
- Only when the automatically calculated minimum period has been reached will
next
be called (barring the initial hook call tonextstart
)
Note
The same rules apply to preonce
, oncestart
and once
for the runonce batch operation mode
Note
The minimum period behavior can be manipulated although it’s not recommended. Should it be wished used the setminperiod(minperiod)
method in either Strategies or Indicators
Up and Running
Getting up and running involves at least 3 Lines objects:
-
A Data feed
-
A Strategy (actually a class derived from Strategy)
-
A Cerebro (brain in Spanish)