Recalculating VWAP every X Days or X Hours

Hello AFL programmers:

Here is a code to plot an array that is recalculated every Day Time intervals (in this case VWAP) .
When I change intervals to intraday it Plots array changing every Day.
I would like to actually plot VWAP recalculating every X Days or every X Hours or Every X Minutes independent on timeframe used.
Example1: In timeframe 1 Hour would like to plot VWAP recalculating every 3 Days.
Example2: In timeframe 1 Min would like to plot VWAP recalculating every 30 Minutes.

With this code I can plot VWAP recalculating every Month, Day, Hour or Minute because a function is provided by Amibroker. I just assign variables: NewMonth or NewDay or NewHour or NewMin to "NewTimeUnitChanged".
Example Goal = How do I detect 3 Days changing and assign to "NewTimeUnitChanged".
Example Goal = How do I detect 30 Minutes changing and assign to "NewTimeUnitChanged".

My problem with this code is not about the VWAP array. The problem is more of how to recalculate VWAP every 3 Days or 30 Minutes, etc..
Any help will be appreciated.

_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " +WriteVal( V, 1.0 ) +" {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 )) ));
Plot( C, "", colorDefault, styleCandle  ); 

//==============================================================
//   VWAP
//==============================================================

//Get diferent Time Units Changes
NewMonth = Month() != Ref(Month(), -1);
NewDay = Day() != Ref(Day(), -1);
NewHour = Hour() != Ref(Hour(), -1);
NewMin = Minute() != Ref(Minute(), -1);

NewTimeUnitChanged = NewDay;
//VWAP starts every interval of time in var = NewTimeUnitChanged
Bars_so_far_today = 1 + BarsSince( NewTimeUnitChanged);
TodayVolume = Sum(V,Bars_so_far_today);
VWAP = Sum (C * V, Bars_so_far_today  ) / TodayVolume;
Plot (VWAP,"VWAP",colorYellow, styleThick);


// Mark when Time Unit changed
Value1=0;
Value1 = ValueWhen(NewTimeUnitChanged, C);
Value1 = IIf(Value1!=0,Value1,Ref(Value1,-1));
Plot (Value1,"",colorWhite, styleline);

Use interval function to define NewTimeUnitChanged

http://www.amibroker.com/guide/afl/interval.html

2 Likes

Thanks, I will try to use interval.
Example I see with this:
With interval I can find current interval like Hours and calculate 3 Days then recalculate VWAP.

Steps for recalculate VWAP every 3 Days with Interval = Hourly:
-Use Interval function to know if chart is in Hour interval
-For Hours interval I would calculate 3 Days = (3x24= 72Hours).
-Then find in 72 bars in the past what is the 1rst bar of that Day.
-Use that Hourly bar to recalculate VWAP in Interval = Hourly.
Let me try to code that and then I will share my progress

If you have other idea please share.

Take a look at functions

CUM( NewTimeUnitChanged )
and
ValueWhen()

Just a question - are you trying to plot a n "AVWAP" from m multiple levels, I am also interested

you guys are masters at coding, kindly help us and write a code which we may use,
tons of thanks

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