Пример #1
0
 def __init__(self, control):
     self._control = control
     self._observer = QueueBasedIterableObserver()
     self._enter_count = 0
     self._should_exit = False
     self._stack = ExitStack()
     self._lock = RLock()
    def __init__(self, stub):
        self._stub = stub
        self._fid_list = KiwoomOpenApiPlusRealType.get_fids_by_realtype('주식시세')

        self._request_observer = QueueBasedIterableObserver()
        self._request_iterator = iter(self._request_observer)
        self._response_iterator = self._stub.BidirectionalRealCall(self._request_iterator)
        self._response_subject = Subject()
        self._response_scheduler_max_workers = 8
        self._response_scheduler = ThreadPoolScheduler(self._response_scheduler_max_workers)
        self._buffered_response_iterator = QueueBasedBufferedIterator(self._response_iterator)
        self._response_observable = rx.from_iterable(self._buffered_response_iterator, self._response_scheduler)
        self._response_subscription = self._response_observable.subscribe(self._response_subject)

        self._subjects_by_code = {}

        self.initialize()
Пример #3
0
class KiwoomOpenApiPlusPriceEventChannel(Logging):

    _krx_timezone = get_calendar("XKRX").tz

    def __init__(self, stub):
        self._stub = stub
        self._fid_list = KiwoomOpenApiPlusRealType.get_fids_by_realtype_name(
            "주식시세")

        self._request_observer = QueueBasedIterableObserver()
        self._request_iterator = iter(self._request_observer)
        self._response_iterator = self._stub.BidirectionalRealCall(
            self._request_iterator)
        self._response_subject = Subject()
        self._response_scheduler_max_workers = 8
        self._response_scheduler = ThreadPoolScheduler(
            self._response_scheduler_max_workers)
        self._buffered_response_iterator = QueueBasedBufferedIterator(
            self._response_iterator)
        self._response_observable = rx.from_iterable(
            self._buffered_response_iterator, self._response_scheduler)
        self._response_subscription = self._response_observable.subscribe(
            self._response_subject)

        self._subjects_by_code = {}

        self.initialize()

    def close(self):
        for _code, (_subject, subscription) in self._subjects_by_code.items():
            subscription.dispose()
        self._response_subscription.dispose()
        self._buffered_response_iterator.stop()
        self._response_iterator.cancel()

    def __del__(self):
        self.close()

    def initialize(self):
        request = KiwoomOpenApiPlusService_pb2.BidirectionalRealRequest()
        request.initialize_request.fid_list.extend(self._fid_list)  # pylint: disable=no-member
        self._request_observer.on_next(request)

    def register_code(self, code):
        request = KiwoomOpenApiPlusService_pb2.BidirectionalRealRequest()
        code_list = [code]
        fid_list = KiwoomOpenApiPlusRealType.get_fids_by_realtype_name("주식시세")
        request.register_request.code_list.extend(code_list)  # pylint: disable=no-member
        request.register_request.fid_list.extend(fid_list)  # pylint: disable=no-member
        self._request_observer.on_next(request)
        self.logger.debug("Registering code %s for real events", code)

    def is_for_code(self, response, code):
        return response.arguments[0].string_value == code

    def filter_for_code(self, code):
        return ops.filter(lambda response: self.is_for_code(response, code))

    def is_valid_price_event(self, response):
        return all(name in response.single_data.names
                   for name in ["20", "27", "28"])

    def filter_price_event(self):
        return ops.filter(self.is_valid_price_event)

    def time_to_timestamp(self, fid20):
        dt = datetime.datetime.now(self._krx_timezone).date()
        tm = datetime.datetime.strptime(fid20, "%H%M%S").time()
        dt = datetime.datetime.combine(dt, tm)
        dt = self._krx_timezone.localize(dt)
        return dt.timestamp() * (10**6)

    def event_to_dict(self, response):
        single_data = dict(
            zip(response.single_data.names, response.single_data.values))
        result = {
            "time": self.time_to_timestamp(single_data["20"]),
            "bid": abs(float(single_data["28"])),
            "ask": abs(float(single_data["27"])),
        }
        return result

    def convert_to_dict(self):
        return ops.map(self.event_to_dict)

    def get_observable_for_code(self, code):
        self.register_code(code)
        if code not in self._subjects_by_code:
            subject = Subject()
            subscription = self._response_subject.pipe(
                self.filter_for_code(code),
                self.filter_price_event(),
                self.convert_to_dict(),
            ).subscribe(subject)
            self._subjects_by_code[code] = (subject, subscription)
        subject, subscription = self._subjects_by_code[code]
        return subject
 def _add_observer(self, callback):
     with self._lock:
         self._remove_observer(callback)
         observer = QueueBasedIterableObserver()
         self._observers[self._stub][self._name][callback] = observer
         return observer