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} 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.
VWAP Bands Problem with Exception Floating-point division by zero
Re: VWAP Bands Problem with Exception Floating-point division by zero
Hi, check this code if works fine.
Excuse Sir TJ
Excuse Sir TJ
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.
- TJ
- Posts: 7746
- Joined: 29 Aug 2006
- Location: Global Citizen
- Has thanked: 1033 times
- Been thanked: 2225 times
- rrams
- Posts: 129
- 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
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".
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".
Re: VWAP Bands Problem with Exception Floating-point division by zero
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'
- Mark Brown
- Posts: 193
- Joined: 29 Nov 2016
- Has thanked: 139 times
- Been thanked: 20 times
Re: VWAP Bands Problem with Exception Floating-point division by zero
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...
Re: VWAP Bands Problem with Exception Floating-point division by zero
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.
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.
-
- Posts: 53
- Joined: 04 Jan 2008
- Has thanked: 21 times
- Been thanked: 3 times
Re: VWAP Bands Problem with Exception Floating-point division by zero
Hello Mark,
That code needs some TLC from you. Can't compile like that.
That code needs some TLC from you. Can't compile like that.
-
- Posts: 53
- Joined: 04 Jan 2008
- Has thanked: 21 times
- Been thanked: 3 times
Re: VWAP Bands Problem with Exception Floating-point division by zero
xxxxxxxxxxxHello 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.
Any luck getting that to work sforbes ?
- Mark Brown
- Posts: 193
- Joined: 29 Nov 2016
- Has thanked: 139 times
- Been thanked: 20 times
Re: VWAP Bands Problem with Exception Floating-point division by zero
Code: Select all
inputs:
time_start(1530), // Start time for calculation in HHMM format
time_stop(2259), // End time for calculation in HHMM format
upColor(blue), // Color for upward movement
dnColor(red); // Color for downward movement
vars:
vwap(0), // Variable for VWAP
pv(0), // Price-volume product for VWAP calculation
Totalvolume(0), // Accumulator for total volume within time range
Barfromstart(0), // Number of bars since start of calculation
Squareddeviations(0), // Squared deviations for standard deviation calculation
Probabilityweighteddeviations(0), // Weighted deviations for volume
deviationsum(0), // Sum of deviations
standarddeviation(0), // Standard deviation of prices
OncePerDay(0); // Flag to ensure VWAP is calculated once per day
// 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 by returning zero if denominator is 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; // Ensure calculations only start once per day
Barfromstart = 0; // Reset bar counter
pv = AvgPrice * volume; // Calculate initial price-volume
Totalvolume = volume; // Set initial volume
vwap = SafeDivide(pv, Totalvolume); // Initial VWAP calculation using SafeDivide
end else begin
Barfromstart = Barfromstart[1] + 1; // Increment bar count from start
pv = pv[1] + AvgPrice * Volume; // Update price-volume accumulation
Totalvolume = Totalvolume[1] + Volume; // Update volume accumulation
vwap = SafeDivide(pv, Totalvolume); // Recalculate VWAP
end;
// Standard deviation calculation
deviationsum = 0;
for value1 = 0 to Barfromstart begin
Squareddeviations = Square(vwap - avgprice[value1]); // Calculate squared deviation from VWAP
if Totalvolume > 0 then begin
Probabilityweighteddeviations = SafeDivide(volume[value1] * Squareddeviations, Totalvolume);
deviationsum = deviationsum + Probabilityweighteddeviations; // Sum weighted deviations
end;
end;
standarddeviation = SquareRoot(deviationsum); // Calculate standard deviation
// Plotting and coloring logic
Plot1(vwap, "VWAP", Default, Default); // Plot VWAP on chart
Plot2(standarddeviation, "StdDev", Default, Default); // Plot Standard Deviation
// Change bar colors based on comparison with VWAP
If C > vwap then
SetPlotColor(1, upColor) // If close is above VWAP, color it blue
else
SetPlotColor(1, dnColor); // If close is below VWAP, color it red
Re: VWAP Bands Problem with Exception Floating-point division by zero
// Function to safely perform division
Function SafeDivide(Numerator, Denominator: Numeric): Numeric;
Thanks for your code Mark, it still throws up the following on trying to compile:
syntax error, unexpected 'identificator'
at line 19, column 9 which is the place in the code that is causing the problem see below:
Function SafeDivide(Numerator, Denominator: Numeric): Numeric;
Begin
If Denominator <> 0 Then
Result = Numerator / Denominator
Else
Result = 0; // Avoid division by zero by returning zero if denominator is zero
End;
Function SafeDivide(Numerator, Denominator: Numeric): Numeric;
Thanks for your code Mark, it still throws up the following on trying to compile:
syntax error, unexpected 'identificator'
at line 19, column 9 which is the place in the code that is causing the problem see below:
Function SafeDivide(Numerator, Denominator: Numeric): Numeric;
Begin
If Denominator <> 0 Then
Result = Numerator / Denominator
Else
Result = 0; // Avoid division by zero by returning zero if denominator is zero
End;
- rrams
- Posts: 129
- 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
sforbes,
For error trapping an indicator that shows on the main subchart; it is not a good idea to just zero out the result to avoid division by zero. That has the effect of compressing the price chart because of the scale. It is better to just use the #Return keyword to avoid plotting that result.
For those that write and distribute indicators, I prefer if they would briefly explain its use in comments on the first lines of code and the bar interval it should use and where the idea came from so I can give them credit if I modify.
It is also a good idea to idiot-proof signals and indicators using alerts. Here is your original indicator re-worked so that it tells you more information if something is wrong:
- I wish MultiCharts allowed inline functions; that would help de-clutter my code database, but alas, no.Function SafeDivide(Numerator, Denominator: Numeric): Numeric;
For error trapping an indicator that shows on the main subchart; it is not a good idea to just zero out the result to avoid division by zero. That has the effect of compressing the price chart because of the scale. It is better to just use the #Return keyword to avoid plotting that result.
For those that write and distribute indicators, I prefer if they would briefly explain its use in comments on the first lines of code and the bar interval it should use and where the idea came from so I can give them credit if I modify.
It is also a good idea to idiot-proof signals and indicators using alerts. Here is your original indicator re-worked so that it tells you more information if something is wrong:
- Attachments
-
- VWAP_Bands_5.pla
- (16.29 KiB) Downloaded 27 times
-
- VWAP.png
- (49.48 KiB) Not downloaded yet