Recalculate everything When Load more bars in Chart

Created at 07 Apr 2021, 17:29
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!
AK

akbarlotfi15168

Joined 07.04.2021

Recalculate everything When Load more bars in Chart
07 Apr 2021, 17:29


Hello

I have written an indicator but every time I download more bars in my chart everything disappear and after recalculate it ,

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

namespace cAlgo
{
    [Indicator(IsOverlay = true, AccessRights = AccessRights.None)]
    public class DoubleMacd_SodDeh : Indicator
    {



        [Parameter("RiskToRiward", Group = "R/R", DefaultValue = 1.3)]
        public double RiskToRiward { get; set; }

        [Parameter("SmallMACD_LongSycle", Group = "SmallMACD", DefaultValue = 34)]
        public int SmallMACD_LongSycle { get; set; }
        [Parameter("SmallMACD_ShortSycle", Group = "SmallMACD", DefaultValue = 21)]
        public int SmallMACD_ShortSycle { get; set; }
        [Parameter("SmallMACD_Signal", Group = "SmallMACD", DefaultValue = 13)]
        public int SmallMACD_Signal { get; set; }
        [Parameter("SmallMACD_Cross_LagPeriod", Group = "SmallMACD", DefaultValue = 55)]
        public int SmallMACD_Cross_LagPeriod { get; set; }


        [Parameter("BigMACD_LongSycle_Cross", Group = "BigMACD", DefaultValue = 610)]
        public int BigMACD_LongSycle { get; set; }
        [Parameter("BigMACD_ShortSycle", Group = "BigMACD", DefaultValue = 233)]
        public int BigMACD_ShortSycle { get; set; }
        [Parameter("BigMACD_Signal", Group = "BigMACD", DefaultValue = 89)]
        public int BigMACD_Signal { get; set; }
        [Parameter("BigMACD_LagPeriod", Group = "BigMACD", DefaultValue = 8)]
        public int BigMACD_LagPeriod { get; set; }


        [Parameter("DefaultTenkensenPeriods", Group = "DefaultIchimoku", DefaultValue = 34)]
        public int DefaultTenkensenPeriods { get; set; }
        [Parameter("DefaultKijunSenPeriods", Group = "DefaultIchimoku", DefaultValue = 89)]
        public int DefaultKijunSenPeriods { get; set; }
        [Parameter("DefaultSenkouPeriods", Group = "DefaultIchimoku", DefaultValue = 233)]
        public int DefaultSenkouPeriods { get; set; }



        [Parameter("PeriodsForATRforPlot", Group = "ATR", DefaultValue = 13)]
        public int PeriodsForATRforStop { get; set; }
        [Parameter("MovingAverageTypeForATR", Group = "ATR")]
        public MovingAverageType MovingAverageTypeForATR { get; set; }






        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }


        [Output("UpPending", IsHistogram = false, Color = Colors.Blue, PlotType = PlotType.Points, Thickness = 5)]
        public IndicatorDataSeries UpPending { get; set; }

        [Output("Up Fractal", IsHistogram = false, Color = Colors.LightBlue, PlotType = PlotType.Points, Thickness = 5)]
        public IndicatorDataSeries UpFractal { get; set; }

        [Output("Up TakeProfit", IsHistogram = false, Color = Colors.LightGreen, PlotType = PlotType.Points, Thickness = 5)]
        public IndicatorDataSeries BuyTakeProfit { get; set; }





        [Output("DownPending", IsHistogram = false, Color = Colors.Red, PlotType = PlotType.Points, Thickness = 5)]
        public IndicatorDataSeries DownPending { get; set; }

        [Output("Down Fractal", IsHistogram = false, Color = Colors.Salmon, PlotType = PlotType.Points, Thickness = 5)]
        public IndicatorDataSeries DownFractal { get; set; }
        [Output("Down TakeProfit", IsHistogram = false, Color = Colors.Orange, PlotType = PlotType.Points, Thickness = 5)]
        public IndicatorDataSeries SellTakeProfit { get; set; }




        public MacdHistogram _smallMacd;
        public MacdHistogram _bigMacd;
        public AverageTrueRange averageForPlot;
        public IchimokuKinkoHyo _defaultIchimoku;



        double hightOfCurrentBar;
        double lowOfCurrentBar;
        double higherHighResistance;
        int peridHigh;
        double buyTakeProfit;




        protected override void Initialize()
        {
            // Initialize and create nested indicators
            _smallMacd = Indicators.MacdHistogram(SmallMACD_LongSycle, SmallMACD_ShortSycle, SmallMACD_Signal);
            _bigMacd = Indicators.MacdHistogram(BigMACD_LongSycle, BigMACD_ShortSycle, BigMACD_Signal);
            averageForPlot = Indicators.AverageTrueRange(PeriodsForATRforStop, MovingAverageTypeForATR);
            _defaultIchimoku = Indicators.IchimokuKinkoHyo(DefaultTenkensenPeriods, DefaultKijunSenPeriods, DefaultSenkouPeriods);
        }

        public override void Calculate(int index)
        {



            if (index > 1300)
            {
                for (int i = index; i > 611; i--)
                {
                    if ((_bigMacd.Signal[i] < _bigMacd.Histogram[i]))
                    {
                        if ((_smallMacd.Signal[i] < _smallMacd.Histogram[i]) && (_smallMacd.Signal[i - 1] > _smallMacd.Histogram[i - 1]))
                        {

                            if ((_smallMacd.Signal[i] < 0) || (_smallMacd.Histogram[i] < 0))
                            {


                                var highest = Bars.HighPrices[i];

                                var lowestForStop = Bars.LowPrices[i];
                                for (int g = i - 1; g > i - SmallMACD_Cross_LagPeriod; g--)
                                {


                                    if ((_smallMacd.Signal[g] < _smallMacd.Histogram[g]) && (_smallMacd.Signal[g - 1] > _smallMacd.Histogram[g - 1]))
                                    {


                                        if ((_smallMacd.Signal[i] < _smallMacd.Signal[g]))
                                        {
                                            for (int f = i - 2; f > g; f--)
                                            {
                                                if (Bars.HighPrices[f] > highest)
                                                {
                                                    highest = Bars.HighPrices[f];

                                                }
                                                if (Bars.LowPrices[f] < lowestForStop)
                                                {
                                                    lowestForStop = Bars.LowPrices[f];

                                                }
                                            }


                                            //  if ((highest > _defaultIchimoku.SenkouSpanA[i]) && (highest > _defaultIchimoku.SenkouSpanB[i]))
                                            {
                                                UpFractal[i] = lowestForStop;


                                                UpPending[i] = highest;


                                                BuyTakeProfit[i] = highest + ((UpPending[i] - UpFractal[i]) * RiskToRiward);




                                                break;

                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (index > 1300)
            {
                for (int i = index; i > 611; i--)
                {
                    if ((_bigMacd.Signal[i] > _bigMacd.Histogram[i]))
                    {
                        if ((_smallMacd.Signal[i] > _smallMacd.Histogram[i]) && (_smallMacd.Signal[i - 1] < _smallMacd.Histogram[i - 1]))
                        {

                            if ((_smallMacd.Signal[i] > 0) || (_smallMacd.Histogram[i] > 0))
                            {



                                var lowest = Bars.LowPrices[i];
                                var highestForSellStop = Bars.HighPrices[i];

                                for (int g = i - 1; g > i - SmallMACD_Cross_LagPeriod; g--)
                                {


                                    if ((_smallMacd.Signal[g] > _smallMacd.Histogram[g]) && (_smallMacd.Signal[g - 1] < _smallMacd.Histogram[g - 1]))
                                    {

                                        if ((_smallMacd.Signal[i] > _smallMacd.Signal[g]))
                                        {
                                            for (int f = i - 2; f > g; f--)
                                            {
                                                if (Bars.LowPrices[f] < lowest)
                                                {
                                                    lowest = Bars.LowPrices[f];

                                                }
                                                if (Bars.HighPrices[f] > highestForSellStop)
                                                {
                                                    highestForSellStop = Bars.HighPrices[f];

                                                }
                                            }

                                            //   if ((lowest < _defaultIchimoku.SenkouSpanA[i]) && (lowest < _defaultIchimoku.SenkouSpanB[i]))
                                            {


                                                DownPending[i] = lowest;

                                                DownFractal[i] = highestForSellStop;
                                                SellTakeProfit[i] = DownPending[i] - ((highestForSellStop - lowest) * RiskToRiward);
                                                break;

                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }



        }
    }
}

it shows again. I want it to be stable like moving average and don't use cpu like this. My program is very elementary I think if any one can rewrite it should be vey useful. 


@akbarlotfi15168
Replies

amusleh
08 Apr 2021, 11:14 ( Updated at: 08 Apr 2021, 11:50 )

Hi,

Whenever you change a parameter of an indicator, load more data, or get disconnected and re-connected cTrader re-initializes the indicator and your indicator have to re-calculate everything from start.

The way cTrader works is much better than other platforms, because it doesn't force developers to manage events and do all the work of updating the indicator outputs for new data or re-adjusting the indicator already calculated data, but yes it consumes more processing power.

To solve the CPU processing power consumption issue, you can write your indicator calculated outputs data on a file and then when indicator initializes check if the file exist, if there is a cache file for indicator data fill the outputs by loading the file data instead of re-calculating the data and escape the bars until you reach the bars that aren't in your file.


@amusleh

akbarlotfi15168
09 Apr 2021, 21:33 ( Updated at: 09 Apr 2021, 21:44 )

RE:

amusleh said:

Hi,

Whenever you change a parameter of an indicator, load more data, or get disconnected and re-connected cTrader re-initializes the indicator and your indicator have to re-calculate everything from start.

The way cTrader works is much better than other platforms, because it doesn't force developers to manage events and do all the work of updating the indicator outputs for new data or re-adjusting the indicator already calculated data, but yes it consumes more processing power.

To solve the CPU processing power consumption issue, you can write your indicator calculated outputs data on a file and then when indicator initializes check if the file exist, if there is a cache file for indicator data fill the outputs by loading the file data instead of re-calculating the data and escape the bars until you reach the bars that aren't in your file.

I think I should add an event to give me a point when more bar loaded to chart. At that time I nead some method to count hove much bar loaded if calgo has this. Then I should use calculate from index till index- bars loaded if current bar is 0 and we go Left hand side till for example 3500. and if current bar is 3500 and we go Left till 0 I shoul calculate from (index-(index-barsLoaded)) till 0.But I dont Know cAlgo do what type of it . I think they use 0 for current bars.

public int LoadMoreHistory()

I think it is helpful. can you give me any example to it returns bars loaded?


@akbarlotfi15168

amusleh
09 Apr 2021, 21:48

Hi,

The Bars LoadMoreHistory method loads more historical data and allows you to do it via code, it doesn't notify you when user loads more data.

Whenever the indicator initialize method is called it means either more data is loaded or platform re-connected after disconnection, that's the time you can check your saved data file on the disk and load the data from it to your outputs instead of re-calculating.


@amusleh