-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example illustrating how to submit an order
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
Showing
2 changed files
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} |