diff --git a/client.js b/client.js index 4b2ff94..a4e9a5d 100644 --- a/client.js +++ b/client.js @@ -17,6 +17,7 @@ import { import { request_placeOrder } from './requests/order.js'; +import Contract from './contract.js'; @@ -2352,6 +2353,38 @@ class Client { this._sendFieldsetRateLimited(msg) */ } + + + + /** + * Create a combo out of an array of position sizes (positive to BUY, negative to SELL) and contracts. + * + * For example, to create a straddle, first create the put and call options, then call + * createCombo([[1, putOption], [1, callOption]]). + * + * @param {Array<[number, Contract]>} legs - array of [+/-X, contract] legs for the combo + */ + async createCombo(legs) { + // Assume that all contracts have the same symbol, exchange and currency + const combo = Contract.combo(legs[0][1].symbol, legs[0][1].exchange, legs[0][1].currency); + + // Get the conIds + const promises = legs.map(([_n, l]) => this.getContractDetails(l)); + const details = await Promise.all(promises); + + combo.comboLegs = []; + for (let i = 0; i < legs.length; i++) { + const [positionAndOperation, contract] = legs[i]; + if (!details[i][0].contract.conId) + throw new Error(`createCombo didn't find conId for leg #${i}`); + combo.comboLegs.push({ + conId: details[i][0].contract.conId, + ratio: Math.abs(positionAndOperation), + action: positionAndOperation > 0 ? 'BUY' : 'SELL', + }); + } + return combo; + } } diff --git a/examples/create-combo.js b/examples/create-combo.js new file mode 100644 index 0000000..c69e785 --- /dev/null +++ b/examples/create-combo.js @@ -0,0 +1,27 @@ +import { Client, Contract } from '../index.js'; + +const api = new Client({ + host: '127.0.0.1', + port: 7496, +}); + + + +try { + const put = Contract.option({ + symbol: 'GOOG', + lastTradeDateOrContractMonth: '20230616', + strike: 2700, + right: 'P', + }); + const call = { ...put, right: 'C' }; + const straddle = await api.createCombo([ + [1, put], + [1, call], + ]); + console.log('Straddle:', straddle); + process.exit(0); +} catch (e) { + console.error('ERROR:', e.message); + process.exit(1); +}