本教程的目标是快速介绍PyAlgoTrade。PyAlgoTrade的目标是帮助您实现股票交易策略。假设您有一个交易策略的想法,并且您希望使用历史数据进行评估,并查看其行为方式,那么PyAlgoTrade应该允许您以最小的努力来做到这一点。
本教程是在UNIX环境中开发的,但将其适应Windows环境的步骤应该很简单。
PyAlgoTrade有6个主要组件:
- 策略(Strategies)
- 数据集(Feeds)
- 券商(Brokers)
- DataSeries
- 技术指标
- 优化
策略
这些是您定义的实现交易逻辑的类。何时购买,何时出售等
数据集
这些是提供抽象的数据。例如,您将使用CSV提要,从CSV(逗号分隔值)格式化的文件中加载条来将数据提供给策略。饲料不限于酒吧。例如,有一个Twitter Feed可以将Twitter事件纳入交易决策。
券商
经纪人负责执行订单。
DataSeries
数据系列是用于管理时间序列数据的抽象。
技术指标
这些是用于在DataSeries之上进行计算的一组过滤器。例如SMA(简单移动平均),RSI(相对强度指数)等。这些过滤器被建模为DataSeries装饰器。
优化
这些是一组允许您在不同计算机或在同一台计算机中运行的不同进程之间进行回测的方法,或两者的组合。它们使水平缩放变得容易。
说完这一切,我们需要测试我们的策略的第一件事是一些数据。让我们使用Oracle 2000年的股票价格,我们将使用以下命令下载:
from pyalgotrade.tools import yahoofinance;
yahoofinance.download_daily_bars('orcl',2000,'orcl-2000.csv')
dw = pd.read_csv("orcl-2000.csv")
dw
Date Open High Low Close Volume Adj Close
0 2000-12-29 30.875000 31.312500 28.6875 29.062500 31702200 26.469546
1 2000-12-28 30.562500 31.625000 30.3750 31.062500 25053600 28.291106
2 2000-12-27 30.375000 31.062500 29.3750 30.687500 26437500 27.949563
3 2000-12-26 31.500000 32.187500 30.0000 30.937500 20589500 28.177258
4 2000-12-22 30.375000 31.984301 30.0000 31.875000 35568200 29.031115
5 2000-12-21 27.812500 30.250000 27.3125 29.500000 46719700 26.868012
6 2000-12-20 28.062500 29.812500 27.5000 28.500000 54440500 25.957232
7 2000-12-19 31.812500 33.125000 30.1250 30.625000 58653700 27.892640
8 2000-12-18 30.000000 32.437500 29.9375 32.000000 61640100 29.144962
9 2000-12-15 29.437500 30.078100 28.1875 28.562500 120004000 26.014156
10 2000-12-14 29.250000 29.937500 27.2500 27.500000 45894400 25.046452
11 2000-12-13 31.937500 32.000000 28.2500 28.375000 37933600 25.843385
12 2000-12-12 31.875000 32.500000 30.4062 30.750000 26481200 28.006487
13 2000-12-11 30.500000 32.250000 30.0000 31.937500 50279700 29.088039
14 2000-12-08 30.062500 30.625000 29.2500 30.062500 40052600 27.380326
15 2000-12-07 29.625000 29.937500 28.1250 28.312500 41088300 25.786461
16 2000-12-06 31.187500 31.625000 29.3125 30.187500 42125600 27.494173
17 2000-12-05 29.437500 31.500000 28.8750 31.500000 59754700 28.689572
18 2000-12-04 26.250000 28.875000 26.1875 28.187500 40710400 25.672613
19 2000-12-01 26.375000 27.875000 25.5000 26.437500 48663500 24.078748
20 2000-11-30 21.750000 27.625000 21.5000 26.500000 84386200 24.135672
21 2000-11-29 23.187500 23.625000 21.8125 22.875000 75409600 20.834094
22 2000-11-28 23.500000 23.812500 22.2500 22.656200 43075300 20.634816
23 2000-11-27 25.437500 25.812500 22.8750 23.125000 45665200 21.061789
24 2000-11-24 23.312500 24.250000 23.1250 24.125000 22443900 21.972569
25 2000-11-22 23.625000 24.062500 22.0625 22.312500 53315300 20.321780
26 2000-11-21 24.812500 25.625000 23.5000 23.875000 58647400 21.744874
27 2000-11-20 24.312500 25.875000 24.0000 24.750000 89778400 22.541807
28 2000-11-17 26.937500 29.250000 25.2500 28.812500 59636000 26.241851
29 2000-11-16 28.750000 29.812500 27.2500 27.375000 37986600 24.932604
... ... ... ... ... ... ... ...
222 2000-02-14 60.875000 62.250000 58.6250 62.187500 37599800 28.319568
223 2000-02-11 62.500000 64.750000 58.7500 59.687500 55774000 27.181093
224 2000-02-10 60.000000 62.625000 58.0000 62.312500 45288600 28.376492
225 2000-02-09 60.062500 61.312500 58.8125 59.937500 52471600 27.294940
226 2000-02-08 60.750000 61.437500 59.0000 59.562500 55718000 27.124169
227 2000-02-07 59.312500 60.000000 58.8750 59.937500 44691200 27.294940
228 2000-02-04 57.625000 58.250000 56.8125 57.812500 40916000 26.327236
229 2000-02-03 55.375000 57.000000 54.2500 56.687500 55533200 25.814923
230 2000-02-02 54.937500 56.000000 54.0000 54.312500 63933000 24.733371
231 2000-02-01 51.250000 54.312500 50.0000 54.000000 57105600 24.591062
232 2000-01-31 47.937500 50.125000 47.0625 49.953098 68148000 22.748143
233 2000-01-28 51.500000 51.937500 46.6250 47.375000 86394000 21.574103
234 2000-01-27 55.812500 56.687500 50.0000 51.812500 61054000 23.594896
235 2000-01-26 56.750000 58.937500 55.0000 55.062500 47569200 25.074914
236 2000-01-25 55.062500 57.500000 54.8750 56.437500 53059200 25.701075
237 2000-01-24 60.250000 60.375000 54.0000 54.187500 50022400 24.676448
238 2000-01-21 61.500000 61.500000 59.0000 59.687500 50891000 27.181093
239 2000-01-20 59.000000 60.250000 58.1250 59.250000 54526800 26.981860
240 2000-01-19 56.125000 58.250000 54.0000 57.125000 49198400 26.014156
241 2000-01-18 107.875000 114.500000 105.6250 111.250000 66780000 25.331071
242 2000-01-14 109.000000 111.375000 104.7500 106.812500 57078000 24.320674
243 2000-01-13 108.500000 109.875000 103.5000 105.062500 55779200 23.922208
244 2000-01-12 112.250000 112.250000 103.6875 105.625000 83443600 24.050286
245 2000-01-11 112.625000 114.750000 109.5000 112.375000 86585200 25.587228
246 2000-01-10 108.000000 116.000000 105.5000 115.750000 91518000 26.355698
247 2000-01-07 95.000000 103.500000 93.5625 103.375000 91755600 23.537972
248 2000-01-06 100.156197 105.000000 94.6875 96.000000 109880000 21.858722
249 2000-01-05 101.625000 106.375000 96.0000 102.000000 166054000 23.224892
250 2000-01-04 115.500000 118.625000 105.0000 107.687500 116824800 24.519907
251 2000-01-03 124.625000 125.187500 111.6250 118.125000 98114800 26.896474