IsLastBar is not working correctly

Created at 17 Jan 2024, 15:30
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!
MI

mihlali700

Joined 22.12.2023

IsLastBar is not working correctly
17 Jan 2024, 15:30


I have this code that's supposed to run once every new bar but it runs every tick , I'm not exactly understanding how  isLastBar    is supposed to work .

                   If(IsLastBar){
                   StandardPivotAlerts(index);
                   }    

 private void StandardPivotAlerts(int index) 
        {
                var barsDailyIndex = BarsDaily.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);
                // Calculate pivot points for the current day
                double HighValue = BarsDaily.HighPrices[barsDailyIndex];
                double LowValue = BarsDaily.LowPrices[barsDailyIndex];
                double CloseValue = BarsDaily.ClosePrices[barsDailyIndex];

                double PPValue = (HighValue + LowValue + CloseValue) / 3;
                double S1Value = (PPValue * 2) - HighValue;
                double S2Value = PPValue - (HighValue - LowValue);
                double S3Value = LowValue - 2 * (HighValue - PPValue);
                double R1Value = (PPValue * 2) - LowValue;
                double R2Value = PPValue + (HighValue - LowValue);
                double R3Value = HighValue + 2 * (PPValue - LowValue);
              
                var currentprice = Bars.ClosePrices[index-1];
                var PPPips = Math.Abs((currentprice - PPValue) / Symbol.PipSize);
                var S1Pips = Math.Abs((currentprice - S1Value) / Symbol.PipSize);
                var S2Pips = Math.Abs((currentprice - S2Value) / Symbol.PipSize);
                var S3Pips = Math.Abs((currentprice - S3Value) / Symbol.PipSize);
                var R1Pips = Math.Abs((currentprice - R1Value) / Symbol.PipSize);
                var R2Pips = Math.Abs((currentprice - R2Value) / Symbol.PipSize);
                var R3Pips = Math.Abs((currentprice - R3Value) / Symbol.PipSize);
                                      if ( S1Pips <=AlertPips && currentprice > S1Value) {
                                         Notify("Price Is Close to S1"); 
                                    }
                                 if ( S2Pips <=AlertPips && currentprice > S2Value) {
                                       Notify("Price Is Close to S2"); 
                                }
                                 if ( S3Pips <=AlertPips && currentprice > S3Value) {
                                       Notify("Price Is Close to S3"); 
                                   }
                                 if ( R1Pips <=AlertPips && currentprice < R1Value) {
                                         Notify("Price Is Close to R1");
                                   }
                                 if ( R2Pips <=AlertPips && currentprice < R2Value) {
                                    Notify("Price Is Close to R2");
                                 }
                                 if ( R3Pips <=AlertPips && currentprice < R3Value) {
                                   Notify("Price Is Close to R3"); 
                             }
        }

The code is just meant to alert me when the price gets close to the levels specifed in pips once every new bar but it does it every single tick . I don't know what the issue is , am I using isLastBar wrong ?


@mihlali700
Replies

PanagiotisCharalampous
18 Jan 2024, 10:41

Hi there,

IsLastBar is just a boolean indicating if the Calculate() method is executing on the last bar of the chart. Calculate() is called once for every historical bar and on every tick for the current bar.

Best regards,

Panagiotis


@PanagiotisCharalampous

Leonardo.Ciaccio
18 Jan 2024, 10:50

It is not clear what you want to achieve, but I would make a distinction between indicators and cbot, indicators do not have OnBar() event, let's try this approach with an indicator:

 

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

namespace cAlgo
{

    [Indicator(AccessRights = AccessRights.None)]
    public class SPAlerts : Indicator
    {

        Bars BarsDaily;
        double AlertPips;
        int lastIndex;

        protected override void Initialize()
        {

            BarsDaily = MarketData.GetBars(TimeFrame.Daily);
            AlertPips = 0.5;
            lastIndex = -1;

        }

        public override void Calculate(int index)
        {

            // cbot have OnBar event indicators no
            if (index != lastIndex)
            {

                lastIndex = index;
                OnBar(index);

            }

        }

        private void StandardPivotAlerts(int index)
        {
            // If it is not the last bar I will not display any alert    
            if (!IsLastBar) return;

            var barsDailyIndex = BarsDaily.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);
            // Calculate pivot points for the current day
            double HighValue = BarsDaily.HighPrices[barsDailyIndex];
            double LowValue = BarsDaily.LowPrices[barsDailyIndex];
            double CloseValue = BarsDaily.ClosePrices[barsDailyIndex];

            double PPValue = (HighValue + LowValue + CloseValue) / 3;
            double S1Value = (PPValue * 2) - HighValue;
            double S2Value = PPValue - (HighValue - LowValue);
            double S3Value = LowValue - 2 * (HighValue - PPValue);
            double R1Value = (PPValue * 2) - LowValue;
            double R2Value = PPValue + (HighValue - LowValue);
            double R3Value = HighValue + 2 * (PPValue - LowValue);

            var currentprice = Bars.ClosePrices[index - 1];
            var PPPips = Math.Abs((currentprice - PPValue) / Symbol.PipSize);
            var S1Pips = Math.Abs((currentprice - S1Value) / Symbol.PipSize);
            var S2Pips = Math.Abs((currentprice - S2Value) / Symbol.PipSize);
            var S3Pips = Math.Abs((currentprice - S3Value) / Symbol.PipSize);
            var R1Pips = Math.Abs((currentprice - R1Value) / Symbol.PipSize);
            var R2Pips = Math.Abs((currentprice - R2Value) / Symbol.PipSize);
            var R3Pips = Math.Abs((currentprice - R3Value) / Symbol.PipSize);
            if (S1Pips <= AlertPips && currentprice > S1Value)
            {
                Notify("Price Is Close to S1");
            }
            if (S2Pips <= AlertPips && currentprice > S2Value)
            {
                Notify("Price Is Close to S2");
            }
            if (S3Pips <= AlertPips && currentprice > S3Value)
            {
                Notify("Price Is Close to S3");
            }
            if (R1Pips <= AlertPips && currentprice < R1Value)
            {
                Notify("Price Is Close to R1");
            }
            if (R2Pips <= AlertPips && currentprice < R2Value)
            {
                Notify("Price Is Close to R2");
            }
            if (R3Pips <= AlertPips && currentprice < R3Value)
            {
                Notify("Price Is Close to R3");
            }

        }

        private void OnBar(int index)
        {
            // Remember, you may be on a different timeframe than daily, such as 5 minutes
            // so you have to manage it
            StandardPivotAlerts(index);

        }

        private void Notify(string message)
        {

            // TODO

        }

    }

}

@Leonardo.Ciaccio

mihlali700
18 Jan 2024, 14:07

RE: IsLastBar is not working correctly

PanagiotisCharalampous said: 

Hi there,

IsLastBar is just a boolean indicating if the Calculate() method is executing on the last bar of the chart. Calculate() is called once for every historical bar and on every tick for the current bar.

Best regards,

Panagiotis

I am looking for a way to have it called once every single new bar , having this called every tick of the current bar means I'm going to be notified every single second and I don't want that.


@mihlali700

mihlali700
18 Jan 2024, 14:17

RE: IsLastBar is not working correctly

Leonardo.Ciaccio said: 

It is not clear what you want to achieve, but I would make a distinction between indicators and cbot, indicators do not have OnBar() event, let's try this approach with an indicator:

 

using System;using cAlgo.API;using cAlgo.API.Internals;namespace cAlgo{    [Indicator(AccessRights = AccessRights.None)]    public class SPAlerts : Indicator    {        Bars BarsDaily;        double AlertPips;        int lastIndex;        protected override void Initialize()        {            BarsDaily = MarketData.GetBars(TimeFrame.Daily);            AlertPips = 0.5;            lastIndex = -1;        }        public override void Calculate(int index)        {            // cbot have OnBar event indicators no            if (index != lastIndex)            {                lastIndex = index;                OnBar(index);            }        }        private void StandardPivotAlerts(int index)        {            // If it is not the last bar I will not display any alert                if (!IsLastBar) return;            var barsDailyIndex = BarsDaily.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);            // Calculate pivot points for the current day            double HighValue = BarsDaily.HighPrices[barsDailyIndex];            double LowValue = BarsDaily.LowPrices[barsDailyIndex];            double CloseValue = BarsDaily.ClosePrices[barsDailyIndex];            double PPValue = (HighValue + LowValue + CloseValue) / 3;            double S1Value = (PPValue * 2) - HighValue;            double S2Value = PPValue - (HighValue - LowValue);            double S3Value = LowValue - 2 * (HighValue - PPValue);            double R1Value = (PPValue * 2) - LowValue;            double R2Value = PPValue + (HighValue - LowValue);            double R3Value = HighValue + 2 * (PPValue - LowValue);            var currentprice = Bars.ClosePrices[index - 1];            var PPPips = Math.Abs((currentprice - PPValue) / Symbol.PipSize);            var S1Pips = Math.Abs((currentprice - S1Value) / Symbol.PipSize);            var S2Pips = Math.Abs((currentprice - S2Value) / Symbol.PipSize);            var S3Pips = Math.Abs((currentprice - S3Value) / Symbol.PipSize);            var R1Pips = Math.Abs((currentprice - R1Value) / Symbol.PipSize);            var R2Pips = Math.Abs((currentprice - R2Value) / Symbol.PipSize);            var R3Pips = Math.Abs((currentprice - R3Value) / Symbol.PipSize);            if (S1Pips <= AlertPips && currentprice > S1Value)            {                Notify("Price Is Close to S1");            }            if (S2Pips <= AlertPips && currentprice > S2Value)            {                Notify("Price Is Close to S2");            }            if (S3Pips <= AlertPips && currentprice > S3Value)            {                Notify("Price Is Close to S3");            }            if (R1Pips <= AlertPips && currentprice < R1Value)            {                Notify("Price Is Close to R1");            }            if (R2Pips <= AlertPips && currentprice < R2Value)            {                Notify("Price Is Close to R2");            }            if (R3Pips <= AlertPips && currentprice < R3Value)            {                Notify("Price Is Close to R3");            }        }        private void OnBar(int index)        {            // Remember, you may be on a different timeframe than daily, such as 5 minutes            // so you have to manage it            StandardPivotAlerts(index);        }        private void Notify(string message)        {            // TODO        }    }}

I posted this thread in the indicator section so I assumed everyone would know I was talking about an indicator but yes this is indicator code meant to run in an indicator and not a Cbot. As someone already mentioned in this thread isLastBar doesn't stop the code from running every tick but I want the code to run once every new bar. 
Is there a reference I can call that will give me the current time of the current bar. Something like iTime from mql4/5 so that I can just translate this to ctrader  


// Returns true if the first call after a new bar opened 
bool NewBar () {
     datetime       currentTime = iTime(Symbol(), Period(), 0);
     static datetime   previousTime = 
     0; // set to currentTime to avoid processing on current bar on start
     if (currentTime == previousTime) return (false);
     previousTime = currentTime;
     return (true) ;
    }


@mihlali700

Leonardo.Ciaccio
18 Jan 2024, 14:34

I have already given you the solution; I have written you a complete and working indicator.


@Leonardo.Ciaccio

mihlali700
18 Jan 2024, 14:48

RE: IsLastBar is not working correctly

Leonardo.Ciaccio said: 

I have already given you the solution; I have written you a complete and working indicator.

Oh , thank you 


@mihlali700

mihlali700
18 Jan 2024, 14:48

RE: IsLastBar is not working correctly

Leonardo.Ciaccio said: 

I have already given you the solution; I have written you a complete and working indicator.

Oh , thank you 


@mihlali700