-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprices.js
100 lines (93 loc) · 3.62 KB
/
prices.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
//var Alpaca = require('@alpacahq/alpaca-trade-api');
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;
import axios from 'axios';
import { AlpacaClient, AlpacaStream } from '@master-chief/alpaca'
const alpaca = new Alpaca({
keyId: API_KEY,
secretKey: API_SECRET,
paper: true,
usePolygon: USE_POLYGON
});
const client = new AlpacaClient({
credentials: {
key: API_KEY,
secret: API_SECRET,
// access_token: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
paper: true,
},
rate_limit: true,
})
import { default as mongodb } from 'mongodb';
let MongoClient = mongodb.MongoClient;
async function hello() {
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 = 500; i < 504; i++) {
data.push(result[i].symbol);
}
//console.log(data);
alpaca.getBars('1D', data, { limit: 1000, start: '2018-01-01T00:00:00-04:00', end: '2021-03-06T00:00:00-04:00' }).then((response) => {
var dummy = [];
for (var sym in data) {
//var dummy = [];
for (var bar in response[data[sym]]) {
var obj = {};
dummy.push({ symbol: data[sym], date: response[data[sym]][bar].startEpochTime, open: response[data[sym]][bar].openPrice, high: response[data[sym]][bar].highPrice, low: response[data[sym]][bar].lowPrice, close: response[data[sym]][bar].closePrice, volume: response[data[sym]][bar].volume });
//console.log(obj)
}
/*
try {
var res = await dbo.collection("prices").findOne({ symbol: data[sym] });
var finalData = res.data;
console.log(finalData);
var arr3 = [...finalData, ...dummy];
const update = {
"$set": {
"symbol": data[sym],
"data": arr3
}
};
var finalRes = await dbo.collection("prices").updateOne({ symbol: data[sym] }, update);
console.log(finalRes);
} catch (error) {
console.log(error);
}
*/
/*
dbo.collection("prices").findOne({ symbol: data[sym] })
.then((result) => {
var finalData = result.data;
var arr3 = [...finalData, ...dummy];
const update = {
"$set": {
"symbol": data[sym],
"data": arr3
}
};
dbo.collection("prices").updateOne({ symbol: data[sym] }, update)
.then((res) => console.log(res))
.catch((err) => console.log(err));
})
.catch(err => console.log(err));*/
}
dbo.collection("prices").insertMany(dummy, function (err, res) {
if (err) throw err;
console.log(res);
})
}).catch(err => console.log(err))
});
}
hello();
/*
async function hello() {
var res = await client.getClock();
console.log(res);
}
hello()
*/