Chart.DrawTrendLine working initially then not after next bar forms

Created at 27 Aug 2020, 11:15
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!
ML

mlcilia

Joined 01.08.2019

Chart.DrawTrendLine working initially then not after next bar forms
27 Aug 2020, 11:15


I have created a script which draws a trend line or line of best fit within a cbot. The cbot works fine initially drawing the first line, but as soon as a another bar opens to redraw a new line, the lines goes almost vertical and continues like this every new bar. I believe it has something to do with the way how I calculate it but I struggling to figure this one out.

Looking for some suggestions/support??

 

CBOT:

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.WAustraliaStandardTime, AccessRights = AccessRights.None)]
    public class TestBot : Robot
    {
        [Parameter(DefaultValue = 8)]
        public int Periods { get; set; }

        public double sumx = 0, sumx2 = 0, sumy = 0, sumxy = 0;

        public double m, b;

        public ChartObject chart1;

        protected override void OnStart()
        {
            // Put your initialization logic here
        }

        protected override void OnBar()
        {

            DateTime start = Server.Time.AddHours(-(Periods));
            Print("start = ", start);

            DateTime end = Server.Time.AddHours(-(1));
            Print("end = ", end);


            for (int i = 1; i <= Periods; i++)
            {
                sumx += 1.0 * i;
                sumx2 += 1.0 * i * i;
                sumy += Bars.ClosePrices.Last(i);
                sumxy += Bars.ClosePrices.Last(i) * i;
            }

            m = (Periods * sumxy - sumx * sumy) / (Periods * sumx2 - sumx * sumx);
            Print("m = ", m);
            b = (sumy - m * sumx) / Periods;
            Print("b = ", b);

            double pr1 = m * (1) + b;
            Print("pr1 = ", pr1);
            double pr2 = m * (Periods) + b;
            Print("pr2 = ", pr2);

            chart1 = Chart.DrawTrendLine("Trend-Line", start, pr1, end, pr2, "Yellow");
        }

    }
}

INITIAL:

AS SOON AS A NEW BAR OPENS:

 

 


 


@mlcilia
Replies

PanagiotisCharalampous
27 Aug 2020, 11:23

Hi mlcillia,

Why do you declare these variables as class variables and not declare them inside the method?

        public double sumx = 0, sumx2 = 0, sumy = 0, sumxy = 0;

        public double m, b;

Best Regards,

Panagiotis 

Join us on Telegram

 


@PanagiotisCharalampous

mlcilia
27 Aug 2020, 11:26

Nevermind, I figured it out. It was due to the declaration of sum variables not reseting back to 0. I moved them into the OnBar calculation.

Now the next issue is start and end values as the start of the week is monday and the end of the week is friday.


@mlcilia

mlcilia
27 Aug 2020, 11:29

RE:

PanagiotisCharalampous said:

Hi mlcillia,

Why do you declare these variables as global?

        public double sumx = 0, sumx2 = 0, sumy = 0, sumxy = 0;

        public double m, b;

Best Regards,

Panagiotis 

Join us on Telegram

 

I declared them there from another script I wrote. I didn't take notice of it when copy this part of my old script over haha. Thanks for your feedback.

Do you have any suggestions around this particular section below:

            DateTime start = Server.Time.AddHours(-(Periods));
            Print("start = ", start);

            DateTime end = Server.Time.AddHours(-(1));
            Print("end = ", end);

If you run the CBOT you will notice the line drawn resets as the market closes and reopens at the end of a week and at the start of the following week. 


@mlcilia

mlcilia
27 Aug 2020, 12:16

RE: RE:

mlcilia said:

PanagiotisCharalampous said:

Hi mlcillia,

Why do you declare these variables as global?

        public double sumx = 0, sumx2 = 0, sumy = 0, sumxy = 0;

        public double m, b;

Best Regards,

Panagiotis 

Join us on Telegram

 

I declared them there from another script I wrote. I didn't take notice of it when copy this part of my old script over haha. Thanks for your feedback.

Do you have any suggestions around this particular section below:

            DateTime start = Server.Time.AddHours(-(Periods));
            Print("start = ", start);

            DateTime end = Server.Time.AddHours(-(1));
            Print("end = ", end);

If you run the CBOT you will notice the line drawn resets as the market closes and reopens at the end of a week and at the start of the following week. 

Figured this out too.

 

Did this:

            DateTime start, end;

            if (Server.Time.DayOfWeek == DayOfWeek.Monday)
            {
                start = Server.Time.AddHours(-(Periods)-48);
                Print("start = ", start);

                end = Server.Time.AddHours(-(1)-48);
                Print("end = ", end);
            }
            else
            {
                start = Server.Time.AddHours(-(Periods));
                Print("start = ", start);

                end = Server.Time.AddHours(-(1));
                Print("end = ", end);
            }


@mlcilia