Skip to content

Commit

Permalink
Merge pull request #31 from Overmuse/SR/normalize_intents
Browse files Browse the repository at this point in the history
normalize intents before sending
  • Loading branch information
SebRollen authored Sep 13, 2021
2 parents dba316c + 1769b63 commit f6da140
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 6 deletions.
7 changes: 4 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "trader"
version = "2.0.0"
version = "2.0.1"
authors = ["RollenRegistratorBot <[email protected]>"]
edition = "2018"

Expand All @@ -15,6 +15,7 @@ dotenv = "0.15"
futures = "0.3"
kafka-settings = { git = "ssh://[email protected]/Overmuse/kafka-settings", tag = "v0.3.3" }
rdkafka = {version = "0.26", features = ["ssl-vendored"]}
rust_decimal = "1.15.0"
sentry = "0.23"
serde = "1.0"
serde_json = "1.0"
Expand Down
70 changes: 68 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ use anyhow::{anyhow, Result};
use futures::StreamExt;
use kafka_settings::consumer;
use rdkafka::{message::OwnedMessage, Message};
use rust_decimal::prelude::*;
use tracing::{info, warn};
use trading_base::TradeIntent;
use trading_base::{OrderType, TradeIntent};

mod settings;
pub use settings::Settings;
Expand All @@ -23,6 +24,45 @@ fn parse_message(msg: OwnedMessage) -> Result<TradeIntent> {
}
}

fn normalize_intent(ti: &mut TradeIntent) {
let new_order_type = if ti.qty.is_positive() {
match ti.order_type {
OrderType::Market => OrderType::Market,
OrderType::Limit { limit_price } => OrderType::Limit {
limit_price: limit_price.round_dp_with_strategy(2, RoundingStrategy::ToZero),
},
OrderType::Stop { stop_price } => OrderType::Stop {
stop_price: stop_price.round_dp_with_strategy(2, RoundingStrategy::AwayFromZero),
},
OrderType::StopLimit {
stop_price,
limit_price,
} => OrderType::StopLimit {
stop_price: stop_price.round_dp_with_strategy(2, RoundingStrategy::AwayFromZero),
limit_price: limit_price.round_dp_with_strategy(2, RoundingStrategy::ToZero),
},
}
} else {
match ti.order_type {
OrderType::Market => OrderType::Market,
OrderType::Limit { limit_price } => OrderType::Limit {
limit_price: limit_price.round_dp_with_strategy(2, RoundingStrategy::AwayFromZero),
},
OrderType::Stop { stop_price } => OrderType::Stop {
stop_price: stop_price.round_dp_with_strategy(2, RoundingStrategy::ToZero),
},
OrderType::StopLimit {
stop_price,
limit_price,
} => OrderType::StopLimit {
stop_price: stop_price.round_dp_with_strategy(2, RoundingStrategy::ToZero),
limit_price: limit_price.round_dp_with_strategy(2, RoundingStrategy::AwayFromZero),
},
}
};
ti.order_type = new_order_type;
}

fn translate_intent(ti: TradeIntent) -> OrderIntent {
let (qty, side) = if ti.qty > 0 {
(ti.qty as usize, Side::Buy)
Expand Down Expand Up @@ -65,7 +105,8 @@ async fn execute_order(api: &Client, oi: &OrderIntent) -> Result<Order> {

#[tracing::instrument(name = "Received message", skip(api, msg))]
async fn handle_message(api: &Client, msg: OwnedMessage) -> Result<Order> {
let trade_intent = parse_message(msg)?;
let mut trade_intent = parse_message(msg)?;
normalize_intent(&mut trade_intent);
let order_intent = translate_intent(trade_intent);
again::retry(|| execute_order(api, &order_intent)).await
}
Expand Down Expand Up @@ -103,6 +144,31 @@ mod test {
use mockito::mock;
use rdkafka::message::Timestamp;

#[test]
fn test_normalize_intent() {
let mut ti = TradeIntent::new("AAPL", 100).order_type(OrderType::Limit {
limit_price: Decimal::new(7777, 3),
});
normalize_intent(&mut ti);
assert_eq!(
ti.order_type,
OrderType::Limit {
limit_price: Decimal::new(777, 2)
}
);

let mut ti = TradeIntent::new("AAPL", -100).order_type(OrderType::Limit {
limit_price: Decimal::new(7777, 3),
});
normalize_intent(&mut ti);
assert_eq!(
ti.order_type,
OrderType::Limit {
limit_price: Decimal::new(778, 2)
}
);
}

#[test]
fn unwrap_msg() {
let payload = r#"{
Expand Down

0 comments on commit f6da140

Please sign in to comment.