Pages

Thursday, January 7, 2016

Streaming OANDA with python and ZeroMQ

I have been looking at its REST API for OANDA, for potential use with an FX trading system I developed.

The API has two streaming endpoints, one for prices and one for account events such as trades opening and stuff like that.  

Asynchronous IO is always a bit fiddly, and I wanted separate processes for incoming tick data and events. This enables them to be managed separately, and generally makes handling disconnects or other errors a bit cleaner.

If you try and mash your various feeds, trade identification, trade management, logging, accounting etc all into one big process it gets a bit messy and convoluted. Sometimes you don’t have a choice due to the API available, but thankfully in this case it’s easy to separate everything out.

Details


I use the requests library for streaming from OANDA, and ZeroMQ endpoints with the pub/sub pattern for passing data to the client(s).

Conceptually, it looks like this:



The prices feed connects to the OANDA prices endpoint, and publishes what it receives to a ZeroMQ socket. Likewise, the events feed does the same, publishing to another zmq socket.

The client connects to the respective sockets and subscribes to its feeds, and can do whatever is required with the data it receives. You are not limited to one client subscriber either.

I use JSON as the serialization format, as it arrives from OANDA. No real point in deserializing JSON to an object, then immediately serializing that with pickle or something for transport with zeromq. Your needs may differ of course.  

All up, a pretty painless process.

Code


The single file of code (here) is the prices feed, events feed and client that just prints out what it receives. Just run it separately three times, passing either prices, events or client as a single argument. You will also need to fill in your OANDA details as well.

I use a 10 second timeout for the prices feed, and a 20 second timeout for the events feed, as per OANDAs recommendations. The feed streams will print heartbeats but not publish them to clients.

Thanks for reading and let me know how you go.

8 comments:

  1. Hello everyone, Are you looking for a professional trader, forex and binary manager who will help you trade and manager your account with good and massive amount of profit in return. you can contact Mr. Anderson for your investment plan, for he helped me earned 12,000usd with little investment funds. Mr Anderson you're the best trader I can recommend for anyone who wants to invest and trade with a genuine trader, he also helps in recovery of loss funds..you can contact him on his whatsapp: (+447883246472) Email:(tdameritrade077@gmail.com)I advice you shouldn't hesitate He's great.

    ReplyDelete
  2. I have read Your Article and Learn a lot from it. I can Imagine the time it took to get all this expertise exposed . You can also look here to see where to easily get a FAKE BANK STATEMENT I've bookmark your site and furthermore include RSS. keep us refreshed all the time.

    While doing more research , I fell on this EXPERTS TELEGRAM GROUP which helped me to obtain a statement for Wells Fargo of which my loan was approved with ease . I was amazed so I decided to share the platform for everyone to enjoy their services.

    ReplyDelete
  3. We have specially programmed ATMs that can be used to withdraw money at ATMs, shops and points of sale. We sell these cards to all our customers and interested buyers all over the world, the cards have a withdrawal limit every week.The programmed ATM card withdraw money from each ATM but have a withdrawal limit every week, only your PIN code is in it, it is a high-tech card system. The PROGRAMMED ATM card works on all card-based ATMs, anywhere in the world.
    -hack into any kind of phone
    _Increase Credit Scores
    _western union, bitcoin and money gram hacking
    _criminal records deletion_BLANK ATM/CREDIT CARDS
    _Hacking of phones(that of your spouse, boss, friends, and see whatever is being discussed behind your back)
    _Security system hacking...and so much more. Contact us now and get whatever you want at
    Email:creditcards.creditscoreupgrade@gmail.com
    whatsapp:+1785 279 3047

    ReplyDelete
  4. My name is Mohammed Ahsan I'm a financial Consultant of a reputable firm Operating out of the United Kingdom
    we provide Discrete Financial, Services for High Net Worth CEO's/Executives of Corporation & Senior Government Officials globally.
    What we do is Legal & within the ambit of the law but following the Leakage of the Infamous United Kingdom
    Papers & subsequent threat by the International Consortium of Investigative Journalists (ICIJ)
    International Criminal Court (ICC)
    Below is what my Sender can offer.
    1, MT103 gpi automatic
    2, MT103 wire transfer
    3, MT103 TT
    4, MT103/202 manual download
    5, SBLC lease and Purchase

    All bank instrument will be done via bank officer to bank officer, Receiver has to provide bank officer details
    on DOA all brokers are welcome and guarantee of there payment after successful deal
    kindly send me a message via email and WhatsApp.


    Email: mohammedahsan877@gmail.com

    WhatsApp- +19893413179

    ReplyDelete
  5. I am Larry Baier. I can never stop thanking Dr Graceondu for transforming my life, i have been playing lottery games for a very long time and i have never one day win any game, i met a friend whom i complained to about my inability to win and he introduced me to Dr Graceondu whom helped him by praying for him and giving him a game that he played and he won $150,000
    so i contacted him through his email and he assured me that i will win big. he asked me to give him two day so he can make some prayers on my behalf so that the winning numbers will be revealed to him, so i waited for two days the second day he sent me the number to play, so i play the game i never had it in my mind that i was going to win such amount of money, so on that faithful day i went to check the game on April 16, 2021 and behold i got the biggest shock of my life i won $2,000,000 and from that moment my life changed.
    all thanks to Dr Graceondu, i want to also advice everyone out there to also contact him if you have been playing lottery and you have never won.
    You can reach him via his email, drgraceondu12@gmail.com or WhatsApp him +15204677763, you can also visit his Website. https://drgraceonduadodo.com

    ReplyDelete
  6. RECOVER YOUR FUNDS FROM FAKE INVESTMENT COMPANY, FAKE BINARY OPTION ETC.
    Have you been ripped by scammers online, have you invested your funds in the wrong investment companies? You do not need to worry any more as we provide you with an answer which is GETTING BACK WHAT YOU HAVE LOST. We at CRYPTOREFUNDER360(AT)GMAIL(DOT)COM provide Btc recovery services. It does not matter how mush you have been ripped off or when it happened, provided we have the right and necessary informations, then we can assure you of a quick and safe recovery. We have testimonies flowing daily on lost funds being recovered safely. You may not be in need of this but tell a friend who is. You can contact us by mail @ Cryptorefunder360(at)gmail(dot)com.

    ReplyDelete
  7. Borrow money here today at 3% interest rate. Sawda Capital Finance offers all kinds of financial services of all sizes ranging from individuals, companies, and traders globally./Our services are 100% guaranteed and risk-free.

    We build just the right financing for each client, with simple paperwork, quick approvals, and flexible payment schedules. For further details, Kindly contact us via email At sawda.finance@gmail.com

    Intermediaries/Consultants/Brokers are welcome to bring their clients and are 100% protected. In complete confidence, we will work together for the benefits of all parties involved, Looking forward to rendering the best of our services to all esteemed clientele globally.

    ReplyDelete