How use the properties of my indicator in my Cbot

Created at 21 Nov 2017, 11:56
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!
itmfar's avatar

itmfar

Joined 08.11.2017

How use the properties of my indicator in my Cbot
21 Nov 2017, 11:56


I have my own indicator, when signalBuy in this indicator is true , i want to execute order in my Cbot:

//indicator 
        bool signalBuy = false;

        public override void Calculate(int index)
        {

            if (1 + 1 == 2)
                signalBuy = true;
        }

 

//Cbot
        protected override void OnTick()
        {
            if(signalBuy == true)

            {

                    ExecuteMarketOrder(TradeType.Buy, Symbol, 20, "lalbelmin", 120, 200);

             }
        }

how it is possible to do that?


@itmfar
Replies

PanagiotisCharalampous
21 Nov 2017, 17:07

Hi itmfar,

You can create a property inside the indicator as follows

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewIndicator : Indicator
    {

        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }

        public bool Test = false;

        protected override void Initialize()
        {
        }

        public override void Calculate(int index)
        {
            if (1 + 1 == 2)
                Test = true;
        }

    }
}

and read it it in your cBot like the example below

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }
        private NewIndicator newIndicator;
        protected override void OnStart()
        {
            newIndicator = Indicators.GetIndicator<NewIndicator>();
        }

        protected override void OnTick()
        {
            var test = newIndicator.Test;           

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

Let me know if this is what you need.

Best Regards,

Panagiotis


@PanagiotisCharalampous

Jiri
21 Nov 2017, 18:09

Don't forget to force the calculation of the referenced indicator if you are not reading any output attribute. Otherwise, the state of the boolean will remain false all the time.


@Jiri

Jiri
21 Nov 2017, 18:12

Here it's explained more-in-depth. I never got an answer so I suppose it's a protection from unnecessary calculation.


@Jiri

itmfar
22 Nov 2017, 09:21

RE:

Panagiotis Charalampous said:

Hi itmfar,

 

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }
        private NewIndicator newIndicator;
        protected override void OnStart()
        {
            newIndicator = Indicators.GetIndicator<NewIndicator>();
        }

        protected override void OnTick()
        {
            var test = newIndicator.Test;           

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

Let me know if this is what you need.

Best Regards,

Panagiotis

Thanks for your response but it did not work, I have modified my Indicator
 

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewIndicator : Indicator
    {

        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }
        private MacdCrossOver _MACD;
        public bool Test = false;
        public int myIndex;
        public bool myBuy;

        protected override void Initialize()
        {
            _MACD = Indicators.MacdCrossOver(26, 12, 9);
            myBuy = false;
        }

        public override void Calculate(int index)
        {
            if (_MACD.MACD[index] > _MACD.Signal[index])
            {
                myBuy = true;
            }
            myIndex = index;
            if (index % 2 == 1)
                Test = true;
        }
    }
}

and it is my Cbot

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }
        private NewIndicator newIndicator;
        protected override void OnStart()
        {
            newIndicator = Indicators.GetIndicator<NewIndicator>();
        }

        protected override void OnTick()
        {
            var test = newIndicator.Test;
            Print("test " + test);
            var myindex = newIndicator.myIndex;
            Print("my index " + myindex);
            var mybuy = newIndicator.myBuy;
            Print("buy " + mybuy);


        }
        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}
 now for each tick output is test false- index 0 - buy false.


@itmfar

itmfar
22 Nov 2017, 09:23

RE:

tmc. said:

Don't forget to force the calculation of the referenced indicator if you are not reading any output attribute. Otherwise, the state of the boolean will remain false all the time.

Thanks tmc. would you please explain more with sample?


@itmfar

Jiri
22 Nov 2017, 12:59

The calculation method in the refenreced indicator won't be invoked unless you read its output value or force it to calculate.

Add one of these lines into OnTick method inside your cBot.

newIndicator.Calculate(MarketSeries.Open.Count - 1);
var unused = newIndicator.Result.LastValue;

Both samples will make the indicator to calculate at current index.


@Jiri

Jiri
22 Nov 2017, 13:01

By the way, if you need historical data to be calculated as well, add this into OnStart method.

for (var index = 0; index < MarketSeries.Open.Count; index++)
{
   newIndicator.Calculate(index);
}

 


@Jiri

itmfar
23 Nov 2017, 13:55

RE:

tmc. said:

The calculation method in the refenreced indicator won't be invoked unless you read its output value or force it to calculate.

Add one of these lines into OnTick method inside your cBot.

newIndicator.Calculate(MarketSeries.Open.Count - 1);
var unused = newIndicator.Result.LastValue;

Both samples will make the indicator to calculate at current index.

Great, It saved my day.

 


@itmfar

itmfar
23 Nov 2017, 13:57

RE:

tmc. said:

By the way, if you need historical data to be calculated as well, add this into OnStart method.

for (var index = 0; index < MarketSeries.Open.Count; index++)
{
   newIndicator.Calculate(index);
}

 

After that how i can use historical data in OnTick() method?


@itmfar