How to Add "From" and "to" Dates

Created at 23 Mar 2023, 14:06
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!
CT

ctid5083541

Joined 25.05.2022

How to Add "From" and "to" Dates
23 Mar 2023, 14:06


How can I add a start and an end date to run the indicator. The problem is my indicator is very slow if it searches too far back, and I would like to manually back test a strategy. I have the from date but I can't get a to date. For example, I want to only look at December's data or only December 1 2022.

Here's some code that I have:

if (DateTime.TryParseExact(FromStringDate, "dd/mm/yyyy", new CultureInfo("en-US"), DateTimeStyles.None, out FromDateTime))
                {
                    if (FromDateTime > Server.Time.Date)
                    {
                        // for Log
                        FromDateTime = Server.Time.Date;
                        Print ($"Invalid DateTime '{FromStringDate}'. Using '{FromDateTime}'");
                    }
                }

Any help or advice would be greatly appreciated.


@ctid5083541
Replies

firemyst
27 Mar 2023, 16:25

You can try doing:

 


//Pseudo code

//Get the start index you want to search from
int startIndex = Bars.OpenTimes.GetIndexByTime( <enter your start date/time value here> );

//get the end index corresponding to the ending date/time you want
int endIndex = Bars.OpenTimes.GetIndexByTime( <enter your end date/time value here> );

//Now go through every bar
for (int x=startIndex; x <= endIndex; x++)
{
  // put your logic here that you want to do
}

 


@firemyst

ctid5083541
01 Apr 2023, 13:46

This code seems to make sense but I couldn't get it working. This may be due to the fact that I have my FromDateTime in a function - I should have stated this in the first post, my appologies.


@ctid5083541

ctid5083541
03 Dec 2023, 21:02

RE: How to Add "From" and "to" Dates

firemyst said: 

You can try doing:

 

//Pseudo code//Get the start index you want to search fromint startIndex = Bars.OpenTimes.GetIndexByTime( <enter your start date/time value here> );//get the end index corresponding to the ending date/time you wantint endIndex = Bars.OpenTimes.GetIndexByTime( <enter your end date/time value here> );//Now go through every barfor (int x=startIndex; x <= endIndex; x++){  // put your logic here that you want to do}

What is the date/time value? :

int endIndex = Bars.OpenTimes.GetIndexByTime( “01/01/2023” );

I have tried with and without quotes. I even tried the code below, but I still have the problem of assigning values to MyStartDate and MyEndDate. I keep getting an error no matter what I try. I suppose it is a case of understanding the syntax, although I have read about it. Most examples are for times not dates.

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

namespace cAlgo
{
    [Indicator(AccessRights = AccessRights.None)]
    public class MyDates : Indicator
    {
        
        private readonly DateTime MyStartDate;
        private readonly DateTime MyEndDate;
        
        protected override void Initialize()
        {
            

            //Get the start index you want to search from
            int startIndex = Bars.OpenTimes.GetIndexByTime(MyStartDate);
            
            //get the end index corresponding to the ending date/time you want
            int endIndex = Bars.OpenTimes.GetIndexByTime(MyEndDate);
            
            //Now go through every bar
            for (int x=startIndex; x <= endIndex; x++)
            {
              // put your logic here that you want to do
            }
                    }
            
                    public override void Calculate(int index)
                    {
                        
                    }
    }
}

 

 


@ctid5083541