Skip to content

Commit

Permalink
wrote more option api functionalities
Browse files Browse the repository at this point in the history
  • Loading branch information
osamahan999 committed Jan 5, 2021
1 parent a6c3786 commit a5022bc
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 35 deletions.
63 changes: 47 additions & 16 deletions build/routes/stockDataRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,34 +60,65 @@ router.route('/getStockQuote').get(function (req, res) {
});
});
/**
* TODO: options router
*
* 1. get option expirations
* 2. get options for symbol with said expirations
* 3. return #2
*
* Takes in a stock symbol
*
* Returns an array of JSON of options for said stock
* Gets you all expiration dates
* @param {string} symbol
* @returns {array<string>} dates
*/
router.route('/getExpirations').get(function (req, res) {
var stock = xss(req.query.symbol);
axios.get("https://sandbox.tradier.com/v1/markets/options/expirations", {
params: {
'symbol': stock
},
headers: {
'Authorization': 'Bearer ' + api.getToken(),
'Accept': 'application/json'
}
}).then(function (response) {
res.json(response.data.expirations.date);
}).catch(function (error) {
res.status(400).json("Error getting dates");
});
});
/**
* Gets option chains for a specific symbol with specific expiration
*
* @param {string} symbol
* @param {string} expiration
* @param {string} optionType //'call' for calls, 'put' for puts, 'all' for both
*
* @returns {array<JSON>} option chain
*/
router.route('/getOptions').get(function (req, res) {
router.route('/getOptionsOnDate').get(function (req, res) {
var stock = xss(req.query.symbol);
axios.get("https://sandbox.tradier.com/v1/markets/options/lookup", {
var expiration = xss(req.query.expiration);
var optionType = xss(req.query.optionType);
axios.get("https://sandbox.tradier.com/v1/markets/options/chains", {
params: {
'underlying': stock
'symbol': stock,
'expiration': expiration,
'greeks': true
},
headers: {
'Authorization': 'Bearer ' + api.getToken(),
'Accept': 'application/json'
}
}).then(function (response) {
var options = response.data.symbols[0].options;
var len = options.length;
len > 2100 ? res.json(options.slice(0, 2100)) : res.json(options);
var options = response.data.options.option;
/**
* If optionType is call or put, return the options filtered for the specified type
*/
if (optionType == 'call' || optionType == 'put') {
res.json(options.filter(function (option) { return option.option_type.includes(optionType); }));
}
else if (optionType == 'any') {
res.json(options);
}
else {
res.status(400).json("Invalid option type");
}
}).catch(function (err) {
res.status(400).json(err);
res.status(400).json("Error getting options");
});
});
module.exports = router;
72 changes: 53 additions & 19 deletions routes/stockDataRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,46 +90,80 @@ router.route('/getStockQuote').get((req: Request, res: Response) => {
})




/**
* TODO: options router
*
* 1. get option expirations
* 2. get options for symbol with said expirations
* 3. return #2
*
* Takes in a stock symbol
*
* Returns an array of JSON of options for said stock
*
*
* Gets you all expiration dates
* @param {string} symbol
* @returns {array<string>} dates
*/
router.route('/getExpirations').get((req: Request, res: Response) => {
let stock: string = xss(req.query.symbol);

router.route('/getOptions').get((req: Request, res: Response) => {

let stock: string = xss(req.query.symbol);
axios.get("https://sandbox.tradier.com/v1/markets/options/expirations", {
params: {
'symbol': stock
},
headers: {
'Authorization': 'Bearer ' + api.getToken(),
'Accept': 'application/json'
}
}).then((response: AxiosResponse) => {
res.json(response.data.expirations.date);
}).catch((error: AxiosError) => {
res.status(400).json("Error getting dates");
})

})


/**
* Gets option chains for a specific symbol with specific expiration for either call, put, or both
*
* @param {string} symbol
* @param {string} expiration
* @param {string} optionType //'call' for calls, 'put' for puts, 'all' for both
*
* @returns {array<JSON>} option chain
*/
router.route('/getOptionsOnDate').get((req: Request, res: Response) => {

let stock: string = xss(req.query.symbol);
let expiration: string = xss(req.query.expiration);
let optionType: string = xss(req.query.optionType);

axios.get("https://sandbox.tradier.com/v1/markets/options/lookup", {
axios.get("https://sandbox.tradier.com/v1/markets/options/chains", {
params: {
'underlying': stock
'symbol': stock,
'expiration': expiration,
'greeks': true
},
headers: {
'Authorization': 'Bearer ' + api.getToken(),
'Accept': 'application/json'
}
}).then((response: AxiosResponse) => {
let options: Array<JSON> = response.data.symbols[0].options;
let len = options.length;

len > 2100 ? res.json(options.slice(0, 2100)) : res.json(options);
let options: Array<JSON> = response.data.options.option;

/**
* If optionType is call or put, return the options filtered for the specified type
*/
if (optionType == 'call' || optionType == 'put') {
res.json(options.filter((option: JSON | any) => option.option_type.includes(optionType)))
}
else if (optionType == 'any') {
res.json(options);
} else {
res.status(400).json("Invalid option type")
}



}).catch((err: AxiosError) => {

res.status(400).json(err);
res.status(400).json("Error getting options");

})

Expand Down

0 comments on commit a5022bc

Please sign in to comment.