Why date comparison is not working as expected?

Created at 17 Apr 2024, 15:02
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!
SA

savetree.eatbeaver

Joined 21.10.2022

Why date comparison is not working as expected?
17 Apr 2024, 15:02


Hi, can anyone tell me why the date comparison is not working as expected in the following example:

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

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class Stat2 : Indicator
    {
        private int lastIndex = 1;
        
        private double top = double.MinValue;
        private DateTime topTime = DateTime.MinValue;
        
        private double bottom = double.MaxValue;
        private DateTime bottomTime = DateTime.MinValue;
        
        protected override void Initialize()
        {
        
        }
        
        public override void Calculate(int index)
        {
            if (index > lastIndex)
            {
                if (lastIndex != 0)
                {
                    if (Bars.OpenTimes[lastIndex].Hour == 0)
                    {
                        if (top != double.MinValue && bottom != double.MaxValue)
                        {
                            Print(Bars.OpenTimes[lastIndex] + ": top = " + top + ", topTime = " + topTime + ", bottom = " + bottom + ", bottomTime = " + bottomTime);
                            if (bottomTime < topTime)
                                Print(Bars.OpenTimes[lastIndex] + ": Bottom first");
                            
                            if (topTime > bottomTime)
                                Print(Bars.OpenTimes[lastIndex] + ": Top first");
                        }
                        
                        top = Bars.HighPrices[lastIndex];
                        topTime = Bars.OpenTimes[lastIndex];
                        
                        bottom = Bars.LowPrices[lastIndex];
                        bottomTime = Bars.OpenTimes[lastIndex];
                    }
                    else
                    {
                        if (Bars.HighPrices[lastIndex] > top)
                        {
                            top = Bars.HighPrices[lastIndex];
                            topTime = Bars.OpenTimes[lastIndex];
                        }
                            
                        if (Bars.LowPrices[lastIndex] < bottom)
                        {
                            bottom = Bars.LowPrices[lastIndex];
                            bottomTime = Bars.OpenTimes[lastIndex];
                        }
                    }
                }
                
                lastIndex = index;
            }
        }
    }
}

 

What I get is that lines

Print(Bars.OpenTimes[lastIndex] + ": Bottom first");

and 

Print(Bars.OpenTimes[lastIndex] + ": Top first");

are both executed.

 

Kind regards,

m.

 


@savetree.eatbeaver
Replies

PanagiotisCharalampous
18 Apr 2024, 06:06

Hi there,

Can you explain why you think both are executed?

Best regards,

Panagiotis


@PanagiotisCharalampous

savetree.eatbeaver
18 Apr 2024, 06:35 ( Updated at: 18 Apr 2024, 09:25 )

RE: Why date comparison is not working as expected?

PanagiotisCharalampous said: 

Hi there,

Can you explain why you think both are executed?

Best regards,

Panagiotis

Hi, I forgot to say that this indicator is only supposed to work on H1.

Answering your question:

 


@savetree.eatbeaver

PanagiotisCharalampous
18 Apr 2024, 11:44

Hi there,

You have a logical problem here

                            if (bottomTime < topTime)
                                Print(Bars.OpenTimes[lastIndex] + ": Bottom first");
                            
                            if (topTime > bottomTime)
                                Print(Bars.OpenTimes[lastIndex] + ": Top first");

bottomTime < topTime and topTime > bottomTime are the same thing.


@PanagiotisCharalampous

savetree.eatbeaver
18 Apr 2024, 11:59

RE: Why date comparison is not working as expected?

PanagiotisCharalampous said: 

Hi there,

You have a logical problem here

                            if (bottomTime < topTime)                                Print(Bars.OpenTimes[lastIndex] + ": Bottom first");                                                        if (topTime > bottomTime)                                Print(Bars.OpenTimes[lastIndex] + ": Top first");

bottomTime < topTime and topTime > bottomTime are the same thing.

LOL. You are right! Im so stupid XDXD


@savetree.eatbeaver