Пример #1
0
 def subscribe_at_startup(self):
     self.started_at_startup = True
     stock_api.subscribe_stock(self.reader, self.code,
                               self.tick_data_handler)
     stock_api.subscribe_stock_bidask(self.reader, self.code,
                                      self.ba_data_handler)
     stock_api.subscribe_stock_subject(self.reader, self.code,
                                       self.subject_handler)
Пример #2
0
 def RequestCybosTickData(self, request, context):
     global is_subscribe_tick
     if request.code not in is_subscribe_tick:
         stock_api.subscribe_stock(morning_client.get_reader(),
                                   request.code, self.handle_stock_tick)
         _LOGGER.info('Start SubscribeStock %s', request.code)
         is_subscribe_tick[request.code] = True
     else:
         _LOGGER.info('Already SubscribeStock %s', request.code)
     return Empty()
Пример #3
0
 def start_watch(self):
     print('request min data')
     min_data = stock_api.request_stock_today_data(self.reader, self.code)
     print('today min len', len(min_data))
     if len(min_data) > 0:
         print('Subscribe Start', self.code)
         self.min_data = min_data
         stock_api.subscribe_stock(self.reader, self.code,
                                   self.tick_data_handler)
         stock_api.subscribe_stock_bidask(self.reader, self.code,
                                          self.ba_data_handler)
         stock_api.subscribe_stock_subject(self.reader, self.code,
                                           self.subject_handler)
         return True
     return False
Пример #4
0
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)
Пример #5
0
from configs import db
from pymongo import MongoClient
from morning.pipeline.converter import dt
import numpy as np
from scipy.signal import find_peaks, peak_prominences
import pandas as pd


def wait_loop():
    while True:
        gevent.sleep(0.03)


def tick_data_handler(code, data):
    if len(data) != 1:
        return
    print(data[0])


market_code = morning_client.get_market_code()  # KOSDAQ
kospi_market_code = morning_client.get_market_code(message.KOSPI)

market_code.extend(kospi_market_code)
print('all', len(market_code))

for code in market_code:
    stock_api.subscribe_stock(morning_client.get_reader(), code,
                              tick_data_handler)

gevent.joinall([gevent.spawn(wait_loop)])
Пример #6
0
    min = int(data['time_with_sec'] % 10000 / 100)
    second = int(data['time_with_sec'] % 100)
    data['date'] = datetime.combine(now.date(), time(hour, min, second))
    if now - data['date'] > timedelta(seconds=30):
        print('delayed', (now - data['date']).seconds)
        out_time_count += 1
    else:
        within_time_count += 1

def count_check():
    while True:
        print('within', within_time_count, 'out', out_time_count)
        gevent.sleep(5)

if __name__ == '__main__':
    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()
    market_code = stock_api.request_stock_code(message_reader, message.KOSDAQ)
    print('request code done')
    market_code = market_code[:200]
    gevent.spawn(count_check)

    for code in market_code:
        print('request subscribe', code)
        stock_api.subscribe_stock(message_reader, code, handler)
    print('waiting data')
    message_reader.join()
Пример #7
0
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))