A Node.js communication layer for cTrader Open API
npm install @reiryoku/ctrader-layerA Node.js communication layer for cTrader Open API.
This implementation is created and maintained by Reiryoku Technologies and its contributors.
console
npm install @reiryoku/ctrader-layer
`Usage
For the cTrader Open API usage refer to the Open API Documentation.$3
`javascript
const { CTraderConnection } = require("@reiryoku/ctrader-layer");const connection = new CTraderConnection({
host: "demo.ctraderapi.com",
port: 5035,
});
await connection.open();
`$3
You can use the sendCommand method to send a command with payload to the server.
The method returns a Promise resolved only when a response from the server is received.
If the response to the command contains an error code then the returned Promise is rejected.`javascript
await connection.sendCommand("PayloadName", {
foo: "bar",
});
`$3
`javascript
await connection.sendCommand("ProtoOAApplicationAuthReq", {
clientId: "foo",
clientSecret: "bar",
});
`How to authenticate a trading account
You can get the access token to use your account from Open API Applications.
First, you have to authenticate the application, then you can authenticate your trading accounts as follows.
`javascript
await connection.sendCommand("ProtoOAAccountAuthReq", {
accessToken: "foo",
ctidTraderAccountId: "bar",
});
`$3
You can send a heartbeat message every 25 seconds to keep the connection alive.
`javascript
setInterval(() => connection.sendHeartbeat(), 25000);
`$3
`javascript
connection.on("EventName", (event) => {
console.log(event);
});
`$3
Through HTTP request.
`javascript
console.log(await CTraderConnection.getAccessTokenProfile("access-token"));
`$3
Through HTTP request.
`javascript
console.log(await CTraderConnection.getAccessTokenAccounts("access-token"));
``