Permalink
Browse files

Initial commit

  • Loading branch information...
1 parent c9a487f commit c0d4c1a1bd7d51c34417942ca5a6ac125f1d776a @AlgoTrader committed Dec 13, 2012
Showing with 399 additions and 0 deletions.
  1. +15 −0 .gitignore
  2. +4 −0 lib/betfair_invocation.js
  3. +142 −0 lib/betfair_price.js
  4. +60 −0 lib/betfair_session.js
  5. +46 −0 test/common.js
  6. +16 −0 test/login.js
  7. +116 −0 util/forever.js
View
@@ -0,0 +1,15 @@
+lib-cov
+*.seed
+*.log
+*.csv
+*.dat
+*.out
+*.pid
+*.gz
+
+pids
+logs
+results
+
+node_modules
+npm-debug.log
@@ -0,0 +1,4 @@
+//(C) 2012 Anton Zemlyanov
+
+//This module describes Betfair invocation
+//see Sports API documentation on http://bdp.betfair.com
View
@@ -0,0 +1,142 @@
+//(C) 2012 Anton Zemlyanov
+
+//This module describes Betfair price
+//see Sports API documentation on http://bdp.betfair.com
+
+exports.newBetfairPrice = function(size) {
+ return new BetfairPrice(size);
+}
+
+var epsilon = 0.001;
+
+function BetfairPrice(size) {
+ if (!size)
+ size = 1.01;
+
+ this.setStake(size);
+}
+
+BetfairPrice.prototype.setStake = function(size) {
+ if (!size)
+ size = 1.01;
+ if (typeof (size) === 'string')
+ size = parseFloat(size);
+
+ if (size < 1.01)
+ size = 1.01;
+ else if (size < 2)
+ size = Math.round(size * 100.0) / 100.0;
+ else if (size < 3)
+ size = Math.round(size * 50.0) / 50.0;
+ else if (size < 4)
+ size = Math.round(size * 20.0) / 20.0;
+ else if (size < 6)
+ size = Math.round(size * 10.0) / 10.0;
+ else if (size < 10)
+ size = Math.round(size * 5.0) / 5.0;
+ else if (size < 20)
+ size = Math.round(size * 2.0) / 2.0;
+ else if (size < 30)
+ size = Math.round(size * 1.0) / 1.0;
+ else if (size < 50)
+ size = Math.round(size * 0.5) / 0.5;
+ else if (size < 100)
+ size = Math.round(size * 0.2) / 0.2;
+ else if (size < 1000)
+ size = Math.round(size * 0.1) / 0.1;
+ else
+ size = 1000.0;
+
+ this.size = size;
+ return;
+}
+
+BetfairPrice.prototype.toString = function(size) {
+ var str = this.size.toFixed(2);
+ if (str.charAt(str.length - 1) === '0')
+ str = str.substr(0, str.length - 1);
+ // if( str.charAt(str.length-1)==='0' )
+ // str = str.substr(0, str.length-2);
+ return str;
+}
+
+BetfairPrice.prototype.increasePrice = function() {
+ var size = this.size;
+
+ if (size < (2.0 - epsilon))
+ size += 0.01;
+ else if (size < (3.0 - epsilon))
+ size += 0.02;
+ else if (size < (4.0 - epsilon))
+ size += 0.05;
+ else if (size < (6.0 - epsilon))
+ size += 0.1;
+ else if (size < (10.0 - epsilon))
+ size += 0.2;
+ else if (size < (20.0 - epsilon))
+ size += 0.5;
+ else if (size < (30.0 - epsilon))
+ size += 1.0;
+ else if (size < (50.0 - epsilon))
+ size += 2.0;
+ else if (size < (100.0 - epsilon))
+ size += 5.0;
+ else if (size < (1000.0 - epsilon))
+ size += 10.0;
+ else
+ size = 1000.0;
+
+ this.size = size;
+ return this;
+}
+
+BetfairPrice.prototype.decreasePrice = function() {
+ var size = this.size;
+
+ if (size > (100.0 + epsilon))
+ size -= 10.0;
+ else if (size > (50.0 + epsilon))
+ size -= 5.0;
+ else if (size > (30.0 + epsilon))
+ size -= 2.0;
+ else if (size > (20.0 + epsilon))
+ size -= 1.0;
+ else if (size > (10.0 + epsilon))
+ size -= 0.5;
+ else if (size > (6.0 + epsilon))
+ size -= 0.2;
+ else if (size > (4.0 + epsilon))
+ size -= 0.1;
+ else if (size > (3.0 + epsilon))
+ size -= 0.05;
+ else if (size > (2.0 + epsilon))
+ size -= 0.02;
+ else if (size > (1.01 + epsilon))
+ size -= 0.01;
+ else
+ size = 1.01;
+
+ this.size = size;
+ return this;
+}
+
+BetfairPrice.prototype.spreadToPrice = function(size) {
+ var other = new BetfairPrice(size);
+ if (Math.abs(this.size - other.size) < epsilon)
+ return 0;
+ var spread = 0;
+ if (other.size < this.size) {
+ // negative spread
+ while (other.size < (this.size - epsilon)) {
+ other.increasePrice();
+ --spread;
+ }
+ } else {
+ // positive spread
+ while (other.size > (this.size + epsilon)) {
+ other.decreasePrice();
+ ++spread;
+ }
+ }
+ return spread;
+}
@@ -0,0 +1,60 @@
+//(C) 2012 Anton Zemlyanov
+
+//This module describes Betfair session
+//see Sports API documentation on http://bdp.betfair.com
+
+// no login/logout in the new API yet
+var v6 = require('betfair-sports-api');
+
+exports.newSession = newSession;
+function newSession() {
+ return new BetfairSession();
+}
+
+function BetfairSession() {
+ var self = this;
+}
+
+// expose setCurrentExchange via session
+//BetfairSession.prototype.setCurrentExchange = exchangeService.setCurrentExchange;
+
+// Open current session
+BetfairSession.prototype.open = function(login, password, appKey, cb) {
+ var self = this;
+
+ self.login = login;
+ self.password = password;
+ self.applicationKey = appKey;
+ cb = cb || function() {};
+
+ self.v6Session = v6.newSession(login, password);
+ self.v6Session.open(function(err,v6res) {
+ if (err) {
+ cb(err, v6res);
+ return;
+ }
+ if (v6res.result.errorCode !== "OK") {
+ cb(inv.result.errorCode, inv);
+ return;
+ }
+ self.sessionToken = v6res.result.header.sessionToken;
+ cb(null, v6res);
+ });
+}
+
+// Close current session
+BetfairSession.prototype.close = function(cb) {
+ var self = this;
+
+ cb = cb || function() {};
+
+ self.v6Session.close(function(err, v6res) {
+ if (err) {
+ cb(err, v6res);
+ return;
+ }
+ self.sessionKey = undefined;
+ cb(null, v6res);
+ });
+}
+
View
@@ -0,0 +1,46 @@
+// This module contains functions shared by multiple tests
+var util = require('util');
+
+// session to use for all the invocations, should be set by test
+exports.session = null;
+exports.loginName = null;
+exports.password = null;
+exports.applicationKey = null;
+
+// login to Betfair
+exports.login = function(par, cb) {
+ if(!cb)
+ // cb is first parameter
+ cb = par;
+
+ console.log('===== Logging in to Betfair =====');
+ var session = exports.session;
+ session.open(exports.loginName, exports.password, exports.applocationKey, function(err, res) {
+ if (err) {
+ console.log('Login error', err);
+ } else {
+ console.log('Login OK');
+ }
+ exports.loginCookie = res.responseCookie;
+ cb(err);
+ });
+}
+
+// logout from Betfair
+exports.logout = function(par, cb) {
+ if(!cb)
+ // cb is first parameter
+ cb = par;
+
+ console.log('===== Logging out... =====');
+ var session = exports.session;
+ session.close(function(err, res) {
+ if (err) {
+ console.log('Logout error', err);
+ } else {
+ console.log('Logout OK');
+ }
+ cb(err);
+ });
+}
+
View
@@ -0,0 +1,16 @@
+// Betfair account data
+var betfair = require("../index.js");
+var async = require('async');
+var common = require('./common.js');
+
+// Create session to Betfair
+var session = betfair.newSession();
+common.session = session;
+common.loginName = process.env['BF_LOGIN'] || "nobody";
+common.password = process.env['BF_PASSWORD'] || "password";
+common.applicationKey = process.env['BF_APPLICATION_KEY']|| "invalid";
+
+async.waterfall([common.login, common.logout], function(err,res) {
+ console.log("Done");
+ process.exit(0);
+});
Oops, something went wrong.

0 comments on commit c0d4c1a

Please sign in to comment.