Market Holiday missing when checking for back testing application (python)

Created at 24 Aug 2023, 05:17
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!
DA

darrelkley

Joined 03.04.2023

Market Holiday missing when checking for back testing application (python)
24 Aug 2023, 05:17


Hello,

I am developing a back testing application and there seems to be some discrepancies between the The ProtoOASymbol property for schedule and holiday and what the candle/trendbar data is that I receive.

My questions are:

  1. Will the holiday entry be removed from the ProtoOASymbol.holiday collection once it has passed? Like say Monday 19 June 2023 the markets were closed in honor of Juneteenth. If the symbol's market were closed for that day, will it be in the current symbol response?
  2. If the Holiday entry is removed, is there a way to get the “history” of holidays when the markets were closed?

 

I cache the candles/trendbars and then I loop trough the backtest start and end date to check if a candle exists and should exist. But for the XAUUSD pair, the markets were closed for some time Monday 19 June 2023, but there is no holiday entry for this in the ProtoOASymbol.holiday collection…

Sample code:

    def is_holiday(self, timestamp: int, holidays: List[Holiday]):
        for holiday in holidays:
            if timestamp > (holiday.holidayDate + holiday.startSecond) and timestamp < (
                holiday.holidayDate + holiday.endSecond
            ):
                return True
        return False
    
    def is_valid_date(
        self, timestamp: int, schedules: List[Schedule], time_zone: str
    ) -> bool:
        """
        Check if a given timestamp is valid and should have a candle.

        :param timestamp: The timestamp to check.
        :param schedules: A list of Schedule entities representing the market trading sessions.
        :param time_zone: The time zone to use for the conversion.
        :return: True if the date is valid and should have a candle, False otherwise.
        """
        # Get previous Sunday 00:00 in the specified time zone
        sunday = self.get_previous_sunday(timestamp,time_zone)

        # Check if market is open during the specified time
        for schedule in schedules:
            if timestamp >= sunday + schedule.startSecond and timestamp < sunday + schedule.endSecond:
                return True

        return False
    
    def get_previous_sunday(sefl,timestamp: int, time_zone:str) -> int:
        tz = pytz.timezone(time_zone)
        date = datetime.fromtimestamp(timestamp, tz) 
        sunday = date - timedelta(days=date.weekday() + 1)
        sunday = sunday.replace(hour=0, minute=0, second=0, microsecond=0)
        return int(sunday.timestamp())
        
     #Sample usage
  			while current_time < end_time:
            if self.is_valid_date(
                current_time, symSchedule, symZone
            ) and not self.is_holiday(current_time, holidays):
                interval_start = current_time

                if not any(interval_start == candle.timestamp for candle in candles):
                    missing_candles.append(interval_start)

            current_time += int(interval.total_seconds())


@darrelkley