How to perform math with stock quotes
-
Hi Alexander,
Thanks for the useful plug-in. As I’m not a programmer, I’ve been struggling to figure out how to perform a few simple math calculations with the TradeVantage quotes, but have been unable to get anything I try to work. I am designing a stock research website and would like to display the YTD and overall performance for a number stocks inside a table on my WordPress site (have tested the plugin both with an HTML table and using a plugin, TablePress).
I’m using another plug-in that allows you to add Javascript functions to the header or footer and have assigned ids to the <th> elements in my table for the stock quote (used either template=”%price%” or “%raw_price%” and raw=”1″), the original price for calculating overall return (manually entered), and the closing price on 12-31-2021 (also manually entered). I’ve tried calling the JS function two different ways: an onclick=”function_name()” attribute inside a button and window.onload=”function_name()” or just onload=”function_name()” (inside a span tag now for testing, but will need to be placed inside the <th> tags, if possible.
I’ve tried enclosing the JS function in parseFloat() or parseInt(), either appending .value or omitting it, and enclosing the function in toString(), but nothing works (I either get a null reference syntax error or “NaN” as the result). test1 and test2 are testing variables (whole numbers placed inside span tags).
Here’s an example of the JS code (one attempted version):
<script> function overallReturn() { var num1 = document.getElementById("startValue").value; var num2 = document.getElementById("ytdValue").value; var test1 = parseInt(document.getElementById("testValue1")); var test2 = parseInt(document.getElementById("testValue2")); document.getElementById("testResult").innerHTML = test1+test2; document.getElementById("overallResult").innerHTML = ((num2-num1)/num1)*100; }
</script>
Any suggestions you can provide would be greatly appreciated. Also, please let me know if there’s a way to retrive historical quotes (e.g. for calculating the 52-wk. return)?
- The topic ‘How to perform math with stock quotes’ is closed to new replies.