Skip to content

Add createCombo method and example. Fix #20. #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions client.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
import {
request_placeOrder
} from './requests/order.js';
import Contract from './contract.js';



Expand Down Expand Up @@ -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;
}
}


Expand Down
27 changes: 27 additions & 0 deletions examples/create-combo.js
Original file line number Diff line number Diff line change
@@ -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);
}