Can't get custom events from one cbot to another working

Created at 13 Oct 2023, 05:40
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
BA

bazisfree

Joined 17.01.2022

Can't get custom events from one cbot to another working
13 Oct 2023, 05:40


Hi,

I have tried and tried, spent a week googling, AI asking etc but now I'm here.

My plan is to build a Management robot that is a subscriber to multiple Strategy robot's so it can be the master controller of which trades are taken and with what parameters (size, SL, PT, etc). 

I have the Strategy robots creating an object of a custom class that is similar to what cTrader has when using its orders. By this I mean I place all of the details into the object (Symbol, Trade Direction, Label, Size, SL, PT, Comment, custom..). The Management robot might ignore some of this but some information like symbol and label is absolutely necessary.

When the Strategy robot has determined a good entry and created this object I want to invoke an event so the management robot knows there is a strategy that wants to enter, and can then look at the object (reference variable being passed in the event) to see what is what.

But I cannot get the thing to work! I stripped it back to two bare robots with just the eventhandler to try get this working, but I cannot. I reference the Strategy robot in the Management robot either using VS 2022 or locating the dll file within cTrader (once I've built using VS otherwise I can't see any dll file). I'll post below the bare bones of what I'm trying to do.

Just before though…I did use pending orders event notification by actually making a pending order within the strategy robot and subscribing to all pending orders in Management robot but for several reasons I don't like this approach. Also, although I've never done it I know I can get the strategy robot to write to a file and have the Management robot constantly checking this file. But with multiple strategy robots I can imagine this getting messy and being rather inefficient. Please can someone help me with an example of how to use an event to get one cbot listening to multiple others?

//------- Strategy Robot (Sbot)
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.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class SbotSignal : Robot
    {
        [Parameter(DefaultValue = "I'm an Sbot")]
        public string Message { get; set; }

        public event EventHandler SignalEvent; 

        protected override void OnStart()
        {
            Print(Message);
        }

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

        protected override void OnBar()
        {
            Print ("OnBar event");
            SignalEvent?.Invoke(this, EventArgs.Empty);
        }

        protected override void OnStop()
        {
            // Handle cBot stop here
        }
    }
}
//----------- Management Robot
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;
using cAlgo;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class TestManager : Robot
    {

        protected override void OnStart()
        {
            SbotSignal sBotSignal = new(); 
            sBotSignal.SignalEvent += Sbot_Signal_Event; 
        }

        public void Sbot_Signal_Event(object sender, EventArgs e)
        {
            Print("Test Manager received event ");
            // Handle cBot stop here
        }

        protected override void OnTick()
        {
        }

        protected override void OnBar()
        {

        }
        
        protected override void OnStop()
        {
            // Handle cBot stop here
        }
    }
}

@bazisfree
Replies

PanagiotisChar
13 Oct 2023, 06:24

Hi,

You won't be able to do that this way since you do not have access to the cBot instance cTrader is running. Therefore you cannot listen to events of that instance. You need to consider other ways of communication e.g. reading/writing to files, named pipes etc.


@PanagiotisChar

bazisfree
13 Oct 2023, 07:06

RE: Can't get custom events from one cbot to another working

PanagiotisChar said: 

Hi,

You won't be able to do that this way since you do not have access to the cBot instance cTrader is running. Therefore you cannot listen to events of that instance. You need to consider other ways of communication e.g. reading/writing to files, named pipes etc.

Thank you. Do you have any examples of using pipes for this or something similar? Also, should I make these asynchronous or not necessary?

Cheers.

Baz


@bazisfree

bazisfree
14 Oct 2023, 00:57

A possible solution...

Although I must admit I don't know what I'm really talking about much yet…

I will use a Nuget package called MessagePack to serialize the data and then use a Memory Mapped Writer / Reader to send and receive data between two cbots.
 

A big thanks to Spotware and Scyware.com to pointing me in the right direction and providing me with some code to adapt and use.


@bazisfree

PanagiotisChar
15 Oct 2023, 04:05

RE: RE: Can't get custom events from one cbot to another working

bazisfree said: 

PanagiotisChar said: 

Hi,

You won't be able to do that this way since you do not have access to the cBot instance cTrader is running. Therefore you cannot listen to events of that instance. You need to consider other ways of communication e.g. reading/writing to files, named pipes etc.

Thank you. Do you have any examples of using pipes for this or something similar? Also, should I make these asynchronous or not necessary?

Cheers.

Baz

Unfortunately no


@PanagiotisChar