npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

trading_bot

v2.1.0

Published

Trading bot super-class for GDAX

Downloads

19

Readme

Trading bot helper for GDAX exchanges

Note: Version 2.x is generally far easier to use and is much more up to date, but breaks a lot of compatibility with version 1.x.

This package provides a base from which to easily write automated trading bots on GDAX's exchanges. The bots can be run on the Sandbox (with play money, for testing) and with real money, using your API keys.

Installation

npm install --save trading_bot

Usage

Extend the base Trading Bot class and implement the _execute_trading_strategy method. REST data is available to the method in the object instance (i.e. the this variable). Each variable has the following form

trade_data : {
  updated : {Date},
  data    : {Object | Array | Primitive }
  errors  : {Array of Error objects}
}

The updated field is a timestamp indicating when the data in the data field was obtained. The errors field, if present, lists any errors coming from the API (e.g. connection lost)

The following trade data are available as properties:

  • ticker - the ticker object (bid, ask, price, volume) for the selected product
  • myorders - Your orders (all orders are included)

These properties are not automatically updated. Call refresh_indicators to reload the latest values. You can also call fetch_ticker or fetch_myorders to reload the respective data with promises

In addition, a connection to the GDAX websocket feed is made to provide realtime updates of the following properties:

  • last_price - The last traded price of the exchange
  • midmarket_price - The current midpoint between best bid and ask
  • orderbook - The full level 3 orderbook, updated in realtime, or if more fine-grained control is wanted:
  • synced_book - The underlyign SyncedOrderbook instance

The constructor for the base class takes the following options:

  • product - The order book to use. Defaults to 'BTC-USD'.
  • log - An optional logging utility. Defaults to console.log
  • auth - An object containing your API key credentials. See the example below.
  • client - Instead of auth, a valid AuthenticatedClient instance can be provided instead.
  • webSocketURI - Optional. If provided, creates a connection to the given WS server.
  • websocketClient - If provided, is available as this.ws_client

The following utility methods are also available

  • start_trading(options) - Start executing the trading strategy. The options parameter may contain the following:
    • interval: the frequency (in ms) with which to try and execute the strategy. If the result of the last execution has not been received yet, the strategy will not fire.
  • stop_trading(options) - Pause the strategy execution. If options.cancel is true, all existing orders will be cancelled
  • cancel_order(id) - place a cancel order instruction on the exchange for order with id
  • cancel_all_orders() - place a "cancel all orders" instruction on the exchange
  • refresh_indicators() - place requests to update all the trade data. This call should be made manually when you want the latest set of trade data. The function returns immediately, and the data will be updated when the data arrives. As of this version of the code, no notifications are given when this happens

Writing the strategy method

The _execute_trading_strategy method must return a Promise that resolves when the trade is "done". You can decide what this means, but the method will not fire again until the promise has resolved.

The client property of the super class is an AuthenticatedClient instance of the GDAX exchange API for node.js which you can use to make trades, or do whatever you want. You might also find the API docs useful.

Example

Here is a simple example of a very loss-making bot that executes random trades on the exchange. This is an example. Do not run this bot as it is, since it will lose a lot of money very quickly!

'use strict';
const TradingBot = require('trading_bot');

class RandomBot extends TradingBot {
    constructor() {
        let auth = {
            key: xxx,
            secret: xxxxxxxx,
            passphrase: xxxxxxx,
            apiURI: 'https://api-public.sandbox.exchange.gdax.com'
        };
        super({ auth: auth });
    }

    _execute_trading_strategy() {
        return new Promise((resolve, reject) => {
            const self = this;
            let side = (self.trades % 2 === 0) ? 'buy' : 'sell';
            let size = 0.1 + 0.9 * Math.random();
            size = size.toFixed(2);
            var order = {
                product_id: self.product,
                type: 'market',
                size: size
            };
            self.client[side](order, (err, res, body) => {
                if (err) {
                    console.log('Errror placing trade');
                    return reject(err);
                }
                console.log(body);
                resolve();
            });
        });
    }
}

const bot = new RandomBot();
console.log('RandomBot is starting to lose money again');
bot.start_trading({ interval: 1000 });

Disclaimer

You could lose money by using this software! Do not trade with this bot if you don't understand the risks or are unwilling to potentially suffer significant financial loss. Neither the Author of this code, nor Coinbase Inc. will be liable for any or all losses sustained through the direct or indirect use of this software or derivative works.

THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.

IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.