/node-trading212

NodeJS Trading212 Unofficial API

Primary LanguageJavaScript

About

This module aims to utilize and expose Trading212's RESTFul API and WebSocket APIs to allow users to easily interact with the API to create for instance a custom UI, automated trading, Discord bot, or what ever

Table of Contents

  1. About
  2. Example UI App
  3. Disclaimer
  4. Installing
  5. Usage/Features
  6. Missing features
  7. Discord
  8. Donate
  9. Trading212 referral

Uiapp

For an example app which uses this API, check out my other project Trading212 UI UI

Disclaimer

This code is work in progress. I am in no way affiliated with trading212 and trading212 does not endorse this project.
Use this code at your own risk. I afford no guarantee that the code is stable or error free.

Installing

You must first have NodeJS and NPM installed

npm install trading212

alternatively

download the project source from this repo and then create a trading212 folder within your node_modules folder

Features

Setup

//Setup
//Get these values from your browser cookie at live.trading212.com (F12 -> Application -> Storage -> Cookies)
const CUSTOMER_SESSION = "ab01-cde3-f45g-6789-hijkl1337";
const TRADING212_SESSION_LIVE = "ab01-cde3-f45g-6789-hijkl1337";

//Create handle and client
const trading212Handler = require('trading212');
const trading212 = new trading212Handler('live', CUSTOMER_SESSION, TRADING212_SESSION_LIVE);

Rest

Equitydata

Get all data (name, description, ticker, company history, etc) for equities available

let result = trading212.getAvailableEquities();

Restprices

Get Bid Ask Prices

let result = trading212.getCurrentPrice(["GME_US_EQ", "AMC_US_EQ"]);

//REST api

//Get current price
trading212.getCurrentPrice(["GME_US_EQ"]);
//Listen for response
trading212.on('price', (data) => {
  //data.ticker // GME_US_EQ
  //data.bid // Bid price
  //data.ask // Ask price
});

Restbuy

Place BUY order

//Place limit buy of 5 $GME @ $50
let ticker = "GME_US_EQ";
let orderType = "LIMIT";
let stopPrice = null;
let limitPrice = "50";
let quantity = 5;
let timeValidity = "DAY";
trading212.placeOrder(ticker, orderType, stopPrice, limitPrice, quantity, timeValidity);
//If order fails
trading212.on('order-failture', (data) => {
  //Axios error
});
//If order succeeds
trading212.on('account', (data) => {
  //data.orders
  //data.positions
  //console.log(data);
});

Restsell

Place SELL order


//Place limit sell of 5 $GME @ $50
let ticker = "GME_US_EQ";
let orderType = "LIMIT";
let stopPrice = null;
let limitPrice = "50";
let quantity = -5;
let timeValidity = "DAY";
trading212.placeOrder(ticker, orderType, stopPrice, limitPrice, quantity, timeValidity);
//If order fails
trading212.on('order-failture', (data) {
  //Axios error
});
//If order succeeds
trading212.on('account', (data) => {
  //data.orders
  //data.positions
  //console.log(data);
});

Deleteorder

Delete an order using the REST API

let order = account.orders[0];//Find order
let orderId = order.orderId;
trading212.deleteOrder(orderId);
//success
trading212.on('account', (data) => {
  //account data, containing positions, orders
  console.log(data);
})
trading212.on('order-failure', (err) {
  //error object
  //console.log(err);
})

Hotlist

Get rising (falling to come) stocks

let range = 'hourly';
//let range = 'daily';
let delta = 1;//1 hour, see https://www.trading212.com/en/hotlist
trading212.getHotlist(range, delta);
trading212.on('hotlist', (period, delta, data) => {
    //do stuff
});

Websocketapi

Websocketestablished

//WebSocket
trading212.on('platform-subscribed', () => {//Trading212 WebSocket established
  //Logic for when connection is established
});

Accountevents

Listen for account events. This data contains your available cash, invested cash, open positions, orders, etc

trading212.on('account', (data) => {
  console.log(data);
})
//Todo: document data structure

Pricesubscribe

Subscribe to one (or more) symbols

let subscribeSymbols = [
  "GME_US_EQ",
  "AMC_US_EQ",
]
trading212.bulkSubscribe(subscribeSymbols);//Subscribe to price feeds for this ticker

Priceevents

Listen for price events

 //Listen for price events from trading212 websocket (must subscribe to ticker by using #bulkSubscribe
trading212.on('price', (data) => {
  console.log(data.ticker);//Symbol/Ticker for equity
  console.log(data.ask);//Asking price
  console.log(data.bid);//Bidding price
});

websocketroutes

Subscribe to websocket routes

trading212.subscribeRoute('WEBPLATFORM');//This route is subscribed automatically when the websocket is opened. Gives data such as orders complete and watchlists
trading212.subscribeRoute('ACCOUNT');//This route is subscribed automatically when the websocket is opened. Gives data for the account such as available cash, invested, positions, orders. Listen with trading212.on('account', (data) => {})
//Document more routes

Missingfeatures

Missing features

  • Market orders
  • Charting
  • Caching
  • Better internal functions (get equity/price data from cache)
  • Much more

Discord

Join the discord to make suggestions, offer help or report bugs
https://discord.gg/sgr43Yv3ue

Donate

Donate Bitcoin to help this project bc1q03vpdg8e7xq7uh9myh7dx6atfx2me94v69rah6

Referral

If don't have a Trading212 account already, you can use my referral link and we'll both get a share