-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions-single-trading.js
113 lines (98 loc) · 2.84 KB
/
options-single-trading.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
// node options-single-trading.js "200717 BIDU Call 115"
require("dotenv").config({ path: __dirname + "/.env" });
const puppeteer = require("puppeteer");
const LoggerModule = require("./logger");
function countDecimals(value) {
if (Math.floor(value) === value) return 0;
return value.toString().split(".")[1].length || 0;
}
function getStrikeInNasdaqUrlForOptions(strikeValue) {
const maxDecimalDigits = 3;
const maxStrikeDigitCount = 8;
let strike = strikeValue.replace(".", "");
const decimalDigits = countDecimals(parseFloat(strikeValue));
let allowedDecimalDigits = maxDecimalDigits - decimalDigits;
while (allowedDecimalDigits) {
strike += "0";
allowedDecimalDigits--;
}
let remainingDigitCount = maxStrikeDigitCount - strike.length;
while (remainingDigitCount) {
strike = "0" + strike;
remainingDigitCount--;
}
return strike;
}
function sprintf(format) {
for (var i = 1; i < arguments.length; i++) {
format = format.replace(/%s/, arguments[i]);
}
return format;
}
function getNasdaqUrlForOptions(expiry, scripName, callPut, strike) {
scripName = scripName.toLowerCase();
callPut = callPut.toLowerCase();
strike = getStrikeInNasdaqUrlForOptions(strike);
console.log(
sprintf(
"https://old.nasdaq.com/symbol/%s/option-chain/%s%s%s-%s-%s",
scripName,
expiry,
callPut[0].toUpperCase(),
strike,
scripName,
callPut
)
);
return sprintf(
"https://old.nasdaq.com/symbol/%s/option-chain/%s%s%s-%s-%s",
scripName,
expiry,
callPut[0].toUpperCase(),
strike,
scripName,
callPut
);
}
const params = process.argv[2];
(async () => {
const logger = new LoggerModule();
process.on("unhandledRejection", (err) => {
logger
.sendMessageToSlack("Jaysomaney Caught exception: " + err.toString())
.then(() => {
process.exit();
});
});
const browser = await puppeteer.launch({
headless: true,
ignoreHTTPSErrors: true,
});
const page = await browser.newPage();
const paramsList = params.split(",");
const lastSaleList = [];
for (let index = 0; index < paramsList.length; index++) {
let [expiry, symbol, callPut, strike] = paramsList[index].split(" ");
const pageUrl = getNasdaqUrlForOptions(expiry, symbol, callPut, strike);
try {
await page.goto(pageUrl);
} catch (exc) {
logger
.sendMessageToSlack("Jaysomaney " + pageUrl + " unable to load")
.then(() => {
process.exit();
});
}
let lastSale = await page.evaluate(() => {
var lastSaleRef = document.querySelector("#opdetails-last > .floatR > b");
if (lastSaleRef !== null) {
return lastSaleRef.innerText;
}
return "undefined";
});
lastSaleList.push(lastSale);
}
console.log(lastSaleList.join(" "));
await browser.close();
process.exit();
})();