stockfighter-client

1.0.5 • Public • Published

stockfighter-client

Instructions

> npm install stockfighter-client

Then, in your code:

var sf = require('stockfighter-client')(apiKey);

Supported methods

Unless otherwise stated, all API methods return a Promise which resolves to an object.

  • sf.heartbeat() => Promise
  • sf.heartbeat(venue) => Promise
  • sf.stocks(venue) => Promise
  • sf.orderbook(venue, stock) => Promise
  • sf.quote(venue, stock) => Promise
  • sf.orders.create(params) => Promise
  • sf.orders.status(venue, stock, id) => Promise
  • sf.orders.all(venue, account) => Promise
  • sf.orders.stock(venue, account, stock) => Promise
  • sf.orders.cancel(venue, stock, id) => Promise
  • sf.orders.cancelPost(venue, stock, id) => Promise

Socket methods return an event emitter which emits a stream of 'message' events.

  • sf.sockets.tickertape(account, venue) => EventEmitter
  • sf.sockets.tickertape(account, venue, stock) => EventEmitter
  • sf.sockets.executions(account, venue) => EventEmitter
  • sf.sockets.executions(account, venue, stock) => EventEmitter

GameMaster (GM) API for starting/stopping levels

  • sf.gm.create(name) => Promise
  • sf.gm.status(id) => Promise
  • sf.gm.restart(id) => Promise
  • sf.gm.stop(id) => Promise
  • sf.gm.resume(id) => Promise

Calling the API from a Coroutine

'use strict';
var API_KEY = 'XXXXXXXXX(replace this value)XXXXXXXXXXX';
var sf = require('stockfighter-client')(API_KEY);
var Promise = require('bluebird');

// We like coroutines because it allows very clean control flow
// using the yield keyword.
Promise.coroutine(function*() {
	try {
		let result;
		// *** heartbeat
		result = yield sf.heartbeat();
		console.log(result);
		// *** orderbook
		result = yield sf.orderbook('TESTEX', 'FOOBAR');
		console.log(result);
		// *** place order
		result = yield sf.orders.create({
			account: 'ACCOUNT123',
			venue: 'TESTEX',
			stock: 'FOOBAR',
			qty: 100,
			direction: 'buy',
			orderType: 'market'
		});
		// *** capture 5 seconds of tickertape from WebSockets API
		let tickertape = sf.sockets.tickertape('ACCOUNT123', 'TESTEX');
		tickertape.on('message', (e) => console.log('tickertape:', e));
		setTimeout(() => tickertape.close(), 5000);
	}
	catch(err) {
		console.error(err.stack);
	}
})();

Using Promises

var API_KEY = 'XXXXXXXXX(replace this value)XXXXXXXXXXX';
var sf = require('stockfighter-client')(API_KEY);
// *** heartbeat
sf.heartbeat().then(function (res) {
	console.log('heartbeat:', res);
	// *** stock quote
	return sf.quote('TESTEX', 'FOOBAR');
})
.then(function (res) {
	console.log('quote:', res);
	// *** create order
	return sf.orders.create({
		account: 'ACCOUNT123',
		venue: 'TESTEX',
		stock: 'FOOBAR',
		qty: 100,
		direction: 'buy',
		orderType: 'market'
	});
})
.then(function (order) {
	console.log('created:', order);
	// *** cancel order
	return sf.orders.cancel('TESTEX', 'FOOBAR', order.id);
});
// *** now, let's use the websocket api:
var tickertape = sf.sockets.tickertape('ACCOUNT123', 'TESTEX');
tickertape.on('message', function (message) {
	console.log('tickertape:', message);
});
var executions = sf.sockets.executions('ACCOUNT123', 'TESTEX');
exectutions.on('message', function (message) {
	console.log('executions:', message);
})
// *** shut down websockets after 5 seconds of activity
setTimeout(function () {
	tickertape.close();
	executions.close();
}, 5000);

Package Sidebar

Install

npm i stockfighter-client

Weekly Downloads

10

Version

1.0.5

License

ISC

Last publish

Collaborators

  • arthurmilliken