NSE
by Rahul Sharma
HTML
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.0/knockout-min.js"></script>
<div id='share' class='liveExample'>
<form>
Symbol List:
<br>
<input type="width:600px" data-bind='value: list'>
<br> Threshold:
<br>
<input type="width:60px" data-bind='value: threshold'>
</form>
<table>
<thead>
<tr>
<th>Share</th>
<th>Last</th>
<th>Value</th>
<th>Change</th>
</tr>
</thead>
<tbody data-bind="foreach: showTable">
<tr data-bind="style: { color: color }">
<td data-bind="text: t"></td>
<td data-bind="text: pcls_fix"></td>
<td data-bind="text: l"></td>
<td data-bind="text: cp"></td>
</tr>
</tbody>
</table>
</div>
JavaScript
var ViewModel = function() {
var self = this;
this.list = ko.observable("ACC,AMBUJACEM,ASIANPAINT,AUROPHARMA,AXISBANK,BAJAJ-AUTO,BHARTIARTL,BOSCHLTD,BPCL,CIPLA,COALINDIA,DRREDDY,EICHERMOT,GAIL,GRASIM,HCLTECH,HDFC,HDFCBANK,HEROMOTOCO,HINDALCO,HINDUNILVR,IBULHSGFIN,ICICIBANK,INDUSINDBK,INFRATEL,INFY,IOC,KOTAKBANK,LT,LUPIN,MARUTI,NTPC,ONGC,POWERGRID,RELIANCE,SBIN,SUNPHARMA,TATAMOTORS,TATAMTRDVR,TATAPOWER,TATASTEEL,TCS,TECHM,ULTRACEMCO,WIPRO,YESBANK,ZEEL");
this.notifications = []
this.stocks = ko.computed(function() {
return self.list().split(",");
}, this);
this.table = [];
this.showTable = ko.observableArray([]);
$.each(self.stocks(), function(index, value) {
self.table.push({
"id": index,
"t": value,
"e": "NSE",
"l": "0.00",
"l_fix": "0.00",
"l_cur": "₹0.00",
"s": "0",
"ltt": "3:41PM GMT+5:30",
"lt": "Mar 31, 3:41PM GMT+5:30",
"lt_dts": "2017-03-31T15:41:40Z",
"c": "0.00",
"c_fix": "0.00",
"cp": "0.00",
"cp_fix": "0.00",
"ccol": "chr",
"pcls_fix": "0.00"
});
});
this.threshold = ko.observable(4);
this.notifications = 0;
this.getData = function(idx, code) {
$.ajax({
type: "POST",
dataType: 'jsonp',
url: "https://finance.google.com/finance/info?client=ig&q=NSE:" + code,
crossDomain: true
})
.done(function(data) {
self.table[idx] = data[0];
if(parseFloat(data[0].cp) < 0)
data[0].color = '#AF0000';
else
data[0].color = '#007F00';
if (Math.abs(data[0].cp) >= self.threshold()) {
var msg = "Check '" + code + "' " + data[0].cp;
var shareNotification = new Notification(msg);
setTimeout(function() {
shareNotification.close()
}, 5000 + self.notifications*1000);
self.notifications++;
}
});
}
this.sortTable = function() {
self.table.sort(function(a, b) {
...