没有合适的资源?快使用搜索试试~ 我知道了~
backtrader参考手册.pdf

温馨提示
一个功能丰富的Python测试和交易框架。backtrader能够让策略研究员专注于编写可重用的交易策略、指标和分析器,而不是花时间构建基础设施。
资源推荐
资源详情
资源评论











1 Installation
Requirements and versions
backtrader is self-contained with no external dependencies (except if you want to plot)
Basic requirements are:
Python 2.7
Python 3.2/3.3/3.4/3.5
pypy/pypy3
Additional requirements if plotting is wished:
Matplotlib >= 1.4.1
It may work with previous versions, but this the one used for development
Note
At the time of writing Matplotlib is not supported under pypy/pypy3
Python 2.x/3.x compatibility
Development takes place under Python 2.7 and sometimes under 3.4. Tests are run locally with both
versions.
Compatibility with 3.2/3.3/3.5 and pypy/pyp3 is checked with continuous integration under Travis
Install from pypi
For example using pip:
pip install backtrader
easy_install with the same syntax can also be applied
Install from pypi (including matplotlib )
Use this if plotting capabilities are wished:

pip install backtrader[plotting]
This pulls in matplotlib which will in turn pull in other dependencies.
Again you may prefer (or only have access to ...) easy_install
Install from source
First downloading a release or the latest tarball from the github site:
https://github.com/mementum/backtrader
And after unpacking run the command:
python setup.py install
Run from source in your project
Download a release or the latest tarball from the github site:
https://github.com/mementum/backtrader
And then copy the backtrader package directory to your own project. Under a Unix-like OS for example:
Again tar xzf backgrader.tgz
cd backtrader
cp -r backtrader project_directory
Remember that you would then need to manually install matplotlib for plotting.
2 Quickstart
Note
The data files used in the quickstart guide are updated from time to time, which means that the
adjusted close changes and with it the close (and the other components). That means that the actual
output may be different to what was put in the documentation at the time of writing.
Using the platform
Let’s run through a series of examples (from almost an empty one to a fully fledged strategy) but not
without before roughly explaining 2 basic concepts when working with backtrader
1. Lines

Data Feeds, Indicators and Strategies have lines.
A line is a succession of points that when joined together form this line. When talking about the
markets, a Data Feed has usually the following set of points per day:
Open, High, Low, Close, Volume, OpenInterest
The series of “Open”s along time is a Line. And therefore a Data Feed has usually 6 lines.
If we also consider “DateTime” (which is the actual reference for a single point), we could count 7
lines.
2. Index 0 Approach
When accessing the values in a line, the current value is accessed with index: 0
And the “last” output value is accessed with -1. This in line with Python conventions for iterables
(and a line can be iterated and is therefore an iterable) where index -1 is used to access the “last” item of
the iterable/array.
In our case is the last output value what’s getting accessed.
As such and being index 0 right after -1 , it is used to access the current moment in line.
With that in mind and if we imagine a Strategy featuring a Simple Moving average created during
initialization:
self.sma = SimpleMovingAverage(.....)
The easiest and simplest way to access the current value of this moving average:
av = self.sma[0]
There is no need to know how many bars/minutes/days/months have been processed, because “0”
uniquely identifies the current instant.

Following pythonic tradition, the “last” output value is accessed using -1 :
previous_value = self.sma[-1]
Of course earlier output values can be accessed with -2, -3, ...
From 0 to 100: the samples
Basic Setup
Let’s get running.
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import backtrader as bt
if __name__ == '__main__':
cerebro = bt.Cerebro()
print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
cerebro.run()
print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
After the execution the output is:
Starting Portfolio Value: 10000.00
Final Portfolio Value: 10000.00
In this example:
backtrader was imported
The Cerebro engine was instantiated
The resulting cerebro instance was told to run (loop over data)
And the resulting outcome was printed out
Although it doesn’t seem much, let’s point out something explicitly shown:
The Cerebro engine has created a broker instance in the background
The instance already has some cash to start with
This behind the scenes broker instantiation is a constant trait in the platform to simplify the life of the
user. If no broker is set by the user, a default one is put in place.

And 10K monetary units is a usual value with some brokers to begin with.
Setting the Cash
In the world of finance, for sure only “losers” start with 10k. Let’s change the cash and run the example
again.
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import backtrader as bt
if __name__ == '__main__':
cerebro = bt.Cerebro()
cerebro.broker.setcash(100000.0)
print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
cerebro.run()
print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
After the execution the output is:
Starting Portfolio Value: 1000000.00
Final Portfolio Value: 1000000.00
Mission accomplished. Let’s move to tempestuous waters.
Adding a Data Feed
Having cash is fun, but the purpose behind all this is to let an automated strategy multiply the cash
without moving a finger by operating on an asset which we see as a Data Feed
Ergo ... No Data Feed - > No Fun. Let’s add one to the ever growing example.
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
if __name__ == '__main__':
# Create a cerebro entity
cerebro = bt.Cerebro()
# Datas are in a subfolder of the samples. Need to find where the script is
# because it could have been called from anywhere
剩余752页未读,继续阅读

zhongguodachuan
- 粉丝: 12
- 资源: 52
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


会员权益专享
安全验证
文档复制为VIP权益,开通VIP直接复制

- 1
- 2
前往页