Ejemplo n.º 1
0
def turtle_test():
    # Load the yahoo feed from the CSV file
    feed = yahoofeed.Feed()
    feed.addBarsFromCSV("orcl", "D:/data2/600687.csv")
    
    # Evaluate the strategy with the feed's bars.
    #myStrategy = pyalg_test.SMACrossOver(feed, "orcl", 20)
    myStrategy = pyalg_test.turtle(feed, "orcl",20,10)
    # 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.    
    ds = pyalg_utils.dataSet(myStrategy)   #抽取交易数据集语句,若使用系统自带画图功能则不需要该项
    myStrategy.run()
    myStrategy.info("Final portfolio value: $%.2f" % myStrategy.getResult())
    
    rs = ds.getDefault()       #获取默认的交易信息,dic格式
    plot(rs["cumulativeReturns"][:,0],rs["cumulativeReturns"][:,1])  #简单作图示例
    # Plot the strategy.
    plt.plot()
Ejemplo n.º 2
0
def turtle_test():
    # Load the yahoo feed from the CSV file
    feed = yahoofeed.Feed()
    feed.addBarsFromCSV("orcl", "D:/data2/600223.csv")
    
    # Evaluate the strategy with the feed's bars.
    #myStrategy = pyalg_test.SMACrossOver(feed, "orcl", 20)
    myStrategy = pyalg_test.turtle(feed, "orcl",20,10)
    # 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()
Ejemplo n.º 3
0
def turtle_test(load_type='dataFrame', dataString='pyalg'):
    if load_type == 'csv':
        #Load the yahoo feed from the CSV file
        feed = yahoofeed.Feed()
        feed.addBarsFromCSV("orcl", "D:/data2/600687.csv")
    elif load_type == 'dataFrame':
        #从dataFrame中加载,
        import os
        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 = dataFramefeed.Feed()
        feed.addBarsFromDataFrame("orcl", dat)
    elif load_type == 'sql':
        #此处也是
        dat = data_sql.get_h_data('600848')
        feed = dataFramefeed.Feed()
        feed.addBarsFromDataFrame("orcl", dat)

    # Evaluate the strategy with the feed's bars.
    #myStrategy = pyalg_test.SMACrossOver(feed, "orcl", 20)
    myStrategy = pyalg_test.turtle(feed, "orcl", 20, 10)
    # Attach a returns analyzers to the strategy.
    returnsAnalyzer = returns.Returns()
    myStrategy.attachAnalyzer(returnsAnalyzer)

    # Attach the plotter to the strategy.
    plt = plotter.StrategyPlotter(myStrategy)
    # Plot the simple returns on each bar.
    plt.getOrCreateSubplot("returns").addDataSeries(
        "Simple returns", returnsAnalyzer.getReturns())

    if dataString == 'pyalg_util':
        ds = pyalg_utils.dataSet(myStrategy)  #抽取交易数据集语句,若使用系统自带画图功能则不需要该项
    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])  #简单作图示例

    plt.plot()
Ejemplo n.º 4
0
def turtle_test(load_type = 'dataFrame',dataString = 'pyalg'):
    if load_type =='csv':
        #Load the yahoo feed from the CSV file
        feed = yahoofeed.Feed()
        feed.addBarsFromCSV("orcl", "D:/data2/600687.csv")
    elif load_type =='dataFrame':
        #从dataFrame中加载,
        import os
        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 = dataFramefeed.Feed()
        feed.addBarsFromDataFrame("orcl", dat)
    elif load_type == 'sql':
        #此处也是
        dat = data_sql.get_h_data('600848')
        feed = dataFramefeed.Feed()
        feed.addBarsFromDataFrame("orcl", dat)
    
    # Evaluate the strategy with the feed's bars.
    #myStrategy = pyalg_test.SMACrossOver(feed, "orcl", 20)
    myStrategy = pyalg_test.turtle(feed, "orcl",20,10)
    # Attach a returns analyzers to the strategy.
    returnsAnalyzer = returns.Returns()
    myStrategy.attachAnalyzer(returnsAnalyzer)
    
    # Attach the plotter to the strategy.
    plt = plotter.StrategyPlotter(myStrategy)
    # Plot the simple returns on each bar.
    plt.getOrCreateSubplot("returns").addDataSeries("Simple returns", returnsAnalyzer.getReturns())  
    
    if dataString =='pyalg_util':
        ds = pyalg_utils.dataSet(myStrategy)   #抽取交易数据集语句,若使用系统自带画图功能则不需要该项
    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])  #简单作图示例
     
    plt.plot()
Ejemplo n.º 5
0
def turtle_test(load_type='csv', dataString='pyalg'):
    if load_type == 'csv':
        #Load the yahoo feed from the CSV file
        feed = yahoofeed.Feed()
        feed.addBarsFromCSV("orcl", "000004.csv")
    elif load_type == 'dataFrame':
        #从dataFrame中加载,
        #        dat = pd.read_csv('000004.csv',encoding='gbk',sep='\t',index_col=0)
        #        dat['date']=dat['date'].apply(lambda x:x.replace('/','-'))
        dat = ts.get_hist_data('000838', start='2016-01-01')
        feed = dataFramefeed.Feed()
        feed.addBarsFromDataFrame("orcl", dat)
    elif load_type == 'sql':
        #此处也是
        dat = data_sql.get_h_data('600848')
        feed = dataFramefeed.Feed()
        feed.addBarsFromDataFrame("orcl", dat)

    # Evaluate the strategy with the feed's bars.
    #myStrategy = pyalg_test.SMACrossOver(feed, "orcl", 20)
    pars = [20, 10]
    myStrategy = pyalg_test.turtle(feed, "orcl", *pars)
    # Attach a returns analyzers to the strategy.
    returnsAnalyzer = returns.Returns()
    myStrategy.attachAnalyzer(returnsAnalyzer)

    # Attach the plotter to the strategy.
    plt = plotter.StrategyPlotter(myStrategy)
    # Plot the simple returns on each bar.
    plt.getOrCreateSubplot("returns").addDataSeries(
        "Simple returns", returnsAnalyzer.getReturns())

    if dataString == 'pyalg_util':
        ds = pyalg_utils.dataSet(myStrategy)  #抽取交易数据集语句,若使用系统自带画图功能则不需要该项
    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])  #简单作图示例

    plt.plot()