To obtain a period’s open time, we can use either
CopyTime()
https://www.mql5.com/en/docs/series/copytime
or
SymbolInfoInteger()
https://www.mql5.com/en/docs/marketinformation/symbolinfointeger
I’ve attached a script testing the speed of each showing that SymbolInfoInteger() is faster than CopyTime() by 50%, even after optimisation.
//#property script_show_inputs input uint loops = 1000000; void OnStart(){ uint start = 0; //Method 1: CopyTime() start = GetTickCount(); datetime LastPeriodOpenTime_Method1 = 0; for(uint i=loops; i > 0; i--){ datetime Time[]; CopyTime(_Symbol, _Period, 0, 1, Time); //starting from current period(0), copy one(1) period's open time to Time[] if(LastPeriodOpenTime_Method1 != Time[0])LastPeriodOpenTime_Method1 = Time[0]; //update LastPeriodOpenTime } Print("CopyTime() method: ", GetTickCount() - start); //~750 //Method 2: SeriesInfoInteger() start = GetTickCount(); datetime LastPeriodOpenTime_Method2 = 0; for(uint i=loops; i > 0; i--){ datetime CurrentPeriodOpenTime = (datetime)SeriesInfoInteger(_Symbol, _Period, SERIES_LASTBAR_DATE); //get current period's open time if(LastPeriodOpenTime_Method2 != CurrentPeriodOpenTime)LastPeriodOpenTime_Method2 = CurrentPeriodOpenTime; //update LastPeriodOpenTime } Print("SeriesInfoInteger() method: ", GetTickCount() - start); //~406 //Method 1A optimised: CopyTime() start = GetTickCount(); datetime LastPeriodOpenTime_Method1A = 0; datetime Time1A[]; //place declaration of Time[] outside of loop for(uint i=loops; i > 0; i--){ CopyTime(_Symbol, _Period, 0, 1, Time1A); if(LastPeriodOpenTime_Method1A != Time1A[0])LastPeriodOpenTime_Method1A = Time1A[0]; } Print("CopyTime() method 1A optimised: ", GetTickCount() - start); //~625 //Method 1B optimised: CopyTime() start = GetTickCount(); datetime LastPeriodOpenTime_Method1B = 0; datetime Time1B[1]; //made array static for(uint i=loops; i > 0; i--){ CopyTime(_Symbol, _Period, 0, 1, Time1B); if(LastPeriodOpenTime_Method1B != Time1B[0])LastPeriodOpenTime_Method1B = Time1B[0]; } Print("CopyTime() method 1B optimised: ", GetTickCount() - start); //~625 //Method 1C optimised: CopyTime() start = GetTickCount(); datetime LastPeriodOpenTime_Method1C = 0; datetime Time1C[1]; //made array static datetime temp_Time1C = 0; for(uint i=loops; i > 0; i--){ CopyTime(_Symbol, _Period, 0, 1, Time1C); temp_Time1C = Time1C[0]; //assign Time1C[0] to temp_Time1C, save 1 calling of Time1C[] if(LastPeriodOpenTime_Method1C != temp_Time1C)LastPeriodOpenTime_Method1C = temp_Time1C; } Print("CopyTime() method 1C optimised: ", GetTickCount() - start); //~625 }
Just throwing it out there.
Discussion
No comments yet.