Skip to content

Messages

Summary

Provides access to messaging API.

Signature

1
public abstract interface Messages

Namespace

cAlgo.API

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
 using cAlgo.API;
 using System;
 namespace cAlgo.Robots;
 [Robot(AccessRights = AccessRights.None)]
 public class Sender : Robot
 {
     [Parameter(DefaultValue = "Test")]
     public string Subject {get; set;}
     public record Point(double X, double Y);
     protected override void OnStart()
     {
         Timer.Start(1);
     }
     protected override void OnTimer()
     {
         Messages.Send(Subject, new Point(Random.Shared.NextDouble(), Random.Shared.NextDouble()));
     }
 }
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
 using cAlgo.API;
 namespace cAlgo.Robots
 {
     [Robot(AccessRights = AccessRights.None, AddIndicators = true)]
     public class Receiver : Robot
     {
         [Parameter(DefaultValue = "Test")]
         public string Subject { get; set; }
         public record Point(double X, double Y);
         protected override void OnStart()
         {
             var subscription = Messages.Subscribe<Point>(Subject, OnMessageReceived);
         }
         private void OnMessageReceived(MessageArgs<Point> args)
         {
             Print($"Received Point: {args.Message}");
         }
     }
 }
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
 import clr
 clr.AddReference("cAlgo.API")
 # Import cAlgo API types
 from cAlgo.API import *
 # Import trading wrapper functions
 from robot_wrapper import *
 from System import String
 class Sender():
     def on_start(self):
         api.Timer.Start(1)
     def on_timer(self):
         api.Messages.Send[String]("Subject", f"Time is: {api.Server.Time}")
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
 import clr
 clr.AddReference("cAlgo.API")
 # Import cAlgo API types
 from cAlgo.API import *
 # Import trading wrapper functions
 from robot_wrapper import *
 from System import String
 from System import Action
 class Receiver():
     def on_start(self):
         self.subscription = api.Messages.Subscribe[String]("Subject", Action[MessageArgs[String]](self.on_message_received))
     def on_message_received(self, args):
         print(f"Received: {args.Message}")

Methods

Subscribe

Summary

Subscribes to a message subject.

Signature

1
public abstract MessageSubscription Subscribe(string subject, Action<MessageArgs<TMessage>> handler)

Parameters

Name Type Description
subject string Message subject.
handler Action> Message handler callback.

Return Value

MessageSubscription

Send

Summary

Sends a message for a specific subject.

Signature

1
public abstract void Send(string subject, TMessage message)

Parameters

Name Type Description
subject string Message subject.
message TMessage Message to be sent, it can be any type including string.

Return Value

void