def MakeDataToCsv(): ''' 生成沪深300的股票数据到指定的文件夹 :return: ''' stockList = mt_read_index_codes('000300.SH') stockList = list(map(lambda x: x.split(".")[0], stockList)) stockList = list(map(mt_add_suffix_name, stockList)) count = 0 resultTemp = pd.DataFrame() for stock in stockList: df = QAStock2btData(stock, '2010-01-01', '2020-10-10') df = IndicateBias(df, 20, 60, 120) df = IndicatePctChange(df, 1) df = IndicatePctChange(df, 3) df = IndicatePctChange(df, 5) df = IndicatePctChange(df, 10) df = IndicatePctChange(df, 20) df = IndicatePctChange(df, 60) df = IndicatePctChange(df, 120) df = IndicatePctChange(df, 1, True) df = IndicatePctChange(df, 3, True) df = IndicatePctChange(df, 5, True) df = IndicatePctChange(df, 10, True) df = IndicatePctChange(df, 20, True) df = IndicateBias(df, 20, 60, 120) df.to_csv('./selectStock/' + stock + '.csv') count += 1 print("current code is :%s" % stock) resultTemp = pd.concat([resultTemp, df]) if count > 5000: break
def runstrat(): args = parse_args() cerebro = bt.Cerebro(cheat_on_open=True) cerebro.broker.setcommission(commission=0.0012, stocklike=True) # cerebro.broker.set_coo(True) # Data feed kwargs # '15min', '30min', '60min', # dataframe = read_dataframe(args.data, args.years, ['15min', '60min', 'd']) # dataframe = readFromDb(args.data, args.fq, args.years, ["5min", '15min', '60min', 'd']) # dataframe = readFromDb(args.data, args.fq, args.years, ['d']) # for i in range(len(dataframe)): # cerebro.adddata(bt.feeds.PandasData(dataname=dataframe[i])) # for i in range(len(dataframe)): # temp_df = macd_extend_data(dataframe[i]) # cerebro.adddata(pandas_divergence(dataname=temp_df, # divergence_top=temp_df.columns.to_list().index('divergence_top'), # divergence_bottom=temp_df.columns.to_list().index('divergence_bottom'))) # dataframe = QAIndex2btData("159934", '2014-01-01', '2020-10-13') dataframe = QAStock2btData("000002", '2008-01-01', '2021-04-30') # dataframe = read_dataframe('000651.csv', '2014-2020', ['d'])[0] cerebro.adddata(bt.feeds.PandasData(dataname=dataframe)) cerebro.addstrategy(lnkyzhangSt) # cerebro.addstrategy(MacdDivergenceSt) # cerebro.addstrategy(LeiGoneSt) cerebro.addanalyzer(bt.analyzers.DrawDown, _name='DrawDown') cerebro.addanalyzer(bt.analyzers.AnnualReturn, _name='AnnualReturn') cerebro.addanalyzer(bt.analyzers.PyFolio, _name='pyfolio') strat = cerebro.run(stdstats=True, runonce=False) drawDown = strat[0].analyzers.DrawDown.get_analysis() print("max drawdown: %f", drawDown.max.drawdown) annualReturn = strat[0].analyzers.AnnualReturn.get_analysis() print("annualReturn: %s", annualReturn) pyfoliozer = strat[0].analyzers.getbyname('pyfolio') returns, positions, transactions, gross_lev = pyfoliozer.get_pf_items() transactions.to_csv("transtion.csv") if args.plot: # cerebro.plot(style='line') cerebro.plot(style='candle', iplot=False)
def MakeDataToCsv(): ''' 生成沪深300的股票数据到指定的文件夹 :return: ''' stockList = mt_read_index_codes('000300.SH') stockList = list(map(lambda x: x.split(".")[0], stockList)) stockList = list(map(mt_add_suffix_name, stockList)) count = 0 resultTemp = pd.DataFrame() for stock in stockList: df = QAStock2btData(stock, '2010-01-01', '2020-10-10') df_indBias = IndicateBias(df, 20, 60, 120) df = stopPriceStatistic(df) df = df.merge(df_indBias[["s_bias", "m_bias", "l_bias"]], left_on="startDate", right_index=True) df.to_csv('./analysisData/' + stock + '.csv') count += 1 print("current code is :%s" % stock) resultTemp = pd.concat([resultTemp, df]) if count > 5000: break
# # Create a calendar # sse = mcal.get_calendar('SSE', open_time=time(9, 30), close_time=time(15, 00)) # print(sse.tz.zone) # print('open, close: %s, %s' % (sse.open_time, sse.close_time)) # # Show available calendars # # print(mcal.get_calendar_names()) # early = sse.schedule(start_date='2000-01-01', end_date='2000-04-10') # # print(early) # from BackTraderTest.BackTraderFunc.makeData import QAStock2btData from matplotlib import pyplot as plt import sympy dataframe = QAStock2btData("000651", '2012-01-01', '2029-01-13') # 测试natr # a = ta.ATR(dataframe.high, dataframe.low, dataframe.close) # ema20 = ta.EMA(dataframe.close) # # a = a / ema20 upperband, middleband, lowerband = ta.BBANDS(dataframe.close,timeperiod=20) real = (upperband - lowerband) / middleband fig, ax = plt.subplots(figsize=(10, 7)) ax.hist(real, bins=20, rwidth=0.5) ax.set_title("Simple Histogram")