def get_tdx_barfeed(code, start=None, dl=30): df = tdd.get_tdx_append_now_df_api(code, start=start) # df2 = get_tdx_append_now_df_api(code=stock_code, start=start, end=end, dl=dl).sort_index(ascending=True) df.rename(columns={'vol': 'volume'}, inplace=True) df = df.sort_index(ascending=True) barfeed = dataFramefeed.Feed() barfeed.addBarsFromDataFrame(code, df) # print barfeed.get_dataFrame() return barfeed
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()