def start_provide_tick(): db_collection = MongoClient('mongodb://127.0.0.1:27017').trade_alarm market_codes = ready_queue.get() print('start_provide_tick', datetime.current_datetime, finish_time, len(market_codes)) while datetime.now() < finish_time: all_data = [] start_time = datetime.now() until_time = datetime.now() + timedelta(seconds=60) for code in market_codes: all_data.extend( collect_db(code, db_collection, start_time, until_time)) all_data = sorted(all_data, key=lambda x: x['date']) for tick in all_data: datetime.current_datetime = tick['date'] if '68' in tick: stock_api.send_bidask_data(tick['code'], tick) stock_api.set_current_first_bid(tick['code'], tick['4']) else: stock_api.send_tick_data(tick['code'], tick) gevent.sleep() if datetime.now() == start_time: datetime.current_datetime += timedelta(seconds=60) print('progressing', datetime.now(), 'handle', len(all_data), 'ticks')
def start_trader(ready_queue=None): print('start trader', datetime.now()) market_code = [] kosdaq_code = morning_client.get_market_code() kospi_code = morning_client.get_market_code(message.KOSPI) market_code.extend(kosdaq_code) market_code.extend(kospi_code) market_code = list(dict.fromkeys(market_code)) market_code = list( filter(lambda x: len(x) > 0 and x[1:].isdigit(), market_code)) yesterday_list = get_yesterday_data(datetime.now(), market_code) yesterday_list = sorted(yesterday_list, key=lambda x: x['amount'], reverse=True) yesterday_list = yesterday_list[:1000] for yesterday_data in yesterday_list: code = yesterday_data['code'] sf = stock_follower.StockFollower(morning_client.get_reader(), code, yesterday_data, code in kospi_code) sf.subscribe_at_startup() followers.append(sf) if ready_queue is not None: ready_queue.put_nowait([yl['code'] for yl in yesterday_list]) gevent.joinall([gevent.spawn(heart_beat), gevent.spawn(data_process)])
def heart_beat(): last_processed_time = datetime.now() finish_flag = False while True: while datetime.now() - last_processed_time < timedelta(seconds=1): gevent.sleep() candidates = [] while not candidate_queue.empty(): candidates.append(candidate_queue.get()) #print('candidate size', len(candidates), datetime.now()) for c in candidates: if c['code'] == 'exit': logger.warning('SET FINISH FLAG') finish_flag = True if finish_flag: logger.warning('FINISH TODAY WORK') break else: for fw in followers: if fw.is_in_market(): fw.process_tick() last_processed_time = datetime.now()
def remove_stock(code, price, quantity): global total_amount if code not in current_stocks: print('ERROR cannot not be in current_stocks when remove') return current_stocks[code]['sell'].append((datetime.now(), price, quantity)) buy_quantity = current_stocks[code]['buy'][2] sell_quantity = sum([d[2] for d in current_stocks[code]['sell']]) if buy_quantity == sell_quantity: buy_record = current_stocks[code]['buy'] sell_record = current_stocks[code]['sell'] total_sell_amount = sum([s[1] * s[2] for s in sell_record]) * 0.9975 total_buy_amount = buy_record[1] * buy_record[2] profit_amount = (total_sell_amount - total_buy_amount) total_amount += profit_amount print('*' * 50, 'CURRENT EARN', total_amount, '*' * 50) profit = (total_sell_amount - total_buy_amount) / total_buy_amount * 100 sell_datetime_arr = [s[0] for s in current_stocks[code]['sell']] sell_price_arr = [s[1] for s in current_stocks[code]['sell']] tick_analysis.start_tick_analysis( code, current_stocks[code]['start'], [current_stocks[code]['buy'][0]], sell_datetime_arr, [current_stocks[code]['buy'][1]], sell_price_arr, os.environ['MORNING_PATH'] + os.sep + 'output' + os.sep + 'my_tick', code + '\tprofit:' + str(profit) + '\tprofit amount:' + str(profit_amount)) current_stocks.pop(code, None)
def get_ready_timeout_order(self): timeout_list = [] order_list = self.get_ready_order_list() for order in order_list: if datetime.now() - order.order_time > DEFAULT_TIMEOUT: timeout_list.append(order) return timeout_list
def data_process(): picker = pickstock.PickStock() now = datetime.now() start_time = now.replace(hour=9 + DELTA, minute=0, second=3) done_time = now.replace(hour=15 + DELTA, minute=10, second=0) pick_finish_time = now.replace(hour=15 + DELTA, minute=5, second=0) entered = False last_processed_time = datetime.now() while True: if start_time <= datetime.now() <= done_time: entered = True while datetime.now() - last_processed_time < timedelta( seconds=PICK_SEC): gevent.sleep() candidates = [] print(datetime.now(), 'RUN PICKER') for sf in followers: snapshot = sf.snapshot(PICK_SEC) if (snapshot is None or not sf.is_in_market() or snapshot['profit'] < 0.5 or snapshot['yesterday_close'] == 0 or snapshot['today_open'] == 0 or snapshot['minute_max_volume'] / (15 / PICK_SEC) > snapshot['buy_volume'] + snapshot['sell_volume'] or snapshot['today_open'] < snapshot['yesterday_close'] or snapshot['amount'] < MINIMUM_AMOUNT): continue candidates.append(snapshot) picked = picker.pick_one(candidates) if picked is not None and datetime.now() <= pick_finish_time: search_profit(picked['code'], datetime.now(), picked['amount'], picked['profit'], picked['minute_max_volume'], picked['buy_volume'], picked['sell_volume'], picked['buy_speed'], picked['sell_speed']) logger.info('PICKED %s, amount: %d, profit: %f', picked['code'], picked['amount'], picked['profit']) last_processed_time = datetime.now() else: if entered: logger.info('QUEUE EXIT') date_str = datetime.now().strftime('%Y%m%d') pd.DataFrame(results).to_excel('picker_' + date_str + '_' + str(PICK_SEC) + '.xlsx') candidate_queue.put_nowait({'code': 'exit', 'info': None}) entered = False break gevent.sleep(0.02)
def __init__(self, price, qty): self.price = price self.order_quantity = qty self.order_quantity_org = qty self.order_number = 0 self.order_time = datetime.now() self.is_cancel_progressing = False self.is_cut = False self.status = tradestatus.SELL_ORDER_IN_TRANSACTION
def heart_beat(): now = datetime.now() last_processed_time = datetime.now() close_time = now.replace(hour=15 + DELTA, minute=40, second=3) finish_flag = False while True: while datetime.now() - last_processed_time < timedelta(seconds=1): gevent.sleep() candidates = [] while not candidate_queue.empty(): candidates.append(candidate_queue.get()) for c in candidates: if c['code'] == 'exit': logger.warning('SET FINISH FLAG') finish_flag = True if finish_flag: if datetime.now() > close_time: logger.warning('EXIT LOOP') break for fw in followers: if fw.get_status() != StockFollower.READY: fw.finish_work() else: for fw in followers: fw.process_tick() if fw.is_in_market() and fw.get_status( ) == StockFollower.READY: for c in candidates: if c['code'] == fw.code: fw.start_trading(c['info']) candidates.clear() last_processed_time = datetime.now()
def process_tick(self): # every 1 second this will be called if len(self.tick_data) == 0: return amount = sum( [d['current_price'] * d['volume'] for d in self.tick_data]) avg_price = np.array([d['current_price'] for d in self.tick_data]).mean() volume_in_price = dict() buy_volume = 0 sell_volume = 0 high_price = 0 for d in self.tick_data: if d['current_price'] in volume_in_price: volume_in_price[d['current_price']] += d['volume'] else: volume_in_price[d['current_price']] = d['volume'] if d['buy_or_sell'] == ord('1'): buy_volume += d['volume'] else: sell_volume += d['volume'] if d['current_price'] > high_price: high_price = d['current_price'] self.sec_data.append({ 'amount': amount, 'open': self.tick_data[0]['current_price'], 'close': self.tick_data[-1]['current_price'], 'high': high_price, 'volume_in_price': volume_in_price, 'buy_volume': buy_volume, 'sell_volume': sell_volume, 'date': datetime.now() }) self.avg_prices.append(avg_price) self.tick_data.clear() if self.trader is not None: peaks = list(price_info.get_peaks(self.avg_prices)) if peaks != self.top_edges: self.top_edges = peaks self.trader.top_edge_detected() bottom_peaks = list(price_info.get_peaks(self.avg_prices, False)) if bottom_peaks != self.bottom_edges: self.bottom_edges = bottom_peaks self.trader.bottom_edge_detected()
def data_process(): picker = pickstock.PickStock() now = datetime.now() start_time = now.replace(hour=9 + DELTA, minute=0, second=3) done_time = now.replace(hour=15 + DELTA, minute=10, second=0) pick_finish_time = now.replace(hour=15 + DELTA, minute=5, second=0) entered = False last_processed_time = datetime.now() while True: if start_time <= datetime.now() <= done_time: entered = True while datetime.now() - last_processed_time < timedelta( seconds=PICK_SEC): gevent.sleep() candidates = [] for sf in followers: snapshot = sf.snapshot(PICK_SEC) if (snapshot is None or not sf.is_in_market() or snapshot['profit'] < 0.5 or snapshot['yesterday_close'] == 0 or snapshot['today_open'] == 0 or snapshot['minute_max_volume'] / (15 / PICK_SEC) > snapshot['buy_volume'] + snapshot['sell_volume'] or snapshot['sell_volume'] > snapshot['buy_volume'] or snapshot['today_open'] < snapshot['yesterday_close'] or 10000 <= snapshot['current_price'] <= 20000 or snapshot['amount'] < MINIMUM_AMOUNT): continue candidates.append(snapshot) print('candidate size', len(candidates)) for c in candidates: print(c['code'], c['buy_volume'], c['sell_volume']) picked = picker.pick_one(candidates) if picked is not None and datetime.now() <= pick_finish_time: logger.info('PICKED %s, amount: %d, profit: %f', picked['code'], picked['amount'], picked['profit']) candidate_queue.put_nowait({ 'code': picked['code'], 'info': picked }) last_processed_time = datetime.now() else: if entered: logger.info('QUEUE EXIT') candidate_queue.put_nowait({'code': 'exit', 'info': None}) entered = False break time.sleep(0.02)
def modify_order(reader, order_num: int, code, price): print('process modify_order', order_num, code, price) previous_order = None for order in order_wait_queue: if order['order_number'] == order_num and order['code'] == code: previous_order = order break if previous_order is None: print('*' * 100, 'ERROR') return new_num = get_order_num() previous_order['order_number'] = new_num previous_order['flag'] = '2' previous_order['price'] = price gevent.spawn(send_modify_confirm_result, new_num, code, price, previous_order['quantity'], previous_order['order_type'], datetime.now()) return {'order_number': new_num}
def sleep(sec): now = datetime.now() while datetime.now() - now < timedelta(seconds=sec): gevent.sleep()
def modify(self, status, price): self.status = status self.price = price self.order_time = datetime.now()
def add_stock(code, price, quantity): if code not in current_stocks: print('ERROR cannot be in current_stocks when add') return current_stocks[code]['buy'] = (datetime.now(), price, quantity)
def set_start_time(code): if code in current_stocks: current_stocks.pop(code, None) current_stocks[code] = {'start': datetime.now(), 'buy': None, 'sell': []}
def order_stock(reader, code, price, quantity, is_buy): gevent.spawn(send_order_confirm_result, code, price, quantity, is_buy, datetime.now()) return {'status': 0, 'msg': 'OK'}