Skip to main content

SpreadBuilder

Struct SpreadBuilder 

Source
pub struct SpreadBuilder { /* private fields */ }
Expand description

Spread/Combo builder

Implementations§

Source§

impl SpreadBuilder

Source

pub fn new() -> Self

Create an empty spread builder ready to accept legs.

Source§

impl SpreadBuilder

Source

pub fn add_leg( self, contract_id: impl Into<ContractId>, action: LegAction, ) -> LegBuilder

Begin configuring a new leg for the spread.

Source

pub fn long_calendar( self, near_id: impl Into<ContractId>, far_id: impl Into<ContractId>, ) -> Self

Long calendar spread: sell the near-dated leg, buy the far-dated leg.

Buy the combo to open. Both legs normally share strike and right.

Source

pub fn short_calendar( self, near_id: impl Into<ContractId>, far_id: impl Into<ContractId>, ) -> Self

Short calendar spread: buy the near-dated leg, sell the far-dated leg.

Buy the combo to open. Both legs normally share strike and right.

Source

pub fn vertical( self, long_id: impl Into<ContractId>, short_id: impl Into<ContractId>, ) -> Self

Vertical spread: buy the long leg, sell the short leg.

For an iron condor, use iron_condor.

use ibapi::contracts::Contract;

let (long_call, short_call) = (100, 105);
let bull_call = Contract::spread().symbol("AAPL").vertical(long_call, short_call).build()?;
Source

pub fn straddle( self, call_id: impl Into<ContractId>, put_id: impl Into<ContractId>, ) -> Self

Long straddle: buy a call and a put with the same strike and expiry.

Buy the combo to open.

use ibapi::contracts::Contract;

let (call, put) = (100, 101);
let straddle = Contract::spread().symbol("AAPL").straddle(call, put).build()?;
Source

pub fn strangle( self, call_id: impl Into<ContractId>, put_id: impl Into<ContractId>, ) -> Self

Long strangle: buy an out-of-the-money call and put with the same expiry and different strikes.

Buy the combo to open. The legs are those of straddle; only the strikes differ.

use ibapi::contracts::Contract;

let (call, put) = (110, 90);
let strangle = Contract::spread().symbol("AAPL").strangle(call, put).build()?;
Source

pub fn butterfly(self, legs: ButterflyLegs) -> Self

Long butterfly: buy 1 lower, sell 2 middle, buy 1 upper.

All legs share right and expiry. Buy the combo to open. The legs are named fields, so a wing cannot be swapped with the body by argument order.

use ibapi::contracts::{ButterflyLegs, Contract};

let butterfly = Contract::spread()
    .symbol("AAPL")
    .butterfly(ButterflyLegs {
        lower: 100.into(),
        middle: 105.into(),
        upper: 110.into(),
    })
    .build()?;
Source

pub fn iron_condor(self, legs: IronCondorLegs) -> Self

Iron condor: a put vertical plus a call vertical, each long the wing and short the body.

Legs are added as buy long put, sell short put, buy long call, sell short call. Buy the combo to open: it opens for a credit, so the limit price is negative. TWS rejects a positive price with 201 (“Guaranteed-to-Lose”). The legs are named fields because four ids in a fixed order compile when swapped and build a different strategy.

use ibapi::contracts::{Contract, IronCondorLegs};

let iron_condor = Contract::spread()
    .symbol("AAPL")
    .iron_condor(IronCondorLegs {
        long_put: 100.into(),
        short_put: 105.into(),
        short_call: 110.into(),
        long_call: 115.into(),
    })
    .build()?;
Source

pub fn collar(self, legs: CollarLegs) -> Self

Collar: buy the stock, buy a put below it as a floor, sell a call above it as a cap.

Legs are added as buy stock (ratio shares_per_contract), buy put, sell call. Buy the combo to open. The limit price is per combo, a debit of about shares_per_contract × spot + put − call.

§Examples
use ibapi::contracts::{CollarLegs, Contract};

let collar = Contract::spread()
    .symbol("AAPL")
    .collar(CollarLegs {
        stock: 265598.into(),
        put: 100.into(),
        call: 101.into(),
        shares_per_contract: 100,
    })
    .build()?;
Source

pub fn symbol(self, symbol: impl Into<Symbol>) -> Self

Set the combo’s symbol. TWS requires it to place the combo and rejects one without it with 321.

Usually the underlying. For a combo across underlyings, TWS’s samples join them with a comma ("IBKR,MCD").

§Examples
use ibapi::contracts::Contract;

let (long_call, short_call) = (100, 105);
let bull_call = Contract::spread().symbol("AAPL").vertical(long_call, short_call).build()?;
assert_eq!(bull_call.symbol.to_string(), "AAPL");
Source

pub fn in_currency(self, currency: impl Into<Currency>) -> Self

Override the spread currency, useful for non-USD underlyings.

Source

pub fn on_exchange(self, exchange: impl Into<Exchange>) -> Self

Route the spread order to a specific exchange.

Source

pub fn build(self) -> Result<Contract, Error>

Finalize the spread contract, returning an error if no legs were added.

A combo built without symbol builds, but TWS rejects it with 321.

Trait Implementations§

Source§

impl Clone for SpreadBuilder

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SpreadBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for SpreadBuilder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.