Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump dependencies, make it compile again #12

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
target
Cargo.lock
.secrets
.idea/
20 changes: 13 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@ name = "oandars"
readme = "README.md"
repository = "https://github.com/blankenshipz/oanda-rs"
version = "0.1.2"
edition = "2021"

[dependencies]
chrono = { version = "0.3", features = ["serde"] }
hyper = "0.10.9"
hyper-native-tls = "0.2.2"
serde = "0.9"
serde_derive = "0.9"
serde_json = "0.9"
ratelimit = "0.4.1"
chrono = { version = "0.4.24", features = ["serde"] }
hyper = { version = "0.14.25", features = ["client", "http1", "http2", "tcp"] }
hyper-tls = { version = "0.5.0", features = [] }
tokio = { version = "1", use-default-features = false, features = ["macros"] }
serde = "1"
serde_derive = "1"
serde_json = "1"
ratelimit = "0.5.1"

[features]
default = []
example = ["tokio/full"]
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ See the [examples](examples/) and the [documentation](https://docs.rs/oandars/0.
1. Create an `Oanda` test account and name it `Testv20`
1. Copy the `.secrets.sample` to `.secrets` and update the variables based on your new test account

### Running example

```sh
cargo run --example todays_candlesticks --feature example
```

### Running Tests

The test suite can be run with `docker-compose`:
Expand Down
36 changes: 22 additions & 14 deletions examples/todays_candlesticks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,33 @@ use chrono::prelude::*;

extern crate oandars;

use std::env;
use oandars::client::Client;
use std::env;

fn main() {
#[tokio::main]
async fn main() {
// Eastern Daylight Time - -04:00
let est = chrono::FixedOffset::west(4 * 60 * 60);
let start_of_day = chrono::NaiveTime::from_hms(8, 0, 0);
let start_of_day = UTC::today().naive_utc().and_time(start_of_day);
let start_of_day = est.from_local_datetime(&start_of_day)
let est = FixedOffset::west_opt(4 * 60 * 60).unwrap();
let start_of_day = NaiveTime::from_hms_opt(8, 0, 0).unwrap();
let start_of_day = Utc::now().date_naive().and_time(start_of_day);
let start_of_day = est
.from_local_datetime(&start_of_day)
.unwrap()
.with_timezone(&UTC);
.with_timezone(&Utc);
// Pull in the url for OANDA API and the API Key
// e.g https://api-fxpractice.oanda.com/v3
let url = env::var("OANDA_API_URL").unwrap();
let key = env::var("OANDA_API_KEY").unwrap();
// create a new client
let url = env::var("OANDA_API_URL").expect("OANDA_API_URL must be set");
let key = env::var("OANDA_API_KEY").expect("OANDA_API_KEY must be set");
// Create a new client
let client = Client::new(&url, &key);

// Get the first set of candles for today for this instrument
let mut results = client.pricing_for("EUR_USD".to_string(), start_of_day)
let mut results = client
.pricing_for("EUR_USD".to_string(), start_of_day)
.with_include_first(false)
.execute();
.execute()
.await
.unwrap();

while results.candles.len() > 0 {
// For Each Candle in our new Set Print the Open price and the Timestamp
Expand All @@ -40,8 +45,11 @@ fn main() {
}

// Load the next set of Candles
results = client.pricing_for("EUR_USD".to_string(), results.candles.last().unwrap().time)
results = client
.pricing_for("EUR_USD".to_string(), results.candles.last().unwrap().time)
.with_include_first(false)
.execute();
.execute()
.await
.unwrap();
}
}
116 changes: 62 additions & 54 deletions src/account/details.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
use chrono::datetime::DateTime;
use chrono::UTC;
use chrono::DateTime;
use chrono::Utc;
use serde_derive::Deserialize;

#[derive(Deserialize)]
pub enum TradeState {
OPEN,
CLOSED,
CLOSE_WHEN_TRADEABLE
#[serde(rename = "OPEN")]
Open,
#[serde(rename = "CLOSED")]
Closed,
#[serde(rename = "CLOSE_WHEN_TRADEABLE")]
CloseWhenTradeable,
}

#[derive(Deserialize)]
pub enum OrderState {
PENDING,
FILLED,
TRIGGERED,
CANCELLED
#[serde(rename = "PENDING")]
Pending,
#[serde(rename = "FILLED")]
Filled,
#[serde(rename = "TRIGGERED")]
Triggered,
#[serde(rename = "CANCELLED")]
Cancelled,
}

#[derive(Deserialize)]
Expand All @@ -23,7 +31,7 @@ pub struct ClientExtensions {
/// A tag associated with the Order/Trade
pub tag: String,
/// A comment associated with the Order/Trade
pub comment: String
pub comment: String,
}

#[derive(Deserialize)]
Expand All @@ -34,34 +42,34 @@ pub struct TradeSummary {
/// The Trade’s Instrument.
pub instrument: String,
/// The execution price of the Trade.
pub price: f32,
pub price: String,
/// The date/time when the Trade was opened.
pub open_time: DateTime<UTC>,
pub open_time: DateTime<Utc>,
/// The current state of the Trade.
pub state: TradeState,
/// The initial size of the Trade. Negative values indicate a short Trade,
/// and positive values indicate a long Trade.
pub initial_units: f32,
pub initial_units: String,
/// The number of units currently open for the Trade. This value is reduced
/// to 0.0 as the Trade is closed.
pub current_units: f32,
pub current_units: String,
/// The total profit/loss realized on the closed portion of the Trade.
#[serde(rename = "realizedPL")]
pub realized_pl: f32,
pub realized_pl: String,
/// The unrealized profit/loss on the open portion of the Trade.
#[serde(rename = "unrealizedPL")]
pub unrealized_pl: f32,
pub unrealized_pl: String,
/// The average closing price of the Trade. Only present if the Trade has
/// been closed or reduced at least once.
pub average_close_price: Option<f32>,
pub average_close_price: Option<String>,
/// The IDs of the Transactions that have closed portions of this Trade.
#[serde(rename = "closingTransactionIDs")]
pub closing_transaction_ids: Vec<String>,
/// The financing paid/collected for this Trade.
pub financing: f32,
pub financing: String,
/// The date/time when the Trade was fully closed. Only provided for Trades
/// whose state is CLOSED.
pub close_time: Option<DateTime<UTC>>,
pub close_time: Option<DateTime<Utc>>,
/// The client extensions of the Trade.
pub client_extensions: ClientExtensions,
/// ID of the Trade’s Take Profit Order, only provided if such an Order
Expand All @@ -74,32 +82,32 @@ pub struct TradeSummary {
/// ID of the Trade’s Trailing Stop Loss Order, only provided if such an
/// Order exists.
#[serde(rename = "trailingStopLossOrderID")]
pub trailing_stop_loss_order_id: Option<String>
pub trailing_stop_loss_order_id: Option<String>,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PositionSide {
/// Number of units in the position (negative value indicates short position,
/// positive indicates long position).
pub units: f32,
pub units: String,
/// Volume-weighted average of the underlying Trade open prices for the
/// Position.
pub average_price: f32,
pub average_price: String,
/// List of the open Trade IDs which contribute to the open Position.
#[serde(rename = "tradeIDs")]
pub trade_ids: Vec<String>,
/// Profit/loss realized by the PositionSide over the lifetime of the
/// Account.
pub pl: f32,
pub pl: String,
/// The unrealized profit/loss of all open Trades that contribute to this
/// PositionSide.
#[serde(rename = "unrealizedPL")]
pub unrealized_pl: f32,
pub unrealized_pl: String,
/// Profit/loss realized by the PositionSide since the Account’s resettablePL
/// was last reset by the client.
#[serde(rename = "resettablePL")]
pub resettable_pl: f32
pub resettable_pl: String,
}

#[derive(Deserialize)]
Expand All @@ -108,19 +116,19 @@ pub struct Position {
/// The Position’s Instrument.
pub instrument: String,
/// Profit/loss realized by the Position over the lifetime of the Account.
pub pl: f32,
pub pl: String,
/// The unrealized profit/loss of all open Trades that contribute to this
/// Position.
#[serde(rename = "unrealizedPL")]
pub unrealized_pl: f32,
pub unrealized_pl: String,
/// Profit/loss realized by the Position since the Account’s resettablePL was
/// last reset by the client.
#[serde(rename = "resettablePL")]
pub resettable_pl: f32,
pub resettable_pl: String,
/// The details of the long side of the Position.
pub long: PositionSide,
/// The details of the short side of the Position.
pub short: PositionSide
pub short: PositionSide,
}

#[derive(Deserialize)]
Expand All @@ -129,12 +137,12 @@ pub struct Order {
/// The Order’s identifier, unique within the Order’s Account.
pub id: String,
/// The time when the Order was created.
pub create_time: DateTime<UTC>,
pub create_time: DateTime<Utc>,
/// The current state of the Order.
pub state: OrderState,
/// The client extensions of the Order. Do not set, modify, or delete
/// clientExtensions if your account is associated with MT4.
pub client_extensions: ClientExtensions
pub client_extensions: ClientExtensions,
}

#[derive(Deserialize)]
Expand All @@ -149,34 +157,34 @@ pub struct Details {
pub currency: String,
/// The current balance of the Account. Represented in the Account’s home
/// currency.
pub balance: f32,
pub balance: String,
/// ID of the user that created the Account.
#[serde(rename = "createdByUserID")]
pub created_by_user_id: i32,
/// The date/time when the Account was created.
pub created_time: DateTime<UTC>,
pub created_time: DateTime<Utc>,
/// The total profit/loss realized over the lifetime of the Account.
/// Represented in the Account’s home currency.
pub pl: f32,
pub pl: String,
/// The total realized profit/loss for the Account since it was last reset by
/// the client. Represented in the Account’s home currency.
#[serde(rename = "resettablePL")]
pub resettable_pl: f32,
pub resettable_pl: String,
/// The date/time that the Account’s resettablePL was last reset.
#[serde(rename = "resettablePLTime")]
pub resettabled_pl_time: Option<DateTime<UTC>>,
pub resettabled_pl_time: Option<DateTime<Utc>>,
/// Client-provided margin rate override for the Account. The effective
/// margin rate of the Account is the lesser of this value and the OANDA
/// margin rate for the Account’s division. This value is only provided if a
/// margin rate override exists for the Account.
pub margin_rate: Option<f32>,
pub margin_rate: Option<String>,
/// The date/time when the Account entered a margin call state. Only provided
/// if the Account is in a margin call.
pub margin_call_enter_time: Option<DateTime<UTC>>,
pub margin_call_enter_time: Option<DateTime<Utc>>,
/// The number of times that the Account’s current margin call was extended.
pub margin_call_extension_count: Option<i32>,
/// The date/time of the Account’s last margin call extension.
pub last_margin_call_extension_time: Option<DateTime<UTC>>,
pub last_margin_call_extension_time: Option<DateTime<Utc>>,
/// The number of Trades currently open in the Account.
pub open_trade_count: i32,
/// The number of Positions currently open in the Account.
Expand All @@ -188,41 +196,41 @@ pub struct Details {
/// The total unrealized profit/loss for all Trades currently open in the
/// Account. Represented in the Account’s home currency.
#[serde(rename = "unrealizedPL")]
pub unrealized_pl: f32,
pub unrealized_pl: String,
/// The net asset value of the Account. Equal to Account balance +
/// unrealizedPL. Represented in the Account’s home currency.
#[serde(rename = "NAV")]
pub nav: f32,
pub nav: String,
/// Margin currently used for the Account. Represented in the Account’s home
/// currency.
pub margin_used: f32,
pub margin_used: String,
/// Margin available for Account. Represented in the Account’s home currency.
pub margin_available: f32,
pub margin_available: String,
/// The value of the Account’s open positions represented in the Account’s
/// home currency.
pub position_value: f32,
pub position_value: String,
/// The Account’s margin closeout unrealized PL.
#[serde(rename = "marginCloseoutUnrealizedPL")]
pub margin_closeout_unrealized_pl: f32,
pub margin_closeout_unrealized_pl: String,
/// The Account’s margin closeout NAV.
#[serde(rename = "marginCloseoutNAV")]
pub margin_closeout_nav: f32,
pub margin_closeout_nav: String,
/// The Account’s margin closeout margin used.
pub margin_closeout_margin_used: f32,
pub margin_closeout_margin_used: String,
/// The Account’s margin closeout percentage. When this value is 1.0 or above
/// the Account is in a margin closeout situation.
pub margin_closeout_percent: f32,
pub margin_closeout_percent: String,
/// The value of the Account’s open positions as used for margin closeout
/// calculations represented in the Account’s home currency.
pub margin_closeout_position_value: f32,
pub margin_closeout_position_value: String,
/// The current WithdrawalLimit for the account which will be zero or a
/// positive value indicating how much can be withdrawn from the account.
pub withdrawal_limit: f32,
pub withdrawal_limit: String,
/// The Account’s margin call margin used.
pub margin_call_margin_used: f32,
pub margin_call_margin_used: String,
/// The Account’s margin call percentage. When this value is 1.0 or above the
/// Account is in a margin call situation.
pub margin_call_percent: f32,
pub margin_call_percent: String,
/// The ID of the last Transaction created for the Account.
#[serde(rename = "lastTransactionID")]
pub last_transaction_id: String,
Expand All @@ -231,7 +239,7 @@ pub struct Details {
/// The details all Account Positions.
pub positions: Vec<Position>,
/// The details of the Orders currently pending in the Account.
pub orders: Vec<Order>
pub orders: Vec<Order>,
}

#[derive(Deserialize)]
Expand All @@ -241,5 +249,5 @@ pub struct AccountDetails {
pub account: Details,
/// The ID of the most recent Transaction created for the Account.
#[serde(rename = "lastTransactionID")]
pub last_transaction_id: String
pub last_transaction_id: String,
}
Loading