-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
167 lines (149 loc) · 6.38 KB
/
app.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
var express = require('express')
bodyParser = require('body-parser')
, moment = require('moment')
, format = require('string-format')
, fs = require('fs')
, request = require('request')
, Zip = require('adm-zip')
, pg = require('pg')
, copyFrom = require('pg-copy-streams').from
, app = express()
, _ = require("underscore");
app.use(bodyParser.urlencoded());
app.use(bodyParser.json());
format.extend(String.prototype);
//eg.: DERIVATIVES/2015/OCT/fo07OCT2015bhav.csv.zip
var bhavFileBaseURL = 'http://www.nseindia.com/content/historical/DERIVATIVES/{0}/{1}/fo{2}{1}{0}bhav.csv.zip';
var bhavFileName = 'fo{2}{1}{0}bhav.csv';
var bhavFilesZipDir = "./tmp/bhavFilesZip/";
var bhavFilesCsvDir = "./tmp/bhavFilesCsv/";
var bhavFileRequestHeader = {"User-Agent":"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11","Referer":"http://www.nseindia.com/products/content/all_daily_reports.htm","Accept-Encoding":"gzip,deflate,sdch","encoding":"null","Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8","Cookie":"cookie"};
// var conString = "postgres://username:password@localhost/database";
var conString = "postgres://postgres:cronj123@localhost/skt";
function copyToDB(fileName){
pg.connect(conString, function(err, client, done) {
var stream = client.query(copyFrom('COPY bhav FROM STDIN CSV HEADER'));
var fileStream = fs.createReadStream(bhavFilesCsvDir + fileName);
fileStream.pipe(stream).on('finish', function(){
console.log("Data Imported to PG: ", fileName);
done();
});
});
}
function extractFile(fileName, success){
var zip = new Zip(bhavFilesZipDir + fileName + ".zip");
zip.extractAllTo(bhavFilesCsvDir);
console.log("CSV Extracted: ", fileName);
success(fileName);
}
var ctr = 0;
function importBhavFile(year, month, day){
ctr += 1000;
setTimeout(function(){
var fileName = bhavFileName.format(year, month, day);
var fileURL = bhavFileBaseURL.format(year, month, day);
var r = request({ url: fileURL, headers: bhavFileRequestHeader });
r.on('response', function (resp) {
if(resp.statusCode == 200){
r.pipe(fs.createWriteStream(bhavFilesZipDir + fileName + ".zip"))
.on('close', function(){
console.log("ZIP Downloaded: ", fileName);
extractFile(fileName, copyToDB);
});
}
});
},ctr);
}
function importBhavFilesBetween(startDate, endDate){
for(var date = startDate; date.isBefore(endDate); date.add(1,'days'))
importBhavFile(date.format('YYYY'),date.format('MMM').toUpperCase(),date.format('DD'));
}
app.get('/import', function (req, res) {
// importBhavFilesBetween(moment('2015-09-30'), moment('2015-10-29'));
importBhavFilesBetween(moment('2014-10-30'), moment('2015-10-29'));
res.send('Importing NSE India Data...');
});
app.get('/getSymbols',function(req,res){
//this initializes a connection pool
//it will keep idle connections open for a (configurable) 30 seconds
//and set a limit of 20 (also configurable)
pg.connect(conString, function(err, client, done) {
if(err) {
return console.error('error fetching client from pool', err);
}
console.log('fetching symbols');
client.query('SELECT DISTINCT symbol from bhav order by symbol',function(err, result) {
//call `done()` to release the client back to the pool
done();
res.send(result.rows);
if(err) {
return console.error('error running query', err);
}
//output: 1
});
});
});
app.post('/getDataOfSymbol',function(req,res){
//this initializes a connection pool
//it will keep idle connections open for a (configurable) 30 seconds
//and set a limit of 20 (also configurable)
pg.connect(conString, function(err, client, done) {
if(err) {
return console.error('error fetching client from pool', err);
}
client.query("SELECT instrument, option_typ, strike_pr, close, timestamp, expiry_dt from bhav where symbol = $1 and timestamp between $2 and $3 and expiry_dt < timestamp + INTERVAL '30 days' order by timestamp", [req.body.symbol, req.body.from, req.body.to],function(err, result) {
//call `done()` to release the client back to the pool
done();
var resultdata = result.rows;
// res.send(resultdata);
// var t0 = performance.now();
var datedData = _.groupBy(resultdata, 'timestamp');
var straddleData = _.map(datedData, function(values, date) {
//find the first future entry
var futidxes = _.filter(values, function(value) {
return value.instrument == "FUTIDX" || value.instrument == "FUTSTK";
});
var futidx = _.reduce(futidxes, function(memo, value){
return (new Date(value.expiry_dt) < new Date(memo.expiry_dt) ? value : memo);
})
//close price
var close = futidx.close;
//find closest call entry
//todo: initial memo is first element, error when no strike point found
var call = _.reduce(values, function(memo, value) {
if (value.option_typ == "CE")
return (!memo.strike_pr || Math.abs(value.strike_pr - close) < Math.abs(memo.strike_pr - close) ? value : memo);
return memo;
}, {strike_pr: undefined});
//find closest put entry
var put = _.reduce(values, function(memo, value) {
if (value.option_typ == "PE")
return (!memo.strike_pr || Math.abs(value.strike_pr - close) < Math.abs(memo.strike_pr - close) ? value : memo);
return memo;
}, {strike_pr: undefined});
return {
futidx: futidx,
call: call,
put: put,
date: new Date(date),
straddle: call.close + put.close
};
});
// console.log(straddleData);
//performance
// var t1 = performance.now();
// console.log("Call to doSomething took " + (t1 - t0) + " milliseconds.")
res.send(_.sortBy(straddleData, 'date'));
if(err) {
return console.error('error running query', err);
}
//output: 1
});
});
});
app.use('/client', express.static(__dirname + '/client'));
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});