How to pass data from Custom Indicator to cBot

Created at 27 Aug 2022, 20:36
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!
BE

belasto

Joined 11.02.2019

How to pass data from Custom Indicator to cBot
27 Aug 2022, 20:36


Hi all,

I read many posts about this and the reference help as well, but I am not able to get it to work - feeling totally lost. I don't get the C# concept yet.

I wrote a small indicator and would like to pass the current HighPrice and LowPrice of the Inidcator to a cBot. Could someone provide the things I need to add please? 

Indicator code below, the cBot is rather empty yet.
 

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(IsOverlay = true, TimeZone = TimeZones.UTC, AutoRescale = false, AccessRights = AccessRights.None)]
    public class Draw_Test : Indicator
    {
        
        double HighPrice = 0;
        double LowPrice = 100000000;
        double Distance;
        int FirstIndex = -1;
        int LastIndex = -1;
        
        DateTime MyDate;
        
        protected override void Initialize()
        {

        }

        public override void Calculate(int index)
        {
            
            // TO DO: CALC BOXES FOR CURRENT DAY AND LAST X DAYS
            MyDate = Bars.LastBar.OpenTime.Date;

            if(Bars.OpenTimes[index].Hour == 6 && Bars.OpenTimes[index].Date == MyDate){

                if(FirstIndex == -1){
                    FirstIndex = index;
                    LastIndex = FirstIndex + 11;
                }

                if(HighPrice < Bars[index].High){
                    HighPrice = Bars[index].High;
                }

                if(LowPrice > Bars[index].Low){
                    LowPrice = Bars[index].Low;
                }

                Distance = HighPrice - LowPrice;
                int intervalDistance = 263;
                int lineAmount = 7;

                Color MainCol = Color.FromHex("55FF0000");
                Color LineColDark = Color.FromHex("FF000000");
                Color LineColBright = Color.FromHex("55000000");

                // DRAW NO TRADING ZONE
                ChartRectangle rectangleFilled = Chart.DrawRectangle("rectangleFilled", FirstIndex, HighPrice, LastIndex+intervalDistance, LowPrice, MainCol);
                rectangleFilled.IsFilled = true;
                rectangleFilled.Color = MainCol;

                // DRAW SOURCE ZONE
                Chart.DrawRectangle("rectangleLine", FirstIndex, HighPrice, LastIndex, LowPrice, LineColDark);

                // DRAW LINES ABOVE
                for(int i = 1; i <= lineAmount; i++){
                    Color col;
                    if(i < 4){
                        col = LineColDark;
                    }else{
                        col = LineColBright;
                    }
                    Chart.DrawTrendLine("lineAbove"+i.ToString(), FirstIndex, HighPrice+Distance*i, LastIndex+intervalDistance, HighPrice+Distance*i, col);
                }

                // DRAW LINES BELOW
                for(int j = 1; j <= lineAmount; j++){
                
                    Color col;
                    if(j < 4){
                        col = LineColDark;
                    }else{
                        col = LineColBright;
                    }
                    Chart.DrawTrendLine("lineBelow"+j.ToString(), FirstIndex, LowPrice-Distance*j, LastIndex+intervalDistance, LowPrice-Distance*j, col);
                }

// ADD FUNCTION FOR PASSING HIGH AND LOW PRICES HERE..
// ....
// --------

            }else{
                HighPrice = 0;
                LowPrice = 100000000;
                FirstIndex = -1;
                LastIndex = -1;
                Distance = 0;
            }
        }
    }
}



Thanks a lot if you can help.


@belasto
Replies

PanagiotisCharalampous
29 Aug 2022, 10:09

Hi belasto,

First you need to declare some Output IndicatorDataSeries

        [Output("Threshold", LineColor = "Green")]
        public IndicatorDataSeries High { get; set; }

        [Output("Threshold", LineColor = "Red")]
        public IndicatorDataSeries Low { get; set; }

Then you need to pass the values to the data series


                if(HighPrice < Bars[index].High){
                    HighPrice = Bars[index].High;
                    High[index] = HighPrice;
                }

                if(LowPrice > Bars[index].Low){
                    LowPrice = Bars[index].Low;
                    Low[index] = LowPrice;
                }

At last, you can access these data series from your cBot

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous

belasto
30 Aug 2022, 01:42 ( Updated at: 30 Aug 2022, 01:44 )

RE:

Hi Panagiotis,

Okay - with your input, I finally got it to work.

If some other Super Newbie like me has the same question, here is a super noob recap.


Indicator Code

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(IsOverlay = true, TimeZone = TimeZones.UTC, AutoRescale = false, AccessRights == AccessRights.None)]
    public class YourCustomIndicator : Indicator
    {
        [Output("Value1", LineColor = "Transparent")]
        public IndicatorDataSeries MyValue { get; set; }

        ......
    }


    public override void Calculate(int index)
    {
         MyValue[index] = WhateverYouWantToPass;
    }
}



cBot Code
(Don't forget to click on "Manage References" on the top and include your custom indicator!)
 

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 YourBot : Robot
    {
    
        private YourCustomIndicator theIndicator;

        protected override void OnStart()
        {
            theIndicator = Indicators.GetIndicator<YourCustomIndicator>();
        }

        protected override void OnTick()
        {
            Print("YourValue: ", theIndicator.MyValue.LastValue.ToString());
        }

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




Best regards.


@belasto