def main(plot): # --------数据格式转换,常用国内A股数据,转换为yahoo财经格式 cod = "002739" # 万达院线 cname = 'wanda' fss = "dat\\" + cod + ".csv" df = pd.read_csv(fss, encoding='gbk') df2 = zwx.df2yhaoo(df) cfn = "dat\\" + cod + "_yh.csv" print(fss) df2.to_csv(cfn, encoding='utf-8') # ------------ # instrument = "aapl",使用新变量名cname替代 # smaPeriod = 163 smaPeriod = 20 # Download the bars. # feed = yahoofinance.build_feed([instrument], 2011, 2012, ".") feed = yahoofeed.Feed() feed.addBarsFromCSV(cname, cfn) strat = sma_crossover.SMACrossOver(feed, cname, smaPeriod) sharpeRatioAnalyzer = sharpe.SharpeRatio() strat.attachAnalyzer(sharpeRatioAnalyzer) if plot: plt = plotter.StrategyPlotter(strat, True, False, True) plt.getInstrumentSubplot(cname).addDataSeries("sma", strat.getSMA()) strat.run() print "夏普指数 Sharpe ratio: %.2f" % sharpeRatioAnalyzer.getSharpeRatio(0.05) if plot: plt.plot()
def main(plot): cod = "002739" #万达院线 cname = 'wanda' 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(fss) df2.to_csv(cfn, encoding='utf-8') # #instrument = "aapl" #使用新变量名cname替代 vwapWindowSize = 5 threshold = 0.01 # Download the bars. #feed = yahoofinance.build_feed([cname], 2011, 2012, ".") feed = yahoofeed.Feed() feed.addBarsFromCSV(cname, cfn) strat = VWAPMomentum(feed, cname, vwapWindowSize, threshold) sharpeRatioAnalyzer = sharpe.SharpeRatio() strat.attachAnalyzer(sharpeRatioAnalyzer) if plot: plt = plotter.StrategyPlotter(strat, True, False, True) plt.getInstrumentSubplot(cname).addDataSeries("vwap", strat.getVWAP()) 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): cod = "002739" #万达院线 cname = 'wanda' 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(fss) df2.to_csv(cfn, encoding='utf-8') # #instrument = "yhoo" #使用新变量名cname替代 #bBandsPeriod = 40 bBandsPeriod = 10 # Download the bars. #feed = yahoofinance.build_feed([instrument], 2011, 2012, ".") feed = yahoofeed.Feed() feed.addBarsFromCSV(cname, cfn) strat = BBands(feed, cname, bBandsPeriod) sharpeRatioAnalyzer = sharpe.SharpeRatio() strat.attachAnalyzer(sharpeRatioAnalyzer) if plot: mpl.style.use('seaborn-whitegrid') plt = plotter.StrategyPlotter(strat, True, True, True) plt.getInstrumentSubplot(cname).addDataSeries( "upper", strat.getBollingerBands().getUpperBand()) plt.getInstrumentSubplot(cname).addDataSeries( "middle", strat.getBollingerBands().getMiddleBand()) plt.getInstrumentSubplot(cname).addDataSeries( "lower", strat.getBollingerBands().getLowerBand()) strat.run() print("夏普指数 Sharpe ratio: %.2f" % sharpeRatioAnalyzer.getSharpeRatio(0.05)) if plot: plt.plot()
if self.__position is None: if bar.getPrice() > self.__sma[-1]: # Enter a buy market order for 10 shares. The order is good till canceled.输入10股买入市价。 订单是好的,直到取消。 self.__position = self.enterLong(self.__instrument, 10, True) # Check if we have to exit the position. elif bar.getPrice() < self.__sma[-1] and not self.__position.exitActive(): self.__position.exitMarket() #--------数据格式转换,常用国内A股数据,转换为yahoo财经格式 cod="601069"; cname='xibu'; 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(fss); df2.to_csv(cfn,encoding='utf-8') # Load the yahoo feed from the CSV file feed = yahoofeed.Feed() feed.addBarsFromCSV(cname,cfn) # Evaluate the strategy with the feed's bars. #myStrategy = SMACrossOver(feed, "orcl", 20) myStrategy = MyStrategy(feed,cname, 26) # Attach different analyzers to a strategy before executing it. retAnalyzer = returns.Returns() myStrategy.attachAnalyzer(retAnalyzer) sharpeRatioAnalyzer = sharpe.SharpeRatio()
if self.__position is None: if cross.cross_above(self.__prices, self.__sma) > 0: shares = int(self.getBroker().getCash() * 0.9 / bars[self.__instrument].getPrice()) # Enter a buy market order. The order is good till canceled. self.__position = self.enterLong(self.__instrument, shares, True) # Check if we have to exit the position. elif not self.__position.exitActive() and cross.cross_below(self.__prices, self.__sma) > 0: self.__position.exitMarket() # -------- cod = "002739"; # 万达院线 fss = "dat\\" + cod + ".csv"; df = pd.read_csv(fss, encoding='gbk'); # df2=zwBox.zw_df2yhaoo(df); df2 = zwBox.df2yhaoo(df); fss = "dat\\" + cod + "_yh.csv"; print(fss); df2.to_csv(fss, encoding='utf-8') # # Load the yahoo feed from the CSV file feed = yahoofeed.Feed() # feed.addBarsFromCSV("orcl", "dat\\orcl-2000.csv") # fss="dat\\"+cod+".csv"; feed.addBarsFromCSV("wanda", fss) # Evaluate the strategy with the feed's bars. myStrategy = SMACrossOver(feed, "wanda", 20) # Attach different analyzers to a strategy before executing it.