Giter Site home page Giter Site logo

smk_python_sdk's Introduction

smk-python-sdk

Build status

Smarkets Streaming API Python client.

Compatible with Python 2.7, 3.5 and PyPy 1.9+.

Documentation: http://smarkets-python-sdk.readthedocs.org/en/latest/

Installing using source distribution

When you install smk-python-sdk using PyPI distribution there are no non-Python dependencies. All the Python dependencies will be fetched for you when you install smk-python-sdk.

PyPI page: https://pypi.python.org/pypi/smk_python_sdk

pip install smk-python-sdk

Getting Started

import logging
logging.basicConfig(level=logging.DEBUG)

from smarkets.streaming_api.api import (
    SessionSettings, Session, StreamingAPIClient,
)

username = 'username'
password = 'password'

settings = SessionSettings(username, password)
settings.host = 'stream.smarkets.com'

session = Session(settings)

client = StreamingAPIClient(session)

client.login()
client.ping()
client.read()
client.flush()

client.logout()

Registering callbacks

def login_response(message):
    print('eto.login_response', msg)

def global_callback(name, message):
    print(name, message.protobuf)

client.add_handler('eto.login_response', login_response)
client.add_global_handler(global_callback)

Placing orders

from smarkets.streaming_api.api import SIDE_BID
from smarkets.streaming_api.seto import OrderCreate

def order_accepted(message):
    reference = message.order_accepted.reference
    order_id = message.order_accepted.order_id
    print(
        'ORDER_ACCEPTED: reference {} corresponding to order_id {}'.format(
            reference, order_id,
        )
    )

def order_rejected(message):
    reference = message.order_rejected.reference
    reason = message.order_rejected.reason
    print('ORDER_REJECTED with reference {} with reason {}'.format(reference, reason))

client.add_handler('seto.order_accepted', order_accepted)
client.add_handler('seto.order_rejected', order_rejected)

market_id = 100000
contract_id = 200000

order = OrderCreate()
order.quantity = 400000 # £40 payout
order.price = 2500 # 25.00%
order.side = SIDE_BID
order.market_id = market_id
order.contract_id = contract_id

client.send(order)
client.flush()

Cancelling orders

from smarkets.streaming_api.seto import OrderCancel

order_id = ...  # received in seto.order_accepted message

def order_cancelled(message):
    order_id = message.order_cancelled.order_id
    reason = message.order_cancelled.reason
    print('ORDER_CANCELLED order_id {} with reason {}'.format(order_id, reason))

def order_cancel_rejected(message):
    order_id = message.order_cancel_rejected.order_id
    reason = message.order_cancel_rejected.reason
    print('ORDER_CANCEL_REJECTED: with order_id {} with reason {}'.format(order_id, reason))

client.add_handler('seto.order_cancelled', order_cancelled)
client.add_handler('seto.order_cancel_rejected', order_cancel_rejected)

cancel = OrderCancel()
cancel.order_id = order_id
client.send(cancel)
client.flush()

Thread Safety

Functions and class members contained in this package are thread safe. Instance members are not thread safe.

Development

GitHub repository: https://github.com/smarkets/smk_python_sdk/

Non-Python dependencies:

  • piqi
  • Google protocol buffers compiler and libraries

You can install Python dependencies by executing:

pip install -r requirements.txt

To build and install call:

python setup.py clean build install

Note: building the package does not fetch the most recent piqi files from their respective locations in setup.py. In order to do so, you must call python setup.py clean, and then python setup.py build.

License

Copyright (C) Smarkets Limited <[email protected]>

This module is released under the MIT License: http://www.opensource.org/licenses/mit-license.php (or see the LICENSE file)

smk_python_sdk's People

Contributors

hntrmrrs avatar jaimelennox avatar jcpetruzza avatar jstasiak avatar mike-baker avatar ozamosi avatar pianiel avatar russss avatar terencesmarkets avatar u-quark avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

smk_python_sdk's Issues

Can't resolve "stream.smarkets.com"

Hi, I try to run the intro script and can't resolve host "stream.smarkets.com"
I also cant resolve it using google DNS server. On older "api.smarkets.com" I get timeout error.

>>> client.login()
INFO:smarkets.session.socket:connecting with new socket to stream.smarkets.com:3801
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/anaconda2/lib/python2.7/site-packages/smarkets/streaming_api/client.py", line 54, in login
    self.session.connect()
  File "/opt/anaconda2/lib/python2.7/site-packages/smarkets/streaming_api/session.py", line 90, in connect
    if self.socket.connect():
  File "/opt/anaconda2/lib/python2.7/site-packages/smarkets/streaming_api/session.py", line 253, in connect
    reraise(ConnectionError(self._error_message(exc)))
  File "/opt/anaconda2/lib/python2.7/site-packages/smarkets/errors.py", line 13, in reraise
    _six.reraise(type(exception), exception, tb)
  File "/opt/anaconda2/lib/python2.7/site-packages/smarkets/streaming_api/session.py", line 249, in connect
    sock.connect((self.settings.host, self.settings.port))
  File "/opt/anaconda2/lib/python2.7/ssl.py", line 876, in connect
    self._real_connect(addr, False)
  File "/opt/anaconda2/lib/python2.7/ssl.py", line 863, in _real_connect
    socket.connect(self, addr)
  File "/opt/anaconda2/lib/python2.7/socket.py", line 228, in meth
    return getattr(self._sock,name)(*args)
smarkets.streaming_api.exceptions.ConnectionError: Error -2 connecting stream.smarkets.com:3801. Name or service not known.
>>> 

Is this package still being maintained?

I'm following documentation and I'm getting an error trying to log in.

Is this package still being maintained?
Is there a plan to maintain it?

Error
smarkets.streaming_api.exceptions.ConnectionError: Error -2 connecting stream.smarkets.com:3801. Name or service not known.

import logging
logging.basicConfig(level=logging.DEBUG)

from smarkets.streaming_api.api import SessionSettings, Session, StreamingAPIClient

username = '[email protected]'
password = 'barpassword'

settings = SessionSettings(username, password)
settings.host = 'stream.smarkets.com'
session = Session(settings)

client = StreamingAPIClient(session)
client.login()

Thanks

Can't instantiate more than one smarkets.clients.Smarkets

If you have more than one client, their Callback objects are the same.

>>> import smarkets.clients
>>> a = smarkets.clients.Smarkets('_')
>>> b = smarkets.clients.Smarkets('_')
>>> def h(msg): print repr(("client a received", msg))
... 
>>> a.add_handler('seto.http_found', h)

# pretend b is connected and got a message...
>>> b.callbacks['seto.http_found']('example_message')
('client a received', 'example_message')

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.