Stock Quote & Chart Dashing Dashboard consists of two widgets. While one displays a stock's current quote and other details; another displays its last 30-days closing stock prices.
Add the gem to your dashing gemfile:
gem 'json'
and run bundle install
.
First, copy stock.coffee
, stock.html
, and stock.sass
into the /widgets/stock
directory and chart.coffee
, chart.html
, and chart.sass
into the /widgets/chart
directory. Put the yahoo_stock_quote.rb
and yahoo_stock_quote.rb
files in the /jobs
folder.
To use the widget, put the following codes into one of the /dashboards
directory's .erb
file:
<li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
<div data-id="yahoo_stock_quote" data-view="Stock" data-prefix="$"></div>
</li>
<li data-row="1" data-col="1" data-sizex="3" data-sizey="1">
<div data-id="yahoo_stock_chart" data-view="Chart" data-prefix="$"></div>
</li>
There are two jobs running for this dashboard, which are yahoo_stock_quote.rb
and yahoo_stock_chart.rb
. yahoo_stock_quote.rb
selects data from yahoo.finance.quotes
table, and yahoo_stock_chart.rb
retrieves data from yahoo.finance.historicaldata
table. You can change to another stock with the following line within the jobs.
symbol = "YHOO"
Another thing I noticed is that the
points
array in line 29 ofyahoo_stock_chart.rb
doesn't work correctly with the.reverse
method. What that is trying to achieve is take the objects (orpoints
) that are pushed to form that array and reverse the order so the graph/widget shows the current share price instead of the one that is 30 days old. Since the.reverse
method doesn't work as intended, the workaround that I think actually ends up being a little cleaner is modifying the YQL query in line 12 ofyahoo_stock_chart.rb
to:"select * from yahoo.finance.historicaldata where symbol='#{symbol}' and startDate='#{startdate}' and endDate='#{enddate}' | reverse()&format=json&env=http://datatables.org/alltables.env&callback="
Note the
| reverse()
function. This returns the dates in reverse order (day 30 is first rather than the current day) and pipes as we need it to into thepoints
array, displaying the graph is we need it to. If you do add this, you'll want to remove the.reverse
method from line 29 inyahoo_stock_chart.rb
.