def load_data(self, code, target_date): yesterday = holidays.get_yesterday(target_date) print('target_date', target_date, 'yesterday', yesterday) yesterday_min_data = stock_api.request_stock_minute_data(message_reader, code, yesterday, yesterday) if len(yesterday_min_data) <= 10: print('NO or LESS YESTERDAY MIN DATA', code, yesterday) return today_min_data = stock_api.request_stock_minute_data(message_reader, code, target_date, target_date) if len(today_min_data) <= 10: print('NO or LESS TODAY MIN DATA', code, target_date) return past_datas = stock_api.request_stock_day_data(message_reader, code, yesterday - timedelta(days=30), yesterday) if len(past_datas) <= 10: print('NO or LESS PAST DATA', code, yesterday - timedelta(days=30), yesterday) return self.yesterday = yesterday self.today = target_date vol = 0 for d in past_datas: vol += d['6'] self.volume_average = int(vol / len(past_datas)) self.clear_datas() yesterday_min_close = yesterday_min_data[-1]['5'] self.price_range[0] = yesterday_min_close - int(yesterday_min_close * 0.1) self.price_range[1] = yesterday_min_close + int(yesterday_min_close * 0.1) for ym in yesterday_min_data: if ym['4'] < self.price_range[0]: self.price_range[0] = ym['4'] if ym['3'] > self.price_range[1]: self.price_range[1] = ym['3'] self.yesterday_min_data_c.append(dt.cybos_stock_day_tick_convert(ym)) for tm in today_min_data: if tm['4'] < self.price_range[0]: self.price_range[0] = tm['4'] if tm['3'] > self.price_range[1]: self.price_range[1] = tm['3'] self.today_min_data_c.append(dt.cybos_stock_day_tick_convert(tm)) self.calc_moving_average(self.yesterday_min_data_c, self.today_min_data_c) ef = edgefinder.EdgeFinder(self.moving_average) self.edges.extend(ef.get_peaks(True)) self.edges.extend(ef.get_peaks(False)) self.edges = sorted(self.edges, key=lambda x: x[0]) yesterday_average = self.create_average_data(yesterday, self.yesterday_min_data_c) today_average = self.create_average_data(target_date, self.today_min_data_c) self.average_data.extend(yesterday_average) self.average_data.extend(today_average) self.up_to = datetime.combine(yesterday, time(12)) self.set_figure_data(self.up_to)
def get_today_min_data(code, from_date): today_min_data = stock_api.request_stock_minute_data( message_reader, code, from_date, from_date) if len(today_min_data) == 0: print('NO MIN DATA', code, from_date) return [] today_min_data_c = [] for tm in today_min_data: today_min_data_c.append(dt.cybos_stock_day_tick_convert(tm)) return today_min_data_c
def get_minute_data(code, from_date, until_date, t=0): from_date = from_date if from_date.__class__.__name__ == 'date' else from_date.date( ) until_date = until_date if until_date.__class__.__name__ == 'date' else until_date.date( ) minute_data = stock_api.request_stock_minute_data(get_reader(), code, from_date, until_date) minute_data = _convert_min_data_readable(code, minute_data) if t != 0: minute_data = list(filter(lambda x: x['time'] < t, minute_data)) return minute_data
def add_watcher(reader, code, code_info, today, state, is_simulation): tt = None if not is_simulation: tt = TodayTrader(code, code_info, today, state) stock_api.subscribe_stock(reader, code, tt.tick_handler) tt.start_timer() else: tt = TodayTrader(code, code_info, today, state) today_data = stock_api.request_stock_minute_data(reader, code, today, today) today_min_data = [] for td in today_data: today_min_data.append(dt.cybos_stock_day_tick_convert(td)) #print('data len', len(today_min_data)) tt.set_simulation_data(today_min_data) tt.start() today_traders.append(tt)
for det in determine_time: print(det) from_date = date(2019, 12, 1) until_date = date(2019, 12, 2) record_from, record_until = from_date, until_date while from_date <= until_date: if holidays.is_holidays(from_date): from_date += timedelta(days=1) continue #print('RUN', from_date) candidates = [] for i, code in enumerate(market_code): today_min_data = stock_api.request_stock_minute_data( message_reader, code, from_date, from_date) if len(today_min_data) == 0: continue today_min_data_c = [] for tm in today_min_data: today_min_data_c.append(dt.cybos_stock_day_tick_convert(tm)) candidates.append({ 'code': code, 'data': today_min_data_c, 'start_price': today_min_data_c[0]['start_price'] }) #print(f'Collect {i}/{len(market_code)}', end='\r') print('Start Best Candidates') best_candidates = find_best(candidates, det) final_candidates = best_candidates[:5]
def save(message_reader, peak_data, data, start_time, until_time, matched_data): price_array, date_array, volume_array_old, price_average = data code = peak_data['code'] volume_array = [] matched_data_price_array = [] matched_data_date_array = [] matched_data_volume_array = [] titles = ['comparision', peak_data['code']] current_volume = 0 for v in volume_array_old: volume_array.append(v - current_volume) current_volume = v for m in matched_data: current_volume = 0 min_data = stock_api.request_stock_minute_data(message_reader, m['code'], m['from_date'].date(), m['until_date'].date()) if len(min_data) == 0: print('SAVE NO MIN DATA', code, m['from_date'], m['until_date']) continue min_data_c = [] for md in min_data: min_data_c.append(dt.cybos_stock_day_tick_convert(md)) min_data_price_array = [ int((mdata['lowest_price'] + mdata['close_price'] + mdata['highest_price']) / 3) for mdata in min_data_c ] min_data_date_array = [] min_data_volume_array = [] for mdata in min_data_c: min_data_volume_array.append(mdata['volume']) tc = time_converter.intdate_to_datetime(mdata['0']).date() if tc == m['from_date'].date(): min_data_date_array.append(date_array[0].replace( hour=int(mdata['time'] / 100), minute=int(mdata['time'] % 100))) else: min_data_date_array.append(date_array[-1].replace( hour=int(mdata['time'] / 100), minute=int(mdata['time'] % 100))) matched_data_price_array.append(min_data_price_array) matched_data_date_array.append(min_data_date_array) matched_data_volume_array.append(min_data_volume_array) titles.append(m['code'] + '_' + m['from_date'].strftime('%Y%m%d-') + m['until_date'].strftime('%Y%m%d')) fig = make_subplots(rows=len(matched_data_price_array) + 2, cols=1, shared_xaxes=True, specs=[[{ "secondary_y": True }]] * (len(matched_data_price_array) + 2), subplot_titles=titles) fig.add_trace(go.Scatter(x=date_array, y=profit_price(price_array), line=dict(color='black', width=2)), row=1, col=1) for i, data in enumerate(matched_data_price_array): fig.add_trace(go.Scatter(x=matched_data_date_array[i], y=profit_price(matched_data_price_array[i]), line=dict(width=1), opacity=0.8), row=1, col=1) fig.update_xaxes(type='category', tickformat='%H%M', row=1, col=1) fig.add_trace(go.Scatter(x=date_array, y=price_array, name=code, line=dict(color='black', width=1)), secondary_y=False, row=2, col=1) fig.add_trace(go.Scatter(x=date_array, y=price_average, line=dict(color='green')), secondary_y=False, row=2, col=1) fig.add_trace(go.Bar(x=date_array, y=volume_array), row=2, col=1, secondary_y=True) fig.update_xaxes(type='category', tickformat='%H%M', row=2, col=1) fig.update_yaxes(title_text='price', secondary_y=False, row=2, col=1) fig.update_yaxes(title_text='volume', secondary_y=True, row=2, col=1) for i, data in enumerate(matched_data_price_array): fig.add_trace(go.Scatter(x=matched_data_date_array[i], y=data), row=i + 3, col=1, secondary_y=False) fig.add_trace(go.Bar(x=matched_data_date_array[i], y=matched_data_volume_array[i]), row=i + 3, col=1, secondary_y=True) fig.update_xaxes(type='category', tickformat='%H%M', row=i + 3, col=1) shapes = [ dict(x0=until_time, x1=until_time, y0=0, y1=1, xref='x', yref='paper', line_width=2) ] annotations = [] for p in peak_data['peak'][1:]: annotations.append( go.layout.Annotation(x=p['time'], y=p['price'], text=('t' if p['type'] == 1 else 'b'), xref='x2', yref='y3', showarrow=True, arrowhead=7)) fig.update_layout(title=code + '_' + start_time.strftime('%Y%m%d'), shapes=shapes, annotations=annotations, yaxis_tickformat='d', autosize=True, width=1920, height=(len(matched_data_price_array) + 2) * 300) fig.write_html(code + '_' + until_time.strftime('%Y%m%d%H%M_') + str(len(peak_data['peak'])) + '.html', auto_open=False) print( 'save done', code + '_' + until_time.strftime('%Y%m%d%H%M_') + str(len(peak_data['peak'])) + '.html')
def start_today_trading(reader, market_code, today, choosers): code_dict = dict() yesterday = holidays.get_yesterday(today) by_amounts = [] for progress, code in enumerate(market_code): print('collect past data', today, f'{progress+1}/{len(market_code)}', end='\r') yesterday_data = stock_api.request_stock_day_data( reader, code, yesterday, yesterday) if len(yesterday_data) != 1: continue elif yesterday_data[0]['5'] < 900: continue code_dict[code] = { 'code': code, 'past_min_data': [], 'today_min_data': None, 'time': 0, 'yesterday_close': yesterday_data[0]['5'], 'today_gap': 0, 'until_now_profit': 0 } min_req_from = today - timedelta(days=10) min_req_until = today while min_req_from <= min_req_until: if holidays.is_holidays(min_req_from): min_req_from += timedelta(days=1) continue min_data = stock_api.request_stock_minute_data( reader, code, min_req_from, min_req_from) if len(min_data) > 0: min_data_c = [] for md in min_data: min_data_c.append(dt.cybos_stock_day_tick_convert(md)) code_dict[code]['past_min_data'].append(min_data_c) min_req_from += timedelta(days=1) if len(code_dict[code]['past_min_data']) > 5: code_dict[code]['today_min_data'] = code_dict[code][ 'past_min_data'][-1] code_dict[code]['today_gap'] = ( code_dict[code]['today_min_data'][0]['start_price'] - code_dict[code]['yesterday_close'] ) / code_dict[code]['yesterday_close'] * 100 code_dict[code]['past_min_data'] = code_dict[code][ 'past_min_data'][:-1] else: #print('not enough data', code) code_dict.pop(code, None) print('') for c in choosers: new_code_dict = dict() result = c(reader, code_dict) for r in result: new_code_dict[r] = code_dict[r] code_dict = new_code_dict return code_dict
def consumer(): global message_reader sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_address = (message.SERVER_IP, message.CLIENT_SOCKET_PORT) sock.connect(server_address) message_reader = stream_readwriter.MessageReader(sock) message_reader.start() #stock_api.subscribe_trade(message_reader, display_trade_result) while True: val = q.get(True) command = val.decode('ascii').rstrip() #print(command) if command == 'long': print(stock_api.request_long_list(message_reader)) elif command.startswith('stats'): print(stock_api.request_subscribe_stat(message_reader)) elif command.startswith('subscribe_code'): codes = stock_api.request_subscribe_codes(message_reader) for code in codes: if len(code) > 10 or not code.startswith('A'): print(code) elif command.startswith('statc'): cinfo = stock_api.request_collector_stat(message_reader) print('total collector', len(cinfo)) print('total subscribe count', sum([c['subscribe_count'] for c in cinfo])) for i, ci in enumerate(cinfo): print(i, '\t', 'name', ci['name'], 'vendor', ci['vendor'], 'subscribe count', ci['subscribe_count']) elif command.startswith('trade_subscribe'): stock_api.subscribe_trade(message_reader, display_trade_result) elif command.startswith('trade_stop_subscribe'): stock_api.stop_subscribe_trade(message_reader) elif command.startswith('min_data'): min_detail = command.split(',') if len(min_detail) != 2: print('min_data,code') else: result = stock_api.request_stock_minute_data(message_reader, min_detail[1], date(2020,2,3), date(2020,2,3)) print(result) elif command.startswith('todaym'): todaym_detail = command.split(',') if len(todaym_detail) != 2: print('todaym,code') else: result = stock_api.request_stock_today_data(message_reader, todaym_detail[1]) if len(result) > 1: print('DATA LEN', len(result)) print('HEAD', result[0]) print(result[1]) print('TAIL', result[-1]) print(result) elif command.startswith('todayt'): todaym_detail = command.split(',') if len(todaym_detail) != 2: print('todayt,code') else: result = stock_api.request_stock_today_tick_data(message_reader, todaym_detail[1]) if len(result) > 1: print('DATA LEN', len(result)) print('HEAD', result[0]) print(result[1]) print('TAIL', result[-1]) elif command.startswith('buy') or command.startswith('sell'): buy_detail = command.split(',') print(buy_detail) if len(buy_detail) != 4: print('buy|sell,code,price,quantity') else: is_buy = True if buy_detail[0] == 'buy': pass elif buy_detail[0] == 'sell': is_buy = False else: print('wrong buy/sell command') continue code = buy_detail[1] price = int(buy_detail[2]) quantity = int(buy_detail[3]) result = stock_api.order_stock(message_reader, code, price, quantity, is_buy) print(result) elif command.startswith('modify'): modify_detail = command.split(',') if len(modify_detail) != 4: print('modify,order_num,code,price') else: order_num = int(modify_detail[1]) code = modify_detail[2] price = int(modify_detail[3]) result = stock_api.modify_order(message_reader, order_num, code, price) print(result) elif command.startswith('cancel'): cancel_detail = command.split(',') if len(cancel_detail) != 4: print('cancel,order_num,code,amount') else: order_num = int(cancel_detail[1]) code = cancel_detail[2] amount = int(cancel_detail[3]) result = stock_api.cancel_order(message_reader, order_num, code, amount) print(result) elif command.startswith('queue'): print(stock_api.request_order_in_queue(message_reader)) elif command.startswith('balance'): print(stock_api.get_balance(message_reader)['balance']) elif command.startswith('subject'): subject_detail = command.split(',') if len(subject_detail) != 2: print('subject,code') else: code = subject_detail[1] stock_api.subscribe_stock_subject(message_reader, code, display_subject_data) elif command.startswith('stop_subject'): subject_detail = command.split(',') if len(subject_detail) != 2: print('stop_subject,code') else: code = subject_detail[1] stock_api.stop_subscribe_stock_subject(message_reader, code) elif command.startswith('stop_bidask'): bidask_detail = command.split(',') if len(bidask_detail) != 2: print('stop_bidask,code') else: code = bidask_detail[1] stock_api.stop_subscribe_stock_bidask(message_reader, code) elif command.startswith('bidask'): bidask_detail = command.split(',') if len(bidask_detail) != 2: print('bidask,code') else: code = bidask_detail[1] stock_api.subscribe_stock_bidask(message_reader, code, display_bidask_data) elif command.startswith('stock'): stock_detail = command.split(',') if len(stock_detail) != 2: print('stock,code') else: code = stock_detail[1] stock_api.subscribe_stock(message_reader, code, display_stock_data) elif command.startswith('stop_stock'): stock_detail = command.split(',') if len(stock_detail) != 2: print('stop_stock,code') else: code = stock_detail[1] stock_api.stop_subscribe_stock(message_reader, code) elif command.startswith('req'): req_detail = command.split(',') if len(req_detail) != 2: print('req,code') else: code = req_detail[1] print(stock_api.request_stock_day_data(message_reader, code, date(2020,1,31), date(2020,1,31))) elif command.startswith('index'): index_detail = command.split(',') if len(index_detail) != 2: print('index,code') else: code = index_detail[1] stock_api.subscribe_index(message_reader, code, display_index_data) elif command.startswith('stop_index'): index_detail = command.split(',') if len(index_detail) != 2: print('index,code') else: code = index_detail[1] stock_api.stop_subscribe_index(message_reader, code) elif command.startswith('abroad'): abroad_detail = command.split(',') if len(abroad_detail) != 2: print('abroad,code') else: code = abroad_detail[1] print(stock_api.request_abroad_data(message_reader, code, message.PERIOD_DAY, 30)) elif command.startswith('uscode'): #print(stock_api.request_us_stock_code(message_reader, message.USTYPE_ALL)) print(len(stock_api.request_us_stock_code(message_reader, message.USTYPE_ALL))) elif command.startswith('world'): world_detail = command.split(',') if len(world_detail) != 2: print('world,code') else: code = world_detail[1] stock_api.subscribe_world(message_reader, code, display_world_data) elif command.startswith('stop_world'): world_detail = command.split(',') if len(world_detail) != 2: print('stop_world,code') else: code = world_detail[1] stock_api.stop_subscribe_world(message_reader, code) elif command.startswith('investor'): inv_detail = command.split(',') if len(inv_detail) != 2: print('investor,code') else: code = inv_detail[1] print(stock_api.request_investor_data(message_reader, code, date(2020,1,31), date(2020,1,31))) elif command.startswith('kinvestorc'): invc_detail = command.split(',') if len(invc_detail) != 2: print('investorc,code') else: code = invc_detail[1] print(stock_api.request_investor_accumulate_data(message_reader, code, date(2020,2,7), date(2020,2,7))) elif command.startswith('alarm'): stock_api.subscribe_alarm(message_reader, display_alarm_data) elif command.startswith('stop_alarm'): stock_api.stop_subscribe_alarm(message_reader) elif command.startswith('code_to_name'): code_detail = command.split(',') if len(code_detail) != 2: print('code_to_name,code') else: code = code_detail[1] print(stock_api.request_code_to_name(message_reader, code))