2017 © Pedro Peláez
 

library stockfighter

A Stockfighter API Wrapper.

image

sammarks/stockfighter

A Stockfighter API Wrapper.

  • Sunday, January 24, 2016
  • by sammarks
  • Repository
  • 1 Watchers
  • 0 Stars
  • 19 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Stockfighter PHP API Wrapper

I use this library inside my solutions repository, and thought I'd share it with the world. My solutions repository can be found here (spoiler alert)., (*1)

Installation

Just install this library using Composer., (*2)

composer install sammarks/stockfighter

Or you can require it into your project using:, (*3)

composer require sammarks/stockfighter

Note: You'll also need to specify the repository settings as this package relies on my fork of phpws. Add the following to your composer.json:, (*4)

"repositories": [
    {
        "type": "vcs",
        "url": "https://github.com/sammarks/phpws"
    }
],

Additionally, because the library relies on a fork like this, you'll need to make sure the minimum stability of your project is set to "dev:", (*5)

"minimum-stability": "dev"

Usage

This library is setup so that it follows the URL structure of the Stockfighter API documentation very closely. With that said, here's an example of the library usage:, (*6)

use \Marks\Stockfighter\Stockfighter;

// Set the API key.
Stockfighter::setApiKey('apikey');

// Create an instance of the API.
$stockfighter = new Stockfighter();

// Check if the API is working.
$api_working = $stockfighter->heartbeat();

// Check if a venue exists and is working.
$test_working = $stockfighter->venue('test')->heartbeat();

// Get all stocks in a venue.
$stocks = $stockfighter->venue('test')->stocks();

// Get information about a stock.
$stock_info = $stockfighter->venue('test')->stock('ABCD')->info();

// Order some ABCD stock.
$order = $stockfighter->venue('test')->stock('ABCD')->order($account, $price, $quantity, $direction, $order_type);
// Direction and Order Type have constants in the Order class, like Order::DIRECTION_BUY,
// Order::DIRECTION_SELL, Order::TYPE_MARKET, etc.

Web Sockets

You can also connect and listen for quotes using WebSockets. Here's an example of that:, (*7)

// Create a websocket instance.
$websocket = $this->stockfighter->getWebSocketCommunicator()->quotes($account, $venue, $stock);

// Set the receive callback.
$websocket->receive(function (Quote $quote) {
    // Do stuff with the quote...
    // Once you're done with the websocket connection, return true
    // from this method and the connection will be closed.
});

// Open the connection.
$websocket->connect();

Asynchronous Calling

This library uses Guzzle for its HTTP requests, which uses PSR-7 promises. Naturally, I have included support for promises in this library. Here's an example of how to place an order using asynchronous calls:, (*8)

// Assuming you already have a stockfighter instance...
// Here's an example that places an order asynchronously.
$stockfighter->venue('test')->stock('ABCD')->orderAsync($account, $price, $quantity, $direction,
    $order_type)->then(function (Order $order) {
        echo "Oh boy, the order finished! " . $order->totalFulfilled;   
    }, function (StockfighterRequestException $e) {
        echo "Oh no, there was an error with the order! " . $e->getMessage();   
    });

Event Loop

Important Note: If you're using either the websockets or the asynchronous calling, you'll need to initialize the ReactPHP event loop. Usually this is done as the last call of your application (as it is a blocking method). Do all of your initialization and processing logic, and then call the following right before the end of your application:, (*9)

// Start the Event Loop.
$stockfighter->run();

Contributing

If you find an error in my Stockfighter library, or would like to improve it because you're using it in your own Stockfighter solutions, just send me a pull request! I promise I'll be very open to suggestions., (*10)

The Versions

24/01 2016

dev-node-websocket

dev-node-websocket https://github.com/sammarks/stockfighter-php

A Stockfighter API Wrapper.

  Sources   Download

MIT

The Requires

 

by Sam Marks

24/12 2015