Skip to content

Commit dc091dc

Browse files
author
Wil Boayue
committed
cleanup README
1 parent 190591d commit dc091dc

File tree

3 files changed

+35
-41
lines changed

3 files changed

+35
-41
lines changed

README.md

+19-25
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,6 @@ fn main() {
189189
### Placing Orders
190190

191191
```rust
192-
use ibapi::contracts::Contract;
193-
use ibapi::orders::{order_builder, Action, OrderNotification};
194-
use ibapi::Client;
195-
196192
pub fn main() {
197193
let connection_url = "127.0.0.1:4002";
198194
let client = Client::connect(connection_url, 100).expect("connection to TWS failed!");
@@ -205,11 +201,11 @@ pub fn main() {
205201

206202
let subscription = client.place_order(order_id, &contract, &order).expect("place order request failed!");
207203

208-
for notice in subscription {
209-
if let OrderNotification::ExecutionData(data) = notice {
204+
for event in &subscription {
205+
if let PlaceOrder::ExecutionData(data) = event {
210206
println!("{} {} shares of {}", data.execution.side, data.execution.shares, data.contract.symbol);
211207
} else {
212-
println!("{:?}", notice);
208+
println!("{:?}", event);
213209
}
214210
}
215211
}
@@ -259,7 +255,6 @@ Some TWS API calls do not have a unique request ID and are mapped back to the in
259255
To avoid this issue, you can use a model of one client per thread. This ensures that each client instance handles only its own messages, reducing potential conflicts:
260256

261257
```rust
262-
use std::sync::Arc;
263258
use std::thread;
264259

265260
use ibapi::contracts::Contract;
@@ -273,7 +268,7 @@ fn main() {
273268
for (symbol, client_id) in symbols {
274269
let handle = thread::spawn(move || {
275270
let connection_url = "127.0.0.1:4002";
276-
let client = Arc::new(Client::connect(connection_url, client_id).expect("connection to TWS failed!"));
271+
let client = Client::connect(connection_url, client_id).expect("connection to TWS failed!");
277272

278273
let contract = Contract::stock(symbol);
279274
let subscription = client
@@ -304,29 +299,28 @@ use ibapi::market_data::realtime::{BarSize, WhatToShow};
304299
use ibapi::{Client, Error};
305300

306301
fn main() {
307-
env_logger::init();
308-
309302
let connection_url = "127.0.0.1:4002";
310303
let client = Client::connect(connection_url, 100).expect("connection to TWS failed!");
311304

312305
let contract = Contract::stock("AAPL");
313-
stream_bars(&client, &contract);
314-
}
315306

316-
// Request real-time bars data with 5-second intervals
317-
fn stream_bars(client: &Client, contract: &Contract) {
318-
let subscription = client
319-
.realtime_bars(&contract, BarSize::Sec5, WhatToShow::Trades, false)
320-
.expect("realtime bars request failed!");
307+
loop {
308+
// Request real-time bars data with 5-second intervals
309+
let subscription = client
310+
.realtime_bars(&contract, BarSize::Sec5, WhatToShow::Trades, false)
311+
.expect("realtime bars request failed!");
321312

322-
for bar in &subscription {
323-
// Process each bar here (e.g., print or use in calculations)
324-
println!("bar: {bar:?}");
325-
}
313+
for bar in &subscription {
314+
// Process each bar here (e.g., print or use in calculations)
315+
println!("bar: {bar:?}");
316+
}
317+
318+
if let Some(Error::ConnectionReset) = subscription.error() {
319+
println!("Connection reset. Retrying stream...");
320+
continue;
321+
}
326322

327-
if let Some(Error::ConnectionReset) = subscription.error() {
328-
println!("Connection reset. Retrying stream...");
329-
stream_bars(client, contract);
323+
break;
330324
}
331325
}
332326
```

examples/readme_multi_threading_2.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::sync::Arc;
21
use std::thread;
32

43
use ibapi::contracts::Contract;
@@ -14,7 +13,7 @@ fn main() {
1413
for (symbol, client_id) in symbols {
1514
let handle = thread::spawn(move || {
1615
let connection_url = "127.0.0.1:4002";
17-
let client = Arc::new(Client::connect(connection_url, client_id).expect("connection to TWS failed!"));
16+
let client = Client::connect(connection_url, client_id).expect("connection to TWS failed!");
1817

1918
let contract = Contract::stock(symbol);
2019
let subscription = client

examples/stream_retry.rs

+15-14
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,23 @@ fn main() {
99
let client = Client::connect(connection_url, 100).expect("connection to TWS failed!");
1010

1111
let contract = Contract::stock("AAPL");
12-
stream_bars(&client, &contract);
13-
}
1412

15-
// Request real-time bars data with 5-second intervals
16-
fn stream_bars(client: &Client, contract: &Contract) {
17-
let subscription = client
18-
.realtime_bars(&contract, BarSize::Sec5, WhatToShow::Trades, false)
19-
.expect("realtime bars request failed!");
13+
loop {
14+
// Request real-time bars data with 5-second intervals
15+
let subscription = client
16+
.realtime_bars(&contract, BarSize::Sec5, WhatToShow::Trades, false)
17+
.expect("realtime bars request failed!");
2018

21-
for bar in &subscription {
22-
// Process each bar here (e.g., print or use in calculations)
23-
println!("bar: {bar:?}");
24-
}
19+
for bar in &subscription {
20+
// Process each bar here (e.g., print or use in calculations)
21+
println!("bar: {bar:?}");
22+
}
23+
24+
if let Some(Error::ConnectionReset) = subscription.error() {
25+
println!("Connection reset. Retrying stream...");
26+
continue;
27+
}
2528

26-
if let Some(Error::ConnectionReset) = subscription.error() {
27-
println!("Connection reset. Retrying stream...");
28-
stream_bars(client, contract);
29+
break;
2930
}
3031
}

0 commit comments

Comments
 (0)