Exemplo n.º 1
0
def vwap(plot):
    instrument = ["lenovo", "mi"]
    vwapWindowSize = 5
    threshold = 0.01
    # Download the bars.
    feed = yahoofeed.Feed()
    feed.addBarsFromCSV("lenovo", "D:/data2/600687.csv")
    feed.addBarsFromCSV("mi", "D:/data2/600701.csv")

    strat = pyalg_test.VWAPMomentum(feed, instrument, vwapWindowSize,
                                    threshold)
    sharpeRatioAnalyzer = sharpe.SharpeRatio()
    strat.attachAnalyzer(sharpeRatioAnalyzer)

    if plot:
        plt = plotter.StrategyPlotter(strat, True, True, True)
        #plt.getPortfolioSubplot().addDataSeries("vwap", strat.getVWAP()[instrument[-1]])
    ds = pyalg_utils.dataSet(strat)  #抽取交易数据集语句,若使用系统自带画图功能则不需要该项
    strat.run()
    print "Sharpe ratio: %.2f" % sharpeRatioAnalyzer.getSharpeRatio(0.05)

    if plot:
        plt.plot()

    rs = ds.getReturns()  #获取默认的交易信息,dic格式,可忽略
Exemplo n.º 2
0
def vwap(plot):
    import os

    instrument = ["lenovo", "mi"]
    vwapWindowSize = 5
    threshold = 0.01
    # Download the bars.
    if (True):
        #mid 从dataFrame中加载,即直接加载tushare格式数据
        feed = dataFramefeed.Feed()
        dataRoot = os.path.abspath(
            os.path.join(os.path.dirname(__file__), os.pardir, os.pardir,
                         os.pardir, 'histdata', 'tushare'))

        code = '600209'
        filename = dataRoot + os.sep + 'day' + os.sep + ('%s.csv' % code)
        dat = pd.read_csv(filename, index_col=0, encoding='gbk')

        feed.addBarsFromDataFrame("lenovo", dat)

        code = '600051'
        filename = dataRoot + os.sep + 'day' + os.sep + ('%s.csv' % code)
        dat = pd.read_csv(filename, index_col=0, encoding='gbk')

        feed.addBarsFromDataFrame("mi", dat)

    else:  #mid 加载tushare数据转化为yahoo格式后的数据
        feed = yahoofeed.Feed()
        feed.addBarsFromCSV("lenovo", "D:/data2/600687.csv")
        feed.addBarsFromCSV("mi", "D:/data2/600701.csv")

    strat = pyalg_test.VWAPMomentum(feed, instrument, vwapWindowSize,
                                    threshold)
    sharpeRatioAnalyzer = sharpe.SharpeRatio()
    strat.attachAnalyzer(sharpeRatioAnalyzer)

    if plot:
        plt = plotter.StrategyPlotter(strat, True, True, True)
        #plt.getPortfolioSubplot().addDataSeries("vwap", strat.getVWAP()[instrument[-1]])
    ds = pyalg_utils.dataSet(strat)  #抽取交易数据集语句,若使用系统自带画图功能则不需要该项
    strat.run()
    print "Sharpe ratio: %.2f" % sharpeRatioAnalyzer.getSharpeRatio(0.05)

    rs = ds.getReturns()  #获取默认的交易信息,dic格式,可忽略
    for items in rs:
        print items[0], items[1]

    if plot:
        plt.plot()