[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [amibroker] DaysBetweenDates()



PureBytes Links

Trading Reference Links

I had similar question about comparing DateTime formatted values to determine the days between them.
I wanted to be able to tell if it had been more than 30 days since I exited a stock before taking an entry signal
in the same stock.

This is the elegant solution Marcin Gorznski at Amibroker Support supplied.

All I did here was convert the 'dt' array in Marcin's solution from DateTime() to DateNum().

dt = DateNum();
dy = DayOfYear() + 365 * Year();

fb = Status("rangefromdate");
eb = Status("rangetodate");

Days = LastValue(ValueWhen( dt == eb, dy ) - ValueWhen( dt == fb, dy ));

Plot(days,"days",colorBlack,styleLine);

If you really are comparing the start/end dates of a backtest be sure your 'to date' is not past the last day in the data
or this won't work because the dt array stops at the last bar.

The solution doesn't deal with leap years, but I could live with the possibilty of missing a trade or two every 4 years so 
I didn't attempt to add that.

Regards,

Bob Johnson

----- Original Message ----- 
  From: Mr. Valley 
  To: amibroker@xxxxxxxxxxxxxxx 
  Sent: Tuesday, October 03, 2006 7:38 PM
  Subject: [amibroker] DaysBetweenDates()



  I'm having trouble with calculating the number of days between the dates of
  two conditions, either daily or intraday intervals. I looked at the
  DeDateTime.dll
  These were planned functions, but I don't see them implemented.
  GetDaysBetween() and GetTradeDaysBetween()

  Also tried...

  /* Days Between Dates v3 */

  price = Close;
  sig = MA(High,20);
  Cond1 = Cross(price,sig);
  Cond2 = Cross(sig,price);

  startDay = LastValue( ValueWhen( Cond1 , 1 ) );
  endDay = LastValue( ValueWhen( Cond2 , 1 ) );
  Daysbetween = abs(endDay - startDay );
  Plot(Close,"Close",1,1);
  Plot(sig,"sig",2,1);
  Plot(Daysbetween ,"Daysbetween ",10,1 | styleOwnScale);

  //////////////
  Also Tried this and wasn't able to resolve it to what I am trying to do.

  /* Days Between dates v2 */
  SetBarsRequired(1000,0);
  function GetDaysInRange()
  { fb = Status("firstbarinrange");
  eb = Status("lastbarinrange");
  startday = LastValue( ValueWhen( fb, DayOfYear() ) );
  startyear = LastValue( ValueWhen( fb, Year() ) );
  endDay = LastValue( ValueWhen( eb, DayOfYear() ) );
  endYear = LastValue( ValueWhen( eb, Year() ) );
  Days = 365 * ( endYear - startyear ) + endDay - StartDay;
  return Days;
  }""+GetDaysInRange();

  //Title = "" + GetDaysInRange();
  Plot(Close,"Close",1,1);
  Plot(GetDaysInRange(),"GetDaysInRange()",1,1);

  Any Suggestions?
  Thanks in advance.

  Mr Valley