def log(self, content): if len(self.dict_data) > 0: content = self.dict_data['date'] + '----' + str( self.dict_data['close']) + '----' + content Base.txt_write(self.symbol, content) self.log_content = content print(content)
def __init__(self): self.symbol = '' self.coin = '' self.param = {} self.dict_data = {} self.list_wg_trade = [] self.dict_acc = {} self.dict_record = {} #遍历参数 list_symbol = ['ltc', 'eth', 'eth', 'eos'] list_interval = [2, 1, 3] list_paper_shun = [0.05, 0.1, 0.025] list_paper_ni = [0.05, 0.025] list_profit_close = [1, 2] list_profit_close_all = [1, 2, 3] index_path = 0 for i1 in range(len(list_symbol)): for i2 in range(len(list_interval)): for i3 in range(len(list_paper_shun)): for i4 in range(len(list_paper_ni)): for i5 in range(len(list_profit_close)): for i6 in range(len(list_profit_close_all)): param = { 'symbol': list_symbol[i1], # 单边网格格数 'lattice_num': 50, # 单边网格格数 'interval': list_interval[i2], # 间隔占均线的比例 'paper_shun': list_paper_shun[i3], # 顺势网格相对对冲张数 'paper_ni': list_paper_ni[i4], # 逆势网格张数 'profit_close': list_profit_close[i5], # 震荡止盈,1就是百分之一1 'profit_close_all': list_profit_close_all[i6], # 整体止盈 } #创建目录 index_path += 1 self.path = './result/' + str(index_path) if not os.path.exists(self.path): os.makedirs(self.path) Base.txt_write( './result/' + 'index', str(self.path) + '--------' + str(param)) self.main(date_start='2018-09-16 12:00', date_end='2019-10-15 12:00', param=param)
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 log(self, content): content = self.date + '---------' + content print(content) Base.txt_write(self.coinname + '.txt', content)