Indicator Values are not correctly appearing when used in Robot

Created at 28 Jul 2015, 02:01
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

Indicator Values are not correctly appearing when used in Robot
28 Jul 2015, 02:01


Hi,

I am using the basic Polynomial regression indicator and then using it inside a robot and printing the last value.  I noticed the printed value is not reconciling to the output displayed by the indicator. 

//THE CODE FOR THE ROBOT IS

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

using System.Collections.Generic;


namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class NEWBOT : Robot
    {
        [Parameter(DefaultValue = "EURJPY")]
        public string tradepair { get; set; }

        private string[] tradePairs = new string[] 
        {
            "AUDCAD",
            "AUDCHF",
            "AUDJPY",
            "AUDNZD",
            "AUDUSD",
            "CADCHF",
            "CADJPY",
            "CHFJPY",
            "EURAUD",
            "EURCAD",
            "EURCHF",
            "EURGBP",
            "EURJPY",
            "EURNZD",
            "EURUSD",
            "GBPAUD",
            "GBPCAD",
            "GBPCHF",
            "GBPJPY",
            "GBPNZD",
            "GBPUSD",
            "NZDCAD",
            "NZDCHF",
            "NZDJPY",
            "NZDUSD",
            "USDCAD",
            "USDCHF",
            "USDJPY"
        };

        // private ATRPMR pipsATRS1;
        public PolyRegression PolyRegressionIndicator;

        protected override void OnTick()
        {
            foreach (string tp in tradePairs)
            {
                Symbol s = MarketData.GetSymbol(tp);
                //Print("Attempting to initialise Indicator for tradepair {0}", tp);
                PolyRegressionIndicator = Indicators.GetIndicator<PolyRegression>(tp, TimeFrame.Hour4, 3, 120, 1.62, 2);
                //Print("Indicator initialised for tradepair {0}", tp);
                if (!double.IsNaN(PolyRegressionIndicator.sql.Last(1)))
                    Print("SYMBOL {0} has SQL Value of = {1}", tp, Math.Round(PolyRegressionIndicator.sql.Last(2), s.Digits));
                if (!double.IsNaN(PolyRegressionIndicator.sql.Last(1)))
                    Print("SYMBOL {0} has PRC Value of = {1}", tp, Math.Round(PolyRegressionIndicator.prc.Last(2), s.Digits));


            }
        }


    }
}


 

//THE CODE FOR THE INDICATOR IS


// -------------------------------------------------------------------------------
//
//   Polynomial Regression
//
// -------------------------------------------------------------------------------

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

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = false, ScalePrecision = 5, AccessRights = AccessRights.FullAccess)]
    public class PolyRegression : Indicator
    {

        [Parameter("Search Symbol")]
        public string tradePair { get; set; }

        [Parameter("Indicator TimeFrame", DefaultValue = "Hour4")]
        public TimeFrame ITimeFrame { get; set; }


        [Parameter(DefaultValue = 3.0)]
        public int degree { get; set; }

        [Parameter(DefaultValue = 120)]
        public int period { get; set; }

        [Parameter(DefaultValue = 1.62)]
        public double strdDev { get; set; }

        [Parameter(DefaultValue = 2)]
        public double strdDev2 { get; set; }



        [Output("PRC")]
        public IndicatorDataSeries prc { get; set; }

        [Output("SQH")]
        public IndicatorDataSeries sqh { get; set; }

        [Output("SQL")]
        public IndicatorDataSeries sql { get; set; }

        [Output("SQL2")]
        public IndicatorDataSeries sql2 { get; set; }

        [Output("SQH2")]
        public IndicatorDataSeries sqh2 { get; set; }



        private double[,] ai = new double[10, 10];
        private double[] b = new double[10];
        private double[] x = new double[10];
        private double[] sx = new double[10];
        private double sum;
        private int ip;
        private int p;
        private int n;
        //private int f;
        private double qq;
        private double mm;
        private double tt;
        private int ii;
        private int jj;
        private int kk;
        private int ll;
        private int nn;
        private double sq;
        private double sq2;
        private int i0 = 0;
        private int mi;




        public Symbol s;
        public MarketSeries mktSeries;


        protected override void Initialize()
        {

            s = MarketData.GetSymbol(tradePair);
            mktSeries = MarketData.GetSeries(s, ITimeFrame);

        }


        public override void Calculate(int index)
        {
            ip = period;
            p = ip;
            sx[1] = p + 1;
            nn = degree + 1;
            //----------------------sx-------------------------------------------------------------------
            // 
            for (mi = 1; mi <= nn * 2 - 2; mi++)
            {
                sum = 0;
                for (n = i0; n <= i0 + p; n++)
                {
                    sum += Math.Pow(n, mi);
                }
                sx[mi + 1] = sum;
            }
            //----------------------syx-----------
            for (mi = 1; mi <= nn; mi++)
            {
                sum = 0.0;
                for (n = i0; n <= i0 + p; n++)
                {
                    if (mi == 1)
                        sum += mktSeries.Close[index - n];
                    else
                        sum += mktSeries.Close[index - n] * Math.Pow(n, mi - 1);
                }
                b[mi] = sum;
            }
            //===============Matrix=======================================================================================================
            for (jj = 1; jj <= nn; jj++)
            {
                for (ii = 1; ii <= nn; ii++)
                {
                    kk = ii + jj - 1;
                    ai[ii, jj] = sx[kk];
                }
            }
            //===============Gauss========================================================================================================
            for (kk = 1; kk <= nn - 1; kk++)
            {
                ll = 0;
                mm = 0;
                for (ii = kk; ii <= nn; ii++)
                {
                    if (Math.Abs(ai[ii, kk]) > mm)
                    {
                        mm = Math.Abs(ai[ii, kk]);
                        ll = ii;
                    }
                }
                if (ll == 0)
                    return;
                if (ll != kk)
                {
                    for (jj = 1; jj <= nn; jj++)
                    {
                        tt = ai[kk, jj];
                        ai[kk, jj] = ai[ll, jj];
                        ai[ll, jj] = tt;
                    }
                    tt = b[kk];
                    b[kk] = b[ll];
                    b[ll] = tt;
                }
                for (ii = kk + 1; ii <= nn; ii++)
                {
                    qq = ai[ii, kk] / ai[kk, kk];
                    for (jj = 1; jj <= nn; jj++)
                    {
                        if (jj == kk)
                            ai[ii, jj] = 0;
                        else
                            ai[ii, jj] = ai[ii, jj] - qq * ai[kk, jj];
                    }
                    b[ii] = b[ii] - qq * b[kk];
                }
            }
            x[nn] = b[nn] / ai[nn, nn];
            for (ii = nn - 1; ii >= 1; ii--)
            {
                tt = 0;
                for (jj = 1; jj <= nn - ii; jj++)
                {
                    tt = tt + ai[ii, ii + jj] * x[ii + jj];
                    x[ii] = (1 / ai[ii, ii]) * (b[ii] - tt);
                }
            }
            sq = 0.0;
            sq2 = 0.0;
            for (n = i0; n <= i0 + p; n++)
            {
                sum = 0;
                for (kk = 1; kk <= degree; kk++)
                {
                    sum += x[kk + 1] * Math.Pow(n, kk);
                }
                prc[index - n] = (x[1] + sum);
                sq += Math.Pow(mktSeries.Close[index - n] - prc[index - n], 2);
                sq2 += Math.Pow(mktSeries.Close[index - n] - prc[index - n], 2);
            }
            sq = Math.Sqrt(sq / (p + 1)) * strdDev;
            sq2 = Math.Sqrt(sq2 / (p + 1)) * strdDev2;
            for (n = i0; n <= i0 + p; n++)
            {
                sqh[index - n] = (prc[index - n] + sq);
                sql[index - n] = (prc[index - n] - sq);
                sqh2[index - n] = (prc[index - n] + sq2);
                sql2[index - n] = (prc[index - n] - sq2);
            }
        }


        private int GetIndexByDate(MarketSeries series, DateTime time)
        {
            for (int i = series.Close.Count - 1; i > 0; i--)
            {
                if (time == series.OpenTime[i])
                    return i;
            }
            return -1;
        }
    }


}

The code is developed to be able to get the last indicator value for multiple tradepairs and timeframes.  I am not sure what the returned values actually reconcile to.

Many thanks.

 

 

 

 

 


Replies

jhtrader
28 Jul 2015, 02:20 ( Updated at: 21 Dec 2023, 09:20 )

Screen shots

So here is the value for the AUDUSD on the 4 hour timefram being printed by the robot

Here is the values on the indicator

 

 


Spotware
28 Jul 2015, 12:04

Dear Trader,

We do not provide coding assistance services. We more than glad to assist you with specific questions about cAlgo.API. You also can contact one of our Partners or post a job in Development Jobs section for further coding assistance.


@Spotware

jhtrader
28 Jul 2015, 19:38

This is using the sample code

Spotware said:

Dear Trader,

We do not provide coding assistance services. We more than glad to assist you with specific questions about cAlgo.API. You also can contact one of our Partners or post a job in Development Jobs section for further coding assistance.

Dear Spotware,

I am simply printing a value from an indicator inside a robot.  This is not an algorithm please assist.  I am asking the question why the value when printed is different to the value on the graph when the indicator is attached to the chart.  I want to be sure that when using an indicator inside a robot the values are not different. That is all.


Spotware
29 Jul 2015, 11:51

Dear Trader,

You print to the log of the cBot the values calculated by the indicator 2 trendbars ago and in the screenshot of the indicator you are showing the last calculated values.

As said we do not provide coding assistance services. We more than glad to assist you with specific questions about cAlgo.API. You also can contact one of our Partners or post a job in Development Jobs section for further coding assistance.


@Spotware