-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathextension.js
114 lines (98 loc) · 3.6 KB
/
extension.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const vscode = require('vscode')
const KiteTicker = require("kiteconnect").KiteTicker
const KiteConnect = require("kiteconnect").KiteConnect
let config = vscode.workspace.getConfiguration('vscode-kite')
let kc = new KiteConnect(config['api_key'])
let store = require('store')
let statusItems = []
function refresh() {
// Reload configuration
config = vscode.workspace.getConfiguration('vscode-kite')
}
function cleanup() {
// Hide and despose all instance of `StatusBarItem`
for (let item of statusItems){
item.hide()
item.dispose()
}
}
function fetchMargin() {
kc.margins("equity")
.then(function(response) {
vscode.window.showInformationMessage(`Margin: ${response.data['net']} `)
}).catch(function(err) {
vscode.window.showErrorMessage(err.message)
})
}
function fetchQuote(tradingSymbol) {
kc.quote(config['exchange'], tradingSymbol)
.then(function(response) {
let open = response.data.ohlc['open']
let close= response.data.ohlc['close']
let ltp = response.data['last_price']
let high = response.data.ohlc['high']
let low = response.data.ohlc['low']
vscode.window.showInformationMessage(`${tradingSymbol} LTP: ${ltp} Open: ${open} High: ${high} Low: ${low} Close: ${close}`)
}).catch(function(err) {
vscode.window.showErrorMessage(err.message)
})
}
function streamStock() {
// Cleanup the statusBar first to remove
// all older entries as otherwise new entries would
// stack up.
cleanup()
// Fetch `public_token` from cache
user_data = store.get('user_data')
let ticker = new KiteTicker(config['api_key'], user_data['user_id'], user_data['public_token'])
ticker.connect()
ticker.on("tick", setTick)
ticker.on("connect", subscribe)
function setTick(ticks) {
for (let tick of ticks) {
// for each instrument create a new statusBarItem
let status = vscode.window.createStatusBarItem(1,100)
statusItems.push(status)
status.text = `${tick['Token']} : LTP ${tick['LastTradedPrice']}`
status.show()
}
}
function subscribe() {
let items = config['live_stocks']
ticker.subscribe(items)
ticker.setMode(ticker.modeFull, items)
}
}
function activate(context) {
let login = vscode.commands.registerCommand('extension.initializeKite', function () {
kc.requestAccessToken(config['request_token'], config['api_secret'])
.then(function(response) {
vscode.window.showInformationMessage("Logged In")
store.set('user_data', {public_token:response.data['public_token'], user_data:response.data['user_id']})
})
.catch(function(err) {
vscode.window.showErrorMessage(err.message)
})
})
let quote = vscode.commands.registerCommand('extension.getQuote', function (stock) {
vscode.window.showInformationMessage(stock)
for (let tradingSymbol of config['symbols']) {
fetchQuote(tradingSymbol)
}
})
let stream = vscode.commands.registerCommand('extension.streamData', function (stock) {
streamStock()
})
let margin = vscode.commands.registerCommand('extension.fetchMargin', function () {
fetchMargin()
})
context.subscriptions.push(margin)
context.subscriptions.push(login)
context.subscriptions.push(stream)
context.subscriptions.push(quote)
context.subscriptions.push(vscode.workspace.onDidChangeConfiguration(refresh))
}
exports.activate = activate
function deactivate() {
}
exports.deactivate = deactivate