Need Help How I use a specific value form indicator inside my CBot

mohsabry.ms since: 15 Sep 2021;

  14 Nov 2021, 22:13
Need Help How I use a specific value form indicator inside my CBot

Hi there,

I'm just a beginner

How I use a specific value form indicator inside my CBot

How can I use a value of (double a) from indicator to my CBot

I called Linear Regression Channel  Indicator  in CBot and check it in manage references

I use Linear Regression Channel  Indicator 

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

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, AccessRights = AccessRights.None)]
    public class LinearRegressionChannel : Indicator
    {
        [Parameter(DefaultValue = 200)]
        public int Bars { get; set; }

        [Parameter(DefaultValue = "Yellow")]
        public string Color { get; set; }

        [Parameter(DefaultValue = 1.0)]
        public double LineThickness { get; set; }

        [Parameter("Center", DefaultValue = true)]
        public bool ShowCenter { get; set; }

        [Parameter("Channel", DefaultValue = true)]
        public bool ShowChannel { get; set; }

        [Parameter("Standard deviation", DefaultValue = true)]
        public bool ShowDeviantion { get; set; }




        private Colors color;

        protected override void Initialize()
        {

            // Parse color from string, e.g. "Yellow", "Green", "Red". string must start with large letter, "Red" is valid, "red" - not.
            if (!Enum.TryParse(Color, out color))
                color = Colors.Yellow;
        }


        public override void Calculate(int index)
        {
            if (IsLastBar)
                LinearRegression(MarketSeries.Close);

        }

        private void LinearRegression(DataSeries series)
        {
            // Linear regresion

            double sum_x = 0, sum_x2 = 0, sum_y = 0, sum_xy = 0;

            int start = series.Count - Bars;
            int end = series.Count - 1;

            for (int i = start; i <= end; i++)
            {
                sum_x += 1.0 * i;
                sum_x2 += 1.0 * i * i;
                sum_y += series[i];
                sum_xy += series[i] * i;
            }

            double a = (Bars * sum_xy - sum_x * sum_y) / (Bars * sum_x2 - sum_x * sum_x);
            double b = (sum_y - a * sum_x) / Bars;




   
            // Calculate maximum and standard devaitions

            double maxDeviation = 0;
            double sumDevation = 0;

            for (int i = start; i <= end; i++)
            {
                double price = a * i + b;
                maxDeviation = Math.Max(Math.Abs(series[i] - price), maxDeviation);
                sumDevation += Math.Pow(series[i] - price, 2.0);
            }

            double stdDeviation = Math.Sqrt(sumDevation / Bars);

            // draw in future
            end += 20;

            double pr1 = a * start + b;
            double pr2 = a * end + b;

            if (ShowCenter)
            {
                ChartObjects.DrawLine("center", start, pr1, end, pr2, color, LineThickness, LineStyle.Lines);

            }

            if (ShowChannel)
            {
                ChartObjects.DrawLine("top", start, pr1 + maxDeviation, end, pr2 + maxDeviation, color, LineThickness, LineStyle.Solid);
                ChartObjects.DrawLine("bottom", start, pr1 - maxDeviation, end, pr2 - maxDeviation, color, LineThickness, LineStyle.Solid);
            }

            if (ShowDeviantion)
            {
                ChartObjects.DrawLine("dev-top", start, pr1 + stdDeviation, end, pr2 + stdDeviation, color, LineThickness, LineStyle.DotsVeryRare);
                ChartObjects.DrawLine("dev-bottom", start, pr1 - stdDeviation, end, pr2 - stdDeviation, color, LineThickness, LineStyle.DotsVeryRare);
            }



        }

    }
}

Thank You in Advance


 

PanagiotisCharalampous's avatar

PanagiotisCharalampous since: 13 Jan 2017;

  15 Nov 2021, 08:31

Hi mohsabry.ms,

To be able to access your indicator's values from a cBot, you first need to expose them in a public variable, preferably an IndicatorDataSeries.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook 

Head of Community Management at cTrader

mohsabry.ms since: 15 Sep 2021;

  15 Nov 2021, 14:47
RE:

PanagiotisCharalampous said:

Hi mohsabry.ms,

To be able to access your indicator's values from a cBot, you first need to expose them in a public variable, preferably an IndicatorDataSeries.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook 

Appreciate your valuable help,

Forgive me as just a begineer

But I tired trying expose  "a" variable to be an output as a IndicatorDataSeries.

The calculation made as a series not index

I search how to make series type deals with IndicatorDataSeries but I really can't find a way

 private void LinearRegression(DataSeries series) 

   // Linear regresion

            double sum_x = 0, sum_x2 = 0, sum_y = 0, sum_xy = 0;

            int start = series.Count - Bars;
            int end = series.Count - 1;

            for (int i = start; i <= end; i++)
            {
                sum_x += 1.0 * i;
                sum_x2 += 1.0 * i * i;
                sum_y += series[i];
                sum_xy += series[i] * i;
            }

            double a = (Bars * sum_xy - sum_x * sum_y) / (Bars * sum_x2 - sum_x * sum_x);

}

and there is no output 

Thank you in Advance

Regards,

amusleh's avatar

amusleh since: 01 Mar 2021;

  17 Nov 2021, 07:57

Hi,

Did you checked the API references example: cAlgo API Reference - IndicatorDataSeries Interface (ctrader.com)

You have to store your calculation result inside an IndicatorDataSeries, then you can use that data series on your cBots.

Community Developer | Spotware | Telegram: @algodeveloper

mohsabry.ms since: 15 Sep 2021;

  18 Nov 2021, 11:59
RE:

amusleh said:

Hi,

Did you checked the API references example: cAlgo API Reference - IndicatorDataSeries Interface (ctrader.com)

You have to store your calculation result inside an IndicatorDataSeries, then you can use that data series on your cBots.

Thank you for your advice it works well

Regards,