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 testDownloadAndParseDaily(self): instrument = "orcl/USD" barFeed = yahoofeed.Feed() barFeed.addBarsFromCSV(instrument, common.get_data_file_path("orcl-2000-yahoofinance.csv")) strat = sma_crossover.SMACrossOver(barFeed, instrument, 20) plt = plotter.StrategyPlotter(strat, True, True, True) plt.getInstrumentSubplot(instrument).addDataSeries("sma", strat.getSMA()) strat.run() with common.TmpDir() as tmpPath: png = os.path.join(tmpPath, "plotter_test.png") plt.savePlot(png) # Check that file size looks ok. self.assertGreater(os.stat(png).st_size, 44000)
def main(plot): instrument = "AAPL" smaPeriod = 30 # Download the bars. feed = quandl.build_feed("WIKI", [instrument,"NVDA"], 2016, 2018, Constants.data_directory) strategy = sma_crossover.SMACrossOver(feed, instrument, smaPeriod) sharpeRatioAnalyzer = sharpe.SharpeRatio() strategy.attachAnalyzer(sharpeRatioAnalyzer) if plot: plt = plotter.StrategyPlotter(strategy, True, True, True) plt.getInstrumentSubplot(instrument).addDataSeries("sma", strategy.getSMA()) strategy.run() print("Sharpe ratio: %.2f" % sharpeRatioAnalyzer.getSharpeRatio(0.05)) if plot: plt.plot()
def main(plot): instrument = "aapl" smaPeriod = 20 feed = yahoofinance.build_feed([instrument], 2014, 2015, ".") 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 testDownloadAndParseDaily(self): instrument = "orcl" barFeed = yahoofeed.Feed() barFeed.addBarsFromCSV( instrument, common.get_data_file_path("orcl-2000-yahoofinance.csv")) strat = sma_crossover.SMACrossOver(barFeed, instrument, 20) plt = plotter.StrategyPlotter(strat, True, True, True) plt.getInstrumentSubplot(instrument).addDataSeries( "sma", strat.getSMA()) strat.run() with common.TmpDir() as tmpPath: fig = plt.buildFigure() fig.set_size_inches(10, 8) png = os.path.join(tmpPath, "plotter_test.png") fig.savefig(png) self.assertEquals( open(common.get_data_file_path("plotter_test.png"), "r").read(), open(png, "r").read())
def main(plot): instrument = "AAPL" smaPeriod = 163 # Download the bars. feed = quandl.build_feed("WIKI", [instrument], 2011, 2012, ".") 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 testDownloadAndParseDaily(self): instrument = "orcl" barFeed = yahoofeed.Feed() barFeed.addBarsFromCSV(instrument, common.get_data_file_path("orcl-2000-yahoofinance.csv")) strat = sma_crossover.SMACrossOver(barFeed, instrument, 20) plt = plotter.StrategyPlotter(strat, True, True, True) plt.getInstrumentSubplot(instrument).addDataSeries("sma", strat.getSMA()) strat.run() with common.TmpDir() as tmpPath: fig = plt.buildFigure() fig.set_size_inches(10, 8) png = os.path.join(tmpPath, "plotter_test.png") fig.savefig(png) # Check that file size looks ok. # 118458 on Mac # 116210 on Linux self.assertGreater( os.stat(png).st_size, 110000 )
def main(plot): instrument = "aapl" smaPeriod = 163 # Download the bars. #feed = yahoofinance.build_feed([instrument], 2011, 2012, ".") feed = yahoofeed.Feed() feed.addBarsFromCSV(instrument, "dat\\aapl-201x.csv") 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()
# 无风险利率 risk free rate r = 0.04 # Load the bar feed from the CSV file instrument = "moutai" feed = quandlfeed.Feed() # 调整需要的CSV路径 Adjust CSV file feed.addBarsFromCSV(instrument, 'E:\\backtest\\csv\\' + instrument + 'csv.csv') # 修改长短期均线 Adjust fast and slow MA fastMA = 5 slowMA = 20 ##line 55 调整存储图片路径 Change the root of saving image #======================================================================================== # Evaluate the strategy with the feed's bars. myStrategy = sma_crossover.SMACrossOver(feed, instrument, fastMA, slowMA) # Attach a returns analyzers to the strategy. returnsAnalyzer = returns.Returns() sharpeRatioAnalyzer = sharpe.SharpeRatio() drawdown = drawdown.DrawDown() trades = trades.Trades() myStrategy.attachAnalyzer(returnsAnalyzer) myStrategy.attachAnalyzer(sharpeRatioAnalyzer) myStrategy.attachAnalyzer(drawdown) myStrategy.attachAnalyzer(trades) # Attach the plotter to the strategy. plt = plotter.StrategyPlotter(myStrategy) # Include the SMA in the instrument's subplot to get it displayed along with the closing prices. plt.getInstrumentSubplot(instrument).addDataSeries("fastSMA",
from pyalgotrade import plotter from pyalgotrade.barfeed import quandlfeed from pyalgotrade.stratanalyzer import returns import sma_crossover # Load the bar feed from the CSV file feed = quandlfeed.Feed() feed.addBarsFromCSV("orcl", "WIKI-ORCL-2000-quandl.csv") # Evaluate the strategy with the feed's bars. myStrategy = sma_crossover.SMACrossOver(feed, "orcl", 20) # Attach a returns analyzers to the strategy. returnsAnalyzer = returns.Returns() myStrategy.attachAnalyzer(returnsAnalyzer) # Attach the plotter to the strategy. plt = plotter.StrategyPlotter(myStrategy) # Include the SMA in the instrument's subplot to get it displayed along with the closing prices. plt.getInstrumentSubplot("orcl").addDataSeries("SMA", myStrategy.getSMA()) # Plot the simple returns on each bar. plt.getOrCreateSubplot("returns").addDataSeries("Simple returns", returnsAnalyzer.getReturns()) # Run the strategy. myStrategy.run() myStrategy.info("Final portfolio value: $%.2f" % myStrategy.getResult()) # Plot the strategy. plt.plot()
#place below command in console to magnify the plot #>>import matplotlib.pyplot as plt #>>plt.rc('figure',figsize=(50,50)) from pyalgotrade import plotter from pyalgotrade.barfeed import yahoofeed from pyalgotrade.stratanalyzer import returns import sma_crossover # Load the yahoo feed from the CSV file feed = yahoofeed.Feed() ticker = "amzn" feed.addBarsFromCSV(ticker, r'D:\Workspace\data\%s.csv' % ticker) # Evaluate the strategy with the feed's bars. myStrategy = sma_crossover.SMACrossOver(feed, ticker, 15) # Attach a returns analyzers to the strategy. returnsAnalyzer = returns.Returns() myStrategy.attachAnalyzer(returnsAnalyzer) # Attach the plotter to the strategy. plt = plotter.StrategyPlotter(myStrategy) # Include the SMA in the instrument's subplot to get it displayed along with the closing prices. plt.getInstrumentSubplot(ticker).addDataSeries("SMA", myStrategy.getSMA()) # Plot the simple returns on each bar. plt.getOrCreateSubplot("returns").addDataSeries("Simple returns", returnsAnalyzer.getReturns()) # Run the strategy. myStrategy.run()
from pyalgotrade import plotter from pyalgotrade.barfeed import quandlfeed from pyalgotrade.stratanalyzer import returns import sma_crossover priceCurrency = "USD" # Load the bar feed from the CSV file feed = quandlfeed.Feed() feed.addBarsFromCSV("ORCL/USD", "WIKI-ORCL-2000-quandl.csv") # Evaluate the strategy with the feed's bars. myStrategy = sma_crossover.SMACrossOver(feed, "ORCL/USD", 20) # Attach a returns analyzers to the strategy. returnsAnalyzer = returns.Returns(priceCurrency) myStrategy.attachAnalyzer(returnsAnalyzer) # Attach the plotter to the strategy. plt = plotter.StrategyPlotter(myStrategy) # Include the SMA in the instrument's subplot to get it displayed along with the closing prices. plt.getInstrumentSubplot("orcl").addDataSeries("SMA", myStrategy.getSMA()) # Plot the simple returns on each bar. plt.getOrCreateSubplot("returns").addDataSeries("Simple returns", returnsAnalyzer.getReturns()) # Run the strategy. myStrategy.run() myStrategy.info("Final portfolio value: $%.2f" % myStrategy.getResult()) # Plot the strategy. plt.plot()
from pyalgotrade import plotter from pyalgotrade.barfeed import yahoofeed from pyalgotrade.barfeed import feed from pyalgotrade.stratanalyzer import returns import sma_crossover # Load the yahoo feed from the CSV file feed = yahoofeed.Feed() feed.addBarsFromCSV("601766", "000060.csv") # Evaluate the strategy with the feed's bars. myStrategy = sma_crossover.SMACrossOver(feed, "601766", 20) # Attach a returns analyzers to the strategy. returnsAnalyzer = returns.Returns() myStrategy.attachAnalyzer(returnsAnalyzer) # Attach the plotter to the strategy. plt = plotter.StrategyPlotter(myStrategy) # Include the SMA in the instrument's subplot to get it displayed along with the closing prices. plt.getInstrumentSubplot("601766").addDataSeries("SMA", myStrategy.getSMA()) # Plot the simple returns on each bar. plt.getOrCreateSubplot("returns").addDataSeries("Simple returns", returnsAnalyzer.getReturns()) # Run the strategy. myStrategy.run() myStrategy.info("Final portfolio value: $%.2f" % myStrategy.getResult()) # Plot the strategy. plt.plot()
QQ: 592440193 """ from pyalgotrade import plotter from pyalgotrade.barfeed import yahoofeed from pyalgotrade.stratanalyzer import returns import sma_crossover # Load the yahoo feed from the CSV file feed = yahoofeed.Feed() feed.addBarsFromCSV("sz000651", "000651.csv") # Evaluate the strategy with the feed's bars. myStrategy = sma_crossover.SMACrossOver(feed, "sz000651", 20) # Attach a returns analyzers to the strategy. returnsAnalyzer = returns.Returns() myStrategy.attachAnalyzer(returnsAnalyzer) # Attach the plotter to the strategy. plt = plotter.StrategyPlotter(myStrategy) # Include the SMA in the instrument's subplot to get it displayed along with the closing prices. plt.getInstrumentSubplot("sz000651").addDataSeries("SMA", myStrategy.getSMA()) # Plot the simple returns on each bar. plt.getOrCreateSubplot("returns").addDataSeries("Simple returns", returnsAnalyzer.getReturns()) # Run the strategy. myStrategy.run()
if frequency == bar.Frequency.MINUTE: path = os.path.join(r'C:\Users\linner\Desktop\learn\pyalgo\histdata', 'minute') elif frequency == bar.Frequency.DAY: path = os.path.join( r'C:\Users\linner\Desktop\learn\pyalgo\histdata', 'day') filepath = os.path.join(path, instrument + market + ".csv") #############################################don't change ############################33 from pyalgotrade.cn.csvfeed import Feed barfeed = Feed(frequency) barfeed.setDateTimeFormat('%Y-%m-%d') barfeed.loadBars(instrument, market, fromDate, toDate, filepath) pyalgotrade_id = instrument + '.' + market myStrategy = sma_crossover.SMACrossOver(barfeed,pyalgotrade_id, 10) # Attach a returns analyzers to the strategy. returnsAnalyzer = returns.Returns() myStrategy.attachAnalyzer(returnsAnalyzer) # Attach the plotter to the strategy. plt = plotter.StrategyPlotter(myStrategy) # Include the SMA in the instrument's subplot to get it displayed along with the closing prices. plt.getInstrumentSubplot("600288").addDataSeries("SMA", myStrategy.getSMA()) # Plot the simple returns on each bar. plt.getOrCreateSubplot("returns").addDataSeries("Simple returns", returnsAnalyzer.getReturns()) # Run the strategy.