Download software Tutorial videos
Subscription & data-feed pricing Class schedule


New account application Trading resources
Margin rates Stock & option commissions

Attention: Discussion forums are read-only for extended maintenance until further notice.
Welcome Guest, please sign in to participate in a discussion. Search | Active Topics |

Interesting problems with VERY LARGE NUMBERS and decimals Rate this Topic:
Previous Topic · Next Topic Watch this topic · Print this topic ·
tfogle
Posted : Tuesday, February 14, 2012 10:32:21 PM
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?
tfogle
Posted : Tuesday, February 14, 2012 10:45:21 PM
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
tfogle
Posted : Tuesday, February 14, 2012 10:52:13 PM
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. ^^^^
Bruce_L
Posted : Wednesday, February 15, 2012 9:40:02 AM


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
tfogle
Posted : Wednesday, February 15, 2012 9:49:58 AM
Registered User
Joined: 11/2/2004
Posts: 9
The simplest of mistakes, eh?
Bruce_L
Posted : Wednesday, February 15, 2012 9:55:08 AM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
Yes. A pretty simple mistake to make.

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
tfogle
Posted : Wednesday, February 15, 2012 9:55:11 AM
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.
Bruce_L
Posted : Wednesday, February 15, 2012 10:57:00 AM


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
tfogle
Posted : Wednesday, February 15, 2012 8:13:33 PM
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.
Bruce_L
Posted : Thursday, February 16, 2012 8:17:45 AM


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
Users browsing this topic
Guest-1

Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.