-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinalts.js
106 lines (101 loc) · 4.41 KB
/
finalts.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
import Alpaca from '@alpacahq/alpaca-trade-api';
const API_KEY = process.env.ALPACA_API_KEY;
const API_SECRET = process.env.ALPACA_API_SECRET;
const USE_POLYGON = false;
const alpaca = new Alpaca({
keyId: API_KEY,
secretKey: API_SECRET,
paper: true,
usePolygon: USE_POLYGON
});
import { default as mongodb } from 'mongodb';
let MongoClient = mongodb.MongoClient;
async function helloo() {
var db = await MongoClient.connect("mongodb://localhost:27017/");
var dbo = db.db("stocksinfo");
dbo.collection("sp500").find().sort({ "_id": 1 }).toArray(function (err, result) {
//console.log(result);
var data = [];
for (var i = 0; i < 504; i++) {
data.push(result[i].symbol);
}
//console.log(data);
alpaca.getAssets({ status: 'active' })
.then((response) => {
//console.log(response);
var complete = [], incomplete = [], count = 0;
for (var i in data) {
var flag = 0;
for (var j in response) {
if (response[j].symbol == data[i] && response[j].status == 'active' && response[j].tradable) {
flag = 1;
var temp = data[i];
complete.push(temp);
count++;
}
}
if (!flag) {
incomplete.push(data[i]);
}
}
//console.log(complete);
//console.log(count);
var final = [];
for (var k = 0; k < 200; k++) {
final.push(complete[k]);
}
var todayDate = new Date().toISOString().slice(0, 10);
alpaca.getBars('1Min', final, { limit: 1000, start: `${todayDate}T09:30:00-04:00`, end: `${todayDate}T16:00:00-04:00` })
.then((response) => {
var dummy = [];
for (var j in final) {
var len = response[final[j]].length;
dummy.push(response[final[j]][len - 1]);
}
dbo.collection("todayprices").insertMany(dummy, function (err, res) {
if (err) throw err;
//console.log(res);
})
})
.catch(err => console.log(err));
var final1 = [];
for (var k = 200; k < 400; k++) {
final1.push(complete[k]);
}
var todayDate = new Date().toISOString().slice(0, 10);
alpaca.getBars('1Min', final1, { limit: 1000, start: `${todayDate}T09:30:00-04:00`, end: `${todayDate}T16:00:00-04:00` })
.then((response) => {
var dummy = [];
for (var j in final1) {
var len = response[final1[j]].length;
dummy.push(response[final1[j]][len - 1]);
}
dbo.collection("todayprices").insertMany(dummy, function (err, res) {
if (err) throw err;
//console.log(res);
})
})
.catch(err => console.log(err));
var final2 = [];
for (var k = 400; k < count; k++) {
final2.push(complete[k]);
}
var todayDate = new Date().toISOString().slice(0, 10);
alpaca.getBars('1Min', final2, { limit: 1000, start: `${todayDate}T09:30:00-04:00`, end: `${todayDate}T16:00:00-04:00` })
.then((response) => {
var dummy = [];
for (var j in final2) {
var len = response[final2[j]].length;
dummy.push(response[final2[j]][len - 1]);
}
dbo.collection("todayprices").insertMany(dummy, function (err, res) {
if (err) throw err;
//console.log(res);
})
})
.catch(err => console.log(err));
})
.catch(err => console.log(err));
});
}
helloo();