AFL Library
This is read-only version of AFL library entry. Ability to add user formulas
and comment is only available from members-only area.
Details:
Formula name:
VWAP versus Average Price
Author/Uploader:
Edward Pottasch - empottasch [at] skynet.be
Date/Time added:
2010-03-06 07:20:13
Origin:
bits and pieces here and there
Keywords:
Level:
medium
Flags:
indicator
DISCLAIMER: Most formulas present in AFL on-line library are submitted
by the users and are provided here on an "as is" and "as available" basis.
AmiBroker.com
makes no representations or warranties of any kind to the contents or the operation
of material presented here. We do not maintain nor provide technical support
for 3rd party formulas.
Description:
you need intraday data for this code to display, for instance intraday ES data. The chart will show the VWAP and the Average price inside the price chart. I started calculating this to be able to use the result to adjust daily pivot lines (which are calculated using price only). This is test code I thought might be interesting.
Formula:
starttime = 093000;
endtime = 160000;
timearray = TimeNum() >= starttime AND TimeNum() <= endtime;
firstBarOfDay = TimeNum() >= starttime ;
firstBarOfDay = firstBarOfDay - Ref(firstBarOfDay,-1);
vv = V * C * timearray;
cc = C * timearray;
BarsInDay = (1 + BarsSince(firstBarOfDay)) * timearray;
CumVolumeInDay = Sum(V,BarsInDay);
VWAP = Sum(vv, BarsInDay ) / CumVolumeInDay;
AVP = Sum(cc, BarsInDay ) / BarsInDay;
SetChartBkColor( ParamColor("ColorBG", ColorRGB( 0, 0, 0 ) ) );
SetChartOptions( 0, chartShowDates);
Plot(C,"\nClose",colorWhite,64);
Plot(IIf(VWAP,VWAP,Null),"\nVWAP",ParamColor("ColorVWAP",ColorRGB(12,255,62)),styleLine);
Plot(IIf(AVP,AVP,Null),"\nAVP",ParamColor("ColorAVP",ColorRGB(120,25,62)),styleLine);
Plot(timearray, "",ParamColor("ColorBK",ColorRGB(40,40,40)),
styleArea|styleOwnScale,0,1);
SetBarsRequired(sbrAll,sbrAll);
Comments:
Edward Pottasch empottasch [at] skynet.be 2010-03-07 04:00:29adding to this more general VWAP code. Don\\\'t ask me what it can be used for :)
If you want it to calculate over the entrire day use e.g. startime = 000500; endtime = 235500; in a 5-min chart.
Also code will not plot because when posting here this software adds /// inside the plot statements. These need to me removed manually.
starttime = 093000;
endtime = 160000;
timeWindow = TimeNum() >= starttime AND TimeNum() <= endtime;
firstBarOfDay = TimeNum() >= starttime ;
firstBarOfDay = firstBarOfDay - Ref(firstBarOfDay,-1);
P = (H + L)/2;
vv = V * P * timeWindow;
BarsInDay = (1 + BarsSince(firstBarOfDay)) * timeWindow;
CumVolumeInDay = Sum(V,BarsInDay);
VWAP = Sum(vv, BarsInDay ) / CumVolumeInDay;
SV = Sum((P - VWAP)^2,BarsInDay) / BarsInDay; // sample variance
SD = sqrt(SV); // standard deviation
// chart
SetChartBkColor( ParamColor(\\\"ColorBG\\\", ColorRGB( 0, 0, 0 ) ) );
SetChartOptions(0, chartShowDates);
Plot(C,\\\"\\\\nClose\\\",colorWhite,64);
Plot(IIf(VWAP,VWAP,Null),\\\"\\\\nVWAP\\\",ParamColor(\\\"ColorVWAP\\\",ColorRGB(255,20,147)),styleThick);
Plot(IIf(VWAP,VWAP + SD,Null), \\\"\\\\n+1SD\\\", ParamColor(\\\"ColorSDP\\\",ColorRGB(238,174,238)),styleDashed);
Plot(IIf(VWAP,VWAP - SD,Null), \\\"\\\\n-1SD\\\", ParamColor(\\\"ColorSDM\\\",ColorRGB(210,180,140)),styleDashed);
Plot(IIf(VWAP,VWAP + 2*SD,Null), \\\"\\\\n+2SD\\\", ParamColor(\\\"ColorSDPP\\\",ColorRGB(153,102,204)),styleDashed);
Plot(IIf(VWAP,VWAP - 2*SD,Null), \\\"\\\\n-2SD\\\", ParamColor(\\\"ColorSDMM\\\",ColorRGB(238,154,73)),styleDashed);
Plot(IIf(VWAP,VWAP + 3*SD,Null), \\\"\\\\n+3SD\\\", ParamColor(\\\"ColorSDPPP\\\",ColorRGB(128,0,128)),styleDashed);
Plot(IIf(VWAP,VWAP - 3*SD,Null), \\\"\\\\n-3SD\\\", ParamColor(\\\"ColorSDMMM\\\",ColorRGB(238,118,33)),styleDashed);
Plot(timeWindow, \\\"\\\",ParamColor(\\\"ColorBK\\\",ColorRGB(20,20,20)), styleArea|styleOwnScale,0,1);
reinsley 2010-05-14 05:16:29To plot something, as said in the comment :
open market hour + 5 min
close market hour - min
Nota : VWAP means Volume Weighted Average Price
reinsley 2010-05-14 05:17:23To plot something, as said in the comment :
open market hour + 5 min
close market hour - 5 min
Nota : VWAP means Volume Weighted Average Price
soni67c 2010-07-23 04:02:31Hello Edward ,
this is just modification from your VWAP code to ATP and added Y\\\'days Last values.. on current day to identify zones [ like.. R1,R2 , S1,S2 ]
//Coded by Edward Pottasch
SetBarsRequired(sbrAll,sbrAll);
starttime = 090100;
endtime = 153100;
SetChartBkColor( ParamColor(\\\"ColorBG\\\", ColorRGB( 0, 0, 0 ) ) );
SetChartOptions(0, chartShowDates);
Plot(C,\\\"\\\\nClose\\\",colorWhite,64);
timearray = TimeNum() >= starttime AND TimeNum() <= endtime;
firstBarOfDay = TimeNum() >= starttime ;
firstBarOfDay = firstBarOfDay - Ref(firstBarOfDay,-1);
cc = C * timearray;
BarsInDay = (1 + BarsSince(firstBarOfDay)) * timearray;
AVP = Sum(cc, BarsInDay ) / BarsInDay;
SV = Sum((Cc - AvP)^2,BarsInDay) / BarsInDay;
SD = sqrt(SV);
EndSeesion = Day() != Ref(Day(), 1) ;
Plot(ValueWhen(EndSeesion,AvP + 3*SD), \\\"\\\\n+3R\\\", colorGreen,128+16);
Plot(ValueWhen(EndSeesion,AvP + 2*SD), \\\"\\\\n+2R\\\", colorSeaGreen,128+16);
Plot(ValueWhen(EndSeesion,AvP + SD), \\\"\\\\n+1R\\\", colorPaleGreen,128+16);
Plot(ValueWhen(EndSeesion, AvP),\\\"\\\\nY\\\'AVP\\\",7,128+16);
Plot(ValueWhen(EndSeesion,AvP - SD), \\\"\\\\n-1S\\\", colorLightOrange,128+16);
Plot(ValueWhen(EndSeesion,AvP - 2*SD),\\\"\\\\n-2S\\\", colorOrange,128+16);
Plot(ValueWhen(EndSeesion,AvP - 3*SD),\\\"\\\\n-3S\\\", colorRed,128+16);
Plot(IIf(avp,AVP,Null),\\\"\\\\nAVP\\\",ParamColor(\\\"ColorAVP\\\",ColorRGB(120,25,62)),styleLine);
Kindly confirm , weather coding is correct or not !
Thank you .
Edward Pottasch empottasch [at] skynet.be 2010-07-23 05:42:27soni, one way to do it is coded below. If you get too many forward slashes please give your Email and I will send it to you.
P = (H + L + C)/3;
vv = V * P;
BarsInDay = 1 + BarsSince( Day() != Ref(Day(), -1));
CumVolumeInDay = Sum(V,BarsInDay);
VWAP = Sum(vv, BarsInDay ) / CumVolumeInDay;
SV = Sum((P - VWAP)^2,BarsInDay) / BarsInDay; // sample variance
SD = sqrt(SV); // standard deviation
// VWAP
VWAPp = TimeFrameCompress(VWAP, inDaily, compressLast);
VWAPp = Ref(VWAPp,-1);
VWAPp = TimeFrameExpand( VWAPp, inDaily, expandFirst );
// + SD
VWAPpSD = TimeFrameCompress(VWAP + SD, inDaily, compressLast);
VWAPpSD = Ref(VWAPpSD,-1);
VWAPpSD = TimeFrameExpand( VWAPpSD, inDaily, expandFirst );
// - SD
VWAPmSD = TimeFrameCompress(VWAP - SD, inDaily, compressLast);
VWAPmSD = Ref(VWAPmSD,-1);
VWAPmSD = TimeFrameExpand( VWAPmSD, inDaily, expandFirst );
// + 2SD
VWAPp2SD = TimeFrameCompress(VWAP + 2 * SD, inDaily, compressLast);
VWAPp2SD = Ref(VWAPp2SD,-1);
VWAPp2SD = TimeFrameExpand( VWAPp2SD, inDaily, expandFirst );
// - 2SD
VWAPm2SD = TimeFrameCompress(VWAP - 2 * SD, inDaily, compressLast);
VWAPm2SD = Ref(VWAPm2SD,-1);
VWAPm2SD = TimeFrameExpand( VWAPm2SD, inDaily, expandFirst );
// + 3SD
VWAPp3SD = TimeFrameCompress(VWAP + 3 * SD, inDaily, compressLast);
VWAPp3SD = Ref(VWAPp3SD,-1);
VWAPp3SD = TimeFrameExpand( VWAPp3SD, inDaily, expandFirst );
// - 3SD
VWAPm3SD = TimeFrameCompress(VWAP - 3 * SD, inDaily, compressLast);
VWAPm3SD = Ref(VWAPm3SD,-1);
VWAPm3SD = TimeFrameExpand( VWAPm3SD, inDaily, expandFirst );
// chart, 255-R, 255-G, 255-B
SetChartBkColor( ParamColor(\\\"ColorBG\\\", ColorRGB( 0, 0, 0 ) ) );
SetChartOptions(0, chartShowDates);
Plot(C,\\\"\\\\nClose\\\",colorWhite,64);
//Plot(IIf(VWAP,VWAP,Null),\\\"\\\\nVWAP\\\",ParamColor(\\\"ColorVWAP\\\",ColorRGB(255,20,147)),styleThick);
Plot(IIf(VWAPp,VWAPp,Null),\\\"\\\\nVWAP_y\\\",ParamColor(\\\"ColorVWAP\\\",ColorRGB(255,20,147)),styleThick);
//Plot(IIf(VWAP,VWAP + SD,Null), \\\"\\\\n+1SD\\\", ParamColor(\\\"ColorSDP\\\",ColorRGB(238,174,238)),styleDashed);
//Plot(IIf(VWAP,VWAP - SD,Null), \\\"\\\\n-1SD\\\", ParamColor(\\\"ColorSDM\\\",ColorRGB(255-238,255-174,255-238)),styleDashed);
Plot(IIf(VWAP,VWAPpSD,Null), \\\"\\\\n+1SD_y\\\", ParamColor(\\\"ColorSDP\\\",ColorRGB(238,174,238)),styleLine);
Plot(IIf(VWAP,VWAPmSD,Null), \\\"\\\\n-1SD_y\\\", ParamColor(\\\"ColorSDM\\\",ColorRGB(255-238,255-174,255-238)),styleLine);
//Plot(IIf(VWAP,VWAP + 2*SD,Null), \\\"\\\\n+2SD\\\", ParamColor(\\\"ColorSDPP\\\",ColorRGB(153,102,204)),styleDashed);
//Plot(IIf(VWAP,VWAP - 2*SD,Null), \\\"\\\\n-2SD\\\", ParamColor(\\\"ColorSDMM\\\",ColorRGB(255-153,255-102,255-204)),styleDashed);
Plot(IIf(VWAP,VWAPp2SD,Null), \\\"\\\\n+2SD_y\\\", ParamColor(\\\"ColorSDPP\\\",ColorRGB(153,102,204)),styleLine);
Plot(IIf(VWAP,VWAPm2SD,Null), \\\"\\\\n-2SD_y\\\", ParamColor(\\\"ColorSDMM\\\",ColorRGB(255-153,255-102,255-204)),styleLine);
//Plot(IIf(VWAP,VWAP + 3*SD,Null), \\\"\\\\n+3SD\\\", ParamColor(\\\"ColorSDPPP\\\",ColorRGB(128,0,128)),styleDashed);
//Plot(IIf(VWAP,VWAP - 3*SD,Null), \\\"\\\\n-3SD\\\", ParamColor(\\\"ColorSDMMM\\\",ColorRGB(255-128,255-0,255-128)),styleDashed);
Plot(IIf(VWAP,VWAPp3SD,Null), \\\"\\\\n+3SD_y\\\", ParamColor(\\\"ColorSDPPP\\\",ColorRGB(128,0,128)),styleLine);
Plot(IIf(VWAP,VWAPm3SD,Null), \\\"\\\\n-3SD_y\\\", ParamColor(\\\"ColorSDMMM\\\",ColorRGB(255-128,255-0,255-128)),styleLine);
soni67c soni67c[at]yahoo.co.in 2010-07-23 08:41:48Dear Edward,
Wow ..
my mail id= soni67c@yahoo.co.in
i am really grateful ,
Thank you