PyAlgoTrade 0.20 中文文档(一)(2)https://developer.aliyun.com/article/1524141
并将此代码保存到不同的文件中:
from pyalgotrade import plotter from pyalgotrade.barfeed import quandlfeed from pyalgotrade.stratanalyzer import returns import sma_crossover # Load the bar feed from the CSV file feed = quandlfeed.Feed() feed.addBarsFromCSV("orcl", "WIKI-ORCL-2000-quandl.csv") # Evaluate the strategy with the feed's bars. myStrategy = sma_crossover.SMACrossOver(feed, "orcl", 20) # Attach a returns analyzers to the strategy. returnsAnalyzer = returns.Returns() myStrategy.attachAnalyzer(returnsAnalyzer) # Attach the plotter to the strategy. plt = plotter.StrategyPlotter(myStrategy) # Include the SMA in the instrument's subplot to get it displayed along with the closing prices. plt.getInstrumentSubplot("orcl").addDataSeries("SMA", myStrategy.getSMA()) # Plot the simple returns on each bar. plt.getOrCreateSubplot("returns").addDataSeries("Simple returns", returnsAnalyzer.getReturns()) # Run the strategy. myStrategy.run() myStrategy.info("Final portfolio value: $%.2f" % myStrategy.getResult()) # Plot the strategy. plt.plot()
该代码正在做 3 件事:
- 从 CSV 文件加载源数据。
- 使用由 feed 提供的柱和附加了 StrategyPlotter 的策略运行。
- 绘制策略。
这就是绘图的样子:
希望您喜欢这个快速介绍。我建议您在这里下载 PyAlgoTrade:gbeced.github.io/pyalgotrade/downloads/index.html,并开始编写您自己的策略。
您也可以在示例策略 部分找到更多示例。
目录
- 教程
- 交易
- 优化
- 绘图
代码文档
目录:
- K 线 – 工具价格
- 数据序列 – 基本数据序列类
- 数据源 – 基本数据源
- CSV 支持
- CSV 支持示例
- K 线数据源 – K 线提供者
- CSV
- Yahoo! Finance
- Google Finance
- Quandl
- Ninja Trader
- 技术指标 – 技术指标
- 示例
- 移动平均线
- 动量指标
- 其他指标
- 经纪人 – 订单管理类
- 基础模块和类
- 回测模块和类
- 策略 – 基本策略类
- 策略
- 持仓
- 策略分析器 – 策略分析器
- 收益率
- 夏普比率
- 回撤
- 交易
- 示例
- 绘图器 – 策略绘图器
- 优化器 – 并行优化器
- 市场交易时段
条形 – 工具价格
class pyalgotrade.bar.``Frequency
基类: object
枚举类似于条形频率。有效值为:
- Frequency.TRADE: 每个条形代表一次交易。
- Frequency.SECOND: 每个条形总结了一秒的交易活动。
- Frequency.MINUTE: 每个条形总结了一分钟的交易活动。
- Frequency.HOUR: 每个条形总结了一小时的交易活动。
- Frequency.DAY: 每个条形总结了一天的交易活动。
- Frequency.WEEK: 每个条形总结了一周的交易活动。
- Frequency.MONTH: 每个条形总结了一月的交易活动。
class pyalgotrade.bar.``Bar
基类: object
条形是给定期间内安全性交易活动的摘要。
注意
这是一个基类,不应直接使用。
getDateTime()
返回datetime.datetime。
getOpen(adjusted=False)
返回开盘价。
getHigh(adjusted=False)
返回最高价格。
getLow(adjusted=False)
返回最低价格。
getClose(adjusted=False)
返回收盘价。
getVolume()
返回成交量。
getAdjClose()
返回调整后的收盘价。
getFrequency()
条形的周期。
getTypicalPrice()
返回典型价格。
getPrice()
返回收盘或调整后的收盘价。
class pyalgotrade.bar.``Bars(barDict)
基类: object
一组Bar对象。
| 参数: | barDict (map.) – 工具到Bar对象的映射。 |
注意
所有条形必须具有相同的日期时间。
__getitem__(instrument)
返回给定工具的pyalgotrade.bar.Bar。如果未找到该工具,则会引发异常。
__contains__(instrument)
如果给定工具的pyalgotrade.bar.Bar可用,则返回 True。
getInstruments()
返回工具符号。
getDateTime()
返回此条形的datetime.datetime。
getBar(instrument)
返回给定工具的pyalgotrade.bar.Bar或者如果未找到该工具则返回 None。
dataseries – 基本数据系列类
原文:
gbeced.github.io/pyalgotrade/docs/v0.20/html/dataseries.html
数据系列是用于管理时间序列数据的抽象。
class pyalgotrade.dataseries.``DataSeries
基类:object
数据系列的基类。
注意
这是一个基类,不应直接使用。
__getitem__(key)
返回给定位置/切片的值。如果位置无效,则引发 IndexError,如果键类型无效,则引发 TypeError。
__len__()
返回数据系列中的元素数量。
getDateTimes()
返回与每个值关联的 datetime.datetime 列表。
class pyalgotrade.dataseries.``SequenceDataSeries(maxLen=None)
基类:pyalgotrade.dataseries.DataSeries
一个在内存中按顺序保存值的 DataSeries。
| 参数: | maxLen (int.) – 要保存的最大值数量。一旦有界长度已满,当添加新项时,将从相反端丢弃相应数量的项。如果为 None,则使用 dataseries.DEFAULT_MAX_LEN。 |
append(value)
追加一个值。
appendWithDateTime(dateTime, value)
追加一个带有关联日期时间的值。
注意
如果 dateTime 不为 None,则必须大于最后一个。
getMaxLen()
返回要保存的最大值数量。
setMaxLen(maxLen)
设置要保存的最大值数量,并在必要时调整大小。
pyalgotrade.dataseries.aligned.``datetime_aligned(ds1, ds2, maxLen=None)
返回两个 DataSeries,其中仅包含两个 DataSeries 中都存在的日期时间的值。
| 参数: |
- ds1 (
DataSeries.) – 一个 DataSeries 实例。 - ds2 (
DataSeries.) – 一个 DataSeries 实例。 - maxLen (int.) – 返回的
DataSeries可以容纳的最大值数量。一旦有界长度已满,当添加新项时,将从相反端丢弃相应数量的项。如果为 None,则使用 dataseries.DEFAULT_MAX_LEN。
|
class pyalgotrade.dataseries.bards.``BarDataSeries(maxLen=None)
基类:pyalgotrade.dataseries.SequenceDataSeries
一个由 pyalgotrade.bar.Bar 实例组成的 DataSeries。
| 参数: | maxLen (int.) – 要保存的最大值数量。一旦有界长度已满,当添加新项时,将从相反端丢弃相应数量的项。如果为 None,则使用 dataseries.DEFAULT_MAX_LEN。 |
getAdjCloseDataSeries()
返回一个具有调整后的收盘价格的 pyalgotrade.dataseries.DataSeries。
getCloseDataSeries()
返回一个pyalgotrade.dataseries.DataSeries,其中包含收盘价格。
getExtraDataSeries(name)
返回一个pyalgotrade.dataseries.DataSeries,用于额外的列。
getHighDataSeries()
返回一个pyalgotrade.dataseries.DataSeries,其中包含最高价格。
getLowDataSeries()
返回一个pyalgotrade.dataseries.DataSeries,其中包含最低价格。
getOpenDataSeries()
返回一个pyalgotrade.dataseries.DataSeries,其中包含开盘价格。
getPriceDataSeries()
返回一个pyalgotrade.dataseries.DataSeries,其中包含收盘或调整后的收盘价格。
getVolumeDataSeries()
返回一个pyalgotrade.dataseries.DataSeries,其中包含交易量。
class pyalgotrade.dataseries.resampled. ResampledBarDataSeries(dataSeries,frequency,maxLen=None)
基础:pyalgotrade.dataseries.bards.BarDataSeries,pyalgotrade.dataseries.resampled.DSResampler
一个 BarDataSeries,将建立在另一个更高频率的 BarDataSeries 之上。随着新值被推送到被重新采样的数据系列中,重新采样将会发生。
| 参数: |
- dataSeries(
pyalgotrade.dataseries.bards.BarDataSeries) - 正在重新采样的 DataSeries 实例。 - frequency - 以秒为单位的分组频率。必须大于 0。
- maxLen(int.) - 最大保存值的数量。一旦有限长度已满,当添加新项目时,将从另一端丢弃相应数量的项目。
|
注:
- 支持的重新采样频率包括:
- 小于 bar.Frequency.DAY
- bar.Frequency.DAY
- bar.Frequency.MONTH
checkNow(dateTime)
强制进行重新采样检查。根据重新采样频率和当前日期时间,可能会生成一个新值。
| 参数: | dateTime(datetime.datetime) - 当前日期时间。 |
数据源 - 基本数据源
数据源是提供抽象的时间序列数据。当这些数据源包含在事件分派循环中时,它们会在新数据可用时发出事件。数据源还负责更新与数据源提供的每个数据相关联的pyalgotrade.dataseries.DataSeries。
该软件包具有基本数据源。有关 K 线数据源,请参阅 barfeed – K 线数据源 部分。
class pyalgotrade.feed.``BaseFeed(maxLen)
基类:pyalgotrade.observer.Subject
数据源的基类。
| 参数: | maxLen (int.) – 每个pyalgotrade.dataseries.DataSeries将保留的最大值数量。一旦有界长度已满,当添加新项目时,相应数量的项目将从另一端丢弃。 |
注
这是一个基类,不应直接使用。
__contains__(key)
如果给定键的pyalgotrade.dataseries.DataSeries可用,则返回 True。
__getitem__(key)
返回给定键的pyalgotrade.dataseries.DataSeries。
getNewValuesEvent()
返回在新值可用时将发出的事件。要订阅,您需要传入一个可调用对象,该对象接收两个参数:
- 一个
datetime.datetime实例。 - 新值。
PyAlgoTrade 0.20 中文文档(一)(4)https://developer.aliyun.com/article/1524143