User avatar
mosleyh
Intermediate
Intermediate
Posts: 414
Joined: Tue Jul 12, 2005 12:55 pm
Location: Charlotte, NC
Contact: Send private message

Figuring gain/loss with MLStockInfo

Sun Sep 10, 2006 9:18 pm

I've been playing around with MLStockInfo a bit. One of the things I set out to do is be able to see gain or loss as a percentage for the stocks I own.

I got it working, so I thought I'd post the calculations to save others some time.


Here's how it works:

(For this example, let's say the basis cost for IBM is $50.00 per share, and it's trading at $60.00)


set a variable at startup with the basis cost per share for each stock:

Code: Select all

MLServeCmd.SetVariable|stockIBM_basis~50.00
Then set up a server rule or an MLTimer to run a macro made from these commands:

Code: Select all

MLServeCmd.MLMath|Subtract~stocktemp1~{{stockIBM_price}}~{{stockIBM_basis}}
Subtracts the basis cost from the current price - (60 - 50 = 10) and puts the result in {{stocktemp1))

Code: Select all

MLServeCmd.MLMath|Divide~stocktemp2~{{stocktemp1}}~{{stockIBM_basis}}
Divides the result of the first equation by the basis cost (10 / 60 = 0.166666) and puts the result in {{stocktemp2}}

Code: Select all

MLServeCmd.MLMath|Multiply~stocktemp3~{{stocktemp2}}~100
Multiplies the results of the second calculation by 100 to get a percentage and puts it in {{stocktemp3}}

Code: Select all

MLServeCmd.wscStringFunctions|LEFT~stockIBM_gainloss~{{stocktemp3}}~6
Truncates the result of the third calculation at 6 characters. (chosen so as to get two characters after the decimal even when dealing with negative :shock: numbers) and puts the result in {{stockIBM_gainloss}}


Here's the whole macro:

Code: Select all

MLServeCmd.Macro|MLMath|Subtract~stocktemp1~{{stockIBM_price}}~{{stockIBM_basis}}!MLMath|Divide~stocktemp2~{{stocktemp1}}~{{stockIBM_basis}}!MLMath|Multiply~stocktemp3~{{stocktemp2}}~100!wscStringFunctions|LEFT~stockIBM_gainloss~{{stocktemp3}}~6!
enjoy!

Return to “MLStockInfo”

Who is online

Users browsing this forum: No registered users and 1 guest