def main(plot): instrument = "DIA" entrySMA = 200 exitSMA = 5 rsiPeriod = 2 overBoughtThreshold = 90 overSoldThreshold = 10 # Download the bars. feed = yahoofinance.build_feed([instrument], 2009, 2012, ".") strat = rsi2.RSI2(feed, instrument, entrySMA, exitSMA, rsiPeriod, overBoughtThreshold, overSoldThreshold) sharpeRatioAnalyzer = sharpe.SharpeRatio() strat.attachAnalyzer(sharpeRatioAnalyzer) if plot: plt = plotter.StrategyPlotter(strat, True, False, True) plt.getInstrumentSubplot(instrument).addDataSeries( "Entry SMA", strat.getEntrySMA()) plt.getInstrumentSubplot(instrument).addDataSeries( "Exit SMA", strat.getExitSMA()) plt.getOrCreateSubplot("rsi").addDataSeries("RSI", strat.getRSI()) plt.getOrCreateSubplot("rsi").addLine("Overbought", overBoughtThreshold) plt.getOrCreateSubplot("rsi").addLine("Oversold", overSoldThreshold) strat.run() print "Sharpe ratio: %.2f" % sharpeRatioAnalyzer.getSharpeRatio(0.05) if plot: plt.plot()
def main(plot): cod = "002739" cname = 'wanda' csgn = u'万达院线' cod = "600663" cname = 'lujiazui' csgn = u'陆家嘴' cod = "600231" cname = 'lingang' csgn = u'凌钢股份' cod = '002046' cname = 'zouyan' csgn = u'轴研科技' cod = '300239' cname = 'donbao' csgn = u'东宝生物' fss = "dat\\" + cod + ".csv" df = pd.read_csv(fss, encoding='gbk') #df2=zwBox.zw_df2yhaoo(df); df2 = zwx.df2yhaoo(df) cfn = "dat\\" + cod + "_yh.csv" print csgn, cname, fss df2.to_csv(cfn, encoding='utf-8') # #instrument = "DIA" #使用新变量名cname替代 entrySMA = 200 exitSMA = 5 rsiPeriod = 2 overBoughtThreshold = 90 overSoldThreshold = 10 # Download the bars. #feed = yahoofinance.build_feed([instrument], 2009, 2012, ".") feed = yahoofeed.Feed() feed.addBarsFromCSV(cname, cfn) strat = rsi2.RSI2(feed, cname, entrySMA, exitSMA, rsiPeriod, overBoughtThreshold, overSoldThreshold) sharpeRatioAnalyzer = sharpe.SharpeRatio() strat.attachAnalyzer(sharpeRatioAnalyzer) if plot: mpl.style.use('seaborn-whitegrid') plt = plotter.StrategyPlotter(strat, True, False, True) plt.getInstrumentSubplot(cname).addDataSeries("Entry SMA", strat.getEntrySMA()) plt.getInstrumentSubplot(cname).addDataSeries("Exit SMA", strat.getExitSMA()) plt.getOrCreateSubplot("rsi").addDataSeries("RSI", strat.getRSI()) plt.getOrCreateSubplot("rsi").addLine("Overbought", overBoughtThreshold) plt.getOrCreateSubplot("rsi").addLine("Oversold", overSoldThreshold) strat.run() print "夏普指数 Sharpe ratio: %.2f" % sharpeRatioAnalyzer.getSharpeRatio(0.05) if plot: plt.plot()
def main(plot): symbol = "DIA" priceCurrency = "USD" instrument = "%s/%s" % (symbol, priceCurrency) entrySMA = 200 exitSMA = 5 rsiPeriod = 2 overBoughtThreshold = 90 overSoldThreshold = 10 initialBalance = {priceCurrency: 1000000} # Load the bars. These files were manually downloaded from Yahoo Finance. feed = yahoofeed.Feed() for year in range(2009, 2013): fileName = "%s-%d-yahoofinance.csv" % (symbol, year) print("Loading bars from %s" % fileName) feed.addBarsFromCSV(instrument, fileName) strat = rsi2.RSI2(feed, instrument, initialBalance, entrySMA, exitSMA, rsiPeriod, overBoughtThreshold, overSoldThreshold) sharpeRatioAnalyzer = sharpe.SharpeRatio(priceCurrency) strat.attachAnalyzer(sharpeRatioAnalyzer) if plot: from pyalgotrade import plotter plt = plotter.StrategyPlotter(strat, True, False, True) plt.getInstrumentSubplot(instrument).addDataSeries( "Entry SMA", strat.getEntrySMA()) plt.getInstrumentSubplot(instrument).addDataSeries( "Exit SMA", strat.getExitSMA()) plt.getOrCreateSubplot("rsi").addDataSeries("RSI", strat.getRSI()) plt.getOrCreateSubplot("rsi").addLine("Overbought", overBoughtThreshold) plt.getOrCreateSubplot("rsi").addLine("Oversold", overSoldThreshold) strat.run() print("Sharpe ratio: %.2f" % sharpeRatioAnalyzer.getSharpeRatio(0.05)) if plot: plt.plot()