Skip to content

Commit

Permalink
Refactor tick by tick / Cleanup examples (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
wboayue authored Dec 23, 2024
1 parent 63d79a2 commit df44c02
Show file tree
Hide file tree
Showing 10 changed files with 248 additions and 146 deletions.
110 changes: 0 additions & 110 deletions examples/tick_by_tick.rs

This file was deleted.

41 changes: 41 additions & 0 deletions examples/tick_by_tick_all_last.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use std::time::Duration;

use ibapi::contracts::Contract;
use ibapi::market_data::realtime::LastTicks;
use ibapi::Client;

// This example demonstrates how to stream tick by tick data for the last price of a contract.

fn main() {
env_logger::init();

let connection_string = "127.0.0.1:4002";
println!("connecting to server @ {connection_string}");

let client = Client::connect(connection_string, 100).expect("connection failed");

let contract = Contract::stock("NVDA");
let ticks = client.tick_by_tick_all_last(&contract, 0, false).expect("failed to get ticks");

println!(
"streaming all last price for security_type: {:?}, symbol: {}",
contract.security_type, contract.symbol
);

for (i, tick) in ticks.timeout_iter(Duration::from_secs(10)).enumerate() {
match tick {
LastTicks::Trade(trade) => {
println!("{}: {i:?} {trade:?}", contract.symbol);
}
LastTicks::Notice(notice) => {
// server could send a notice if it doesn't recognize the contract
println!("error_code: {}, error_message: {}", notice.code, notice.message);
}
}
}

// check for errors during streaming
if let Some(error) = ticks.error() {
println!("error: {}", error);
}
}
41 changes: 41 additions & 0 deletions examples/tick_by_tick_bid_ask.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use std::time::Duration;

use ibapi::contracts::Contract;
use ibapi::market_data::realtime::BidAskTicks;
use ibapi::Client;

// This example demonstrates how to stream tick by tick data for the bid and ask price of a contract.

fn main() {
env_logger::init();

let connection_string = "127.0.0.1:4002";
println!("connecting to server @ {connection_string}");

let client = Client::connect(connection_string, 100).expect("connection failed");

let contract = Contract::stock("NVDA");
let ticks = client.tick_by_tick_bid_ask(&contract, 0, false).expect("failed to get ticks");

println!(
"streaming bid/ask price for security_type: {:?}, symbol: {}",
contract.security_type, contract.symbol
);

for (i, tick) in ticks.timeout_iter(Duration::from_secs(10)).enumerate() {
match tick {
BidAskTicks::BidAsk(bid_ask) => {
println!("{}: {i:?} {bid_ask:?}", contract.symbol);
}
BidAskTicks::Notice(notice) => {
// server could send a notice if it doesn't recognize the contract
println!("error_code: {}, error_message: {}", notice.code, notice.message);
}
}
}

// check for errors during streaming
if let Some(error) = ticks.error() {
println!("error: {}", error);
}
}
41 changes: 41 additions & 0 deletions examples/tick_by_tick_last.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use std::time::Duration;

use ibapi::contracts::Contract;
use ibapi::market_data::realtime::LastTicks;
use ibapi::Client;

// This example demonstrates how to stream tick by tick data for the last price of a contract.

fn main() {
env_logger::init();

let connection_string = "127.0.0.1:4002";
println!("connecting to server @ {connection_string}");

let client = Client::connect(connection_string, 100).expect("connection failed");

let contract = Contract::stock("NVDA");
let ticks = client.tick_by_tick_last(&contract, 0, false).expect("failed to get ticks");

println!(
"streaming last price for security_type: {:?}, symbol: {}",
contract.security_type, contract.symbol
);

for (i, tick) in ticks.timeout_iter(Duration::from_secs(10)).enumerate() {
match tick {
LastTicks::Trade(trade) => {
println!("{}: {i:?} {trade:?}", contract.symbol);
}
LastTicks::Notice(notice) => {
// server could send a notice if it doesn't recognize the contract
println!("error_code: {}, error_message: {}", notice.code, notice.message);
}
}
}

// check for errors during streaming
if let Some(error) = ticks.error() {
println!("error: {}", error);
}
}
41 changes: 41 additions & 0 deletions examples/tick_by_tick_midpoint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use std::time::Duration;

use ibapi::contracts::Contract;
use ibapi::market_data::realtime::MidpointTicks;
use ibapi::Client;

// This example demonstrates how to stream tick by tick data for the midpoint price of a contract.

fn main() {
env_logger::init();

let connection_string = "127.0.0.1:4002";
println!("connecting to server @ {connection_string}");

let client = Client::connect(connection_string, 100).expect("connection failed");

let contract = Contract::stock("NVDA");
let ticks = client.tick_by_tick_midpoint(&contract, 0, false).expect("failed to get ticks");

println!(
"streaming midpoint price for security_type: {:?}, symbol: {}",
contract.security_type, contract.symbol
);

for (i, tick) in ticks.timeout_iter(Duration::from_secs(10)).enumerate() {
match tick {
MidpointTicks::Midpoint(midpoint) => {
println!("{}: {i:?} {midpoint:?}", contract.symbol);
}
MidpointTicks::Notice(notice) => {
// server could send a notice if it doesn't recognize the contract
println!("error_code: {}, error_message: {}", notice.code, notice.message);
}
}
}

// check for errors during streaming
if let Some(error) = ticks.error() {
println!("error: {}", error);
}
}
10 changes: 5 additions & 5 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::accounts::{AccountSummaries, AccountUpdate, AccountUpdateMulti, Famil
use crate::contracts::{Contract, OptionComputation, SecurityType};
use crate::errors::Error;
use crate::market_data::historical::{self, HistogramEntry};
use crate::market_data::realtime::{self, Bar, BarSize, DepthMarketDataDescription, MarketDepths, MidPoint, TickTypes, WhatToShow};
use crate::market_data::realtime::{self, Bar, BarSize, DepthMarketDataDescription, MarketDepths, MidpointTicks, TickTypes, WhatToShow};
use crate::market_data::MarketDataType;
use crate::messages::{IncomingMessages, OutgoingMessages};
use crate::messages::{RequestMessage, ResponseMessage};
Expand Down Expand Up @@ -1100,7 +1100,7 @@ impl Client {
contract: &Contract,
number_of_ticks: i32,
ignore_size: bool,
) -> Result<Subscription<'a, realtime::Trade>, Error> {
) -> Result<Subscription<'a, realtime::LastTicks>, Error> {
realtime::tick_by_tick_all_last(self, contract, number_of_ticks, ignore_size)
}

Expand All @@ -1115,7 +1115,7 @@ impl Client {
contract: &Contract,
number_of_ticks: i32,
ignore_size: bool,
) -> Result<Subscription<'a, realtime::BidAsk>, Error> {
) -> Result<Subscription<'a, realtime::BidAskTicks>, Error> {
realtime::tick_by_tick_bid_ask(self, contract, number_of_ticks, ignore_size)
}

Expand All @@ -1130,7 +1130,7 @@ impl Client {
contract: &Contract,
number_of_ticks: i32,
ignore_size: bool,
) -> Result<Subscription<'a, realtime::Trade>, Error> {
) -> Result<Subscription<'a, realtime::LastTicks>, Error> {
realtime::tick_by_tick_last(self, contract, number_of_ticks, ignore_size)
}

Expand All @@ -1145,7 +1145,7 @@ impl Client {
contract: &Contract,
number_of_ticks: i32,
ignore_size: bool,
) -> Result<Subscription<'a, MidPoint>, Error> {
) -> Result<Subscription<'a, MidpointTicks>, Error> {
realtime::tick_by_tick_midpoint(self, contract, number_of_ticks, ignore_size)
}

Expand Down
Loading

0 comments on commit df44c02

Please sign in to comment.