Trading data being reported during periods with no active sessions, i.e. MarketSessions=None.

Created at 04 Jan 2024, 00:05
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!
rohingosling's avatar

rohingosling

Joined 02.08.2018

Trading data being reported during periods with no active sessions, i.e. MarketSessions=None.
04 Jan 2024, 00:05


Trading data is being returned, even though MarketSessions reports no active sessions.

Context:

  1. I am seeing unusual behavior that I don't understand when calling MarketSessions to report active sessions during the OnBar event, when backtesting.
  2. There are multiple short periods of time sprinkled throughout the backtesting data, that appear to return OHLC and tick volume data when there are no active sessions, according to MarketSessions. In other words, calling MarketSessions returns None, but there is OHLC bar data for that period. 
  3. There are multiple sites in the backtesting data where I see this phenomenon, mostly on Fridays around 20:00 to 21:00 (UTC+2), where New York should be transitioning to Wellington and Sydney.
  4. For the sake of this post, I have shown just one example below, which may be observed in the backtesting data on a Friday for EURUSD between (2023-12-08, 20:50:00) and (2023-12-08, 21:40:00). See CSV file results below.  
  5. I have written a test robot that saves bar data to a CSV file to demonstrate what I am observing. Both the code and results are listed below. 

Questions:

  1. Based on the code I've submitted below, what am I doing wrong?
  2. If this is indeed a known issue, is there any known way to get around it?

 

CSV File - "MarketSessions=None" Highlighted in Red:

Data appears to show trading data coming from “no” active sessions.  i.e. MarketSessions=None.

 

Test Robot - Saves Bar Data to CSV File:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Globalization;

using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.FullAccess)]
    public class SessionTest : Robot
    {   
        [Parameter ( "Date Range: From", DefaultValue = "2023-12-08 20:00:00" )]
        public string DateFrom { get; set; }

        [Parameter ( "Date Range: To", DefaultValue = "2023-12-10 23:40:00" )]
        public string DateTo { get; set; }
        
        private string csvPath;
        private string csvFile;
        private string csvDelimiter;
        
        protected override void OnStart ()
        {
            // Initialise CSV file path and file name. 
        
            this.csvPath = "X:/financial_data/test/";
            this.csvFile = "forex_data.csv";        
            
            // Initialise CSV file header row. 
            
            this.csvDelimiter   = ",";
            string csvHeaderRow = "";
            csvHeaderRow += $"date{this.csvDelimiter}";
            csvHeaderRow += $"time{this.csvDelimiter}";
            csvHeaderRow += $"open{this.csvDelimiter}";
            csvHeaderRow += $"high{this.csvDelimiter}";
            csvHeaderRow += $"low{this.csvDelimiter}";
            csvHeaderRow += $"close{this.csvDelimiter}";
            csvHeaderRow += $"active_sessions\n";
            
            // Write CSV header row to file. 
            
            File.WriteAllText ( this.csvPath + this.csvFile, csvHeaderRow );
        }

        protected override void OnBar ()
        {
            // Initialise local varaibles. 
        
            int    barIndex       = 1;
            string csvFormatDate  = "yyyy-MM-dd";
            string csvFormatTime  = "HH:mm:ss";
            string csvFormatPrice = ".00000";
            
            // Retrieve date and time range from user parameters. 
                        
            DateTime dateCurrentBar = Bars.OpenTimes.Last ( barIndex );
            DateTime dateFrom;
            DateTime dateTo;
            DateTime.TryParseExact ( this.DateFrom, $"{csvFormatDate} {csvFormatTime}", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateFrom );
            DateTime.TryParseExact ( this.DateTo,   $"{csvFormatDate} {csvFormatTime}", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTo   );
                        
            if ( ( dateCurrentBar >= dateFrom ) && ( dateCurrentBar <= dateTo ) )
            {
                // Compile CSV data row.
                
                string csvDataRow = "";
            
                csvDataRow += $"{dateCurrentBar.ToString ( csvFormatDate )}{this.csvDelimiter}";
                csvDataRow += $"{dateCurrentBar.ToString ( csvFormatTime )}{this.csvDelimiter}";                
                csvDataRow += $"{Bars.OpenPrices.Last ( barIndex ).ToString ( csvFormatPrice )}{this.csvDelimiter}";
                csvDataRow += $"{Bars.HighPrices.Last ( barIndex ).ToString ( csvFormatPrice )}{this.csvDelimiter}";
                csvDataRow += $"{Bars.LowPrices.Last ( barIndex ).ToString ( csvFormatPrice )}{this.csvDelimiter}";
                csvDataRow += $"{Bars.ClosePrices.Last ( barIndex ).ToString ( csvFormatPrice )}{this.csvDelimiter}";
                csvDataRow += $"[  {MarketSessions.ToString ().Replace (","," " )}  ]";
                csvDataRow += "\n";
                
                // Write CSV data row to file.
                
                File.AppendAllText ( this.csvPath + this.csvFile, csvDataRow );
            }
        }
    }
}

 

Parameter Settings - Date and Time Range:

Note: 
The backtesting date range should be set to include the date range specified by the parameters below. For the date range shown by the parameters below, a back-testing date range of at 2023-12-08 to 2023-12-11 may be used. 

 


CSV Reference Data:

For the sake of reference, here is the raw CSV data, in case anyone wants to copy and paste any of the data for any reason. This data is identical to the Excel formatted CSV data above. 

date,time,open,high,low,close,active_sessions
2023-12-08,19:00:00,1.07574,1.07601,1.07574,1.07589,[  NewYork  ]
2023-12-08,19:10:00,1.07591,1.07615,1.07587,1.07589,[  NewYork  ]
2023-12-08,19:20:00,1.07588,1.07610,1.07585,1.07604,[  NewYork  ]
2023-12-08,19:30:00,1.07603,1.07616,1.07570,1.07574,[  NewYork  ]
2023-12-08,19:40:00,1.07571,1.07588,1.07565,1.07577,[  NewYork  ]
2023-12-08,19:50:00,1.07580,1.07600,1.07573,1.07586,[  NewYork  ]
2023-12-08,20:00:00,1.07585,1.07614,1.07570,1.07606,[  NewYork  ]
2023-12-08,20:10:00,1.07607,1.07607,1.07582,1.07607,[  NewYork  ]
2023-12-08,20:20:00,1.07606,1.07653,1.07606,1.07640,[  NewYork  ]
2023-12-08,20:30:00,1.07636,1.07656,1.07629,1.07647,[  NewYork  ]
2023-12-08,20:40:00,1.07646,1.07660,1.07638,1.07646,[  NewYork  ]
2023-12-08,20:50:00,1.07648,1.07648,1.07628,1.07632,[  None  ]
2023-12-08,21:00:00,1.07633,1.07638,1.07623,1.07626,[  None  ]
2023-12-08,21:10:00,1.07627,1.07627,1.07614,1.07614,[  None  ]
2023-12-08,21:20:00,1.07615,1.07632,1.07605,1.07630,[  None  ]
2023-12-08,21:30:00,1.07631,1.07633,1.07616,1.07621,[  None  ]
2023-12-08,21:40:00,1.07622,1.07633,1.07622,1.07627,[  None  ]
2023-12-08,21:50:00,1.07628,1.07635,1.07574,1.07629,[  Wellington  Sydney  ]
2023-12-10,22:00:00,1.07555,1.07589,1.07555,1.07579,[  Wellington  Sydney  ]
2023-12-10,22:10:00,1.07582,1.07595,1.07563,1.07574,[  Wellington  Sydney  ]
2023-12-10,22:20:00,1.07573,1.07580,1.07571,1.07578,[  Wellington  Sydney  ]
2023-12-10,22:30:00,1.07579,1.07613,1.07572,1.07611,[  Wellington  Sydney  ]
2023-12-10,22:40:00,1.07611,1.07616,1.07608,1.07615,[  Wellington  Sydney  ]
2023-12-10,22:50:00,1.07614,1.07616,1.07608,1.07615,[  Wellington  Tokyo  Sydney  ]
2023-12-10,23:00:00,1.07617,1.07674,1.07617,1.07658,[  Wellington  Tokyo  Sydney  ]
2023-12-10,23:10:00,1.07659,1.07664,1.07643,1.07646,[  Wellington  Tokyo  Sydney  ]
2023-12-10,23:20:00,1.07649,1.07665,1.07639,1.07645,[  Wellington  Tokyo  Sydney  ]
2023-12-10,23:30:00,1.07644,1.07654,1.07620,1.07639,[  Wellington  Tokyo  Sydney  ]
2023-12-10,23:40:00,1.07635,1.07646,1.07629,1.07645,[  Wellington  Tokyo  Sydney  ]


 


@rohingosling
Replies

PanagiotisCharalampous
04 Jan 2024, 07:17

Hi there,

I don't see a problem here. An open market session is not a prerequisite to trade some symbols.

Best regards,

Panagiotis


@PanagiotisCharalampous

rohingosling
04 Jan 2024, 10:30 ( Updated at: 04 Jan 2024, 10:32 )

RE: Trading data being reported during periods with no active sessions, i.e. MarketSessions=None.

PanagiotisCharalampous said: 

Hi there,

I don't see a problem here. An open market session is not a prerequisite to trade some symbols.

Best regards,

Panagiotis

Thanks Panagiotis,

Just so that I understand, if there are no open sessions, where is the trading volume coming from?
And the reason I ask, is that intuitively I presume, if there are no open exchanges, like on the weekend ends for example, then I presume there would be no volume coming through. 

Regards,
Rohin


@rohingosling