def ENE(type='old', code='600281'): dat = pd.read_csv("../../api/stock/csv/%sSH_5min.csv" % code, encoding='gbk') dat = dat.fillna(method='pad') dat['Adj Close'] = dat['Close'] dat = dat.rename(columns={'Open': 'open', 'High': 'high', 'Volume': 'volume', 'Close': 'close', 'Low': 'low'}) dat.index = dat['Date Time'] dat.index.name = 'date' # dat = dat.ix[17000:18000,:] feed = dataFramefeed.Feed(frequency=bar.Frequency.MINUTE) feed.addBarsFromDataFrame("orcl", dat) if type == 'macd': myStrategy = mdd.ENE_backtest(feed, 'orcl') elif type == 'old': myStrategy = mdd.ENE_backtest(feed, 'orcl') sharpeRatioAnalyzer = sharpe.SharpeRatio() myStrategy.attachAnalyzer(sharpeRatioAnalyzer) returnsAnalyzer = returns.Returns() myStrategy.attachAnalyzer(returnsAnalyzer) plt = plotter.StrategyPlotter(myStrategy, True, True, True) plt.getOrCreateSubplot("returns").addDataSeries("Simple returns", returnsAnalyzer.getReturns()) ds = pyalg_utils.dataSet(myStrategy) # 抽取交易数据集语句,若使用系统自带画图功能则不需要该项 myStrategy.run() plt.plot() print('最大回撤:\t%f\t 交易笔数:\t%d\t 盈利笔数:\t%d\n' % (ds.getMaxDrawDown(), ds.getCount(), ds.getProfitableCount())) print('累计收益:\t%f\t 夏普比率:\t%f\t' % (returnsAnalyzer.getCumulativeReturns()[-1], ds.getSharpeRatio()))
def turtle_test(dataString='pyalg'): """ :param dataString: :return: """ feed = yahoofeed.Feed() feed.addBarsFromCSV( "orcl", "../../api/stock/csv/orcl-2000.csv") #注意查看该csv的格式,Open为大写 myStrategy = pdr.turtle(feed, "orcl", 20, 10) # Attach a returns analyzers to the trategy. 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()
def turtle_test(loadtype='pgs', dataString='pyalg'): """ :param dataString: :return: """ # 从postgres 中加载 if loadtype == 'pgs': from api.stock.histmd import to_postgres_md as tpd dat = tpd.get_h_data('600848') else: from api.stock.histmd.to_mongodb_md import k_data_dao as tmd w = tmd.KdataDbCache() dat = w.get_k_data(index=False,start='2015-02-05', end='2015-02-19',code='300426') feed = dataFramefeed.Feed() feed.addBarsFromDataFrame("orcl", dat) myStrategy = pdr.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()
def vwap(plot): """ 组合管理示例 :param plot: :return: """ instrument = ["lenovo", "mi"] vwapWindowSize = 5 threshold = 0.01 # Download the bars. feed = yahoofeed.Feed() feed.addBarsFromCSV("lenovo", "../../api/stock/csv/600847.csv") feed.addBarsFromCSV("mi","../../api/stock/csv/F600848.csv") strat = pdr.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格式,可忽略
def kdj(param=[14, 3, 0, 3, 0, 70, 30]): #dat = ts.get_k_data("000001", start='2006-07-01', end='2017-01-05', index=True) #dat.to_csv("000002.csv",encoding='gbk',index=False) dat = pd.read_csv("../../api/stock/csv/000007.csv", encoding='gbk', parse_dates=[0], index_col=0) dat = dat.fillna(method='pad') dat['Adj Close'] = dat['close'] dat.index.name = 'date' dat = dat[['open', 'close', 'high', 'low', 'volume', 'Adj Close']] # the index catch high<open in 2010-02-01 def handle_data(x): if x[2] < x[0]: x[2] = x[0] if x[3] > x[0]: x[3] = x[0] return x dat = dat.apply(handle_data, axis=1) #print dat.head() feed = dataFramefeed.Feed() feed.addBarsFromDataFrame("orcl", dat) myStrategy = kdj_backtest(feed, 'orcl', param=param, df=formular.KDJ( dat, 14, 3, 3)) #set the param of STOCH and crossover sharpeRatioAnalyzer = sharpe.SharpeRatio() myStrategy.attachAnalyzer(sharpeRatioAnalyzer) returnsAnalyzer = returns.Returns() myStrategy.attachAnalyzer(returnsAnalyzer) plt = plotter.StrategyPlotter(myStrategy, True, True, True) plt.getOrCreateSubplot("returns").addDataSeries( "Simple returns", returnsAnalyzer.getReturns()) ds = pyalg_utils.dataSet(myStrategy) # 抽取交易数据集语句,若使用系统自带画图功能则不需要该项 myStrategy.run() #plt.plot() #slowk,slowd = talib.STOCH(dat.high.values,dat.low.values,dat.close.values,14,3,0,3,0) #print slowk[-1],slowd[-1] print(u'drawndown:\t%f\t tradingCount:\t%d\t profitCount:\t%d\n' % (ds.getMaxDrawDown(), ds.getCount(), ds.getProfitableCount())) print(u'culmutiveReturn:\t%f\t sharpRatio:\t%f\t' % (returnsAnalyzer.getCumulativeReturns()[-1], ds.getSharpeRatio()))
def turtle_test(loadtype='tushare', dataString='pyalg'): """ :param dataString: :return: """ # 从dataFrame中加载, if loadtype == 'tushare': import tushare as ts dat = ts.get_h_data('600848') dat.index.name = 'date' else: dat = pd.read_csv("../../api/stock/csv/600848.csv", index_col=0, encoding='gbk') print(dat.info()) print(dat.head()) feed = dataFramefeed.Feed() feed.addBarsFromDataFrame("orcl", dat) myStrategy = pdr.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()