cBot is not picking up values from the indicator used in the cBot

Created at 16 Oct 2022, 09:46
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!
JE

jens_c_nielsen

Joined 03.10.2022

cBot is not picking up values from the indicator used in the cBot
16 Oct 2022, 09:46


Hi,

I have a cBot which uses a custom indicator. It looks like the cBot is not able to see the values produced by the indicator.

I have created an example indicator to show this issue. The indicator provides a value 1 if the bar is green and 0 if it is red.

The example cBot I have created to illustrate the issue draws a vertical line on the car if it sees value 1 from the indicator - i.e. should draw a vertical line for each green bar.

This does not happen - which can be seen from the screenshot below:

 

The code for the sample indicator is:

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
{
    [Indicator(AccessRights = AccessRights.None)]
    public class DemoIndicator : Indicator
    {
        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }

        public const int value1 = 1;
        public const int value0 = 0;
        
        protected override void Initialize()
        {
        }

        public override void Calculate(int index)
        {
            if (Bars.ClosePrices[index] > Bars.OpenPrices[index]) // green bar
                Result[index] = value1;
            else
                Result[index] = value0;
        }
    }
}

 

The code for the sample cBot is:

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 DemocBot : Robot
    {
        private DemoIndicator myDemoIndicator;

        protected override void OnStart()
        {
            myDemoIndicator = Indicators.GetIndicator<DemoIndicator>();
        }

        protected override void OnBar()
        {
            int BarIndex;
            ChartVerticalLine myLine;
            string drawnName;

            if (myDemoIndicator.Result.Last(0) == DemoIndicator.value1)
            {
                BarIndex = Bars.Count - 1; // index starts at 0
                drawnName = "Line" + BarIndex.ToString();
                myLine = Chart.DrawVerticalLine(drawnName, BarIndex, Color.LimeGreen, 2, LineStyle.Solid);
                myLine.IsInteractive = (IsBacktesting == true); // if not set as interactive it will be deleted after end of backtesting. https://ctrader.com/forum/calgo-support/16649
            }
        }

        protected override void OnTick()
        {
        }
        
        protected override void OnStop()
        {
        }
    }
}

Could you please let me know why this is the case?

thanks heaps!


@jens_c_nielsen
Replies

PanagiotisChar
17 Oct 2022, 10:54

Hi Jens,

Everything works fine, the problem is in your logic. On bar change, open and close prices are always the same, therefore the following condition will be always false and the result value will be always 0

            if (Bars.ClosePrices[index] > Bars.OpenPrices[index]) // green bar
                Result[index] = value1;
            else
                Result[index] = value0;

If you change OnBar to the below, you will start seeing green lines

        protected override void OnBar()
        {
            int BarIndex;
            ChartVerticalLine myLine;
            string drawnName;
            Print(myDemoIndicator.Result.Last(1));
            if (myDemoIndicator.Result.Last(1) == DemoIndicator.value1)
            {
                BarIndex = Bars.Count - 1; // index starts at 0
                drawnName = "Line" + BarIndex.ToString();
                myLine = Chart.DrawVerticalLine(drawnName, BarIndex, Color.LimeGreen, 2, LineStyle.Solid);
                myLine.IsInteractive = (IsBacktesting == true); // if not set as interactive it will be deleted after end of backtesting. https://ctrader.com/forum/calgo-support/16649
            }
        }

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us

 


@PanagiotisChar

jens_c_nielsen
18 Oct 2022, 01:06

RE: cBot is not picking up values from the indicator used in the cBot Hi,

Thanks heaps for your advice! 

Is OnBar called at the start of a bar? 

Have a good one!

 


@jens_c_nielsen