首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在PyAlgoTrade中使用多个仪器进行反向测试

在PyAlgoTrade中使用多个仪器进行反向测试
EN

Stack Overflow用户
提问于 2016-08-28 06:03:44
回答 1查看 891关注 0票数 2

您好,我想使用一个数组(“仪器”)将策略从1个可能的投资推广到10个可能的投资,以简化加载10个馈送、创建10个SMA以及每天检查一个(或多个)仪器中是否发生信号交叉的任务。

我被困在这里了。绘图仪也是单独绘制图形,但我想在一个图形中绘制所有仪器的结果。

这是我的代码:

代码语言:javascript
复制
from pyalgotrade import strategy, plotter
from pyalgotrade.barfeed import yahoofeed
from pyalgotrade.technical import ma
from pyalgotrade.tools import yahoofinance

class MyStrategy(strategy.BacktestingStrategy):
    def __init__(self, feed, instruments, smaPeriod):
        strategy.BacktestingStrategy.__init__(self, feed, 1000)
        self.__position = None
        # We'll use adjusted close values instead of regular close values.
        self.setUseAdjustedValues(True)
        self.__sma = {}
        self.__instruments = instruments
        for instrument in instruments:
            self.__sma[instrument] = ma.SMA(feed[instrument].getPriceDataSeries(), smaPeriod)

    def onEnterOk(self, position):
        execInfo = position.getEntryOrder().getExecutionInfo()
        self.info("BUY at $%.2f" % (execInfo.getPrice()))

    def onEnterCanceled(self, position):
        execInfo = position.getEntryOrder().getExecutionInfo()

    def onExitOk(self, position):
        execInfo = position.getExitOrder().getExecutionInfo()
        self.info("SELL at $%.2f" % (execInfo.getPrice()))

    def onExitCanceled(self, position):
        # If the exit was canceled, re-submit it.
        self.__position[str(position.getEntryOrder().getInstrument())].exitMarket()

    def onBars(self, bars):
        # Wait for enough bars to be available to calculate a SMA.
        if self.__sma[-1] is None:
            return

        bar = bars[self.__instrument]
        # If a position was not opened, check if we should enter a long position.
        if self.__position is None:
            if bar.getPrice() > self.__sma[-1]:
            # Enter a buy market order for 25 shares. The order is good till canceled.
                self.__position = self.enterLong(self.__instrument, 25, True)
        # Check if we have to exit the position.
        elif bar.getPrice() < self.__sma[-1] and not self.__position.exitActive():
            self.__position.exitMarket()

def run_strategy(smaPeriod):

    # Load the yahoo feed from the CSV file
    instruments = [
        "AMZN",
        "ADBE",
        "C" ,
        "BA" ,
        "HOG" ,
        "MMM" ,
        "MS" ,
        "MSFT" ,
        "CVS" ,
        "AXP"
    ]
    #Download and Load yahoo feed from CSV files
    #Change year range 2000 to 2001 to your desired one
    feed = yahoofinance.build_feed(instruments, 2000,2001, ".")

    # Evaluate the strategy with the feed.
    myStrategy = MyStrategy(feed, instruments, smaPeriod)

    # Attach a plotter to the strategy
    plt = plotter.StrategyPlotter(myStrategy)


    # Run the strategy
    myStrategy.run()
    print "Final portfolio value: $%.2f" % myStrategy.getBroker().getEquity()

    # Plot the strategy.
    plt.plot()


run_strategy(10)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-25 13:55:37

我刚刚开始处理pyalgotrade,但我认为您正在犯一个相当简单的错误(如gzc所示):Bars类的实例是来自不同工具的条的集合,这些条都具有相同的时间戳。因此,当您的onBars事件被调用时,您实际上必须遍历字典中的所有工具。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39186169

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档