Skip to content

Commit

Permalink
Add example illustrating how to submit an order
Browse files Browse the repository at this point in the history
This change adds an example that illustrates how to use the crate to
instantiate an Alpaca client object and subsequently submits a limit
order using it.
  • Loading branch information
d-e-s-o committed Apr 20, 2020
1 parent af2f1bf commit c2d3d03
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Unreleased
----------
- Added example illustrating how to submit a limit order
- Bumped `http-endpoint` dependency to `0.2`


Expand Down
34 changes: 34 additions & 0 deletions examples/order.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (C) 2020 Daniel Mueller <[email protected]>
// SPDX-License-Identifier: GPL-3.0-or-later

use apca::api::v2::order;
use apca::ApiInfo;
use apca::Client;

use num_decimal::Num;

#[tokio::main]
async fn main() {
// Requires the following environment variables to be present:
// - APCA_API_KEY_ID -> your API key
// - APCA_API_SECRET_KEY -> your secret key
//
// Optionally, the following variable is honored:
// - APCA_API_BASE_URL -> the API base URL to use (set to
// https://api.alpaca.markets for live trading)
let api_info = ApiInfo::from_env().unwrap();
let client = Client::new(api_info);

// Create request for a limit order for AAPL with a limit price of USD
// 100.
let request = order::OrderReqInit {
type_: order::Type::Limit,
limit_price: Some(Num::from(100)),
..Default::default()
}
// We want to go long on AAPL, buying a single share.
.init("AAPL", order::Side::Buy, 1);

let order = client.issue::<order::Post>(request).await.unwrap();
println!("Created order {}", order.id.to_hyphenated_ref());
}

0 comments on commit c2d3d03

Please sign in to comment.