PyalgoTrade 打印收盘价(二)

本文介绍了一种使用Python实现的简单策略,该策略通过加载Yahoo Finance的CSV数据并利用PyAlgoTrade库来打印每日收盘价格。策略的核心是定义了一个名为MyStrategy的类,其onBars方法用于获取并打印每根K线的收盘价。

让我们从一个简单的策略开始,就是在打印收盘价格的过程中:

from pyalgotrade import strategy
from pyalgotrade.barfeed import yahoofeed


class MyStrategy(strategy.BacktestingStrategy):
    def __init__(self, feed, instrument):
        super(MyStrategy, self).__init__(feed)
        self.__instrument = instrument

    def onBars(self, bars):
        bar = bars[self.__instrument]
        self.info(bar.getClose())

# Load the yahoo feed from the CSV file
feed = yahoofeed.Feed()
feed.addBarsFromCSV("orcl", "orcl-2000.csv")

# Evaluate the strategy with the feed's bars.
myStrategy = MyStrategy(feed, "orcl")
myStrategy.run()

代码做三件主要事情:

  • 声明新策略 只有一种必须定义的方法,onBars,它被称为Feed中的每个栏。
  • 从CSV文件加载Feed。
  • 使用Feed提供的栏来运行策略。
    如果您运行脚本,您应该按顺序看到收盘价:
    2000-01-03 00:00:00 strategy [INFO] 118.12
    2000-01-04 00:00:00 strategy [INFO] 107.69
    2000-01-05 00:00:00 strategy [INFO] 102.0
    .
    .
    .
    2000-12-27 00:00:00 strategy [INFO] 30.69
    2000-12-28 00:00:00 strategy [INFO] 31.06
    2000-12-29 00:00:00 strategy [INFO] 29.06



作者:readilen
链接:http://www.jianshu.com/p/9f9658474df2
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

转载于:https://www.cnblogs.com/zhanglong8681/p/7569196.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值