def main(self, coinname): # 获取数据文件列表 klineFileList = self.model.getKlineFileList(coinname) # 获取运行期间 timestamp_start = Base.date_to_timestamp(self.dict_param['date_start']) timestamp_end = Base.date_to_timestamp(self.dict_param['date_end']) self.dict_record['timestampNow'] = timestamp_start # 获取面值 mianzhi = Base.get_contract_size(coinname) # 遍历listKline for klineFile in klineFileList: df_1m = pd.read_csv(klineFile, index_col=0).reset_index() for i in range(len(df_1m)): timestamp = int(df_1m.loc[i, 'timestamp']) if timestamp_start <= self.dict_record[ 'timestampNow'] < timestamp <= timestamp_end: # 解析数据 self.dict_record['timestampNow'] = timestamp date = df_1m.loc[i, 'date'] self.date = df_1m.loc[i, 'date'] open = float(df_1m.loc[i, 'open']) high = float(df_1m.loc[i, 'high']) low = float(df_1m.loc[i, 'low']) close = float(df_1m.loc[i, 'close']) # 执行循环 self.loop(timestamp, date, open, high, low, close, mianzhi) elif timestamp > timestamp_end: # 总结数据 self.dict_param['isStop'] = True print('end') break if self.dict_param['isStop']: break #总结 self.zongjie() return
def merge1m(path, symbol): df = None symbol = str(symbol).lower() list_csv = sorted(os.listdir(path), reverse=False) is_creat = False for i in range(len(list_csv)): csv = list_csv[i] if int(str(csv)[:10]) > Base.date_to_timestamp('2018-09-14 08:00'): print('i=' + str(i) + ' ' + csv) if not is_creat: is_creat = True df = pd.read_csv(path + csv, index_col=0) else: df = df.append(pd.read_csv(path + csv, index_col=0)) # 如果超过104.8w条,就跳出循环 if len(df) > 1048000: break # 去重 df.drop_duplicates(keep='last', inplace=True) df.to_csv(symbol + 'merge1m.csv', encoding="utf-8") print(symbol + '合并完成')
def get_atr(coin, path): atr_0615 = { 'btc': 378.52, 'eth': 45.54, 'eos': 1.5553, 'ltc': 8.71, 'etc': 1.6945, } df = pd.read_csv(path, index_col=0) df = df.reset_index() df['yesterday_close'] = df['close'].shift(1) # 计算tr df['tr'] = 0 print(df) print(df) for i in range(len(df)): df.loc[i, 'tr'] = max( abs(df.loc[i, 'high'] - df.loc[i, 'low']), abs(df.loc[i, 'high'] - df.loc[i, 'yesterday_close']), abs(df.loc[i, 'yesterday_close'] - df.loc[i, 'low'])) # 计算atr df['atr'] = 0 for i in range(len(df)): if i == 0: df.loc[i, 'atr'] = atr_0615[coin] else: if coin == 'etc' and df.loc[i, 'timestamp'] == Base.date_to_timestamp( '2018-06-25 08:00'): df.loc[i, 'atr'] = atr_0615[coin] df.loc[i, 'atr'] = round( (df.loc[i - 1, 'atr'] * 13 + df.loc[i, 'tr']) / 14, 4) # 删除多余的列 df.drop(columns=['yesterday_close', 'tr'], inplace=True) df.to_csv(path, index=None, encoding="utf-8") print(path + '增加atr成功')
def main(self, date_start, date_end, param=None): if param is None: param = {} return # 初始化容器 self.coin = str(param['symbol']).lower()[0:3] self.symbol = param['symbol'] self.param = param self.param['contract_size'] = Base.get_contract_size(self.coin) self.list_wg_trade = [] self.dict_acc = { 'money': 10000, 'margin_balance': 0, 'fund_start': 0, 'price_start': 0, 'lun_price_start': 0, 'lun_timestamp_start': 0, 'lun_margin_balance_start': 0, 'lun_fund_start': 0, } self.dict_record = { 'timestamp': 0, 'list_margin_balance': [], 'list_rate_kline': [], 'list_rate_profit': [], 'list_fund': [], 'list_money': [], 'list_rate_paper_duo': [], 'list_rate_paper_kong': [], 'list_rate_paper': [], } Base.txt_remove(self.symbol + '.txt') self.log('进入策略main函数,准备容器完成,param=' + str(param)) self.log('dict_acc=' + str(self.dict_acc)) # 遍历数据 path = os.path.abspath( os.getcwd()) + '/klineok/' + self.coin + '1m1dok.csv' df_1m = pd.read_csv(path, index_col=0).reset_index() for i in range(len(df_1m)): timestamp = int(df_1m.loc[i, 'timestamp']) if Base.date_to_timestamp( date_start) < timestamp < Base.date_to_timestamp(date_end): close = float(df_1m.loc[i, 'close']) high = float(df_1m.loc[i, 'high']) low = float(df_1m.loc[i, 'low']) atr = float(df_1m.loc[i, 'atr']) self.dict_data = { 'date': df_1m.loc[i, 'date'], 'timestamp': timestamp, 'open': float(df_1m.loc[i, 'open']), 'high': high, 'low': low, 'close': close, 'day_date': df_1m.loc[i, 'day_date'], 'ma60': float(df_1m.loc[i, 'ma60']), 'ma91': float(df_1m.loc[i, 'ma91']), 'atr': atr, } # 判断是否首次遍历 if self.dict_acc['price_start'] == 0: self.dict_acc['price_start'] = close self.dict_acc['fund_start'] = self.dict_acc['money'] continue # 判断买币 if self.dict_acc['margin_balance'] == 0 and self.dict_acc[ 'money'] == self.dict_acc['fund_start']: self.trade_coin(close, round(self.dict_acc['money'] / close, 4), True) continue # 判断创建网格 if len(self.list_wg_trade) <= 1: self.creat_wg_trade(close, self.dict_data['ma91']) continue # 合约交易 self.trade_contract(high, low, close, self.param['contract_size']) # 记录日志 self.record(close) elif timestamp > Base.date_to_timestamp(date_end): break elif len(self.dict_record['list_rate_profit']) > 0: if self.dict_record['list_rate_profit'][-1] < -30: Base.txt_write( './result/' + 'index', 'err' + str(self.dict_record['list_rate_profit'][-1])) break # 绘图 Base.txt_write( './result/' + 'index', '通过,最大回撤' + str(min(self.dict_record['list_rate_profit']))) Base.chart('margin_balance', self.dict_record['list_margin_balance'], path=self.path) Base.chart('rate_kline_rate_profit', self.dict_record['list_rate_kline'], self.dict_record['list_rate_profit'], path=self.path) Base.chart('fund_money', self.dict_record['list_fund'], self.dict_record['list_money'], path=self.path) Base.chart('paper_duo_paper_kong_paper', self.dict_record['list_rate_paper_duo'], self.dict_record['list_rate_paper_kong'], self.dict_record['list_rate_paper'], path=self.path) Base.wg_csv('final', int(self.dict_data['timestamp']), self.list_wg_trade, self.path)
def main(self, symbol, date_start, date_end, param=None): if param is None: param = {} coin = str(symbol).lower()[0:3] # 填充容器 self.symbol = symbol self.param = param self.param['contract_size'] = Base.get_contract_size(coin) self.list_wg = [] self.dict_acc = { 'fund_start': 10000, 'price_start': 0, 'lun_timestamp_start': 0, 'lun_margin_start': 0, 'margin_balance': 0, 'money': 10000, 'record_timestamp': 0, 'record_list_margin_balance': [], 'record_list_fund': [], 'record_list_rate_kline': [], 'record_list_rate_profit': [], 'record_list_rate_paper': [], 'record_list_rate_paper_duo': [], 'record_list_rate_paper_kong': [] } Base.txt_remove(self.symbol + '.txt') self.log('进入策略main函数,准备容器完成,param=' + str(param)) self.log('dict_acc=' + str(self.dict_acc)) # 遍历数据 path = os.path.abspath(os.getcwd()) + '/klineok/' + coin + '1m1dok.csv' df_1m = pd.read_csv(path, index_col=0).reset_index() for i in range(len(df_1m)): timestamp = int(df_1m.loc[i, 'timestamp']) if Base.date_to_timestamp( date_start) < timestamp < Base.date_to_timestamp(date_end): close = float(df_1m.loc[i, 'close']) high = float(df_1m.loc[i, 'high']) low = float(df_1m.loc[i, 'low']) atr = float(df_1m.loc[i, 'atr']) self.dict_data = { 'date': df_1m.loc[i, 'date'], 'timestamp': timestamp, 'open': float(df_1m.loc[i, 'open']), 'high': high, 'low': low, 'close': close, 'day_date': df_1m.loc[i, 'day_date'], 'ma60': float(df_1m.loc[i, 'ma60']), 'ma91': float(df_1m.loc[i, 'ma91']), 'atr': atr, } if self.dict_acc['price_start'] == 0: self.dict_acc['price_start'] = close margin_balance = self.dict_acc['margin_balance'] contract_size = self.param['contract_size'] paper_cover = int(margin_balance * close / contract_size) paper_each = max(1, round(paper_cover * self.param['paper'], 2)) self.check(close, timestamp) self.wg_trade(close, high, low, atr, timestamp, paper_each) elif timestamp > Base.date_to_timestamp(date_end): break # 绘图 Base.chart('margin_balance', self.dict_acc['record_list_margin_balance']) Base.chart('fund', self.dict_acc['record_list_fund']) Base.chart('kline_profit', self.dict_acc['record_list_rate_kline'], self.dict_acc['record_list_rate_profit']) Base.chart('record_list_rate_paper', self.dict_acc['record_list_rate_paper']) Base.chart('kline_paperduo_paperkong', self.dict_acc['record_list_rate_kline'], self.dict_acc['record_list_rate_paper_duo'], self.dict_acc['record_list_rate_paper_kong']) Base.wg_csv('final', int(self.dict_data['timestamp']), self.list_wg)