|  | @@ -183,8 +183,8 @@ def to_df(lt):
 | 
											
												
													
														|  |                                 '最小盈利', '总亏损', '平均亏损', '最大亏损', '最小亏损'])
 |  |                                 '最小盈利', '总亏损', '平均亏损', '最大亏损', '最小亏损'])
 | 
											
												
													
														|  |      df.sort_values(by=['周期', '波动率', '量能增长率'], ascending=True, inplace=True)
 |  |      df.sort_values(by=['周期', '波动率', '量能增长率'], ascending=True, inplace=True)
 | 
											
												
													
														|  |      df = df.reset_index(drop=True)
 |  |      df = df.reset_index(drop=True)
 | 
											
												
													
														|  | -    # df.to_csv(f"D:\Daniel\策略\策略穷举{dt.now().strftime('%Y%m%d')}.csv", index=True, encoding='utf-8', mode='w')
 |  | 
 | 
											
												
													
														|  | -    df.to_csv(f"/Users/daniel/Documents/策略/策略穷举{dt.now().strftime('%Y%m%d')}.csv", index=True, encoding='utf-8', mode='w')
 |  | 
 | 
											
												
													
														|  | 
 |  | +    df.to_csv(f"D:\Daniel\策略\策略穷举{dt.now().strftime('%Y%m%d')}.csv", index=True, encoding='utf-8', mode='w')
 | 
											
												
													
														|  | 
 |  | +    # df.to_csv(f"/Users/daniel/Documents/策略/策略穷举{dt.now().strftime('%Y%m%d')}.csv", index=True, encoding='utf-8', mode='w')
 | 
											
												
													
														|  |      print(df)
 |  |      print(df)
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  
 |  |  
 | 
											
										
											
												
													
														|  | @@ -196,68 +196,71 @@ def backtrader(list_date, table_list, result, result_change, result_change_fall,
 | 
											
												
													
														|  |          # print(stock)
 |  |          # print(stock)
 | 
											
												
													
														|  |          stk_df = pd.read_sql_table(stock, engine)
 |  |          stk_df = pd.read_sql_table(stock, engine)
 | 
											
												
													
														|  |          stk_df.time = pd.to_datetime(stk_df.time)
 |  |          stk_df.time = pd.to_datetime(stk_df.time)
 | 
											
												
													
														|  | -        stk_df['HL'] = stk_df['HL'].map({'L': 1,
 |  | 
 | 
											
												
													
														|  | -                                         'LL': 2,
 |  | 
 | 
											
												
													
														|  | -                                         'L*': 3,
 |  | 
 | 
											
												
													
														|  | -                                         'H': 4,
 |  | 
 | 
											
												
													
														|  | -                                         'HH': 5,
 |  | 
 | 
											
												
													
														|  | -                                         'H*': 6,
 |  | 
 | 
											
												
													
														|  | -                                         '-': 7})
 |  | 
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  | -        if len(stk_df) > 60:
 |  | 
 | 
											
												
													
														|  | -            cerebro = bt.Cerebro()
 |  | 
 | 
											
												
													
														|  | -            cerebro.addstrategy(TestStrategy, num=num, Volatility=Volatility, rate=rate)
 |  | 
 | 
											
												
													
														|  | -            cerebro.addsizer(bt.sizers.FixedSize, stake=10000)
 |  | 
 | 
											
												
													
														|  | -            data = MyPandasData(dataname=stk_df,
 |  | 
 | 
											
												
													
														|  | -                                fromdate=datetime.datetime(2017, 1, 1),
 |  | 
 | 
											
												
													
														|  | -                                todate=datetime.datetime(2022, 10, 30),
 |  | 
 | 
											
												
													
														|  | -                                datetime='time',
 |  | 
 | 
											
												
													
														|  | -                                open='open_back',
 |  | 
 | 
											
												
													
														|  | -                                close='close_back',
 |  | 
 | 
											
												
													
														|  | -                                high='high_back',
 |  | 
 | 
											
												
													
														|  | -                                low='low_back',
 |  | 
 | 
											
												
													
														|  | -                                volume='volume_back',
 |  | 
 | 
											
												
													
														|  | -                                hl='HL',
 |  | 
 | 
											
												
													
														|  | -                                dif='dif',
 |  | 
 | 
											
												
													
														|  | -                                dea='dea',
 |  | 
 | 
											
												
													
														|  | -                                macd='macd',
 |  | 
 | 
											
												
													
														|  | -                                rsi_6='rsi_6',
 |  | 
 | 
											
												
													
														|  | -                                rsi_12='rsi_12',
 |  | 
 | 
											
												
													
														|  | -                                rsi_24='rsi_24',
 |  | 
 | 
											
												
													
														|  | -                                # change,_pct='change_pct',
 |  | 
 | 
											
												
													
														|  | -                                # net_amount_main='net_amount_main',
 |  | 
 | 
											
												
													
														|  | -                                # net_pct_main='net_pct_main',
 |  | 
 | 
											
												
													
														|  | -                                # net_amount_xl='net_amount_xl',
 |  | 
 | 
											
												
													
														|  | -                                # net_pct_xl='net_pct_xl',
 |  | 
 | 
											
												
													
														|  | -                                # net_amount_l='net_amount_l',
 |  | 
 | 
											
												
													
														|  | -                                # net_pct_l='net_pct_l',
 |  | 
 | 
											
												
													
														|  | -                                # net_amount_m='net_amount_m',
 |  | 
 | 
											
												
													
														|  | -                                # net_pct_m='net_pct_m',
 |  | 
 | 
											
												
													
														|  | -                                # net_amount_s='net_amount_s',
 |  | 
 | 
											
												
													
														|  | -                                # net_pct_s='net_pct_s',
 |  | 
 | 
											
												
													
														|  | -                                )
 |  | 
 | 
											
												
													
														|  | -            # print('取值完成')
 |  | 
 | 
											
												
													
														|  | -            cerebro.adddata(data, name=stock)
 |  | 
 | 
											
												
													
														|  | -            cerebro.broker.setcash(100000.0)
 |  | 
 | 
											
												
													
														|  | -            cerebro.broker.setcommission(0.005)
 |  | 
 | 
											
												
													
														|  | -            cerebro.addanalyzer(bt.analyzers.PyFolio)
 |  | 
 | 
											
												
													
														|  | -            # 策略执行前的资金
 |  | 
 | 
											
												
													
														|  | -            # print('启动资金: %.2f' % cerebro.broker.getvalue())
 |  | 
 | 
											
												
													
														|  | -            try:
 |  | 
 | 
											
												
													
														|  | -                # 策略执行
 |  | 
 | 
											
												
													
														|  | -                cerebro.run()
 |  | 
 | 
											
												
													
														|  | -            except IndexError:
 |  | 
 | 
											
												
													
														|  | -                err_list.append(stock)
 |  | 
 | 
											
												
													
														|  | -            else:
 |  | 
 | 
											
												
													
														|  | -                if cerebro.broker.getvalue() > 100000.0:
 |  | 
 | 
											
												
													
														|  | -                    result_change.append((cerebro.broker.getvalue() / 10000 - 1))
 |  | 
 | 
											
												
													
														|  | -                    result.append(stock)
 |  | 
 | 
											
												
													
														|  | -                    # print('recode!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')
 |  | 
 | 
											
												
													
														|  | -                    # print(result)
 |  | 
 | 
											
												
													
														|  | 
 |  | +        try:
 | 
											
												
													
														|  | 
 |  | +            stk_df['HL'] = stk_df['HL'].map({'L': 1,
 | 
											
												
													
														|  | 
 |  | +                                             'LL': 2,
 | 
											
												
													
														|  | 
 |  | +                                             'L*': 3,
 | 
											
												
													
														|  | 
 |  | +                                             'H': 4,
 | 
											
												
													
														|  | 
 |  | +                                             'HH': 5,
 | 
											
												
													
														|  | 
 |  | +                                             'H*': 6,
 | 
											
												
													
														|  | 
 |  | +                                             '-': 7})
 | 
											
												
													
														|  | 
 |  | +        except BaseException:
 | 
											
												
													
														|  | 
 |  | +            print(stock, 'HL 可能没有')
 | 
											
												
													
														|  | 
 |  | +        else:
 | 
											
												
													
														|  | 
 |  | +            if len(stk_df) > 60:
 | 
											
												
													
														|  | 
 |  | +                cerebro = bt.Cerebro()
 | 
											
												
													
														|  | 
 |  | +                cerebro.addstrategy(TestStrategy, num=num, Volatility=Volatility, rate=rate)
 | 
											
												
													
														|  | 
 |  | +                cerebro.addsizer(bt.sizers.FixedSize, stake=10000)
 | 
											
												
													
														|  | 
 |  | +                data = MyPandasData(dataname=stk_df,
 | 
											
												
													
														|  | 
 |  | +                                    fromdate=datetime.datetime(2017, 1, 1),
 | 
											
												
													
														|  | 
 |  | +                                    todate=datetime.datetime(2022, 10, 30),
 | 
											
												
													
														|  | 
 |  | +                                    datetime='time',
 | 
											
												
													
														|  | 
 |  | +                                    open='open_back',
 | 
											
												
													
														|  | 
 |  | +                                    close='close_back',
 | 
											
												
													
														|  | 
 |  | +                                    high='high_back',
 | 
											
												
													
														|  | 
 |  | +                                    low='low_back',
 | 
											
												
													
														|  | 
 |  | +                                    volume='volume_back',
 | 
											
												
													
														|  | 
 |  | +                                    hl='HL',
 | 
											
												
													
														|  | 
 |  | +                                    dif='dif',
 | 
											
												
													
														|  | 
 |  | +                                    dea='dea',
 | 
											
												
													
														|  | 
 |  | +                                    macd='macd',
 | 
											
												
													
														|  | 
 |  | +                                    rsi_6='rsi_6',
 | 
											
												
													
														|  | 
 |  | +                                    rsi_12='rsi_12',
 | 
											
												
													
														|  | 
 |  | +                                    rsi_24='rsi_24',
 | 
											
												
													
														|  | 
 |  | +                                    # change,_pct='change_pct',
 | 
											
												
													
														|  | 
 |  | +                                    # net_amount_main='net_amount_main',
 | 
											
												
													
														|  | 
 |  | +                                    # net_pct_main='net_pct_main',
 | 
											
												
													
														|  | 
 |  | +                                    # net_amount_xl='net_amount_xl',
 | 
											
												
													
														|  | 
 |  | +                                    # net_pct_xl='net_pct_xl',
 | 
											
												
													
														|  | 
 |  | +                                    # net_amount_l='net_amount_l',
 | 
											
												
													
														|  | 
 |  | +                                    # net_pct_l='net_pct_l',
 | 
											
												
													
														|  | 
 |  | +                                    # net_amount_m='net_amount_m',
 | 
											
												
													
														|  | 
 |  | +                                    # net_pct_m='net_pct_m',
 | 
											
												
													
														|  | 
 |  | +                                    # net_amount_s='net_amount_s',
 | 
											
												
													
														|  | 
 |  | +                                    # net_pct_s='net_pct_s',
 | 
											
												
													
														|  | 
 |  | +                                    )
 | 
											
												
													
														|  | 
 |  | +                # print('取值完成')
 | 
											
												
													
														|  | 
 |  | +                cerebro.adddata(data, name=stock)
 | 
											
												
													
														|  | 
 |  | +                cerebro.broker.setcash(100000.0)
 | 
											
												
													
														|  | 
 |  | +                cerebro.broker.setcommission(0.005)
 | 
											
												
													
														|  | 
 |  | +                cerebro.addanalyzer(bt.analyzers.PyFolio)
 | 
											
												
													
														|  | 
 |  | +                # 策略执行前的资金
 | 
											
												
													
														|  | 
 |  | +                # print('启动资金: %.2f' % cerebro.broker.getvalue())
 | 
											
												
													
														|  | 
 |  | +                try:
 | 
											
												
													
														|  | 
 |  | +                    # 策略执行
 | 
											
												
													
														|  | 
 |  | +                    cerebro.run()
 | 
											
												
													
														|  | 
 |  | +                except IndexError:
 | 
											
												
													
														|  | 
 |  | +                    err_list.append(stock)
 | 
											
												
													
														|  |                  else:
 |  |                  else:
 | 
											
												
													
														|  | -                    result_change_fall.append((1 - cerebro.broker.getvalue() / 10000))
 |  | 
 | 
											
												
													
														|  | -                    # print('aaaaaaaaaaa')
 |  | 
 | 
											
												
													
														|  | -                    # print(result_change_fall)
 |  | 
 | 
											
												
													
														|  | 
 |  | +                    if cerebro.broker.getvalue() > 100000.0:
 | 
											
												
													
														|  | 
 |  | +                        result_change.append((cerebro.broker.getvalue() / 10000 - 1))
 | 
											
												
													
														|  | 
 |  | +                        result.append(stock)
 | 
											
												
													
														|  | 
 |  | +                        # print('recode!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')
 | 
											
												
													
														|  | 
 |  | +                        # print(result)
 | 
											
												
													
														|  | 
 |  | +                    else:
 | 
											
												
													
														|  | 
 |  | +                        result_change_fall.append((1 - cerebro.broker.getvalue() / 10000))
 | 
											
												
													
														|  | 
 |  | +                        # print('aaaaaaaaaaa')
 | 
											
												
													
														|  | 
 |  | +                        # print(result_change_fall)
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |      if len(result) * len(result_change) * len(result_change_fall) != 0:
 |  |      if len(result) * len(result_change) * len(result_change_fall) != 0:
 | 
											
												
													
														|  |          print(f'以{num}内最低值波动{Volatility}为支撑、量能增长率为{rate}%,结果状态为:')
 |  |          print(f'以{num}内最低值波动{Volatility}为支撑、量能增长率为{rate}%,结果状态为:')
 |