Coder Social home page Coder Social logo

betfair / stream-api-sample-code Goto Github PK

View Code? Open in Web Editor NEW
73.0 31.0 60.0 1.78 MB

Sample code for the exchange stream api which provide real time market and order data from betfair.

C# 78.92% Batchfile 0.06% Java 20.52% JavaScript 0.29% Dockerfile 0.21%

stream-api-sample-code's Introduction

stream-api-sample-code

Sample code for the exchange stream api which provides real-time market and order data from the betfair exchange.

Console App

The console app enables you to explore most of the API functions; please note that:

  1. Your appkey must be setup for streaming (contact Developer Support via https://developer.betfair.com/support/)
  2. Credentials for .net are stored in AppData in plain text

Note: Use stream-api.betfair.com in production (stream-api-integration.betfair.com is for testing and has no backup).

Client overview

The basic client structure is separated into a number of components:

  • AppKeyAndSessionProvider - this class is used to authenticate & provide a session token.
  • Client - this class provides a connection to the stream
  • ClientCache - this class provides a thread safe cache that may be used to:
  • Respond to discrete changes
  • Respond to batch changes
  • Directly query the cache

Getting started

The below is found in the ClientCacheTest and exhibits the basic setup user story:

        //1: Create a session provider
        AppKeyAndSessionProvider sessionProvider = new AppKeyAndSessionProvider(
            AppKeyAndSessionProvider.SSO_HOST_COM,
            AppKey,
            UserName,
            Password);

        //2: Create a client
        Client client = new Client(
            "stream-api-integration.betfair.com", //NOTE: use production endpoint in prod: stream-api.betfair.com
            443,
            sessionProvider);

        //3: Create a cache
        ClientCache cache = new ClientCache(client);

        //4: Setup order subscription
        //Register for change events
        cache.OrderCache.OrderMarketChanged += 
            (sender, arg) => Console.WriteLine("Order:" + arg.Snap);
        //Subscribe to orders    
        cache.SubscribeOrders();

        //5: Setup market subscription
        //Register for change events
        cache.MarketCache.MarketChanged += 
            (sender, arg) => Console.WriteLine("Market:" + arg.Snap);
        //Subscribe to markets (use a valid market id or filter)
        cache.SubscribeMarkets("1.125037533");

Connection semantics

A few tips on basic connections:

  1. No need to explicitly start / stop if using client cache
  2. Status event allows you to monitor status
  3. AutoReconnect is enabled by default and will establish and re-subscribe any subscriptions.
  4. Exceptions are routed up to subscribe methods

Market Subscription

A few tips on market subscription:

  1. MarketDataFilter - correctly setting this on the cache improves performance
  2. MarketFilter - you are not limited to knowing specific market ids; wildcards let you know as soon as a market appears
  3. ConflateMs - you can slow down the data rate (to say a GUI refresh rate).
  4. By default markets are deleted on close

Order Subscription

A few tips on order subscription:

  1. Orders are retrieved for your id
  2. On initial connection (or re-connect) only unmatched / executable orders are returned.
  3. Matches are price point aggregated
  4. By default markets are deleted on close

Docker

It is possible to run the java application through a docker container, if you want to build yourself and run, execute on parent directory:

  1. docker build -t "tag" -f java/Dockerfile .
  2. docker run -it "tag"

Otherwise make use of docker-compose features:

  1. Simply with a single command "docker-compose run esaconsole" will set everything up.

Specification & Schema

###Specification: https://github.com/betfair/stream-api-sample-code/blob/master/stream-api-specification.pdf

###Schema We publish a swagger schema to define the object model: http://editor.swagger.io/#/?import=https://raw.githubusercontent.com/betfair/stream-api-sample-code/master/ESASwaggerSchema.json (Note: the stream is not a rest service so generated swagger clients are of limited use although you can use the generated object model)

stream-api-sample-code's People

Contributors

bfapi avatar felipefonsecadev avatar funnellm-ppb avatar graterolc avatar samorton avatar

Stargazers

 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  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  avatar  avatar  avatar  avatar  avatar  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  avatar  avatar  avatar  avatar  avatar  avatar

stream-api-sample-code's Issues

MarketChangeMessage and OrderChangeMessage Id and Op not overriding Change message properties

I'm not sure if this is intended behaviour, the way swagger creates definitions or an oversight, but is there a reason for this.

Derived classes Betfair.ESASwagger.Model.MarketChangeMessage and Betfair.ESASwagger.Model.OrderChangeMessage hide both Id and Op properties from Betfair.ESASwagger.Model.ChangeMessage. With the demo code it doesn't cause any issue, however extending the behaviour of the code does through some unintended behaviour.

Thanks
Wook

Socket has been ended by other party

I have tried using the following code to get the market data and it is working but sometimes I'm getting the error "socket has been ended by other party" and then I have to restart the server to make it work again.

var tls = require('tls');

/* Socket connection options */

var options = {
host: 'stream-api.betfair.com',
// host: 'stream-api-integration.betfair.com',
port: 443
}

/* Establish connection to the socket */

var client = tls.connect(options, function () {
console.log("Connected");
});

/* Send authentication message */

client.write('{"op": "authentication", "appKey": "APIKEY", "session": "TOKEN"}\r\n');

/* Subscribe to order/market stream */
stream.write({"op":"marketSubscription","marketFilter":{"marketIds":["1.160865586"],"bettingTypes":["ODDS"],"eventTypeIds":["2"],"eventIds":["29390842"]},"marketDataFilter":{}}\r\n);

client.on('data', function(data) {
console.log('Received: ' + data);
});

client.on('close', function() {
console.log('Connection closed');
});

client.on('error', function(err) {
console.log('Error:' + err);
})

OrderCache.cs is broken after f7c2432f3a2449d9159d648d11663585ad652d6f commit

There are two main issues:

  1. Curly bracket closed to early and some members are left out of OrderCache class (defined in the namespace).
  2. OrderMarketChange has no FullImage member (I can see that in swagger schema, but not in the class - maybe I am missing something).

In the current state, this sample is basically not usable.

ECONNRESET on the js example code

Hi.

I got the swagger schema and used the editor to generate the JS code.
The example I'm given to try :

var BetfairExchangeStreamingApi = require('betfair_exchange_streaming_api');

var api = new BetfairExchangeStreamingApi.DefaultApi()

var requestMessage = new BetfairExchangeStreamingApi.AllRequestTypesExample(); // {AllRequestTypesExample} Requests are sent to socket


var callback = function(error, data, response) {
  if (error) {
    console.error(error);
  } else {
    console.log('API called successfully. Returned data: ' + data);
  }
};
api.requestPost(requestMessage, callback);

gives back this :

{ Error: socket hang up
    at createHangUpError (_http_client.js:345:15)
    at Socket.socketOnEnd (_http_client.js:437:23)
    at emitNone (events.js:110:20)
    at Socket.emit (events.js:207:7)
    at endReadableNT (_stream_readable.js:1059:12)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9) code: 'ECONNRESET', response: undefined }

Any idea why I'm getting this error?

Stop from receiving the data

I'm using the following code multiple times to get the odds of different event.

var tls = require('tls');

/* Socket connection options */

var options = {
host: 'stream-api.betfair.com',
// host: 'stream-api-integration.betfair.com',
port: 443
}

/* Establish connection to the socket */

var client = tls.connect(options, function () {
console.log("Connected");
});

function getMarketData(marketIdArray, eventTypeIdArray, eventIdArray) {
/* Send authentication message */
client.write('{"op": "authentication", "appKey": "APIKEY", "session": "TOKEN"}\r\n');

/* Subscribe to order/market stream */
stream.write({"op":"marketSubscription","marketFilter":{"marketIds":[${marketIdArray}],"bettingTypes":["ODDS"],"eventTypeIds":[${eventTypeIdArray}],"eventIds":[${eventIdArray}]},"marketDataFilter":{}}\r\n);

client.on('data', function(data) {
console.log('Received: ' + data);
});

client.on('close', function() {
console.log('Connection closed');
});

client.on('error', function(err) {
console.log('Error:' + err);
});
}

router.get('/getMarketData', function(req, res, next) {

getMarketData(req.marketIdArray, req.eventTypeIdArray, req.eventIdArray);

})

The problem is that once I start getting the odds then it continue even if I close my browser.
Lets suppose in one browser tab I have open odds for cricket and in 2nd browser tab for tennis. Now, I'm getting the odds for both which is good. Now, if I close the first browser tab of cricket then I want to stop receiving the odds but in my server I can still see the odds receiving.

So, how can I stop receiving the odds once the browser is closed or is there any event to stop receving the odds.

Any help or suggestions or ideas?

AppKey is not configured for service

When we try to connect via unit test ClientTest#testStartStop, always response "AppKey is not configured for service",
API: stream-api-integration.betfair.com:443
Version:1.0-DELAY

LOG here:
`[TestNG] Running:
/Users/wilson/Library/Caches/IntelliJIdea2017.1/temp-testng-customsuite.xml
[main] INFO com.betfair.esa.client.auth.AppKeyAndSessionProvider - SSO Login - expiring session token now
[main] INFO com.betfair.esa.client.auth.AppKeyAndSessionProvider - SSO Login host=identitysso.betfair.com, appkey=YIzyW8PCO4xmRrV9, username=[email protected]
[main] INFO com.betfair.esa.client.auth.AppKeyAndSessionProvider - identitysso.betfair.com: Response: com.betfair.esa.client.auth.AppKeyAndSessionProvider$SessionDetails@56cdfb3b
[main] INFO com.betfair.esa.client.Client - ESAClient: Opening socket to: stream-api-integration.betfair.com:443
[ESAClient] INFO com.betfair.esa.client.Client - ESAClient: Processing thread started
[ESAClient] INFO com.betfair.esa.client.protocol.RequestResponseProcessor - ESA->Client: {"op":"connection","connectionId":"050-010818025850-353593"}
[ESAClient] INFO com.betfair.esa.client.protocol.RequestResponseProcessor - ESAClient: Status changed STOPPED -> CONNECTED
[main] INFO com.betfair.esa.client.auth.AppKeyAndSessionProvider - SSO Login - session not expired - re-using
[main] INFO com.betfair.esa.client.protocol.RequestResponseProcessor - Client->ESA: {"op":"authentication","id":1,"appKey":"YIzyW8PCO4xmRrV9","session":"bMb44v+ueU/muxPxVzavbPLNQ0RE1MctSMH5bMsX4eo="}
[ESAClient] INFO com.betfair.esa.client.protocol.RequestResponseProcessor - ESA->Client: {"op":"status","id":1,"statusCode":"FAILURE","errorCode":"NOT_AUTHORIZED","errorMessage":"AppKey is not configured for service","connectionClosed":true,"connectionId":"050-010818025850-353593"}
[ESAClient] ERROR com.betfair.esa.client.Client - ESAClient: Error received processing socket - disconnecting:
java.io.IOException: Socket closed - EOF
at com.betfair.esa.client.Client.run(Client.java:346)
at java.lang.Thread.run(Thread.java:748)
[ESAClient] INFO com.betfair.esa.client.protocol.RequestResponseProcessor - ESAClient: Status changed CONNECTED -> STOPPED
[ESAClient] WARN com.betfair.esa.client.Client - ESAClient: Processing thread stopped

com.betfair.esa.client.protocol.ConnectionException: Future failed:

at com.betfair.esa.client.Client.waitFor(Client.java:283)
at com.betfair.esa.client.Client.authenticate(Client.java:244)
at com.betfair.esa.client.Client.connectAndAuthenticate(Client.java:228)
at com.betfair.esa.client.Client.start(Client.java:122)
at com.betfair.esa.client.ClientTest.testStartStop(ClientTest.java:46)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:696)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:882)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1189)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:124)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:348)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:343)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:305)
at org.testng.SuiteRunner.run(SuiteRunner.java:254)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:127)

Caused by: com.betfair.esa.client.protocol.ConnectionException: Connection reset - task cancelled
at com.betfair.esa.client.protocol.RequestResponseProcessor.reset(RequestResponseProcessor.java:112)
at com.betfair.esa.client.protocol.RequestResponseProcessor.stopped(RequestResponseProcessor.java:132)
at com.betfair.esa.client.Client.disconnected(Client.java:387)
at com.betfair.esa.client.Client.run(Client.java:360)
at java.lang.Thread.run(Thread.java:748)

===============================================
Default Suite
Total tests run: 1, Failures: 1, Skips: 0

`

AppKey is not configured for service

I have configured my keys in
https://docs.developer.betfair.com/visualisers/api-ng-account-operations/
yet am getting this error when running node/app.js:

$ node app.js
Connected
Received: {"op":"connection","connectionId":"105-181219162252-316581"}

Received: {"op":"status","statusCode":"FAILURE","errorCode":"NOT_AUTHORIZED","errorMessage":"AppKey is not configured for service","connectionClosed":true,"connectionId":"105-181219162252-316581"}

Connection closed

Any help what could be the problem?

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.