Loop through values in string variable and check with current date

 

Hi Folks!

Facing an issue when trying to run the code attached via backtest. Getting a array out of bounds error and a failure of the backtest saying that a critical error had ocurred in the pass. Debugging does not show this error for some reason.

NoTradeOnDates is a variable containing a comma delimited set of dates such as 2015.01.05,2015.01.07

My objective is to split that variable based on the comma into an array and to loop through the array to verify whether any of the date matches the current date. If it does match, set TradeToday to false and break from the loop.

Basically I am deriving the current date in the following format YYYY.MM.DD and doing the comparison with the values in the array.

This code is within a void OnTick() function. Not sure whether this might be the reason.

Appreciate your expertise on this and thanks in advance! 

hisham 

   bool TradeToday = true;
   
   string CurrentYear = IntegerToString(Year(),4,"");
   string CurrentMonth = IntegerToString(Month(),2,"0");
   string CurrentDay = IntegerToString(Day(),2,"0");
   string TodayDate = StringConcatenate(CurrentYear,CurrentMonth,CurrentDay);
        
   // Split provided list of dates (comma seperated) into an array
   string ListOfDates[];
   string sep=",";
   ushort u_sep=StringGetCharacter(sep,0);
   int k=StringSplit(NoTradeOnDates,u_sep,ListOfDates);
   
   for (int i = 0; i <= ArraySize(ListOfDates); i++){
     if (ListOfDates[i] == TodayDate)
         {
           TradeToday = false;
           break;
         }  
   }
 

Loop index must go from 0 to ArraySize-1. Instead of "<=" there must be "<" 


for (int i = 0; i < ArraySize(ListOfDates); i++){
     if (ListOfDates[i] == TodayDate)
         {
           TradeToday = false;
           break;
         }  
   }

 

 Also, print " TodayDate" variable value - as it seems it does not include "." symbol. 

 
  1. You have not shown the contents of "NoTradeOnDates", so without it we can only speculate what may be the result.
  2. Also, you are not testing the result of "StringSplit()", namely "k", to see if there was any problem and to see how many items were split.
  3. In your "for" loop, you are using the comparison "i <= ArraySize(ListOfDates)" when it should be "i < ArraySize(ListOfDates)" because if "ArraySize()" returns "0" it means that the array is empty and not that it has a 0th element.
 

drazen64 - TodayDate includes the . and i have verified that by adding the variable to watch during debugging. Will make the modification you had suggested and let you know how it goes.

FMIC -

(a) I have mentioned what NoTradesOnDates contain -> NoTradeOnDates is a variable containing a comma delimited set of dates such as 2015.01.05,2015.01.07

(b) Based on watching the variable k. It shows me the number 2 which indicates that there are two splits based on the string value in NoTradeOnDates (example given above). So this is correct and yes you are right. I should ideally be testing the variable to see whether the split has happened correctly. Will correct the code.

(c) drazen64 had mentioned this too and i am looking to have this modification made.

 Will update you guys and thanks loads for your replies!

Hisham 

 
hishamzz:

drazen64 - TodayDate includes the . and i have verified that by adding the variable to watch during debugging. Will make the modification you had suggested and let you know how it goes.

FMIC -

(a) I have mentioned what NoTradesOnDates contain -> NoTradeOnDates is a variable containing a comma delimited set of dates such as 2015.01.05,2015.01.07

(b) Based on watching the variable k. It shows me the number 2 which indicates that there are two splits based on the string value in NoTradeOnDates (example given above). So this is correct and yes you are right. I should ideally be testing the variable to see whether the split has happened correctly. Will correct the code.

(c) drazen64 had mentioned this too and i am looking to have this modification made.

 Will update you guys and thanks loads for your replies!

Hisham 

(a) Yes, you said, but sometimes things go wrong in code, so you should have a Print() statement to show its contents to make sure of this (standard debug practice).

(b) Always check the return value. You say it is 2 but your code does not have any such testing being done. Change your code so as to always test that before even attempting to look at the array contents.

 
hishamzz:

drazen64 - TodayDate includes the . and i have verified that by adding the variable to watch during debugging. Will make the modification you had suggested and let you know how it goes.

FMIC -

(a) I have mentioned what NoTradesOnDates contain -> NoTradeOnDates is a variable containing a comma delimited set of dates such as 2015.01.05,2015.01.07

(b) Based on watching the variable k. It shows me the number 2 which indicates that there are two splits based on the string value in NoTradeOnDates (example given above). So this is correct and yes you are right. I should ideally be testing the variable to see whether the split has happened correctly. Will correct the code.

(c) drazen64 had mentioned this too and i am looking to have this modification made.

 Will update you guys and thanks loads for your replies!

Hisham 

 

drazen64/FMIC - I have modified the operator to < in the for statement and its working fine! Will be taking your advice on testing the k variable prior to running the loop FMIC. Thanks again! 

 Hisham 

Reason: