def __init__(self): utils.TradeTest.__init__(self) sdsr = data.StockData_SQLite(self.files[0]) self.curr_closes = {} self.float_str = StockDataSource.float_date(self.dates[0]) self.float_end = StockDataSource.float_date(self.dates[1]) for code in self.codes: sdsr.read_stock(code, self.dates[0], self.dates[1]) if not len(sdsr.stocks): sdsr.load(code, self.dates[0], self.dates[1], self.params[0]) if not len(sdsr.stocks): continue data_list, _dates = sdsr.parse_price() _data_vec = numpy.transpose(data_list) turtle = index.TurtleIndex() index_long = index.LongTurtleIndex(turtle, _data_vec, self.turtle_args[2], self.turtle_args[3], self.turtle_args[0], self.turtle_args[1]) self.fixed_invest(code, _data_vec, index_long) print("%s %s, use time %.02f seconds." % ('single_turtle', code, time.time() - self.start)) self.account.save_records(code, self.files[2]) self.plot(data_list, index_long, code)
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 calc_index(self, data_db, _index_code, stock_count = 300): sdsr = data.StockData_SQLite( data_db ) y, m, count = 2005, 7, 0 sttdate, enddate = '20000101', '20190601' delta1, delta2 = (self.cycle_month[0]/12.)*365., (self.cycle_month[1]/12.)*365. for i in range(0, 56): _end = dt.datetime(y, m, 1) _stt = _end + dt.timedelta(days=-30) _stt = StockDataSource.str_date(_stt) _end = StockDataSource.str_date(_end) m += 3 if m > 12: y, m = y+1, 1 df = StockData_Tushare.ts_api.index_weight(index_code=_index_code, start_date = _stt, end_date = _end) if len(df) < stock_count: continue consts = df.head(stock_count) for index, row in consts.iterrows(): code = row['con_code'] _sql = 'select ts_code, count(*) as rows from strong_indexs where ts_code = "%s";'%code _data_row = pd_sql.read_sql(_sql, self.conn) if _data_row.iloc[0, 1] > 0: continue sdsr.read_stock(code, sttdate, enddate) _len = len(sdsr.stocks) if not _len: continue data_list, _dates = sdsr.parse_price() data_vect = numpy.transpose(data_list) data_vect[4] = list(map(lambda x: math.log(x), data_vect[4])) chgs1, ks1, bs1 = self.calc_index(data_vect[0], data_vect[4], delta1) chgs2, ks2, bs2 = self.calc_index(data_vect[0], data_vect[4], delta2) _sql = 'INSERT INTO strong_indexs (ts_code, trade_date, close, chg1, k1, b1, chg2, k2, b2)\ VALUES (?,?,?,?,?,?,?,?,?);' _codes = [code for _ in range(_len)] _vect = [_codes, _dates, data_vect[4], chgs1, ks1, bs1, chgs2, ks2, bs2] self.curs.executemany( _sql, numpy.transpose( _vect ) ) # count = self.calc(df.head(30), sdsr) print( 'calc %d stocks\' index on %s.'%(count, _end) )
def list_info(stock_data, long_index, stop_loss, count=0): list_len, starti = len(stock_data[0]), 0 if count: starti = list_len - min(count, list_len) _close, _high, _low = stock_data[4], stock_data[2], stock_data[3] info_list = [] for i in range(starti, list_len): append_price = long_index['key_price'][i] + long_index['wave'][i] stop_price = long_index['key_price'][ i] - long_index['wave'][i] * stop_loss info_list.append([ StockDataSource.str_date(stock_data[0][i]), _close[i], _high[i], _low[i], long_index['state'][i], '%.02f' % long_index['key_price'][i], '%.02f' % append_price, '%.02f' % stop_price, '%.02f' % long_index['short'][i], '%.02f' % long_index['wave'][i] ]) cols = [ 'trade_date', 'close', 'high', 'low', 'state', 'key_price', 'append', 'stop', 'profit', 'wave' ] return pandas.DataFrame(info_list, columns=cols)
def list_info(self, stock_data, long_index, count=0): list_len = len(stock_data[0]) starti = 0 if count: starti = list_len - min(count, list_len) info_list = [] for i in range(starti, list_len): if long_index['state'][i]: append_price = long_index['key_price'][i] + long_index['wave'][ i] stop_price = long_index['key_price'][ i] - long_index['wave'][i] * self.turtle_args[0] else: append_price, stop_price = 0, 0 info_list.append([ StockDataSource.str_date(stock_data[0][i]), stock_data[4][i], long_index['state'][i], '%.02f' % long_index['key_price'][i], '%.02f' % append_price, stop_price, long_index['short'][i], '%.02f' % long_index['wave'][i] ]) cols = [ 'trade_date', 'close', 'state', 'key_price', 'append', 'stop', 'profit', 'wave' ] return pandas.DataFrame(info_list, columns=cols)
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 __init__(self): utils.TradeTest.__init__(self) sdsr = data.StockData_SQLite(self.files[0]) self.curr_closes = {} self.float_str = StockDataSource.float_date(self.dates[0]) self.float_end = StockDataSource.float_date(self.dates[1]) for code in self.codes: sdsr.read_stock(code, self.dates[0], self.dates[1]) if not len(sdsr.stocks): sdsr.load(code, self.dates[0], self.dates[1], self.params[0]) if not len(sdsr.stocks): continue data_list, _dates = sdsr.parse_price() _data_vec = numpy.transpose(data_list) pe_table = sdsr.index_daaily(code, self.dates[0], self.dates[1])
def load_const(self, _index_code, stock_count = 300): # sdsr = data.StockData_SQLite(data_db) y, m = 2005, 7 sttdate, enddate = '20000101', '20190601' count, stock_codes = 0, [] for i in range(0, 56): _end = dt.datetime(y, m, 1) _stt = _end + dt.timedelta(days=-30) _stt = StockDataSource.str_date(_stt) _end = StockDataSource.str_date(_end) m += 3 if m > 12: y, m = y+1, 1 df = StockData_Tushare.ts_api.index_weight(index_code=_index_code, start_date = _stt, end_date = _end) if len(df) < stock_count: continue consts = df.head(stock_count) for index, row in consts.iterrows(): code = row['con_code'] self.read_stock(row['con_code'], sttdate, enddate) if len(self.stocks): continue self.load_data(row['con_code'], sttdate, enddate) stock_codes.append(code) count += 1 if count%20 == 0: print( stock_codes ) stock_codes = [] if len(stock_codes): print( stock_codes )
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 daul_turtle0(self, code, data_list, index_long, index_short): self._start_test(self.turtle_args[2]) init_max_count = 3 all_days, long_days, _max_count = len(data_list[0]), 0, init_max_count long_state, short_state, max_value, trade_mode, high_price = 0, 0, 0, 0, 0 opens, highs, lows, closes = data_list[1], data_list[2], data_list[ 3], data_list[4] open_prices, open_dates, max_states = [], [], [] for _idx in range(self.turtle_args[2], all_days): float_date = data_list[0][_idx] int_date = StockDataSource.int_date(float_date) self.print_progress(int_date) if float_date < self.float_str or float_date > self.float_end: # self.account.ProfitDaily() self.market_values.append(self.account.total_value) self.position_ratios.append(self.account.position_value / self.account.total_value) continue # self.account.ProfitDaily(int_date) self.curr_closes[code] = closes[_idx] stock_data = { 'ts_code': code, 'key_price': index_long['key_price'][_idx], 'open': opens[_idx], 'high': highs[_idx], 'low': lows[_idx], 'close': closes[_idx] } if (not index_long['state'][_idx] or self.account.total_value < max_value * .6) and trade_mode: if self.account.total_value < max_value * .6: print(int_date, self.account.total_value / max_value) stock_data['key_price'] = closes[_idx] # if not index_long['state'][_idx] and trade_mode: if len(self.account.stocks): self.single_clear(stock_data['key_price'], stock_data, int_date) max_value = self.account.total_value if not index_long['state'][_idx]: long_state, short_state, trade_mode = 0, 0, 0 self.order_count, _max_count = 0, init_max_count max_states.append(index_long['state'][_idx - 1]) elif index_long['state'][_idx]: long_days += 1 _wave = index_long['wave'][_idx] while trade_mode in [0, 1] and long_state < index_long[ 'state'][_idx] and long_state < _max_count: # print( int_date, index_long['state'][_idx], index_short['state'][_idx], trade_mode, long_state, short_state ) long_state += 1 stock_data['key_price'] = index_long['key_price'][_idx] - ( index_long['state'][_idx] - long_state) * _wave self.open_order(int_date, stock_data, _wave, opens[_idx]) if not trade_mode: trade_mode = 1 # self.open_value = max(1000*10000, self.account.total_value) max_value = self.open_value = self.account.total_value open_prices.append(index_long['key_price'][_idx]) open_dates.append(float_date) elif index_long['state'][_idx] >= 4 and trade_mode < 2: trade_mode = 2 elif not index_short['state'][_idx-1] and index_short['state'][_idx] \ and index_long['state'][_idx] > 4 and self.order_count < 10: short_state, trade_mode = 0, 4 if len(self.account.stocks): _max_count = init_max_count else: _max_count = 1 # print( int_date, self.account.position_value, self.account.total_value ) if trade_mode == 2 and index_long['state'][_idx] >= 6 and len(open_prices) > 1 and \ (open_prices[-1] < open_prices[-2] or open_dates[-1] - open_dates[-2] > 300): trade_mode, high_price = 3, highs[_idx] # print( int_date, self.account.total_value / max_value, index_long['state'][_idx], index_short['state'][_idx] ) # print( int_date, self.order_count, long_state, index_long['state'][_idx], short_state, index_short['state'][_idx] ) if self.account.position_value > self.account.total_value * 7: trade_mode = 5 while trade_mode in [4] and short_state < index_short['state'][ _idx] and short_state < _max_count: short_state += 1 stock_data[ 'key_price'] = index_short['key_price'][_idx] - ( index_short['state'][_idx] - short_state) * _wave self.open_order(int_date, stock_data, _wave, opens[_idx]) if trade_mode == 3: if high_price < highs[_idx]: high_price = highs[_idx] if lows[_idx] < high_price - _wave * 4: self.single_clear(high_price - _wave * 4, stock_data, int_date) self.order_count = 0 self.account.UpdateValue(self.curr_closes, int_date) max_value = max(max_value, self.account.total_value) self.market_values.append(self.account.total_value) self.position_ratios.append(self.account.position_value / self.account.total_value) order_list = pandas.DataFrame(self.hist_orders, columns=[ 'date', 'trade_price', 'stop_price', 'volume', 'total_value', 'credit', 'lever', 'ts_code' ]) out_list = order_list[[ 'date', 'trade_price', 'volume', 'total_value', 'credit', 'lever' ]] for i in range(0, len(out_list), 30): print(out_list[i:i + 30]) print( pandas.DataFrame(self.year_values, columns=['year', 'cash', 'ratio'])) if len(open_dates) > len(max_states): max_states.append(index_long['state'][-1]) open_dates = list( map(lambda x: StockDataSource.str_date(x), open_dates)) print( pandas.DataFrame({ "date": open_dates, "price": open_prices, "max_state": max_states })) print( pandas.DataFrame( self.records, columns=['date', 'price', 'total', 'cash', 'profit', 'ratio'])) print('all day %d, long day %d.' % (all_days, long_days)) self.account.status_info()
def simple_daul_turtle(self, code, data_list, index_long, index_short): self._start_test() init_max_count = 3 all_days, long_days, _max_count = len(data_list[0]), 0, init_max_count long_state, max_value, trade_mode = 0, 0, 0 opens, highs, lows, closes = data_list[1], data_list[2], data_list[ 3], data_list[4] open_prices, open_dates, max_states = [], [], [] for _idx in range(self.turtle_args[0], all_days): float_date = data_list[0][_idx] int_date = StockDataSource.int_date(float_date) self.print_progress(int_date) if float_date < self.float_str or float_date > self.float_end: # self.account.ProfitDaily() self.market_values.append(self.account.total_value) self.position_ratios.append(self.account.position_value / self.account.total_value) continue self.curr_closes[code] = closes[_idx] self.account.ProfitDaily(int_date) stock_data = { 'ts_code': code, 'key_price': index_short['key_price'][_idx], 'open': opens[_idx], 'high': highs[_idx], 'low': lows[_idx], 'close': closes[_idx] } if not index_short['state'][_idx] and trade_mode: if len(self.account.stocks): self.single_clear(stock_data, int_date) long_state, trade_mode = 0, 0 self.order_count, _max_count = 0, init_max_count max_states.append(index_long['state'][_idx - 1]) elif index_short['state'][_idx]: long_days += 1 if not trade_mode: trade_mode = 1 # self.open_value = max(1000*10000, self.account.total_value) max_value = self.open_value = self.account.total_value open_prices.append(index_long['key_price'][_idx]) open_dates.append(float_date) if long_state < index_short['state'][ _idx] and long_state < _max_count: self.open_order(int_date, stock_data, index_short['wave'][_idx], stock_data['key_price'], opens[_idx]) long_state = index_short['state'][_idx] self.account.UpdateValue(self.curr_closes, int_date) max_value = max(max_value, self.account.total_value) self.market_values.append(self.account.total_value) self.position_ratios.append(self.account.position_value / self.account.total_value) order_list = pandas.DataFrame(self.hist_orders, columns=[ 'date', 'trade_price', 'stop_price', 'volume', 'total_value', 'credit', 'lever', 'ts_code' ]) out_list = order_list[[ 'date', 'trade_price', 'volume', 'total_value', 'credit', 'lever' ]] for i in range(0, len(out_list), 30): print(out_list[i:i + 30]) print( pandas.DataFrame(self.year_values, columns=['year', 'cash', 'ratio'])) if len(open_dates) > len(max_states): max_states.append(index_long['state'][-1]) print( pandas.DataFrame({ "date": open_dates, "price": open_prices, "max_state": max_states })) print( pandas.DataFrame( self.records, columns=['date', 'price', 'total', 'cash', 'profit', 'ratio'])) print('all day %d, long day %d.' % (all_days, long_days)) self.account.status_info()
def single_turtle(self, code, data_list, index_long): self._start_test(self.turtle_args[2]) self.open_value, _loss_unit, _open_unit = 0, 0, 0 dates, opens, highs, lows, closes = data_list[0], data_list[ 1], data_list[2], data_list[3], data_list[4] all_days, long_days, long_state = len(dates), 0, 0 for _idx in range(self.turtle_args[0], all_days): float_date = dates[_idx] int_date = StockDataSource.int_date(float_date) self.print_progress(int_date) if float_date < self.float_str or float_date > self.float_end: # self.account.ProfitDaily() self.market_values.append(self.account.total_value) self.position_ratios.append(self.account.position_value / self.account.total_value) continue self.curr_closes[code] = closes[_idx] self.account.ProfitDaily(int_date) stock_data = { 'ts_code': code, 'key_price': index_long['key_price'][_idx], 'open': opens[_idx], 'high': highs[_idx], 'low': lows[_idx], 'close': closes[_idx] } if not index_long['state'][_idx] and len(self.account.stocks): self.single_clear(stock_data['key_price'], stock_data, int_date) long_state = 0 _long_open = long_state < index_long['state'][ _idx] and long_state < 2 if _long_open: # and self.account.credit < self.account.total_value*2: if not long_state: self.open_value = self.account.total_value key_price = index_long['key_price'][_idx] if key_price < opens[_idx]: key_price = opens[_idx] _loss_unit = self.account.total_value * self.turtle_args[ 6] * .001 _open_unit = _loss_unit / index_long['wave'][_idx] _loss_price = key_price - index_long['wave'][ _idx] * self.turtle_args[0] if self._order(int_date, stock_data, _open_unit, key_price, _loss_price): long_state = index_long['state'][_idx] if index_long['state']: long_days += 1 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.year_values, columns=['year', 'cash'])) print( pandas.DataFrame( self.records, columns=['date', 'price', 'total', 'cash', 'profit', 'ratio'])) # print( self.account.get_records()[[ 'order_time', 'price', 'volume', 'cash', 'credit' ]] ) print('all day %d, long day %d.' % (all_days, long_days)) self.account.status_info()
def daul_turtle1(self, code, data_list, index_long, index_short): self._start_test(self.turtle_args[2]) self.open_value, _loss_unit, _open_unit = 0, 0, 0 opens, highs, lows, closes = data_list[1], data_list[2], data_list[ 3], data_list[4] all_days, long_days, long_state, short_state, trade_mode, order_count = len( data_list[0]), 0, 0, 0, 0, 0 open_prices, open_dates, max_states = [], [], [] for _idx in range(self.turtle_args[0], all_days): float_date = data_list[0][_idx] int_date = StockDataSource.int_date(float_date) self.print_progress(int_date) if float_date < self.float_str or float_date > self.float_end: # self.account.ProfitDaily() self.market_values.append(self.account.total_value) self.position_ratios.append(self.account.position_value / self.account.total_value) continue self.curr_closes[code] = closes[_idx] self.account.ProfitDaily(int_date) stock_data = { 'ts_code': code, 'key_price': index_long['key_price'][_idx], 'open': opens[_idx], 'high': highs[_idx], 'low': lows[_idx], 'close': closes[_idx] } if not index_long['state'][_idx] and order_count: if len(self.account.stocks): self.single_clear(stock_data['key_price'], stock_data, int_date) long_state, short_state, trade_mode, order_count = 0, 0, 0, 0 max_states.append(index_long['state'][_idx - 1]) else: long_days += 1 if len(self.account.stocks) and index_long['state'][_idx] >= 6 and not trade_mode and len(open_prices) > 1 and \ (open_prices[-1] < open_prices[-2] or open_dates[-1] - open_dates[-2] > 300): self.single_clear(stock_data['key_price'], stock_data, int_date) if not index_short['state'][ _idx] and index_long['state'][_idx] > 4: short_state, trade_mode = 0, 1 _long_on = long_state < index_long['state'][ _idx] and long_state < 2 _short_on = trade_mode and short_state < index_short['state'][ _idx] and short_state < 2 if ( _long_on or _short_on ) and order_count < 8: #and self.account.credit < self.account.total_value*2: if not long_state: # or not short_state: self.open_value = max(1000 * 10000, self.account.total_value) open_prices.append(index_long['key_price'][_idx]) open_dates.append(float_date) print(int_date, order_count, long_state, short_state, index_short['state'][_idx]) if _long_on: key_price = index_long['key_price'][_idx] elif _short_on: key_price = index_short['key_price'][_idx] if key_price < opens[_idx]: key_price = opens[_idx] _total_value = max(1000 * 10000, self.account.total_value) _loss_unit = _total_value * self.turtle_args[6] * .001 _open_unit = _loss_unit / index_long['wave'][_idx] _loss_price = key_price - index_long['wave'][ _idx] * self.turtle_args[0] self._order(int_date, stock_data, _open_unit, key_price, _loss_price) order_count += 1 long_state = index_long['state'][_idx] short_state = index_short['state'][_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) order_list = pandas.DataFrame(self.hist_orders, columns=[ 'date', 'trade_price', 'stop_price', 'volume', 'total_value', 'credit', 'lever', 'ts_code' ]) out_list = order_list[[ 'date', 'trade_price', 'volume', 'total_value', 'credit', 'lever' ]] for i in range(0, len(out_list), 30): print(out_list[i:i + 30]) print( pandas.DataFrame(self.year_values, columns=['year', 'cash', 'ratio'])) if len(open_dates) > len(max_states): max_states.append(index_long['state'][-1]) print(open_dates) print(max_states) print( pandas.DataFrame({ "date": open_dates, "price": open_prices, "max_state": max_states })) print( pandas.DataFrame( self.records, columns=['date', 'price', 'total', 'cash', 'profit', 'ratio'])) print('all day %d, long day %d.' % (all_days, long_days)) self.account.status_info()
def _Order(self, code, price, volume, order_time): _cost, _commision, volume = self.Format(volume, price) if not volume: # or (self.max_credit > 100 and self.credit + _cost - self.cash > self.max_credit): return 0 order_time = StockDataSource.str_date(order_time) if _cost < 0 and self.credit > 0: self.credit += _cost if self.credit < 0: self.cash -= self.credit self.credit = 0 elif self.cash < _cost: _temp = self.total_value * self.max_credit - self.credit + self.cash if self.max_credit and _temp < _cost - self.cash: _cost, _commision, volume = self.Format(_temp / price, price) # if self.max_credit > 0 and self.total_value*2 < self.credit + _cost - self.cash: # volume = (self.total_value*2 - self.credit) / price # _cost, _commision, volume = self.Format(volume, price) # print('reset order volume: ', order_time, code, price, volume, _cost, _commision, self.cash, self.credit) # if self.max_credit > 0 and self.credit + _cost - self.cash > self.max_credit: # print('reset order volume: ', order_time, code, price, volume, _cost, _commision, self.cash, self.credit) # volume = (self.max_credit - self.credit + self.cash) / price # _cost, _commision, volume = self.Format(volume, price) # print('reset order volume: ', order_time, code, price, volume, _cost, _commision, self.cash, self.credit) self.credit += _cost - self.cash self.cash = 0 else: self.cash -= _cost self.cost += _commision if code in self.stocks.index: _row = self.stocks.loc[code] _volume = _row.volume + volume # if _volume < 0: # print(self.stocks.loc[code]) # raise ValueError("Don't naked short sale.") if _volume == 0: _cost_price = _row.cost_price _loss = _row.volume * _row.cost_price + _cost if _loss < 0: self.succeed += 1 else: self.max_loss = max( self.max_loss, _loss / (_row.volume * _row.cost_price)) else: _cost_price = (_row.volume * _row.cost_price + _cost) / _volume mkt_value = _volume * price self.stocks.loc[code] = [ _volume, price, _cost_price, mkt_value, order_time ] else: # if volume <= 0: # print(order_time, code, volume, price, self.cash) # raise ValueError("Don't naked short sale.") _volume = volume _cost_price = _cost / volume mkt_value = volume * price _row = { 'volume': [volume], 'price': [price], 'cost_price': [_cost_price], 'market_value': [mkt_value], 'order_time': [order_time] } _index = [code] self.stocks = self.stocks.append(pandas.DataFrame(_row, _index)) if volume < 0: self.short_count += 1 else: self.long_count += 1 self._update_param(order_time) lever = self.credit / self.total_value back_pump = 1 - self.total_value / self.max_value _record = (code, order_time, '%.03f' % price, volume, '%.03f' % (self.cash * .0001), '%.03f' % (self.credit * .0001), '%.03f' % (self.total_value * .0001), '%.03f' % (mkt_value * .0001), '%.03f' % (volume * price * .0001), '%.03f' % (_commision * .0001), '%.03f' % (_cost * .0001), _volume, lever, back_pump) self.records.append(_record) _clear_codes = [] for _code, row in self.stocks.iterrows(): if row['volume'] < 10 and row['volume'] > -10: _clear_codes.append(_code) if len(_clear_codes): self.stocks.drop(index=_clear_codes, axis=0, inplace=True) return volume
def __init__(self): utils.TradeTest.__init__(self) self.curr_closes = {} self.float_str = StockDataSource.float_date(self.dates[0]) self.float_end = StockDataSource.float_date(self.dates[1])
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()