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 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 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 _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