VWAP Bands Problem with Exception Floating-point division by zero

Questions about MultiCharts and user contributed studies.
sforbes
Posts: 18
Joined: 23 Aug 2006
Been thanked: 2 times

VWAP Bands Problem with Exception Floating-point division by zero

Postby sforbes » 05 Oct 2023

MultiCharts64 Version 14.0 Release (Build 25218)
Can anybody help? I am not a coder.
I am getting inconsistent results with the attached Study and Function.
The Study works OK on the SPY, OXY and VXX but not on VIX or other charts like DVN etc but not always, there tends to be inconsistency, are there too many data points for it to cope with or too many decimal points ? However it never works on EUR.USD or other currencies.

I get a message as follows:
Error in Study "VWAP_BANDS(ERY-3Minute)" ; {EXCEPTION}
Vwap-_bands_5.pla
(19.66 KiB) Downloaded 58 times
Floating-point division by zero
I cannot identify where the floating point problem is occurring.
I have attached the Study and Function.
Any suggestions as to how I could fix it would be much appreciated.

maxhrc
Posts: 5
Joined: 06 Apr 2021

Re: VWAP Bands Problem with Exception Floating-point division by zero

Postby maxhrc » 15 Oct 2023

Hi, check this code if works fine.

Excuse Sir TJ :wink:

Code: Select all

input: time_start (1530), time_stop (2259), upColor(blue), dnColor(Red); vars: vwap(0), pv(0), Totalvolume(0), Barfromstart(0), Squareddeviations(0), Probabilityweighteddeviations(0), deviationsum(0), standarddeviation(0), OncePerDay(0); If date > date[1] then OncePerDay = 0 ; If Time >= time_start and Time <= time_stop and OncePerDay = 0 then begin OncePerDay = 1 ; Barfromstart=0; pv=AvgPrice*volume; Totalvolume=volume; vwap=pv/totalvolume; end else begin Barfromstart=Barfromstart[1]+1; pv=pv[1] + AvgPrice*Volume; Totalvolume=Totalvolume[1] + Volume; vwap=pv/Totalvolume; end; deviationsum=0; for value1= 0 to Barfromstart begin Squareddeviations=Square( vwap-avgprice[value1]); Probabilityweighteddeviations=volume[value1]*Squareddeviations/Totalvolume; deviationsum=deviationsum +Probabilityweighteddeviations; end; standarddeviation=SquareRoot(deviationsum); Plot1(vwap, "VWAP"); plot2(vwap+standarddeviation*0.25, "0.25VWAP"); Plot3(vwap+standarddeviation*0.50, "0.50VWAP"); plot4(vwap - standarddeviation*0.25, "-025VWAP"); Plot5(vwap - standarddeviation*0.50, "-050VWAP"); plot6(vwap+standarddeviation, "+1 StDev"); plot7(vwap+standarddeviation*1.25, "+1.25 StDev"); plot8(vwap+standarddeviation*0.75, "+0.75 StDev"); plot9(vwap+standarddeviation*2.25, "+2.25 StDev"); plot10(vwap+standarddeviation*1.75, "+1.75 StDev"); plot11(vwap+standarddeviation*3.25, "+3.25 StDev"); plot12(vwap+standarddeviation*2.75, "+2.75 StDev"); plot13(vwap+2*standarddeviation, "+2 StDev"); plot14(vwap+3*standarddeviation, "+3 StDev"); plot15(vwap-standarddeviation, "-1 StDev"); plot16(vwap-2*standarddeviation, "-2 StDev"); plot17(vwap-3*standarddeviation, "-3 StDev"); plot18(vwap-standarddeviation*1.25, "-1.25 StDev"); plot19(vwap-standarddeviation*0.75, "-0.75 StDev"); plot20(vwap-standarddeviation*2.25, "-2.25 StDev"); plot21(vwap-standarddeviation*1.75, "-1.75 StDev"); plot22(vwap-standarddeviation*3.25, "-3.25 StDev"); plot23(vwap-standarddeviation*2.75, "-2.75 StDev"); var: var1(yellow); if vwap > vwap[1] then var1 = upColor; if vwap < vwap[1] then var1 = dnColor; SetPlotColor(1,var1);
Last edited by maxhrc on 16 Oct 2023, edited 1 time in total.

User avatar
TJ
Posts: 7743
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2222 times

Re: VWAP Bands Problem with Exception Floating-point division by zero

Postby TJ » 16 Oct 2023

See posts #1 & #2
1. [FAQ] How to Post Codes ... so that people can read.
viewtopic.php?t=11713

User avatar
rrams
Posts: 128
Joined: 10 Feb 2011
Location: USA
Has thanked: 7 times
Been thanked: 70 times
Contact:

Re: VWAP Bands Problem with Exception Floating-point division by zero

Postby rrams » 04 Nov 2023

sforbes,
If you still want the original Vwap-_bands_5 indicator fixed, I can edit it to behave better. Just realize that Volume Weighted Average Price is bombing with division by zero error because of missing volume data. Some symbols have historical volume available, some only have real-time volume available and forex symbols like EUR.USD do not use volume. For forex you could set "Build Volume On" to "Tick Count" instead of "Trade Volume".

sforbes
Posts: 18
Joined: 23 Aug 2006
Been thanked: 2 times

Re: VWAP Bands Problem with Exception Floating-point division by zero

Postby sforbes » 06 Nov 2023

Thanks rrams for your suggestion, tried changing to 'Tick Count' on (Build Volume on) EUR.USD it made no difference, still comes back with 'floating-point division by zero'

User avatar
Mark Brown
Posts: 182
Joined: 29 Nov 2016
Has thanked: 114 times
Been thanked: 18 times

Re: VWAP Bands Problem with Exception Floating-point division by zero

Postby Mark Brown » 10 Nov 2023

inputs:
time_start(1530),
time_stop(2259),
upColor(blue),
dnColor(Red);

vars:
vwap(0),
pv(0),
Totalvolume(0),
Barfromstart(0),
Squareddeviations(0),
Probabilityweighteddeviations(0),
deviationsum(0),
standarddeviation(0),
OncePerDay(0);

// Function to safely perform division
Function SafeDivide(Numerator, Denominator: Numeric): Numeric;
Begin
If Denominator <> 0 Then
Result = Numerator / Denominator
Else
Result = 0; // Avoid division by zero
End;

// Reset OncePerDay at the start of a new day
If date > date[1] then OncePerDay = 0;

// VWAP calculation
If Time >= time_start and Time <= time_stop and OncePerDay = 0 then begin
OncePerDay = 1;
Barfromstart = 0;
pv = AvgPrice * volume;
Totalvolume = volume;
vwap = SafeDivide(pv, Totalvolume); // Use SafeDivide to avoid division by zero
end else begin
Barfromstart = Barfromstart[1] + 1;
pv = pv[1] + AvgPrice * Volume;
Totalvolume = Totalvolume[1] + Volume;
vwap = SafeDivide(pv, Totalvolume); // Use SafeDivide
end;

// Standard deviation calculation
deviationsum = 0;
for value1 = 0 to Barfromstart begin
Squareddeviations = Square(vwap - avgprice[value1]);
// Ensure Totalvolume is not zero
if Totalvolume > 0 then begin
Probabilityweighteddeviations = SafeDivide(volume[value1] * Squareddeviations, Totalvolume);
deviationsum = deviationsum + Probabilityweighteddeviations;
end;
end;

standarddeviation = SquareRoot(deviationsum);

// Plotting and coloring logic...

sforbes
Posts: 18
Joined: 23 Aug 2006
Been thanked: 2 times

Re: VWAP Bands Problem with Exception Floating-point division by zero

Postby sforbes » 24 Nov 2023

Hello Mark,
Thanks for your suggestion, I have only just seen it.

As I am not a coder please can you indicate where I am making a mistake, I am getting a Compile error
syntax error, unexpected 'identificator'
line 19, column 9:
Which is the line starting:
// Function to safely perform division
Function SafeDivide(Numerator, Denominator: Numeric): Numeric;
Many thanks.


Return to “MultiCharts”