コード例 #1
0
def turtle_test(code,start=None,plot=True,dl=60):
    feed = ptc.get_tdx_barfeed(code,start,dl=dl)
    #myStrategy = pyalg_test.SMACrossOver(feed, "orcl", 20)
    pars=[2, 20, 60, 10]
#    myStrategy = thrSMA_dayinfo(feed, "orcl",*pars)
    myStrategy = BBands(feed, code,20,dl)
    # Attach a returns analyzers to the strategy.
#    returnsAnalyzer = returns.Returns()
#    myStrategy.attachAnalyzer(returnsAnalyzer)


#    if dataString =='pyalg_util':
#        ds = pyalg_utils.dataSet(myStrategy)   #抽取交易数据集语句,若使用系统自带画图功能则不需要该项
    from pyalgotrade.stratanalyzer import returns
    from pyalgotrade.stratanalyzer import sharpe
    from pyalgotrade.stratanalyzer import drawdown
    from pyalgotrade.stratanalyzer import trades

    retAnalyzer = returns.Returns()
    myStrategy.attachAnalyzer(retAnalyzer)
    sharpeRatioAnalyzer = sharpe.SharpeRatio()
    myStrategy.attachAnalyzer(sharpeRatioAnalyzer)
    drawDownAnalyzer = drawdown.DrawDown()
    myStrategy.attachAnalyzer(drawDownAnalyzer)
    tradesAnalyzer = trades.Trades()
    myStrategy.attachAnalyzer(tradesAnalyzer)

        # Attach the plotter to the strategy.
#    plt = plotter.StrategyPlotter(myStrategy)
    # Plot the simple returns on each bar.


    if plot:
        plt = plotter.StrategyPlotter(myStrategy, True, True, True)
        plt.getOrCreateSubplot("returns").addDataSeries("Simple returns", retAnalyzer.getReturns())

    myStrategy.run()
    myStrategy.info("Final portfolio value: $%.2f" % myStrategy.getResult())

#    if dataString =='pyalg_util':
#        rs = ds.getDefault()       #获取默认的交易信息,dic格式
#        plot(rs["cumulativeReturns"][:,0],rs["cumulativeReturns"][:,1])  #简单作图示例

    #夏普率
    sharp = sharpeRatioAnalyzer.getSharpeRatio(0.05)
    #最大回撤
    maxdd = drawDownAnalyzer.getMaxDrawDown()
    #收益率
    return_ = retAnalyzer.getCumulativeReturns()[-1]
    #收益曲线
    return_list = []
    for item in retAnalyzer.getCumulativeReturns():
        return_list.append(item)

    print "Sharpe ratio: %.2f maxdown:%.2f%%  return:%.2f%%" %(sharp,maxdd*100,return_*100)
    myStrategy.get_max_min()
    plt.plot()
コード例 #2
0
ファイル: bollinger_bandit.py プロジェクト: fswzb/pyQuant-2
def testStrategy(code, start=None, plot=False):
    from pyalgotrade import bar
    from pyalgotrade import plotter

    strat = Bollinger_Bandit
    # instrument = '600288'
    market = 'SH'
    fromDate = '20150101'
    toDate = '20150601'
    frequency = bar.Frequency.MINUTE
    paras = [40, 15, 35, 15, 60, 2]
    # plot = True

    #############################################path set ############################33
    import os
    if frequency == bar.Frequency.MINUTE:
        path = os.path.join('..', 'histdata', 'minute')
    elif frequency == bar.Frequency.DAY:
        path = os.path.join('..', 'histdata', 'day')
    # filepath = os.path.join(path, instrument + market + ".csv")

    #############################################don't change ############################33
    # from pyalgotrade.cn.csvfeed import Feed
    from feedutil import dataFramefeed
    import powerTech as pt

    # barfeed = Feed(frequency)
    # barfeed.setDateTimeFormat('%Y-%m-%d %H:%M:%S')
    # barfeed.loadBars(instrument, market, fromDate, toDate, filepath)
    code = '000002'
    start = '2016-01-01'
    barfeed = pt.get_tdx_barfeed(code, start)
    # pyalgotrade_id = instrument + '.' + market

    strat = strat(barfeed, code, *paras)

    from pyalgotrade.stratanalyzer import returns
    from pyalgotrade.stratanalyzer import sharpe
    from pyalgotrade.stratanalyzer import drawdown
    from pyalgotrade.stratanalyzer import trades

    retAnalyzer = returns.Returns()
    strat.attachAnalyzer(retAnalyzer)
    sharpeRatioAnalyzer = sharpe.SharpeRatio()
    strat.attachAnalyzer(sharpeRatioAnalyzer)
    drawDownAnalyzer = drawdown.DrawDown()
    strat.attachAnalyzer(drawDownAnalyzer)
    tradesAnalyzer = trades.Trades()
    strat.attachAnalyzer(tradesAnalyzer)

    if plot:
        plt = plotter.StrategyPlotter(strat, True, True, True)

    strat.run()

    if plot:
        plt.plot()

    #夏普率
    sharp = sharpeRatioAnalyzer.getSharpeRatio(0.05)
    #最大回撤
    maxdd = drawDownAnalyzer.getMaxDrawDown()
    #收益率
    return_ = retAnalyzer.getCumulativeReturns()[-1]
    #收益曲线
    return_list = []
    for item in retAnalyzer.getCumulativeReturns():
        return_list.append(item)