def test_update_day_k_data_in_local(self): manager = HistoryDataManager.HistoryDataManager() local_start_date = datetime.datetime.strptime('2019-05-01', constant.key_date_format) local_end_date = datetime.datetime.strptime('2020-05-07', constant.key_date_format) rtn = manager._update_day_k_data_in_local('sh.600016', local_start_date, local_end_date) print(rtn)
def plot_stock(title): # 获取数据 start= '2015-01-01' end = '2020-07-07' historyManager = HistoryDataManager.HistoryDataManager() dd = historyManager.get_day_k_history('sh.600015', start, end) dd.index = pd.to_datetime(dd.date) dd.close = dd.close.astype(float) dd.close.plot(figsize=(14, 6), color='r') plt.title(title + '价格走势\n' + start + ':' + end, size=15) plt.annotate(f'期间累计涨幅:{(dd.close[-1]/dd.close[0]-1)*100:.2f}%', xy=(dd.index[-150], dd.close.mean()), xytext=(dd.index[-500], dd.close.min()), bbox=dict(boxstyle='round,pad=0.5', fc='yellow', alpha=0.5), arrowprops=dict(facecolor='green', shrink=0.05), fontsize=12) plt.savefig("mygraph.png")
def main(code, start, end='', startcash=10000, qts=500, com=0.001): print('###code:' + code) # 创建主控制器 cerebro = bt.Cerebro() # 导入策略参数寻优 cerebro.optstrategy(MyStrategy) # 获取数据 historyManager = HistoryDataManager.HistoryDataManager() df = historyManager.get_day_k_history(code, '2016-01-01', '2020-07-07') if not base.is_df_validate(df): return startcash df = df.sort_values(by=['date'], ascending=True) df = df[df['volume'].astype(float) > 0.0] if len(df) < 100: return startcash df.index = pd.to_datetime(df.date) df = df[['open', 'high', 'low', 'close', 'volume']].astype(float) data = bt.feeds.PandasData(dataname=df) # 初始化cerebro回测系统设置 cerebro = bt.Cerebro() # 加载数据 cerebro.adddata(data) # 将交易策略加载到回测系统中 cerebro.addstrategy(MyStrategy) # 设置初始资本为100,000 cerebro.broker.setcash(100000.0) # 每次固定交易数量 cerebro.addsizer(bt.sizers.FixedSize, stake=1000) # 手续费 cerebro.broker.setcommission(commission=0.001) print(code + '初始资金: %.2f' % cerebro.broker.getvalue()) cerebro.addanalyzer(bt.analyzers.SharpeRatio, _name='SharpeRatio') cerebro.addanalyzer(bt.analyzers.DrawDown, _name='DW') results = cerebro.run() return cerebro.broker.getvalue()
def main(code, start, end='', startcash=10000, qts=500, com=0.001): # 创建主控制器 cerebro = bt.Cerebro() # 导入策略参数寻优 cerebro.optstrategy(MyStrategy, maperiod=range(3, 31)) # 获取数据 historyManager = HistoryDataManager.HistoryDataManager() df = historyManager.get_day_k_history('sh.600015', '2015-01-01', '2020-07-07') df.index = pd.to_datetime(df.date) df = df[['open', 'high', 'low', 'close', 'volume']].astype(float) # 将数据加载至回测系统 data = bt.feeds.PandasData(dataname=df) cerebro.adddata(data) # broker设置资金、手续费 cerebro.broker.setcash(startcash) cerebro.broker.setcommission(commission=com) # 设置买入设置,策略,数量 cerebro.addsizer(bt.sizers.FixedSize, stake=qts) print('期初总资金: %.2f' % cerebro.broker.getvalue()) cerebro.run(maxcpus=1) print('期末总资金: %.2f' % cerebro.broker.getvalue())
def __init__(self): self.historyManager = HistoryDataManager.HistoryDataManager() self.jqDataCrawler = cj.JQDataCrawler() self.bonusDataManager = BonusDataManager()
def test_get_day_k_history(self): manager = HistoryDataManager.HistoryDataManager() rtn = manager.get_day_k_history('sh.600006', '2020-06-8', '2020-06-08') print(rtn[['code', 'date', 'peTTM', 'psTTM']])
def __init__(self): self.historyManager = HistoryDataManager.HistoryDataManager()