REF(Day(),-1) and REF(Day(),-2)

I am getting same day for REF(Day(),-1) and REF(Day(),-2) and REF(Day(),-3), Is Assumption correct that it returns previous day , previous to previous day .so on.

Not correct assumption.
it returns the Day() for the bar.
If you are in 10 min bars, -1,-2,-3 would most likely all be from the same Day and therefore the same values.

1 Like

Ok thanks, in case of daily time frame, will it refer to previous day

To get better understanding of what is happening in your code and how functions work, use advice given here: How do I debug my formula?

How this works for find new day newday = Day() != Ref(Day(), -1);

There are several ways. Real simple is this ....

datNum = DateNum();
new_day = datNum != Ref(datNum,-1);

i hope it helps

2 Likes

Thanks you. Yes it helps

If you're intraday and you want to find the first tick of a new price bar then you can do this:

#pragma enable_static_decl "myprefx{chartid}"

static _last_timenum;

if (_last_timenum == 0)
  _last_timenum = LastValue(TimeNum());

if (_last_timenum != LastValue(TimeNum()))
{
  // first tick of new intraday bar
  //
  //  AFL Code goes here
  //
  _last_timenum = LastValue(TimeNum());
}
else
{
  // another tick in the same intraday bar
  //
  //  AFL Code
  // 
}

A reason to do this is when you have intraday signals which can either be based on the close of a price bar (detected on the first tick of a new price bar) or when you can have a signal that depends on a particular value comparison (.e.g., price touching a moving average) which can happen at any time.

3 Likes

Thanks SteveH, Got the logic

Another note. If you're looking at bar close only logic, you ignore the current candle's OHLC until the bar closes. But if you're looking at every tick, the first OHLC you look at is the current unfinished price bar. This also applies to indicators. So here's what you can do with the code as a basis for OHLC comparisons:

#pragma enable_static_decl "myprefx{chartid}"

static _last_timenum;

if (_last_timenum == 0)
  _last_timenum = LastValue(TimeNum());

if (_last_timenum != LastValue(TimeNum()))
{
  open0 = Open[BarCount - 2];
  open1 = Open[BarCount - 3];
  open2 = Open[BarCount - 4];
	
  high0 = High[BarCount - 2];
  high1 = High[BarCount - 3];
  high2 = High[BarCount - 4];
	
  low0 = Low[BarCount - 2];
  low1 = Low[BarCount - 3];
  low2 = Low[BarCount - 4];
	
  close0 = Close[BarCount - 2];
  close1 = Close[BarCount - 3];
  close2 = Close[BarCount - 4];

  // first tick of new intraday bar
  //
  //  AFL Code goes here
  //
  _last_timenum = LastValue(TimeNum());
}
else
{
  open0 = Open[BarCount - 1];
  open1 = Open[BarCount - 2];
  open2 = Open[BarCount - 3];
	
  high0 = High[BarCount - 1];
  high1 = High[BarCount - 2];
  high2 = High[BarCount - 3];
	
  low0 = Low[BarCount - 1];
  low1 = Low[BarCount - 2];
  low2 = Low[BarCount - 3];
	
  close0 = Close[BarCount - 1];
  close1 = Close[BarCount - 2];
  close2 = Close[BarCount - 3];

  // another tick in the same intraday bar
  //
  //  AFL Code
  // 
}

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.