Event based Robot Control

Created at 16 Nov 2013, 21:54
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!
jhtrader's avatar

jhtrader

Joined 15.10.2013 Blocked

Event based Robot Control
16 Nov 2013, 21:54


I have built an example that passes an event to a parent robot..

I would like to get some feedback.

using System;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot()]
    public class ParentPortfolioRobot : Robot
    {
        protected string symbolCode;
        public int Volu { get; set; }

        protected string SymbolCode
        {
            get { return Symbol.Code; }
            set { symbolCode = value; }
        }

        protected override void OnStart()
        {
            var symbol = MarketData.GetSymbol(SymbolCode);
            Trade.CreateBuyMarketOrder(symbol, Volu);
        }

        protected override void OnTick()
        {
            foreach (var pos in Account.Positions)
                Print("{0}", pos.Id);
        }

        public void isFirstBar()
        {
            Print("Its 10am");
        }


    }
}


//*************************************************************************************************


//#reference: ParentRobot.algo

//The Event robot sends events to the Parent robot

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC)]
    public class EventRobot : ParentPortfolioRobot
    {


        protected override void OnStart()
        {
            //Initialise the variables of the portfolio class
            Volu = 1000;
            SymbolCode = Symbol.Code;
            base.OnStart();
        }

        protected override void OnBar()
        {

            int index = MarketSeries.OpenTime.Count - 1;
            DateTime currentTime = MarketSeries.OpenTime[index];
            DateTime previousTime = MarketSeries.OpenTime[index - 1];

            //if first bar of the week
            if (currentTime.DayOfWeek == DayOfWeek.Monday && previousTime.DayOfWeek != DayOfWeek.Monday)
                sendEvent();

            base.OnTick();

        }

        public void sendEvent()
        {
            base.isFirstBar();

        }

    }
}

 


Replies

jeex
21 Nov 2013, 12:02

Very interesting

I like this project. this way it's possible to combine several semi-automatic strategies.

Can an indicator induce such a signal as well? Time for some serious testing.


@jeex

hichem
21 Nov 2013, 12:40

RE:

Hi!

I'm not sure what are you trying to achieve but deriving from a Base robot will not create two robots. The derived robot will inherit from the parent robot and you will have only one instance of a robot running. I made the comments because you mentioned passing events between robots. Which is not the case here. Calling base.isFirstBar(); is nothing else but calling a method in the EventRobot instance.

jhtrader said:

I have built an example that passes an event to a parent robot..

I would like to get some feedback.

using System;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot()]
    public class ParentPortfolioRobot : Robot
    {
        protected string symbolCode;
        public int Volu { get; set; }

        protected string SymbolCode
        {
            get { return Symbol.Code; }
            set { symbolCode = value; }
        }

        protected override void OnStart()
        {
            var symbol = MarketData.GetSymbol(SymbolCode);
            Trade.CreateBuyMarketOrder(symbol, Volu);
        }

        protected override void OnTick()
        {
            foreach (var pos in Account.Positions)
                Print("{0}", pos.Id);
        }

        public void isFirstBar()
        {
            Print("Its 10am");
        }


    }
}


//*************************************************************************************************


//#reference: ParentRobot.algo

//The Event robot sends events to the Parent robot

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC)]
    public class EventRobot : ParentPortfolioRobot
    {


        protected override void OnStart()
        {
            //Initialise the variables of the portfolio class
            Volu = 1000;
            SymbolCode = Symbol.Code;
            base.OnStart();
        }

        protected override void OnBar()
        {

            int index = MarketSeries.OpenTime.Count - 1;
            DateTime currentTime = MarketSeries.OpenTime[index];
            DateTime previousTime = MarketSeries.OpenTime[index - 1];

            //if first bar of the week
            if (currentTime.DayOfWeek == DayOfWeek.Monday && previousTime.DayOfWeek != DayOfWeek.Monday)
                sendEvent();

            base.OnTick();

        }

        public void sendEvent()
        {
            base.isFirstBar();

        }

    }
}

 

 


@hichem

Hyperloop
22 Nov 2013, 00:20

I think your best bet right now is just wait for the release of Spotware Connect. That should allow more flexbility.


@Hyperloop

jeex
22 Nov 2013, 13:11

Howto link one bot to another

A simple question on this subject: how do i link one robot to another, like we do with an indicator and robt?

I want to use one robot as library, so that the methods in that bot can be used in other robots.
 


@jeex

hichem
22 Nov 2013, 13:25

RE: Howto link one bot to another

If you want to develop a library then it doesn't need to be implemented  in a Robot. You develop a .dll with VS and import in your robot. A robot is not designed to act as a library of functions

jeex said:

A simple question on this subject: how do i link one robot to another, like we do with an indicator and robt?

I want to use one robot as library, so that the methods in that bot can be used in other robots.
 

 


@hichem

jeex
22 Nov 2013, 13:30

DLL?

And can that be done within cAlgo. I'm an OSX developer so i am already on thin ice here with cAlgo in a virtual Windows on my MacPro.
 


@jeex

hichem
22 Nov 2013, 15:29

RE: DLL?

In that case you can create static methods within a Robot class to be reused. You should declare those methods as static to avoid instantiating a Robot object inside another robot. After compiling the first robot add it as a reference to the second one.

But using VS is the cleanest and the best way to do so.

jeex said:

And can that be done within cAlgo. I'm an OSX developer so i am already on thin ice here with cAlgo in a virtual Windows on my MacPro.
 

 


@hichem