Hello dears, how can I calculate Anchor VWAP data values in specific range of dates?
// Indicator Defination
//===================================================================================================
//===================================================================================================
// Indicator : VWAP-Anchor-Bands(Percentage && STD(01-09-2024)) Indicator.
// Author : Ahmad Elsayed Ibrahim
// Options : Get VWAP && Bands based on Standard devision or factors.
// Exploration used to calculate VWAP and Bands data values.
//===================================================================================================
//===================================================================================================
//============================================================================================================================================================================================================================================
//============================================================================================================================================================================================================================================
//===================================================================================================
//===================================================================================================
/// VWAP code that also plots standard deviations...if you want a 3rd...it should be fairly simple to add
//
// NOTE: the code is SLOOOOWWWW...can someone help speed it up?
// I tried my best, but can't really do much with the two for-loops...
//
// LarryJR - ljr500@hotmail.com
// need this line or else when u scroll 1 min chart over a few days, the vwap bars may change
// size (which is wrong) because of the QuickAFL processing code
// so we ensure we have enough bars loaded for at least a couple of days... set the number appropriately.
//===================================================================================================
//===================================================================================================
SetBarsRequired( 10000, 0 );
dd = ParamStr("Date section","-----------------------------------------");
//BasedDate = ParamToggle( "Based Date Activation", "OFF|ON", 0 );
StartDateTime = ParamDate("Start Date","2023-08-01",0);
EndDateTime = ParamDate("End Date","2023-08-30 ",0);
WP = ParamStr( "VWAP Param","--------------------------------------------------------");
Anchorperiod = ParamList( "Anchor Period", "Daily|Weekly|Monthly|dateRange", 0 );
// this stores true/false based on a new day...
VWAPFPs = ParamList ( "VWAPFPs", "Open|High|Low|Close|High-Low|High-Low-Close",5 );
VWAPStyle = ParamStyle( "VWAP Style" , styleNoDraw );
VWAPClr = ParamColor( "VWAP Clr" , colorGreen);
//--------------------------------
//--------------------------------
bands = ParamStr ( "Bands Param","--------------------------------------------------------");
BnadsMode = ParamList ( "Bnads Mode" , "Precentage|STD",1 );
Bnadsfactor = Param ( "Bnads Factor" , 1, 0.0000000005, 1000000000, 0.5, sincr = 0 ) ;
BnadsRule = ParamList ( "Bnads Rule" , "+|-",0 );
BnadsStyle = ParamStyle( "Bnads Style" , styleNoDraw );
BnadsClr = ParamColor( "Bnads Clr" , colorGreen);
//---
BnadsMode1 = ParamList ( "Bnads Mode1" , "Precentage|STD",1 );
Bnadsfactor1 = Param ( "Bnad Factor1" , 1, 0.0000000005, 1000000000, 0.5, sincr = 0 ) ;
BnadsRule1 = ParamList ( "Bnads Rule1" , "+|-",0 );
BnadsStyle1 = ParamStyle( "Bnads Style1" , styleNoDraw );
BnadsClr1 = ParamColor( "Bnads Clr1" , colorRed);
//--------------------------------
//--------------------------------
bands1 = ParamStr ( "Bands1 Param","--------------------------------------------------------");
Bnads1Mode = ParamList ( "Bnads1 Mode" , "Precentage|STD",1 );
Bnads1factor = Param ( "Bnads1 Factor" , 1, 0.0000000005, 1000000000, 0.5, sincr = 0 ) ;
Bnads1Rule = ParamList ( "Bnads1 Rule" , "+|-",0 );
Bnads1Style = ParamStyle( "Bnads1 Style" , styleNoDraw );
Bnads1Clr = ParamColor( "Bnads1 Clr" , colorGreen);
//---
Bnads1Mode1 = ParamList ( "Bnads1 Mode1" , "Precentage|STD",1 );
Bnads1factor1 = Param ( "Bnads1 Factor1" , 1, 0.0000000005, 1000000000, 0.5, sincr = 0 ) ;
Bnads1Rule1 = ParamList ( "Bnads1 Rule1" , "+|-",0 );
Bnads1Style1 = ParamStyle( "Bnads1 Style1" , styleNoDraw );
Bnads1Clr1 = ParamColor( "Bnads1 Clr1" , colorRed);
//--------------------------------
//--------------------------------
bands2 = ParamStr ( "Bands2 Param","--------------------------------------------------------");
Bnads2Mode = ParamList ( "Bnads2 Mode" , "Precentage|STD",1 );
Bnads2factor = Param ( "Bnads2 Factor" , 1, 0.0000000005, 1000000000, 0.5, sincr = 0 ) ;
Bnads2Rule = ParamList ( "Bnads2 Rule" , "+|-",0 );
Bnads2Style = ParamStyle( "Bnads2 Style" , styleNoDraw );
Bnads2Clr = ParamColor( "Bnads2 Clr" , colorGreen);
//---
Bnads2Mode1 = ParamList ( "Bnads2 Mode1" , "Precentage|STD",1 );
Bnads2factor1 = Param ( "Bnads2 Factor1" , 1, 0.0000000005, 1000000000, 0.5, sincr = 0 ) ;
Bnads2Rule1 = ParamList ( "Bnads2 Rule1" , "+|-",0 );
Bnads2Style1 = ParamStyle( "Bnads2 Style1" , styleNoDraw );
Bnads2Clr1 = ParamColor( "Bnads2 Clr1" , colorRed);
//===================================================================================================
//===================================================================================================
switch ( Anchorperiod )
{
case "Daily":
newday = Day() != Ref( Day(), -1 );
break;
case "Weekly":
newday = DayOfWeek() == 0 AND Ref( DayOfWeek(), -1 ) != 0;
break;
case "Monthly":
newday = Month() != Ref( Month(), -1 );
break;
case "dateRange":
newday = ??????????;
break;
}
//===================================================================================================
//===================================================================================================
SumPriceVolume =0;
totVolume =0;
Vwap2 =0;
stddev =0;
newdayindex =0;
Variance =0;
//--------------------------------
//--------------------------------
// we must use a loop here because we need to save the vwap for each bar to calc the variance later
for( i= 0; i < BarCount; i++ )
{
// only want to reset our values at the start of a new day
if (newday[i]==True)
{
SumPriceVolume=0;
totVolume=0;
newdayindex=i; // this is the index at the start of a new day
Variance=0;
//Vwap2=0;
}
//AvgPrice=( H[i] + L[i] + C[i])/3;
//--------------------------------
//--------------------------------
switch( VWAPFPs )
{
case "Open":
AvgPrice = O[i];
break;
case "High":
AvgPrice = H[i];
break;
case "Low":
AvgPrice = L[i];
break;
case "Close":
AvgPrice = C[i];
break;
case "High-Low":
AvgPrice = ( H[i] + L[i] )/2;
break;
case "High-Low-Close":
AvgPrice = ( H[i] + L[i] + C[i])/3;
break;
}
//--------------------------------
//--------------------------------
// Sum of Volume*price for each bar
sumPriceVolume += AvgPrice * (Volume[i]);
// running total of volume each bar
totVolume += (Volume[i]);
if (totVolume[i] >0)
{
Vwap2[i]=Sumpricevolume / totVolume ;
Vwap2temp=Vwap2[i];
}
//--------------------------------
//--------------------------------
//===================================================================================================
// now the hard part...calculate the variance...
// a separate calc from the start of each day - note it requires the vwap from above
// also note, we calculate starting at the first bar in the new day to today to the curent bar
//===================================================================================================
Variance=0;
for (j=newdayindex; j < i; j++)
{
//AvgPrice=( H[j] + L[j] + C[j])/3;
switch( VWAPFPs )
{
case "Open":
AvgPrice = O[j];
break;
case "High":
AvgPrice = H[j];
break;
case "Low":
AvgPrice = L[j];
break;
case "Close":
AvgPrice = C[j];
break;
case "High-Low":
AvgPrice = ( H[j] + L[j] )/2;
break;
case "High-Low-Close":
AvgPrice = ( H[j] + L[j] + C[j])/3;
break;
}
Variance += (Volume[j]/totVolume) * (Avgprice-Vwap2temp)*(Avgprice-Vwap2temp);
}
//--------------------------------
//--------------------------------
/*
stddev_1_pos[i]=Vwap2temp + sqrt(Variance);
stddev_1_neg[i]=Vwap2temp - sqrt(Variance);
stddev_2_pos[i]=Vwap2temp + 2*sqrt(Variance);
stddev_2_neg[i]=Vwap2temp - 2*sqrt(Variance);
*/
//--------------------------------
//--------------------------------
// Bands * factors
Bnadsval = Vwap2 * Bnadsfactor;
Bnads1val = Vwap2 * Bnads1factor;
Bnads2val = Vwap2 * Bnads2factor;
//--------
std = sqrt(Variance) * Bnadsfactor;
std1 = sqrt(Variance) * Bnads1factor;
std2 = sqrt(Variance) * Bnads2factor;
//--------------------------------
//--------------------------------
}
//===================================================================================================
//===================================================================================================
//Plot (Vwap2,"VWAP2",VWAPclr, styleLine);
switch( BnadsMode )
{
case "Precentage":
FBnadsval = IIf( BnadsRule == "+", Vwap2 + Bnadsval, Vwap2 - Bnadsval );
break;
case "STD":
FBnadsval = IIf( BnadsRule == "+" , Vwap2 + std, Vwap2 - std );
break;
}
switch( BnadsMode1 )
{
case "Precentage":
FBnadsval1 = IIf( BnadsRule1 == "+" , Vwap2 + Bnadsval, Vwap2 - Bnadsval );
break;
case "STD":
FBnadsval1 = IIf( BnadsRule1 == "+" , Vwap2 + std, Vwap2 - std );
break;
}
//-------------------------
switch( Bnads1Mode )
{
case "Precentage":
FBnads1val = IIf( Bnads1Rule == "+" , Vwap2 + Bnads1val, Vwap2 - Bnads1val );
break;
case "STD":
FBnads1val = IIf( Bnads1Rule == "+" , Vwap2 + std1, Vwap2 - std1 );
break;
}
switch( Bnads1Mode1 )
{
case "Precentage":
FBnads1val1 = IIf( Bnads1Rule1 == "+" , Vwap2 + Bnads1val, Vwap2 - Bnads1val );
break;
case "STD":
FBnads1val1 = IIf( Bnads1Rule1 == "+" , Vwap2 + std1, Vwap2 - std1 );
break;
}
//-------------------------
switch( Bnads2Mode )
{
case "Precentage":
FBnads2val = IIf( Bnads2Rule == "+" , Vwap2 + Bnads2val, Vwap2 - Bnads2val );
break;
case "STD":
FBnads2val = IIf( Bnads2Rule == "+" , Vwap2 + std2, Vwap2 - std2 );
break;
}
switch( Bnads1Mode1 )
{
case "Precentage":
FBnads2val1 = IIf( Bnads2Rule1 == "+" , Vwap2 + Bnads2val, Vwap2 - Bnads2val );
break;
case "STD":
FBnads2val1 = IIf( Bnads2Rule1 == "+" , Vwap2 + std2, Vwap2 - std2 );
break;
}
//===================================================================================================
//===================================================================================================
//Plot( std , "STD" , VWAPClr , VWAPStyle , Null, Null, 0, 0, 2 );
Plot( Vwap2 , "Vwap" , VWAPClr , VWAPStyle , Null, Null, 0, 0, 2 );
Plot( FBnadsval , "Bnadsval" , BnadsClr , BnadsStyle , Null, Null, 0, 0, 2 );
Plot( FBnadsval1 , "Bnadsval1" , BnadsClr1 , BnadsStyle1 , Null, Null, 0, 0, 2 );
Plot( FBnads1val , "Bnads1val" , Bnads1Clr , Bnads1Style , Null, Null, 0, 0, 2 );
Plot( FBnads1val1, "Bnads1val1" , Bnads1Clr1 , Bnads1Style1 , Null, Null, 0, 0, 2 );
Plot( FBnads2val , "Bnads2val" , Bnads2Clr , Bnads2Style , Null, Null, 0, 0, 2 );
Plot( FBnads2val1, "Bnads2val1" , Bnads2Clr1 , Bnads2Style1 , Null, Null, 0, 0, 2 );