def record(self, price): # 更新当前权益 self.dict_acc['margin_balance'] = self.dict_acc[ 'lun_margin_balance_start'] + self.get_profit_sum() margin_balance = self.dict_acc['margin_balance'] fund = margin_balance * price lun_rate_profit = round( fund / self.dict_acc['lun_fund_start'] * 100 - 100, 2) if lun_rate_profit > self.param['profit_close_all']: # 绘图 Base.wg_csv('收益率=' + str(lun_rate_profit), int(self.dict_data['timestamp']), self.list_wg_trade, self.path) # temp = self.list_wg_trade[0] self.list_wg_trade.clear() # self.list_wg_trade.append(temp) self.log('整体止盈,本轮收益率' + str(lun_rate_profit)) # 卖币 coin_num_sell = round( (fund - self.dict_acc['lun_fund_start']) / price, 4) self.trade_coin(price, coin_num_sell, False) return else: if self.dict_data['timestamp'] > self.dict_record['timestamp']: # 本轮整体止盈 self.dict_record[ 'timestamp'] = self.dict_data['timestamp'] + 3600 * 6 # 余额 self.dict_record['list_margin_balance'].append(margin_balance) # 基准率 rate_kline = round( price / self.dict_acc['price_start'] * 100 - 100, 2) self.dict_record['list_rate_kline'].append(rate_kline) # 收益率 rate_profit = round( (fund + self.dict_acc['money']) / self.dict_acc['lun_fund_start'] * 100 - 100, 2) self.dict_record['list_rate_profit'].append(rate_profit) # fund money self.dict_record['list_fund'].append(fund) self.dict_record['list_money'].append(self.dict_acc['money']) # 多空张数 paper_duo = self.get_paper('duo_ok') paper_kong = self.get_paper('kong_ok') self.dict_record['list_rate_paper_duo'].append(paper_duo) self.dict_record['list_rate_paper_kong'].append(paper_kong) # 总张数 self.dict_record['list_rate_paper'].append(paper_duo + paper_kong) # 本轮基准率 lun_rate_kline = round( price / self.dict_acc['lun_price_start'] * 100 - 100, 2) m1 = '基准率' + str(rate_kline) + '收益率' + str( rate_profit) + 'money' + str(self.dict_acc['money']) m2 = '多张' + str(paper_duo) + '空张' + str( paper_kong) + '本轮基准率' + str( lun_rate_kline) + '本轮收益率' + str(lun_rate_profit) m3 = 'price=' + str(price) self.log('定时记录' + m1) self.log('定时记录' + m2) self.log('定时记录' + m3) return
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)
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 check(self, price, timestamp): # 买币卖币 if self.dict_acc['margin_balance'] == 0 and self.dict_acc[ 'money'] == self.dict_acc['fund_start']: self.coin_trade(price, round(self.dict_acc['money'] / price, 4), True) # 对冲交易 if len(self.list_wg) == 0: self.cover_trade(price) # 更新账户权益 self.dict_acc['margin_balance'] = self.dict_acc[ 'lun_margin_start'] + self.update_online_profit(price) # 更新最后一次交易记录 num_duo_ing = self.count_status('duo_ing') num_kong_ing = self.count_status('kong_ing') num_duo_closeing = self.count_status('duo_closeing') num_kong_closeing = self.count_status('kong_closeing') if num_duo_ing == 0 and num_duo_closeing == 0: self.dict_record['last_price_duo'] = 99999 self.dict_record['last_timestamp_duo'] = 0 if num_kong_ing == 0 and num_kong_closeing == 0: self.dict_record['last_price_kong'] = 0 self.dict_record['last_timestamp_kong'] = 0 # 获取数据 margin_balance = self.dict_acc['margin_balance'] money = self.dict_acc['money'] fund = round(margin_balance * price, 2) paper_kong_ok = self.get_sum_paper('kong_ok') paper_kong_closeing = self.get_sum_paper('kong_closeing') paper_duo_closeing = self.get_sum_paper('duo_closeing') # 计算收益率 rate_kline = round(price / self.dict_acc['price_start'] * 100 - 100, 2) rate_fund = round(fund / self.dict_acc['fund_start'] * 100 - 100, 2) rate_profit = round(money / self.dict_acc['fund_start'] * 100, 2) rate_paper = paper_duo_closeing + paper_kong_closeing + paper_kong_ok # 记录列表信息 if timestamp - self.dict_acc['record_timestamp'] > 60 * 60 * 8: self.dict_acc['record_timestamp'] = timestamp self.dict_acc['record_list_margin_balance'].append(margin_balance) self.dict_acc['record_list_fund'].append(fund) self.dict_acc['record_list_rate_kline'].append(rate_kline) self.dict_acc['record_list_rate_profit'].append(rate_profit) self.dict_acc['record_list_rate_paper'].append(rate_paper) self.dict_acc['record_list_rate_paper_duo'].append( paper_duo_closeing) self.dict_acc['record_list_rate_paper_kong'].append( paper_kong_closeing) # 输出记录 note1 = '冲' + str(paper_kong_ok) + '多' + str( paper_duo_closeing) + '空' + str( paper_kong_closeing) + '多浮' + str( self.update_online_profit(price, 'duo')) + '空浮' + str( self.update_online_profit(price, 'kong')) note11 = '多均' + str(self.get_price_aver(True)) + '空均' + str( self.get_price_aver(False)) note2 = '权益' + str(round( margin_balance, 4)) + '资产' + str(fund) + '基准率' + str( rate_kline) + '%收益率' + str(rate_profit) + '%' note3 = '本轮初始权益' + str(self.dict_acc['lun_margin_start']) self.log('记录' + note1 + note11 + note2) # 判断是否全平 if rate_fund > self.param['stop_profit_all']: days_cost = round((self.dict_data['timestamp'] - self.dict_acc['lun_timestamp_start']) / 86400, 2) Base.wg_csv('sell' + str(rate_fund) + 'days' + str(days_cost), int(self.dict_data['timestamp']), self.list_wg) self.list_wg.clear() num_sell = round((fund - self.dict_acc['fund_start']) / price, 4) self.coin_trade(price, num_sell, False) self.check(price, timestamp)