[Advanced Topic] Anything Tricky with dates and times

Read before posting.
bowlesj3
Posts: 2028
Joined: 21 Jul 2007
Has thanked: 174 times
Been thanked: 405 times

[Advanced Topic] Anything Tricky with dates and times

Postby bowlesj3 » 22 May 2012

I find MC EL code regarding dates and times far too difficult.
It is not suppose to be that way.
Here are a few tricks.

These explain themselves. The variables get loaded with standard EL date and time format.

Code: Select all

LeftSideOfScreenDate = JulianToDate(GetAppInfo(aiLeftDispDateTime));    
LeftSideOfScreenTime = DateTime2ELTime(GetAppInfo(aiLeftDispDateTime));

RightSideOfScreenDate = JulianToDate(GetAppInfo(aiRightDispDateTime));    
RightSideOfScreenTime = DateTime2ELTime(GetAppInfo(aiRightDispDateTime));


For adding and subtracting seconds this link has a useful function.
viewtopic.php?f=5&t=6862

Although the function in the link above can add and subtract minutes the more common way and possibly the faster way is shown below. Of course you can add or subtract.

Code: Select all

1MinuteBarChartTime3MinutesAheadOfLastBar = MinutesToTime(TimeToMinutes(Time) + 3);



Minutes between bars (after you have put the bar times in NewTime and OldTime).

Code: Select all

MinutesBetween = TimeToMinutes(NewTime) - TimeToMinutes(OldTime);


Finding the date before the current date or 3 days before the current date

Code: Select all

DateBeforeCurrentDate = JulianToDate(DateToJulian(CurrentDate) - 1);
Date3daysBeforeCurrentDate = JulianToDate(DateToJulian(CurrentDate) - 3);


Finding the date after today (future date)

Code: Select all

TomorrowsDate = JulianToDate(DateToJulian(CurrentDate) + 1);
Last edited by bowlesj3 on 24 Apr 2013, edited 7 times in total.

bowlesj3
Posts: 2028
Joined: 21 Jul 2007
Has thanked: 174 times
Been thanked: 405 times

Re: Anything Tricky with dates and times

Postby bowlesj3 » 14 Aug 2012

If you want something to occur a certain number of seconds into the bar here is a way to get that to occur. There is a seconds version and a minutes version. There is a parameter in each for the number of seconds in your bar and a parameter for the second you want it to occur. See the comments for more details.

This is the tricky command it demonstrates.
if Mod(SecondsfromDateTime(ComputerDateTime),SecondsInTheBar) > SecondsToSound then


Code: Select all

Inputs:
   SecondsInTheBar(60), {Enter the number of seconds that are contained in the bar}
   SecondsToSound(45); {Enter the second where you want to code to test for to make the sound - it sounds on the next second}
   

Variables:
   IntraBarPersist PreviousTime(0),
   IntraBarPersist SoundOccured("N");
 
 
 
If LastBarOnChart then
   begin
   if time <> PreviousTime then  {BarStatus=0 can also be tested since it appears to work now}
      begin
      SoundOccured = "N";
        PreviousTime = time;
       end;
   if Mod(SecondsfromDateTime(ComputerDateTime),SecondsInTheBar) > SecondsToSound then
      begin
      if SoundOccured = "N" then
         begin
         SoundOccured = "Y";
          Playsound("C:\WINDOWS\Media\Windows XP Battery Critical.wav");
         end;
      end;
    RecalcLastBarAfter(1); {Force a fake tick every second}
    end;



Code: Select all

Inputs:
   SecondsInTheBar(10), {Enter the number of seconds that are contained in the bar}
   SecondsToSound(8); {Enter the second where you want to code to test for to make the sound - it sounds on the next second }
   

Variables:
   IntraBarPersist PreviousTime(0),
   IntraBarPersist SoundOccured("N");

If LastBarOnChart_s then
   begin
   if time_s <> PreviousTime then {BarStatus=0 can also be tested since it appears to work now}
      begin
      SoundOccured = "N";
        PreviousTime = time_s;
       end;
   if Mod(SecondsfromDateTime(ComputerDateTime),SecondsInTheBar) > SecondsToSound then
      begin
      if SoundOccured = "N" then
         begin
         SoundOccured = "Y";
           Playsound("C:\WINDOWS\Media\Windows XP Battery Critical.wav");
         end;
      end;
    RecalcLastBarAfter(1); {Force a fake tick every second}
    end;

bowlesj3
Posts: 2028
Joined: 21 Jul 2007
Has thanked: 174 times
Been thanked: 405 times

Re: Anything Tricky with dates and times

Postby bowlesj3 » 08 Jan 2013

Here is a function with a test script that can take any pair of old and new date/time_s and calculate the number of days between or minutes between or seconds between. the test script is below. The test script and the function are included in the zip file. After about 1.5 hours I ran out of time to test it but I got in 3 tests (one for days, minutes and seconds). If there is a problem let me know.

Note if you want to submit time in minutes you have to convert it to seconds format. For example 1001 would become 100100.


Code: Select all

Inputs:
   DiffType("S"), {D=Days between, M=MinutesBetween, S=SecondsBetween}
    OldDate(1130110),
    OldTime_s(93830),
    NewDate(1130110),
    NewTime_s(94024);
   
Variables:
   TextID(0),
   Intrabarpersist AlreadyDisplayed("N"),
   Intrabarpersist Between(0);   
   
If currentbar = 1 then
   begin
   TextID = text_New(date,time,close,"");
    end;

   
If lastbarOnChart then
   begin
   if AlreadyDisplayed = "N" then
      begin
      AlreadyDisplayed = "Y";
      Between = A_DateTime_Between(DiffType, OldDate, Oldtime_s, NewDate, NewTime_s);
      value1 = Text_SetLocation(TextID,date,time,close+1);
      value1 = Text_SetString(TextID,numtostr(Between,5));
       end;
   end;


Code: Select all

Inputs:
   DiffType(stringSimple), {D=Days between, M=MinutesBetween, S=SecondsBetween}
    OldDate(NumericSimple),
    OldTime_s(NumericSimple),
    NewDate(NumericSimple),
    NewTime_s(NumericSimple);
     
Variables:
   MinutesInOneDay(1440), {Minutes in one day}
   SecondsInOneDay(86400), {Seconds In one day}
   OneHour(1/24), {1 divided by the number of hours in a day}
   OneMinute(1/1440), {1 divided by the number of minutes in a day}
   OneSecond(1/86400), {1 divided by the number of seconds in a day}

   MyJulianDateOld(0),
   MyJulianTimeOld_m(0),
   MyJulianTimeOld_s(0),
   MyJulianDateTimeOld_m(0),
   MyJulianDateTimeOld_s(0),
   MySecondsCountOld(0),

   MyJulianDateNew(0),
   MyJulianTimeNew_m(0),
   MyJulianTimeNew_s(0),
   MyJulianDateTimeNew_m(0),
   MyJulianDateTimeNew_s(0),
   MySecondsCountNew(0),
 
   DaysBetween(0),
   SecondsBetween(0),
   MinutesBetween(0);

MyJulianDateOld = ELDateToDateTime(OldDate);
MyJulianDateNew = ELDateToDateTime(NewDate);

if DiffType = "D" then
   begin
   DaysBetween = MyJulianDateNew - MyJulianDateOld;
    A_DateTime_Between = DaysBetween;   
   end;
 if DiffType = "M" then
   begin
   MyJulianTimeOld_m = ELTimeToDateTime(OldTime_s / 100); {divide by 100 to calc time in minutes format}
   MyJulianDateTimeOld_m = (MyJulianDateOld * MinutesInOneDay) + (MyJulianTimeOld_m / OneMinute);
   MyJulianTimeNew_m = ELTimeToDateTime(NewTime_s / 100); {divide by 100 to calc time in minutes format}
   MyJulianDateTimeNew_m = (MyJulianDateNew * MinutesInOneDay) + (MyJulianTimeNew_m / OneMinute);
   MinutesBetween = MyJulianDateTimeNew_m - MyJulianDateTimeOld_m;   
    A_DateTime_Between = MinutesBetween;
   end;
if DiffType = "S" then
   begin
   MyJulianTimeOld_s = ELTimeToDateTime_s(OldTime_s);
   MySecondsCountOld = MyJulianTimeOld_s / OneSecond;
   MyJulianDateTimeOld_s = (MyJulianDateOld * SecondsInOneDay) + MySecondsCountOld;
   MyJulianTimeNew_s = ELTimeToDateTime_s(NewTime_s);
   MySecondsCountNew = MyJulianTimeNew_s / OneSecond;
   MyJulianDateTimeNew_s = (MyJulianDateNew * SecondsInOneDay) + (MySecondsCountNew);
   SecondsBetween = MyJulianDateTimeNew_s -  MyJulianDateTimeOld_s;
   A_DateTime_Between = SecondsBetween;
   end;
You do not have the required permissions to view the files attached to this post.
Last edited by bowlesj3 on 12 Jan 2013, edited 7 times in total.

arjfca
Posts: 1273
Joined: 23 Nov 2010
Has thanked: 721 times
Been thanked: 222 times

Re: Anything Tricky with dates and times

Postby arjfca » 10 Jan 2013

bowlesj3 wrote:Here is a function with a test script that can take any pair of old and new date/time_s and calculate the number of days between or minutes between or seconds between. the test script is below. The test script and the function are included in the zip file. After about 1.5 hours I ran out of time to test it but I got in 3 tests (one for days, minutes and seconds). If there is a problem let me know.

Note if you want to submit time in minutes you have to convert it to seconds format. For example 1001 would become 100100.


Code: Select all

Inputs:
   DiffType("M"), {D=Days between, M=MinutesBetween, S=SecondsBetween}
    OldDate(1130108),
    OldTime_s(100000),
    NewDate(1130108),
    NewTime_s(110000);
   
Variables:
   Between(0);   
   
   
If lastbarOnChart then
   begin
   Between = A_DateTime_Between(DiffType, OldDate, Oldtime_s, NewDate, NewTime_s);
   value1 = text_New(date,time,close,numtostr(Between,5));
   end;


Hello John

... Only If you got a minute.....

I got a wrong value return by your function. Any idea?
once print ("Test 518: ", A_DateTime_Between( "S",1130110, 93830, 1130110, 94024));

The return value is : Test 518: 120.00

Bar old = 093830 09:38:30
Bar New= 094024 09:40:24

Difference should be :1 minute 54 seconds or 114 seconds

Martin :)

bowlesj3
Posts: 2028
Joined: 21 Jul 2007
Has thanked: 174 times
Been thanked: 405 times

Re: Anything Tricky with dates and times

Postby bowlesj3 » 10 Jan 2013

Hi Martin,

I found the problem and fixed it. I uploaded the new function in the original post (along with a slightly different test script). Please give it another run and let me know. My original test did not find the problem because I chose even numbers that matched the 1 minute bars using two different dates and made easy to figure out for me (I was concerned about calculations crossing over different dates). The fix was to use the "_s" output Julian field during the divide statement rather than the field which was for the 1 minute calculation.

The new test script will not keep creating new text every new bar. It creates the text on the first bar and just moves it into place while also updating the text result.


Thanks,
John
Last edited by bowlesj3 on 10 Jan 2013, edited 4 times in total.

User avatar
TJ
Posts: 6586
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 971 times
Been thanked: 1907 times

Re: Anything Tricky with dates and times

Postby TJ » 10 Jan 2013

Look for an indicator name "Tick Speedometer"
It will show you how to calculate the time between the 2 bars.
http://www.tradersxchange.com/viewforum.php?f=51

bowlesj3
Posts: 2028
Joined: 21 Jul 2007
Has thanked: 174 times
Been thanked: 405 times

Re: Anything Tricky with dates and times

Postby bowlesj3 » 11 Jan 2013

Since no one has download the corrected "date/time difference " function from post #4 above I took the time to clean up the code. I also put the code directly in post #4 so it can be studied without actually downloading it. I also made it much more self documenting. It provides a good example for the discussion below.

In the "EL debugging (trouble shooting) guide" thread which can be found at this link
viewtopic.php?f=16&t=10397
it mentions that to help reduce bugs one should try not to rush and "take the time" to make variable names "Self Documenting". The code in post #4 is a perfect example. I originally rushed writing the function and used the variable names

Code: Select all

MyJulianTimeOld(0),  {minutes version}
MyJulianTimeOld_s(0),  {seconds version}

I they are now much more self documenting with the "_m" on the end as shown below.

Code: Select all

MyJulianTimeOld_m(0),  {minutes version}
MyJulianTimeOld_s(0),  {seconds version}


The bug in the code never would have occurred had I use the second naming format with the "_m" at the end for the minutes version. Not only that it makes it easier to understand when one comes back to it a few years later. If others are using the code then making names self documenting becomes even more important. Unfortunately (as everyone knows) being in a rush is often a reality. It happens, as they say.

Of note, I did a few extra tests this time. I tested Martin's test again and I did some across date testing. Not a huge amount of testing however, so it is still worth doing a few checks on the initial calculations when you first start using it. I am not actually using this function. I just had this urge to write it - LOL. That is why there is not a huge amount of testing here.

bowlesj3
Posts: 2028
Joined: 21 Jul 2007
Has thanked: 174 times
Been thanked: 405 times

Re: [Advanced Topic] Anything Tricky with dates and times

Postby bowlesj3 » 03 Mar 2013

During currentbar=1 you can determine the date and time of LastBarOnChart using LastCalcDate and LastCalcTime.

Below is an example.

Code: Select all

inputs:
   Price( Close ),
   Length( 14 ),
   HoursBackToPlaceArrows(3),
   DaysBackToPlaceArrows(3);

Variables:
   Arrows_Placement_Time(0),
   Arrows_Placement_Date(0);

if currentbar = 1 then
   begin
   Arrows_Placement_Date = JulianToDate(DateToJulian(LastCalcDate) - DaysBackToPlaceArrows);
   Arrows_Placement_Time = MinutesToTime(TimeToMinutes(LastCalcTime) - HoursBackToPlaceArrows * 60);
   end;

if Date >= Arrows_Placement_Date then
   Begin
   {code to place your arrows properly if you are not worried about no trading weekends}
   End;
if Date = CurrentDate and time >= Arrows_Placement_Time then
   Begin
   {code to place your arrows properly}
   End;


If you were squeezing up the bars and highlighting the arrows to grab the data from that location (using MC_Arw_GetActive and a binary search which can be found in the usercontributed studies) you would need to have MaxBarsBack large enough to go back that far. However if you are just placing arrows for visiual reference I do not think there is any need to have the MaxBarsBack that large.


Return to “MultiCharts FAQ”