VB6: Forex Margin Calculator
oubles:
Creating a math prog that is trying to change the value of a text box.
Balance = 19.00
Leverage = 50 (INTEGER)
Lots = 1.0
If Balance = 19 then a for statement must decrease Lots until Lots x Leverage equals Balance. What is happening is for statement is decreasing Lots by 0.01 and Lot of 1.0 should reach .38 and change text box value to .38 and exit sub. What is happening is when lots equals .43 its saying .42222222223.
email Subgenius at inbox dot com Subject "VB6" .Code:Dim Balance, Lots, Margin As Double Dim Leverage As Integer Const Mini = 50 '50.00 Const Standard = 100 '100.00 Private Sub cmdCalculate1_Click() Dim i As Integer '--- Memory --- Lots = txtLots1.Text Balance = txtBal1.Text If optMini1.Value = True Then Leverage = 50 If optStandard1.Value = True Then Leverage = 100 '--- FindLots --- If chkFindLots.Value = 1 Then Dim Fbal As Integer: Fbal = Balance For i = 1 To 100 If Lots * Leverage = Fbal Then txtLots1.Text = Lots Exit Sub End If Lots = Lots - 0.01 Next i End If 'If Balance < Leverage Then 'Margins ' MsgBox "Leverage of $" & Leverage & ".00 is not met": Exit Sub 'Else ' Margins = Balance / Leverage: lblMargins.Caption = Margins 'End If End Sub Private Sub Form_Load() 'Reclaim memory Lots = 0: ExtLots = 0: Leeway = 0: Balance = 0 'txtBal1 = "": txtLots1 = "": lblLeeway = "": lblMargins = "" End Sub
Brian
Last edited by WingedPanther; 11-14-2008 at 05:41 PM. Reason: add code tags (the # button)
Have you run your program in debug mode to watch the values?
I have to say that I am in debug mode and seeing where it is not doing subtraction right when subtracting 0.01 from 1.0 where once it should see .38 the if is interupted and closed.
Brian
Using a For loop probably isn't the best plan, there. I'm not familiar with VB, but I would expect the exit Sub to be a little too extreme.
Hi again.
I have tried to put the calculation into memory to have within the IF statement a simple comparison of values as it counts down, but that too didnt work. If im mistaken it seems that the if statement is comprimized by having it do the math but what is happening is it gets funky and trips up alot by not showing 43 when it should have, though testing it again it has gotten to 43 but,, Main problem is for this equation once it reached the .38 it should equal the balance text box value and change the text box for lots, weird. I just thought I would post the pictures to show everyone! Ill keep playing around with it, but I thought a for statement would be ideal to do the equation on this.
Brian
You may want to post the original assignment. The logic just looks weird, and I suspect it isn't right for what you're supposed to do.
This is trying to find margin from balance -between two margins 50 or 100.
If the balance is 19.00 then it does not meet the margin level of 50($50).
The way that it fits in is by lots. Lots is 1.0 ($1.00).
I want to subtrace one cent at a time, lot size x 50(or 100) = balance (then update text box with this lot) hope this is easy to understand!
Find the Lot size for $19.00 in the 50 margin
lot size x 50 = "balance"
= .38 x 50 = 19.00
This means that it found what the lot should be, .38 cents!
1.00 = 50.00
.50 = 25.00
.38 = 19.00 <----
.37 = 18.50
.36 = 18.00
.25 = 12.50
.24 = 12.00
.23 = 11.50
.22 = 11.00
.21 = 10.50
.20 = 10.00
.19 = 9.50
Brian
i cannot understand though how at .43 it shows it as .4299999999999999.
All I can say is I wasnt anticipating this but rather the format of the balance for comparison in the if statement.
Brian
You will need to use a format to get it to display properly. Because binary and decimal are different representations, binary can't always do the EXACT representation we want. The result is that it will decode slightly off, such as what you listed above. The solution is to apply a format before assigning the value to text.
But, -I didnt think that using the balance as a integer if thats where you mean it is binary would effect anything other than having a decimal to compare to a non decimal which is why I created a memory of balance into an integer so that it would take the balance compared to the leverage. I just cant get that .43 to incriment into the equation and .42999999999 keeps showing.
I cant think of anything else to do right now but what I will end up having to do is write a case sentence or even a page long if statements just to have what I need written in stone, I dont really mind but I didnt anticipate this happening where it simply deincriments by a factor of 1 cent until the desired outcome works into the equation.
Brian
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks