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

@fugle/trade

v2.1.1

Published

FugleTrade API SDK for Node.js

Downloads

19

Readme

Fugle Trade

NPM version Build Status Coverage Status

FugleTrade API SDK for Node.js

Installation

$ npm install --save @fugle/trade

Importing

// Using Node.js `require()`
const { FugleTrade, Order } = require('@fugle/trade');

// Using ES6 imports
import { FugleTrade, Order } from '@fugle/trade';

Quick Start

import { FugleTrade, Order } from '@fugle/trade';

const fugle = new FugleTrade({ configPath: '/path/to/config.ini' });

fugle.login().then(() => {
  fugle.streamer.connect();

  fugle.streamer.on('connect', () => {
    console.log('streamer connected');

    const order = new Order({
      buySell: Order.Side.Buy,
      price: 25.00,
      stockNo: '2884',
      quantity: 1,
      apCode: Order.ApCode.Common,
      priceFlag: Order.PriceFlag.Limit,
      bsFlag: Order.BsFlag.ROD,
      trade: Order.Trade.Cash,
    });

    // place order
    fugle.placeOrder(order);
  });

  fugle.streamer.on('disconnect', () => {
    console.log('streamer disconnected');
  });

  fugle.streamer.on('order', (data) => {
    console.log('order confirmation:');
    console.log(data);
  });

  fugle.streamer.on('trade', (data) => {
    console.log('execution report:');
    console.log(data);
  });
});

Usage

Make sure you have applied to use Fugle trading API, downloaded the configuration file and certificate to your local machine.

Creating a client

First, create a FugleTrade client with configPath option:

const fugle = new FugleTrade({ configPath: '/path/to/config.ini' });

Logining to server

After creating the client, you need to log in to the remote server to obtain a valid api token:

await fugle.login();

When you log in for the first time, you need to set your account password and certificate password:

? Enter esun account password ********
? Enter cert password ********

Once logged in, you can start placing orders and viewing account information:

// Retrieve existing orders
const orders = await fugle.getOrders();

// Retrieve transaction details within three months
const transactions = await fugle.getTransactions({ duration: '3m' });

// Retrieve all inventories in your account
const inventories = await fugle.getInventories();

// Retrieve incoming settlements
const settlements = await fugle.getSettlements();

// Retrieve bank account balance
const balance = await fugle.getBalance();

// Retrieve trading quota and margin transaction information
const tradeStatus = await fugle.getTradeStatus();

// Retrieve market open status
const marketStatus = await fugle.getMarketStatus();

When you want to log in with another account, or need to reset your certificate password, please log out first:

await fugle.logout();

Placing an order

In order to place an order, you need to import Order from @fugle/trade.

import { Order } from '@fugle/trade';

Then, create an Order instance that represents the order to place:

const order = new Order({
  buySell: Order.Side.Buy,
  price: 25.00,
  stockNo: '2884',
  quantity: 3,
  apCode: Order.ApCode.Common,
  priceFlag: Order.PriceFlag.Limit,
  bsFlag: Order.BsFlag.ROD,
  trade: Order.Trade.Cash,
});

// place the order
await fugle.placeOrder(order);

See /doc/fugle-trade.md for details on available options.

Replacing or canceling the order

If you want to replace or cancel an order, you need to get your all existing orders first.

const orders = await fugle.getOrders();

After that, you can choose one of the orders to replace or cancel it.

const [ order, ...others ] = orders;

// Replace the order to change price
await fugle.replacePrice(order, 24.5);

// Replace the order to change quantity
await fugle.replaceQuantity(order, 1);

// Cancel the order
await fugle.cancelOrder(order);

Connecting to streamer

Streamer is an application that serves streaming data to clients of Fugle. If you want to receive order confirmation or execution report in real time, you need to connect the streamer.

Make sure you are logged in, then connect to the streamer:

fugle.streamer.connect();

The fugle.streamer inherits from EventEmitter that lets you register event listeners to listen for events:

fugle.streamer.on('connect', () => {
  // streamer connected
});

fugle.streamer.on('disconnect', () => {
  // streamer disconnected
});

fugle.streamer.on('order', (data) => {
  // receive order confirmation
});

fugle.streamer.on('trade', (data) => {
  // receive execution report
});

fugle.streamer.on('message', (data) => {
  // receive message from streamer
});

fugle.streamer.on('error', (err) => {
  // handle error
});

If you want to disconnect the streamer, just inovke:

fugle.streamer.disconnect();

Documentation

See /doc/fugle-trade.md for Node.js-like documentation of @fugle/trade classes.

License

MIT