Issues listening for live Trades
Issues listening for live Trades
13 Mar 2025, 21:35
I'm trying to setup a web hook to access all live trades placed on my account, but ProtoOASubscribeSpotsReq always returns the same following:
📊 Tick Update: CTraderLayerEvent {}
📊 Tick Update: CTraderLayerEvent {}
Any suggestions?
const { CTraderConnection } = require("@reiryoku/ctrader-layer");
const CLIENT_SECRET = "******";
const CLIENT_ID = "******";
const ACCESS_TOKEN = "******"; // Replace with a valid one
const accountId = ******;
(async () => {
try {
const connection = new CTraderConnection({
host: "demo.ctraderapi.com",
port: 5035,
});
await connection.open();
console.log("✅ Connected to cTrader API");
// connection.on("ProtoOAAccountAuthRes", async (response) => {
// console.log("✅ Trading Account Authenticated:", response);
// // Now send the symbols request
// await connection.sendCommand("ProtoOASymbolsListReq", {
// ctidTraderAccountId: accountId
// });
// });
// Step 1: Application Authentication
await connection.sendCommand("ProtoOAApplicationAuthReq", {
clientId: CLIENT_ID,
clientSecret: CLIENT_SECRET,
});
console.log("🔑 Application Authenticated");
const accountsResponse = await connection.sendCommand("ProtoOAGetAccountListByAccessTokenReq", {
accessToken: ACCESS_TOKEN
});
console.log("Accounts Response:", accountsResponse);
await connection.sendCommand("ProtoOAAccountAuthReq", {
ctidTraderAccountId: accountId,
accessToken: ACCESS_TOKEN // Ensure this is the correct access token
});
// Step 2: Get Account List
const accounts = await connection.sendCommand("ProtoOAGetAccountListByAccessTokenReq", {
accessToken: ACCESS_TOKEN,
});
const accountInfo = await connection.sendCommand("ProtoOATraderReq", {
ctidTraderAccountId: accountId
});
console.log("👤 Trader Info:", JSON.stringify(accountInfo, null, 2));
await connection.sendCommand("ProtoOASubscribeSpotsReq", {
ctidTraderAccountId: accountId,
symbolId: 1 // Subscribe to updates for this symbol
}).then(() => {
console.log("✅ Successfully Subscribed to Spot Updates!");
}).catch((error) => {
console.error("🚨 Subscription Error:", error);
});
connection.on("ProtoOASpotEvent", (response) => {
console.log("📊 Tick Update:", response);
});
connection.on("message", (event) => {
console.log("📩 Incoming Event:", JSON.stringify(event, null, 2));
});
connection.on("ProtoOAErrorRes", (error) => {
console.error("🚨 API Error:", JSON.stringify(error, null, 2));
});
await connection.sendCommand("ProtoOASubscribeLiveTrendbarReq", {
ctidTraderAccountId: accountId,
symbolId: 7,
period: 1 // M1 timeframe
}).then(() => {
console.log("✅ Subscribed to Trendbar Updates!");
}).catch((error) => {
console.error("🚨 Subscription Error:", error);
});
connection.on("ProtoOASubscribeSpotsReq", (response) => {
console.log("📊 Live Trendbar Update:", response);
});
console.log("✅ Your cTrader Accounts:", accounts);
} catch (error) {
console.error("🚨 Error:", error);
}
})();