def print_progress(self, _date): _year = StockDataSource.datetime(_date).year if self.year != _year: # _total_value = max(1000*10000, self.sums) _total_value = self.sums self.year_values.append( [self.year, '%.02f'%(self.account.total_value*0.0001), \ '%.02f'%(self.account.total_value*100/_total_value-100)] ) self.year = _year self.sums = self.account.total_value
def fixed_invest(self, code, data_list, pe_vec): self._start_test(0, 10000) all_days, curr_month = len(data_list[0]), -1 opens, highs, lows, closes = data_list[1], data_list[2], data_list[ 3], data_list[4] for _idx in range(0, all_days): float_date = data_list[0][_idx] int_date = StockDataSource.int_date(float_date) self.print_progress(int_date) stock_data = { 'ts_code': code, 'open': opens[_idx], 'high': highs[_idx], 'low': lows[_idx], 'close': closes[_idx] } _month = StockDataSource.datetime(int_date).month self.account.ProfitDaily(int_date) if curr_month != _month: curr_month = _month self.account.Rechange(10000) kp = opens[_idx] if pe_vec[_idx] > 20.83: # sell _volume = self.account.credit / kp elif pe_vec[_idx] > 13.89: _volume = self.account.cash / kp else: _volume = self.account.cash / kp * 1.7 self._order(int_date, stock_data, _volume, kp, 0) self.curr_closes[code] = closes[_idx] self.account.UpdateValue(self.curr_closes, int_date) self.market_values.append(self.account.total_value) self.position_ratios.append(self.account.position_value / self.account.total_value) print( pandas.DataFrame(self.hist_orders, columns=[ 'date', 'trade_price', 'stop_price', 'volume', 'total_value', 'credit', 'lever', 'ts_code' ])) print( pandas.DataFrame(self.year_values, columns=['year', 'cash', 'ratio'])) self.account.status_info()
def start_test(self, long_per, init_cash=1000 * 10000): funcName = sys._getframe().f_back.f_code.co_name #获取调用函数名 lineNumber = sys._getframe().f_back.f_lineno #获取行号 coname = sys._getframe().f_code.co_name #获取当前函数名 print(coname, funcName, lineNumber) self.account = StockAccount(init_cash, 0) self.year, self.start = StockDataSource.datetime( self.dates[0]).year, time.time() self.order_count, self.open_value, self.sums = 0, 0, self.account.cash self.records, self.year_values = [], [] self.curr_orders, self.hist_orders = [], [] self.market_values, self.position_ratios = [], [] for i in range(0, long_per): # self.account.ProfitDaily() _total_value = max(1000 * 10000, self.account.total_value) self.market_values.append(self.account.total_value) self.position_ratios.append(self.account.position_value / _total_value)
def fixed_invest(self, code, data_list, index_long): self._start_test(0, 10 * 10000) all_days, curr_month = len(data_list[0]), -1 opens, highs, lows, closes = data_list[1], data_list[2], data_list[ 3], data_list[4] for _idx in range(0, all_days): float_date = data_list[0][_idx] int_date = StockDataSource.int_date(float_date) self.print_progress(int_date) stock_data = { 'ts_code': code, 'open': opens[_idx], 'high': highs[_idx], 'low': lows[_idx], 'close': closes[_idx] } _month = StockDataSource.datetime(int_date).month self.account.ProfitDaily(int_date) if curr_month != _month: curr_month = _month self.account.Rechange(10000) # _open_unit = self.account.cash / opens[_idx] # _open_unit = (self.account.cash + 120000 - self.account.credit) / opens[_idx] # self._order(int_date, stock_data, _open_unit, opens[_idx], 0) # print( int_date, self.account.cash, self.account.credit ) kp = index_long['short'][_idx] * 1.05 kp = index_long['long'][_idx] * 0.95 _cash = self.account.cash + 120000 - self.account.credit if lows[_idx] < kp and _cash > kp * 100: if kp > opens[_idx]: kp = opens[_idx] # if highs[_idx] > kp and _cash > kp*100: # if kp < opens[_idx]: # kp = opens[_idx] _open_unit = _cash / kp self._order(int_date, stock_data, _open_unit, kp, 0) self.curr_closes[code] = closes[_idx] self.account.UpdateValue(self.curr_closes, int_date) self.market_values.append(self.account.total_value) self.position_ratios.append(self.account.position_value / self.account.total_value) print( pandas.DataFrame(self.hist_orders, columns=[ 'date', 'trade_price', 'stop_price', 'volume', 'total_value', 'credit', 'lever', 'ts_code' ])) print( pandas.DataFrame(self.year_values, columns=['year', 'cash', 'ratio'])) self.account.status_info()