def main(): strat = thrSMA instrument = '600288' paras = [2, 20, 60, 10] feeds = tushare.build_feed([instrument], 2016, 2017, "histdata/tushare") strat = strat(feeds, instrument, *paras) retAnalyzer = returns.Returns() strat.attachAnalyzer(retAnalyzer) sharpeRatioAnalyzer = sharpe.SharpeRatio() strat.attachAnalyzer(sharpeRatioAnalyzer) drawDownAnalyzer = drawdown.DrawDown() strat.attachAnalyzer(drawDownAnalyzer) tradesAnalyzer = trades.Trades() strat.attachAnalyzer(tradesAnalyzer) plter = plotter.StrategyPlotter(strat, True, True, True) strat.run() plter.plot() # 夏普率 sharp = sharpeRatioAnalyzer.getSharpeRatio(0.05) # 最大回撤 maxdd = drawDownAnalyzer.getMaxDrawDown() # 收益率 return_ = retAnalyzer.getCumulativeReturns()[-1] # 收益曲线 return_list = [] for item in retAnalyzer.getCumulativeReturns(): return_list.append(item)
def run_strategy(smaPeriod): instrument = '600016' feeds = tushare.build_feed([instrument], 2016, 2018, './histdata/tushare') # Evaluate the strategy with the feed. strat = MyStrategy(feeds, instrument, smaPeriod) strat.run() strat.info("Final portfolio value: $%.2f" % strat.getBroker().getEquity())
def testStrategy(): instrument = '600288' frequency = bar.Frequency.TRADE fromDate = '20160815' toDate = '20160820' strat = OrderBook paras = [5, 20] plot = True from mooquant.tools import tushare feeds = tushare.build_feed([instrument], 2016, 2017, "histdata/tushare") strat = strat(feeds, instrument, *paras) retAnalyzer = returns.Returns() strat.attachAnalyzer(retAnalyzer) sharpeRatioAnalyzer = sharpe.SharpeRatio() strat.attachAnalyzer(sharpeRatioAnalyzer) drawDownAnalyzer = drawdown.DrawDown() strat.attachAnalyzer(drawDownAnalyzer) tradesAnalyzer = trades.Trades() strat.attachAnalyzer(tradesAnalyzer) if plot: plt = plotter.StrategyPlotter(strat, True, True, True) strat.run() if plot: plt.plot() # 夏普率 sharp = sharpeRatioAnalyzer.getSharpeRatio(0.05) # 最大回撤 maxdd = drawDownAnalyzer.getMaxDrawDown() # 收益率 return_ = retAnalyzer.getCumulativeReturns()[-1] # 收益曲线 return_list = [] for item in retAnalyzer.getCumulativeReturns(): return_list.append(item)
def main(plot): instrument = '600016' vwapWindowSize = 5 threshold = 0.01 feeds = tushare.build_feed([instrument], 2010, 2018, './histdata/tushare') strat = VWAPMomentum(feeds, instrument, vwapWindowSize, threshold) sharpeRatioAnalyzer = sharpe.SharpeRatio() strat.attachAnalyzer(sharpeRatioAnalyzer) plter = plotter.StrategyPlotter(strat, True, False, True) plter.getInstrumentSubplot(instrument).addDataSeries( "vwap", strat.getVWAP()) strat.run() strat.info("Sharpe ratio: %.2f" % sharpeRatioAnalyzer.getSharpeRatio(0.05)) plter.plot()
def main(): instruments = ["600036"] feeds = tushare.build_feed(instruments, 2003, 2018, "histdata/tushare") # 3.实例化策略 strat = Strategy(feeds, instruments[0]) # 4.设置指标和绘图 ratio = sharpe.SharpeRatio() strat.attachAnalyzer(ratio) plter = plotter.StrategyPlotter(strat) # 5.运行策略 strat.run() strat.info("最终收益: %.2f" % strat.getResult()) # 6.输出夏普率、绘图 strat.info("夏普比率: " + str(ratio.getSharpeRatio(0))) plter.plot()
def main(plot): instrument = "600016" smaPeriod = 163 feed = tushare.build_feed([instrument], 2011, 2012, "./histdata/tushare") strat = sma_crossover.SMACrossOver(feed, instrument, smaPeriod) sharpeRatioAnalyzer = sharpe.SharpeRatio() strat.attachAnalyzer(sharpeRatioAnalyzer) if plot: plt = plotter.StrategyPlotter(strat, True, False, True) plt.getInstrumentSubplot(instrument).addDataSeries( "sma", strat.getSMA()) strat.run() print("Sharpe ratio: %.2f" % sharpeRatioAnalyzer.getSharpeRatio(0.05)) if plot: plt.plot()
def main(plot): instrument = "600036" vwapWindowSize = 5 threshold = 0.01 # Download the bars. feed = tushare.build_feed([instrument], 2011, 2012, "./tests/data") strat = VWAPMomentum(feed, instrument, vwapWindowSize, threshold) sharpeRatioAnalyzer = sharpe.SharpeRatio() strat.attachAnalyzer(sharpeRatioAnalyzer) if plot: plt = plotter.StrategyPlotter(strat, True, False, True) plt.getInstrumentSubplot(instrument).addDataSeries("vwap", strat.getVWAP()) strat.run() print("Sharpe ratio: %.2f" % sharpeRatioAnalyzer.getSharpeRatio(0.05)) if plot: plt.plot()
def main(plot=False): instruments = ["600036"] bBandsPeriod = 20 feeds = tushare.build_feed(instruments, 2017, 2018, "./tests/data") strat = BBandsStrategy(feeds, instruments[0], bBandsPeriod) sharp = sharpe.SharpeRatio() strat.attachAnalyzer(sharp) if plot: plt = plotter.StrategyPlotter(strat, True, True, True) plt.getInstrumentSubplot(instruments[0]).addDataSeries("upper", strat.getBollingerBands().getUpperBand()) plt.getInstrumentSubplot(instruments[0]).addDataSeries("middle", strat.getBollingerBands().getMiddleBand()) plt.getInstrumentSubplot(instruments[0]).addDataSeries("lower", strat.getBollingerBands().getLowerBand()) strat.run() print("Sharpe ratio: %.2f" % sharp.getSharpeRatio(0.05)) if plot: plt.plot()
def main(plot): instrument = "600016" entrySMA = 150 exitSMA = 5 rsiPeriod = 3 overBoughtThreshold = 93 overSoldThreshold = 25 # Download the bars. # feed = quandl.build_feed("WIKI", [instrument], 2009, 2012, "./data") # feeds = yahoofeed.Feed() # feeds.addBarsFromCSV("dia", "./tests/data/DIA-2009-yahoofinance.csv") # feeds.addBarsFromCSV("dia", "./tests/data/DIA-2010-yahoofinance.csv") # feeds.addBarsFromCSV("dia", "./tests/data/DIA-2011-yahoofinance.csv") # instruments = ["600036"] feeds = tushare.build_feed([instrument], 2009, 2011, "histdata/mootdx") strat = RSI2(feeds, 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("夏普比率: %.2f" % sharpeRatioAnalyzer.getSharpeRatio(0.05)) if plot: plt.plot()
def testStrategy(): strat = thrSMA instrument = '600288' market = 'SH' fromDate = '20150101' toDate = '20150601' frequency = bar.Frequency.MINUTE plot = True paras = [2, 20, 60, 10] feeds = tushare.build_feed([instrument], 2016, 2017, "histdata/tushare") strat = strat(feeds, instrument, *paras) retAnalyzer = returns.Returns() strat.attachAnalyzer(retAnalyzer) sharpeRatioAnalyzer = sharpe.SharpeRatio() strat.attachAnalyzer(sharpeRatioAnalyzer) drawDownAnalyzer = drawdown.DrawDown() strat.attachAnalyzer(drawDownAnalyzer) tradesAnalyzer = trades.Trades() strat.attachAnalyzer(tradesAnalyzer) strat.run() # 夏普率 sharp = sharpeRatioAnalyzer.getSharpeRatio(0.05) # 最大回撤 maxdd = drawDownAnalyzer.getMaxDrawDown() # 收益率 return_ = retAnalyzer.getCumulativeReturns()[-1] # 收益曲线 return_list = [] for item in retAnalyzer.getCumulativeReturns(): return_list.append(item)
from mooquant import plotter from mooquant.analyzer import returns from mooquant.tools import tushare from samples import sma_crossover if __name__ == '__main__': instrument = '600016' feeds = tushare.build_feed([instrument], 2016, 2018, './histdata/tushare') strat = sma_crossover.SMACrossOver(feeds, instrument, 20) # Attach a returns analyzers to the strategy. returnsAnalyzer = returns.Returns() strat.attachAnalyzer(returnsAnalyzer) # Attach the plotter to the strategy. plt = plotter.StrategyPlotter(strat) # Include the SMA in the instrument's subplot to get it displayed along with the closing prices. plt.getInstrumentSubplot("orcl").addDataSeries("SMA", strat.getSMA()) # Plot the simple returns on each bar. plt.getOrCreateSubplot("returns").addDataSeries("Simple returns", returnsAnalyzer.getReturns()) # Run the strategy. strat.run() strat.info("Final portfolio value: $%.2f" % strat.getResult()) # Plot the strategy. plt.plot()
import itertools from mooquant.optimizer import local from mooquant.tools import tushare from rsi2 import RSI2 def parameters_generator(instrument): entrySMA = list(range(150, 251)) exitSMA = list(range(5, 16)) rsiPeriod = list(range(2, 11)) overBoughtThreshold = list(range(75, 96)) overSoldThreshold = list(range(5, 26)) return itertools.product(instrument, entrySMA, exitSMA, rsiPeriod, overBoughtThreshold, overSoldThreshold) if __name__ == '__main__': instruments = ['000848'] feeds = tushare.build_feed(instruments, 2016, 2018, 'histdata/tushare') local.run(RSI2, feeds, parameters_generator(instruments))
def testStrategy(): from mooquant import bar from mooquant import plotter strat = fourSMA instrument = '600288' market = 'SH' fromDate = '20150101' toDate = '20150601' frequency = bar.Frequency.MINUTE paras = [2, 20, 60, 10] plot = True # path set ############################33 # if frequency == bar.Frequency.MINUTE: # path = os.path.join('..', 'histdata', 'minute') # elif frequency == bar.Frequency.DAY: # path = os.path.join('..', 'histdata', 'day') # filepath = os.path.join(path, instrument + market + ".csv") # don't change ############################33 # barfeed = Feed(frequency) # barfeed.setDateTimeFormat('%Y-%m-%d %H:%M:%S') # barfeed.loadBars(instrument, market, fromDate, toDate, filepath) # mooquant_id = instrument + '.' + market # strat = strat(barfeed, mooquant_id, *paras) from mooquant.tools import tushare feeds = tushare.build_feed([instrument], 2016, 2017, "histdata/tushare") strat = strat(feeds, instrument, *paras) retAnalyzer = returns.Returns() strat.attachAnalyzer(retAnalyzer) sharpeRatioAnalyzer = sharpe.SharpeRatio() strat.attachAnalyzer(sharpeRatioAnalyzer) drawDownAnalyzer = drawdown.DrawDown() strat.attachAnalyzer(drawDownAnalyzer) tradesAnalyzer = trades.Trades() strat.attachAnalyzer(tradesAnalyzer) if plot: plt = plotter.StrategyPlotter(strat, True, True, True) strat.run() if plot: plt.plot() # 夏普率 sharp = sharpeRatioAnalyzer.getSharpeRatio(0.05) # 最大回撤 maxdd = drawDownAnalyzer.getMaxDrawDown() # 收益率 return_ = retAnalyzer.getCumulativeReturns()[-1] # 收益曲线 return_list = [] for item in retAnalyzer.getCumulativeReturns(): return_list.append(item)