| Welcome Guest, please sign in to participate in a discussion. | Search | Active Topics | |
Registered User Joined: 11/2/2004 Posts: 9
|
First, I'm not much of a programmer and second don't know much about Visual Basic. So please forgive me if I ask some ignorant questions.
I'd like to create some "Sum" (sigma) statements that I can use in a formula. I don't know of any predefined code and have made up my own. The problem is then, when multiplying to very large numbers, there seems to be an issue of accuracy. As in the numbers are rounded at some point during the calculation and become inaccurate. Here is an example that calculates the sum of 1 to 10 and multiplies it by the sum of the price hisotry for the last 10 days:
'# PH = indicator.Library.Price
'define variables
Dim sigmaX As Single
Dim sigmaY As Single
Dim days As Single
'HOW MANY DAYS?
days = 10
'calculate Sigma X
For i As Integer = 1 To days
sigmaX = sigmaX + i
Next i
'Calculate Sigma Y
For i As Integer = 1 To days
sigmaY = sigmaY + PH.Value(i-1)
Next i
Plot = sigmaX * sigmaY
The discrepancies are most noticable when using the formula with the DJ-30. It seems the numbers are rounded to the nearest whole number, but sometimes StockFinder disagrees with my calculator by up to 2.5. Somewhere, the decimals are getting clipped or rounded or something, and I'm worried it may throw my final calculations off.
Is there a way to get the program to use at least 2 decimals in this instance?
|
|
Registered User Joined: 11/2/2004 Posts: 9
|
Here's another bit I was working on that only returns whole numbers.
I must be doing something wrong?
'# PH = indicator.Library.Price
'Define variables
Dim sigmaXY As Single
Dim days As Single
'NUMBER OF DAYS
days = 10
'Calculate sigmaXY
For i As Integer = 1 To days
sigmaXY = sigmaXY + (days * PH.Value(days - 1))
Next i
plot = sigmaXY
|
|
Registered User Joined: 11/2/2004 Posts: 9
|
I just realized that I need to put 1-10 in an array to get proper calculation above. ^^^^
|
|

 Worden Trainer
Joined: 10/7/2004 Posts: 65,138
|
In the first case, I suspect right-clicking on the Value Scale, selecting Edit and unchecking Abbreviate Large Values will give you the desired result.
In the second case, you are multiplying Prices (which are in hundredths of a dollar) by 10 and adding them together 10 times (in other words multiplying by 100. If you change:
sigmaXY = sigmaXY + (days * PH.Value(days - 1))
To:
sigmaXY = sigmaXY + (days * PH.Value(i - 1))
You should no longer get results which are always even dollar amounts.
-Bruce Personal Criteria Formulas TC2000 Support Articles
|
|
Registered User Joined: 11/2/2004 Posts: 9
|
The simplest of mistakes, eh?
|
|

 Worden Trainer
Joined: 10/7/2004 Posts: 65,138
|
Yes. A pretty simple mistake to make.
-Bruce Personal Criteria Formulas TC2000 Support Articles
|
|
Registered User Joined: 11/2/2004 Posts: 9
|
To quick to hit the post button.
In the first case, I have done all the suggested actions with no result. As in, the very large numbers which would end in a decimal are rounded and wrong when compared to a calculator, and no decimal is ever shown, only "xxxxxxxx.00". It appears that it's numbers that are larger than 1 million. For the sake of it, I tried using "double" instead of "single" for the variables and that didn't help.
|
|

 Worden Trainer
Joined: 10/7/2004 Posts: 65,138
|
The plots are all based on the Single data type (which uses 4 bytes to store a number). And the Single data type only has 7-digit accuracy. So even if we use the Decimal data type (which uses 16 bytes to store a number or twice as much as the Double data type's 8 bytes) all the way through the calculations, at the end, the final result is going to get rounded off to 7-digit accuracy.
'# PH = indicator.Library.Price
'define variables
Dim sigmaX As Decimal
Dim sigmaY As Decimal
Dim days As Decimal
'HOW MANY DAYS?
days = 10
'calculate Sigma X
For i As Decimal = 1 To days
sigmaX = sigmaX + i
Next i
'Calculate Sigma Y
For i As Decimal = 1 To days
sigmaY = sigmaY + System.Convert.ToDecimal(PH.Value(i - 1))
Next i
Dim Result As Decimal = sigmaX * sigmaY
Plot = Result
-Bruce Personal Criteria Formulas TC2000 Support Articles
|
|
Registered User Joined: 11/2/2004 Posts: 9
|
Thanks for holding my hand on this. I'll play with it and see how it works.
|
|

 Worden Trainer
Joined: 10/7/2004 Posts: 65,138
|
I wasn't suggesting you need to use the Decimal data type. The example was just a demonstration. The final result is going to be limited to 7-digit accuracy no matter what you do.
-Bruce Personal Criteria Formulas TC2000 Support Articles
|
|
|
Guest-1 |