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 statis_fut(self, fut_code, fut_index, fut_name): # if len(fut_index): # self.sdsr.load(fut_index, self.dates[0], self.dates[1], 'index') # else: # self.sdsr.load(fut_code, self.dates[0], self.dates[1], 'fut') self.sdsr.load(fut_code, self.dates[0], self.dates[1], 'fut') if not len(self.sdsr.stocks): return data_list, _dates = self.sdsr.parse_price() _data_vec = numpy.transpose(data_list) _last = len(_data_vec[0]) - 1 _close = _data_vec[4][_last] _open, _high, _low = _data_vec[1][_last], _data_vec[2][ _last], _data_vec[3][_last] # self.long_list.append( [fut_code, fut_index, _dates[_last], _close, _high, _low, fut_name] ) 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]) _price, _wave = index_long['key_price'][_last], index_long['wave'][ _last] _high, _low = index_long['long'][_last], index_long['short'][_last] self.long_list.append( [fut_code, _dates[_last], index_long['state'][_last], _close, _high, _low, \ float('%.02f'%((_high-_close)*100/_close)), float('%.02f'%((_low-_close)*100/_close)), '%.02f'%_wave, fut_name] ) turtle = index.TurtleIndex() index_short = index.ShortTurtleIndex(turtle, _data_vec, self.turtle_args[2], self.turtle_args[3], self.turtle_args[0], self.turtle_args[1]) _price, _wave = index_short['key_price'][_last], index_short['wave'][ _last] # _high, _low = index_short['short'][_last], index_short['long'][_last] _high, _low = index_short['long'][_last], index_short['short'][_last] self.short_list.append( [fut_code, _dates[_last], index_short['state'][_last], _close, _high, _low, \ float('%.02f'%((_high-_close)*100/_close)), float('%.02f'%((_low-_close)*100/_close)), '%.02f'%_wave, fut_name] )
def __init__(self): index.TurtleArgs.__init__(self) title = 'plot[' for code in self.codes: title = '%s%s' % (title, code) title = '%s]' % title plot = utils.StockDisp(title, 1) sdsr = data.StockData_SQLite(self.files[0]) turtle = index.TurtleIndex() for code in self.codes: sdsr.load(code, self.dates[0], self.dates[1], self.params[0], self.params[1]) if not len(sdsr.stocks): continue data_list, _dates = sdsr.parse_price() _data_vec = numpy.transpose(data_list) if self.params[2] == 'long': long_index = index.LongTurtleIndex(turtle, _data_vec, self.turtle_args[2], self.turtle_args[3], self.turtle_args[0], self.turtle_args[1]) # short_index = index.LongTurtleIndex(turtle, _data_vec, self.turtle_args[4], # self.turtle_args[5], self.turtle_args[0], self.turtle_args[1]) else: long_index = index.ShortTurtleIndex(turtle, _data_vec, self.turtle_args[2], self.turtle_args[3], self.turtle_args[0], self.turtle_args[1]) # short_index = index.ShortTurtleIndex(turtle, _data_vec, self.turtle_args[4], # self.turtle_args[5], self.turtle_args[0], self.turtle_args[1]) info_list = self.list_info1(_data_vec, long_index) # info_list = self.list_info2(_data_vec, long_index, short_index['state'] ) # print( info_list[-80:-40] ) print(info_list.tail(40)) info_list.to_csv('%s/%s.csv' % (self.files[2], code)) plot.LogKDisp(plot.ax1, data_list) # plot.LogPlot(plot.ax1, _data_vec[0], turtle_index['long'], 'r') # plot.LogPlot(plot.ax1, _data_vec[0], turtle_index['short'], 'y') # plot.Plot(plot.ax1, _data_vec[0], self.stim.data['average'], 'b') plot.save(title, self.files[3])
import numpy, pandas, datetime as dt, time from turtle import trade, index, data, utils from turtle.data import StockDataSource if __name__ == "__main__": args = index.TurtleArgs() sdsr = data.StockData_SQLite(args.files[0]) for code in args.codes: sdsr.load(code, args.dates[0], args.dates[1], args.params[0], args.params[1]) if not len(sdsr.stocks): continue data_list, _dates = sdsr.parse_price() _data_vec = numpy.transpose(data_list) turtle = index.TurtleIndex() if args.params[2] == 'long': long_index = index.LongTurtleIndex(turtle, _data_vec, args.turtle_args[2], args.turtle_args[3], args.turtle_args[0], args.turtle_args[1]) else: long_index = index.ShortTurtleIndex(turtle, _data_vec, args.turtle_args[2], args.turtle_args[3], args.turtle_args[0], args.turtle_args[1]) print(long_index)
def long_turtle(self, code, data_list): _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]) turtle = index.TurtleIndex() index_short = index.LongTurtleIndex(turtle, _data_vec, self.turtle_args[4], self.turtle_args[5], self.turtle_args[0], self.turtle_args[1]) # test.daul_turtle(code, _data_vec, index_long, index_short) # self.simple_daul_turtle(code, _data_vec, index_long, index_short) # self._start_test(self.turtle_args[2]) init_max_count = 3 all_days, long_days, _max_count = len(_data_vec[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_vec[1], _data_vec[2], _data_vec[ 3], _data_vec[4] open_prices, open_dates, max_states = [], [], [] for _idx in range(self.turtle_args[2], all_days): float_date = _data_vec[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] } self.account.UpdateValue(self.curr_closes, int_date) 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_long_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 * 8: 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_long_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'])) self.account.status_info() print('all day %d, long day %d. %s %s, use time %.02f seconds.' % (all_days, long_days, 'single_turtle', code, time.time() - self.start)) # self.account.save_records(code, self.files[2]) self.plot(data_list, index_long, code)
def __init__(self): index.TurtleArgs.__init__(self) if self.files[1] != ':memory:': self.csv_file = self.files[1] else: self.csv_file = 'fund_pos.csv' self.positions = PositionCSV.read_csv(self.csv_file) sdsr = data.StockData_SQLite(self.files[0]) pos_list = self.positions.values.tolist() prev_closes, total_values, profits, may_loss, stop_prices, loss_ratios = [], [], [], [], [], [] total_invest, total_value, total_profit, total_loss = 0, 0, 0, 0 for row in pos_list: code, volume, cost_price, good_type = row[0:4] sdsr.load(code, self.dates[0], self.dates[1], good_type) if not len(sdsr.stocks): continue data_list, _dates = sdsr.parse_price() _data_vec = numpy.transpose(data_list) turtle = index.TurtleIndex() turtle_index = index.LongTurtleIndex(turtle, _data_vec, self.turtle_args[2], self.turtle_args[3], self.turtle_args[0], self.turtle_args[1]) # info_list = self.list_info(_data_vec, turtle_index ) list_len = len(turtle_index['key_price']) _last = list_len - 1 _prev_close = _data_vec[4][_last] _key_price, _profit_price, _wave = turtle_index['key_price'][ _last], turtle_index['short'][_last], turtle_index['wave'][ _last] _value = volume * _prev_close _cost = volume * cost_price _stop_price = _key_price - _wave * self.turtle_args[0] if cost_price > _key_price: _stop_price = cost_price - _wave * self.turtle_args[0] _mayloss = volume * (_stop_price - cost_price) _stop_price = max(_stop_price, _profit_price) total_invest += _cost total_value += _value total_profit += _value - _cost total_loss += _mayloss prev_closes.append(_prev_close) total_values.append(_value) stop_prices.append(_stop_price) loss_ratios.append(100 * _stop_price / _prev_close - 100) profits.append(_value - _cost) # profit_prices.append( _profit_price ) may_loss.append(_mayloss) self.positions['prev'] = prev_closes self.positions['stop'] = stop_prices self.positions['ratio'] = loss_ratios self.positions['value'] = total_values self.positions['profit'] = profits # self.positions['profit_price'] = profit_prices self.positions['may_loss'] = may_loss self.positions.drop('type', axis=1, inplace=True) print(self.positions) print( 'SUM invest: %.03f, value: %.03f, profit: %.03f, mayloss: %.03f.' %\ (total_invest*0.0001, total_value*0.0001, total_profit*0.0001, total_loss*.0001) )
def __init__(self): index.TurtleArgs.__init__(self) sdsr = data.StockData_SQLite(self.files[0]) turtle_list = [] for code in self.codes: 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]) Hl = turtle.high_prices[self.turtle_args[2]] # print( Hl ) turtle = index.TurtleIndex() index_short = index.LongTurtleIndex(turtle, _data_vec, self.turtle_args[3], self.turtle_args[4], self.turtle_args[0], self.turtle_args[1]) long_list = self.list_info(_data_vec, index_long) long_list.tail(30).to_csv('long.csv') print(long_list.tail(5)) short_list = self.list_info(_data_vec, index_short) short_list.tail(30).to_csv('short.csv') print(short_list.tail(5)) _last = len(_data_vec[0]) - 1 _price, _wave = index_long['key_price'][_last], index_long['wave'][ _last] if index_long['state'][_last]: _append = _price + _wave _stoploss = _price - _wave * self.turtle_args[0] _pos_unit = int(1000 / _wave) else: _append, _stoploss, _pos_unit = 0, 0, 0 turtle_list.append([ code, _dates[_last], index_long['state'][_last], _price, _append, _stoploss, _pos_unit, index_long['short'][_last], _wave, _data_vec[4][_last] ]) print( pandas.DataFrame(turtle_list, columns=[ 'ts_code', 'date', 'state', 'key_price', 'append', 'stop_loss', 'pos_unit', 'valley', 'wave', 'close' ]))