Category Other  Published on 26/08/2024

WebSockets Example

Description

Example used in


using System;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None, AddIndicators = true)]
    public class WebSocketsExample : Robot
    {
    
        private WebSocketClient _webSocketClient = new WebSocketClient();
        private readonly Uri _targetUri = new Uri("wss://marketdata.tradermade.com/feedadv");

        protected override void OnStart()
        {
            _webSocketClient.Connect(_targetUri);

            _webSocketClient.TextReceived += _webSocketClient_TextReceived;
            
            var data = "{\"userKey\":\"wsI4foSciCjMyCuoc2xw\", \"symbol\":\"EURUSD\"}";
            
            _webSocketClient.Send(data);

        }

        private void _webSocketClient_TextReceived(WebSocketClientTextReceivedEventArgs obj)
        {
            Print(obj.Text.Replace("{","").Replace("}","").ToString());
        }

        protected override void OnTick()
        {
            // Handle price updates here
        }

        protected override void OnStop()
        {
            _webSocketClient.Close(WebSocketClientCloseStatus.NormalClosure);
        }
    }
}

Spotware's avatar
Spotware

Joined on 23.09.2013

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: Web Sockets Example_withSourceCode.algo
  • Rating: 5
  • Installs: 263
  • Modified: 26/08/2024 12:36
Comments
Log in to add a comment.
KA
kaxalope · 3 months ago

Why can you connect to Websocket with AccessRights.None?