Skip to content

WebSocket Sample

Overview

The WebSocket Sample plugin enables real-time data integration in cTrader through the following key functionalities:

  • Connects cTrader to an external WebSocket server to receive real-time data streams.
  • Displays live content such as news or trading alerts directly in Active Symbol Panel using a custom text block.
  • Automatically updates the panel with new messages when received, enabling timely information.

The plugin operates through a user-configurable panel. It remains active while cTrader Windows or Mac is running and automatically releases system resources when stopped.

Plugin creation

Learn how to create, edit and build plugins from a template or from scratch in our step-by-step guide.

You can find the code of the WebSocket Sample plugin on GitHub, or simply copy it below.

Sample code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Plugins
{
    [Plugin(AccessRights = AccessRights.None)]
    public class WebSocketSample : Plugin
    {
        // Declaring our TextBlock that will display the news contents
        private TextBlock _textBlock = new TextBlock
        {
            Text = "Starting news feed...",
            FontSize = 20,
            FontWeight = FontWeight.ExtraBold,
            TextAlignment = TextAlignment.Center,
            Padding = new Thickness(5, 5, 5, 5),
        };

        // _webSocketClientOptions allow us to define several settings
        // such as the keep-alive interval of the WebSocket connection
        private static WebSocketClientOptions _webSocketClientOptions = new WebSocketClientOptions 
        {
            KeepAliveInterval = new TimeSpan(0, 1, 30),
            UseDefaultCredentials = true,
        };

        // Passing our _webSocketClientOptions to the WebSocketClient
        // constructor
        private WebSocketClient _webSocketClient = new WebSocketClient(_webSocketClientOptions);

        // This API is entirely fictional
        private readonly Uri _targetUri = new Uri("ws://amazingnews.com:8000");

        protected override void OnStart()
        {
            // Connecting to the API and sending the initial message
            _webSocketClient.Connect(_targetUri);
            _webSocketClient.Send("Hello");

            // Declaring a custom handler for the TextReceived event
            _webSocketClient.TextReceived += NewsReceived;

            // Adding our TextBlock as a child of a custom
            // AspBlock
            var aspBlock = Asp.SymbolTab.AddBlock("News");
            aspBlock.Height = 300;
            aspBlock.Child = _textBlock;
        }

        protected override void OnStop()
        {
            // The WebSocketClient must be disposed of in OnStop,
            // otherwise it will consume system resources
            _webSocketClient.Close(WebSocketClientCloseStatus.NormalClosure);
        }

        private void NewsReceived(WebSocketClientTextReceivedEventArgs args) 
        {
            // Updading the text inside the TextBlock on every
            // piece of news received
            if (args.Text.Length != 0) 
            {
                _textBlock.Text = args.Text;
            }
        }
    }        
}

Customisation options

The plugin combines WebSocket settings with UI elements to deliver live text content. Below is a breakdown of its key components and their functions:

Parameter Description Possible values
Text Displays the initial content in the text block. Starting news feed…
AddBlock Adds a block to Active Symbol Panel. asp.symboltab.addblock(news)
_textBlock.Text Updates the text block with the received WebSocket message. args.text
FontSize Sets the size of the text. 16, 18, 20, etc.
FontWeight Defines the thickness of the text. regular, bold, extrabold, etc.
TextAlignment Aligns the text within the text block. left, centre, etc.
Padding Specifies the space around the text block. (5, 5, 5, 5), (8, 8, 8, 8), etc.
aspBlock.Height Sets the height of the custom block in the user interface. 300px, 350px, etc.
_targetUri Provides the WebSocket endpoint for the data feed. wss://marketdata.tradermade.com/feedadv
KeepAliveInterval Defines the interval for WebSocket keep-alive pings. (0, 1, 30) (hours, minutes, seconds)
_webSocketClient.Send Sends the initial subscription message to the WebSocket server. {userkey:..., symbol:eurusd}
UseDefaultCredentials Configures the authentication setting for the WebSocket connection. true or false

Note

Ensure the WebSocket server supports the protocol and message formats expected by your implementation.

Use cases

WebSocket Sample provides a powerful and flexible way to extend cTrader with real-time data feeds. It supports a wide range of integrations tailored to various trading needs. Below are practical use cases that demonstrate how the plugin can enhance the trading experience.

Use case Scenario Value
Newswire feeds View real-time economic news, market-moving headlines and breaking events directly in the platform. Keeps you informed of breaking news that may impact market movements.
Crypto exchange data Stream live trade updates, price tickers, order book changes or alerts on volatility for crypto assets. Enhances real-time awareness of crypto market conditions and sudden price moves.
Volatility and macro metrics Display real-time volatility levels or unusual activity indicators for target instruments in Active Symbol Panel. Supports macro-level awareness and helps identify trading opportunities during market extremes.
Quant model output viewer View dynamic model output (such as momentum increase on EURUSD). Connects quantitative analysis with trading decisions in a visually accessible way.
External trading signal streams Stream automated signals from external services machine learning (ML) models routed through WebSocket. Enables immediate signal delivery for users following algorithmic strategies or third-party bots.

Summary

This plugin sample shows how to integrate real-time external content into cTrader using a WebSocket connection. While the sample demonstrates a live news feed in Active Symbol Panel, the same approach supports signals, alerts, analytics and more.

For further development details, refer to our plugin documentation.