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)