If lst_ma.SelectedIndex = 0 Then Dim Wgt As Integer 'Calculating Ma1 For c1 = PD.Get_Index To 0 Step -1 If (c3 = c1 - Ma1) < 0 Then Exit For End If c4 = Ma1 For c2 = c1 To c3 Step -1 Val1 += (DD(c2).Close * c4) Wgt += c4 c4 -= 1 Next c2 DD(c1).Ma1 = Val1 / Wgt Next c1 'Calculating Ma2 For c1 = PD.Get_Index To 0 Step -1 If (c3 = c1 - Ma2) < 0 Then Exit For End If c4 = Ma2 For c2 = c1 To c3 Step -1 Val2 += (DD(c2).Close * c4) Wgt += c4 c4 -= 1 Next c2 DD(c1).Ma2 = Val2 / Wgt Next c1 ElseIf lst_ma.SelectedIndex = -1 Then MsgBox("Please Select Anyone From Ma Method") Exit Sub End If |
Once again i have a little problem with my program when i calculating the formula for Weighted moving average. an airthmatic overflow error showed up.
In the sample code the if segment is executed when i make a selection from list box and click a button i have many other formulas but all are working fine except this one.
the formula i code is:
let say i have 5 samples
Weighted moving avg = ((s1*5)+(s2*4)+....+(s5*1))/ (5+4+3+2+1)
the above formula is for only five samples i code this formula for n number of samples.
there are two comments cal ma1 and cal ma2 we can only see the one of them because the formula is same only the samples are different.
ok the variables.
there are four counter variables used. c1 ,c2,c3 and c4
ma1 and ma2 are two integer variable containing the number of sample we take from population. here population is dd a structure array variable. single dimension.
the sample values taken from data structue close field is type "single".similarly val1 and val2
reason why i loop it reverse is that to calculate the ma of today we need sample of last 5 days or 10 days whatever the value in ma1 is [ user input in ma1out of this code].
to check this i have past values to calculate todays ma i write a conditional statement that check the possibility. just after the main for and if true simply exits the main for.
ok first of all c1 gets the value[integer] let 3000 the then this loop is run upto "0" zero.
now a condition in which c3 variable is initialize and checked also. here let ma1 is 5
now the nested loop run for 5 times ok. here i initialize the c2 = c1 because every time the value of c1 is change with the every iteration of main for so next time c2 is initialize a different value.
c4 is initialize ma1 which is 5 [supposed]
now the body statements of nested for
here Val1 += (DD(c2).Close * c4) statement simple retrive and multiply and add the value to the previos value as loop progresses its iterations. c4 is also decrease. and in the last iteration c4 is 1. and again in the main for the values we get from the above for loop we calculate the final phase of the formula. and store that formula into population of that day moving average.

Unexpected Airthmatic Overflow Occur.?
MINA7343
MyInteger as Integer = 1.1 ' overflow exception
It would help us to help you if:
1. we new what line is producing the overflow
2. we new what types your variables are.
Just as a note...If you turn on Option Explicit and Option Strict it will help you in tracking down and preventing these types of errors.
I can only assume the 'close' method of your DD class returns a numeric value...but is it a double, integer, long....
and your ma1 & ma2 methods should be doubles because they are quotients...
Scott Kennedy
the above expression shows arithmatic overflow.
but c4 assign a value of ma1 that is integer and c4 is also integer.
during debugging it shows it has the value -1132
i enter value 5 in ma1 through text input
and c4 is used in the nested loop but it don't initialize again
i have a doubt that in the if condition i use an expression to test if it is less than zero or not.
is this expression is evaluated[math perform and save in c3] or just use its result to check the condition.
ezcode
the main problem is in the if condition a logical error cause an overflow. ok thanks spotty and Dman1
Oyvind Habberstad
Dim i As Integer = 0
TryDim x As Integer = 0
i = 10 / x
Catch ex As Exception
End Try
I see you've got divisions in here and as we dont know what the data types or lines causing the overflow are this is another potential.
On these code samples definately try and be as specific as you can with the description - detailing the line error and the line that causes the error. It helps try and identify the problem much quicker.