1

As an example for a class, I'm trying to figure out the most robust way to create a hot Observable that emits current prices for a given stock ticker, but only have it emit a price when it changes. The best I could come up with on my own is create an interval source Observable that will query Google Finance every 3 seconds, parse out the price, and only push it forward if the value has changed via distinct_until_changed().

from threading import Semaphore

from rx import Observable
from googlefinance import getQuotes
import json


Observable.interval(3000) \
    .map(lambda i: json.dumps(getQuotes('AAPL'))) \
    .map(lambda s: json.loads(s)[0]) \
    .map(lambda dict: float(dict["LastTradePrice"])) \
    .distinct_until_changed() \
    .subscribe(print)

sema = Semaphore(0)

sema.acquire()

Does anyone know of a more efficient way to do this, and not have to re-query at an interval but rather have the source be triggered when the price actually changes? If it's overly complicated and requires some specialized library, I'll stick with what I have. I just feel like I am missing a much better way to do this...

tmn
  • 11,121
  • 15
  • 56
  • 112
  • I am no expert here, but I think in order for you to only get updates when something actually updates, you will need google to supply a service that supports websockets or something. – Metareven Nov 02 '16 at 08:13
  • Yeah not an expert either on socket programming, and I don't want to turn this into a "is there a library for... " question else I start breaking guidelines. I guess I'm open to any solution at this point... I'll do some more research today. – tmn Nov 02 '16 at 14:25
  • There can't be better solution. You have to periodically poll data source for changes, as there is no mechanism for this data source to call you back. – Yaroslav Stavnichiy Jan 13 '17 at 08:19

0 Answers0