Skip to content

Commit

Permalink
Request historical data (#82)
Browse files Browse the repository at this point in the history
Implements request historical data
  • Loading branch information
wboayue authored Apr 16, 2023
1 parent c87ca54 commit 0b5bda0
Show file tree
Hide file tree
Showing 27 changed files with 1,142 additions and 277 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ edition = "2021"
authors = ["Wil Boayue <[email protected]>"]
description = "A synchronous implementation of the Interactive Brokers TWS API."
readme = "README.md"
homepage = "https://github.com/wboayue/rust-ibapi/"
repository = "https://github.com/wboayue/rust-ibapi/"
documentation = "https://docs.rs/ibapi/latest/ibapi/"
license = "MIT"
keywords = ["algo-trading", "interactive-brokers", "tws"]
categories = ["finance", "api-bindings"]
Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,16 @@ impl BreakoutChannel {
* [market_rule](https://docs.rs/ibapi/latest/ibapi/struct.Client.html#method.market_rule)
* [matching_symbols](https://docs.rs/ibapi/latest/ibapi/struct.Client.html#method.matching_symbols)

### Market Data
### Historical Market Data

* [head_timestamp](https://docs.rs/ibapi/latest/ibapi/struct.Client.html#method.head_timestamp)
* [historical_data](https://docs.rs/ibapi/latest/ibapi/struct.Client.html#method.historical_data)
* [historical_data_ending_now](https://docs.rs/ibapi/latest/ibapi/struct.Client.html#method.historical_data_ending_now)
* [historical_schedules](https://docs.rs/ibapi/latest/ibapi/struct.Client.html#method.historical_schedules)
* [historical_schedules_ending_now](https://docs.rs/ibapi/latest/ibapi/struct.Client.html#method.historical_schedules_ending_now)

### Realtime Market Data

* [realtime_bars](https://docs.rs/ibapi/latest/ibapi/struct.Client.html#method.realtime_bars)
* [tick_by_tick_all_last](https://docs.rs/ibapi/latest/ibapi/struct.Client.html#method.tick_by_tick_all_last)
* [tick_by_tick_bid_ask](https://docs.rs/ibapi/latest/ibapi/struct.Client.html#method.tick_by_tick_bid_ask)
Expand Down
15 changes: 15 additions & 0 deletions contributors/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## Run Coverage Report

https://github.com/taiki-e/cargo-llvm-cov

```bash
cargo install cargo-tarpaulin
cargo tarpaulin -o html
```

RUST_LOG=debug cargo run --bin find_contract_details

## Troubleshooting

`RUST_LOG=debug`
`IBAPI_RECORDING_DIR=/tmp`
2 changes: 1 addition & 1 deletion examples/head_timestamp.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clap::{arg, Command};

use ibapi::contracts::Contract;
use ibapi::market_data::historical::{self, WhatToShow};
use ibapi::market_data::historical::WhatToShow;
use ibapi::Client;

fn main() {
Expand Down
32 changes: 32 additions & 0 deletions examples/historical_data_ending_now.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use clap::{arg, Command};

use ibapi::contracts::Contract;
use ibapi::market_data::historical::{BarSize, ToDuration, WhatToShow};
use ibapi::Client;

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

let matches = Command::new("historical_data_ending_now")
.about("Gets last 7 days of daily data for given stock")
.arg(arg!(<STOCK_SYMBOL>).required(true))
.arg(arg!(--connection_string <VALUE>).default_value("127.0.0.1:4002"))
.get_matches();

let connection_string = matches.get_one::<String>("connection_string").expect("connection_string is required");
let stock_symbol = matches.get_one::<String>("STOCK_SYMBOL").expect("stock symbol is required");

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

let contract = Contract::stock(stock_symbol);

let historical_data = client
.historical_data_ending_now(&contract, 7.days(), BarSize::Day, WhatToShow::Trades, true)
.expect("historical data request failed");

println!("start_date: {}, end_date: {}", historical_data.start, historical_data.end);

for bar in &historical_data.bars {
println!("{bar:?}");
}
}
33 changes: 33 additions & 0 deletions examples/historical_schedules.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use clap::{arg, Command};
use time::macros::datetime;

use ibapi::contracts::Contract;
use ibapi::market_data::historical::ToDuration;
use ibapi::Client;

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

let matches = Command::new("historical_data")
.about("Get last 30 days of daily data for given stock")
.arg(arg!(<STOCK_SYMBOL>).required(true))
.arg(arg!(--connection_string <VALUE>).default_value("127.0.0.1:4002"))
.get_matches();

let connection_string = matches.get_one::<String>("connection_string").expect("connection_string is required");
let stock_symbol = matches.get_one::<String>("STOCK_SYMBOL").expect("stock symbol is required");

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

let contract = Contract::stock(stock_symbol);

let historical_data = client
.historical_schedules(&contract, datetime!(2023-04-15 0:00 UTC), 30.days())
.expect("historical schedule request failed");

println!("start: {:?}, end: {:?}", historical_data.start, historical_data.end);

for session in &historical_data.sessions {
println!("{session:?}");
}
}
32 changes: 32 additions & 0 deletions examples/historical_schedules_ending_now.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use clap::{arg, Command};

use ibapi::contracts::Contract;
use ibapi::market_data::historical::ToDuration;
use ibapi::Client;

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

let matches = Command::new("historical_schedules_ending_now")
.about("Gets last 7 days of schedules for given stock")
.arg(arg!(<STOCK_SYMBOL>).required(true))
.arg(arg!(--connection_string <VALUE>).default_value("127.0.0.1:4002"))
.get_matches();

let connection_string = matches.get_one::<String>("connection_string").expect("connection_string is required");
let stock_symbol = matches.get_one::<String>("STOCK_SYMBOL").expect("stock symbol is required");

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

let contract = Contract::stock(stock_symbol);

let schedule = client
.historical_schedules_ending_now(&contract, 7.days())
.expect("historical schedule request failed");

println!("start: {}, end: {}, time_zone: {}", schedule.start, schedule.end, schedule.time_zone);

for session in &schedule.sessions {
println!("{session:?}");
}
}
2 changes: 1 addition & 1 deletion src/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct Position {

// Subscribes to position updates for all accessible accounts.
// All positions sent initially, and then only updates as positions change.
pub(crate) fn positions<'a>(client: &'a Client) -> Result<impl Iterator<Item = Position> + 'a, Error> {
pub(crate) fn positions(client: &Client) -> Result<impl Iterator<Item = Position> + '_, Error> {
client.check_server_version(server_versions::ACCOUNT_SUMMARY, "It does not support position requests.")?;

let message = encoders::request_positions()?;
Expand Down
Loading

0 comments on commit 0b5bda0

Please sign in to comment.