Skip to content

Commit

Permalink
Merge pull request #42 from Overmuse/SR/fix_claim_allocation
Browse files Browse the repository at this point in the history
only allocate claims to lots of same sign
  • Loading branch information
SebRollen authored Jul 21, 2021
2 parents 8ec16cb + a0fcd8e commit ce88fb1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "order-manager"
version = "7.0.1"
version = "7.0.2"
authors = ["Sebastian Rollen <seb@overmu.se>"]
edition = "2018"

Expand Down
21 changes: 16 additions & 5 deletions src/types/allocation.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::{Claim, Lot, Owner};
use num_traits::Signed;
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use std::convert::TryFrom;
Expand Down Expand Up @@ -72,6 +73,10 @@ pub fn split_lot(claims: &[Claim], lot: &Lot) -> Vec<Allocation> {
if dollars.is_zero() {
continue;
}
if dollars.signum() != remaining_basis.signum() {
// Only allocate buys to buys and sells to sells
continue;
}
let mut allocated_dollars = dollars.abs().min(remaining_basis.abs());
if dollars.is_sign_negative() {
allocated_dollars.set_sign_negative(true)
Expand Down Expand Up @@ -135,10 +140,16 @@ mod test {
"A".into(),
None,
"AAPL".into(),
AmountSpec::Dollars(Decimal::new(400, 0)),
AmountSpec::Dollars(Decimal::new(-400, 0)),
),
Claim::new(
"B".into(),
None,
"AAPL".into(),
AmountSpec::Dollars(Decimal::new(400, 0)),
),
Claim::new(
"C".into(),
Some("B2".into()),
"AAPL".into(),
AmountSpec::Shares(Decimal::new(25, 1)),
Expand All @@ -150,8 +161,8 @@ mod test {
allocations[0],
Allocation {
id: allocations[0].id,
owner: Owner::Strategy("A".into(), None),
claim_id: Some(claims[0].id),
owner: Owner::Strategy("B".into(), None),
claim_id: Some(claims[1].id),
lot_id: lot.id,
ticker: "AAPL".into(),
shares: Decimal::new(4, 0),
Expand All @@ -162,8 +173,8 @@ mod test {
allocations[1],
Allocation {
id: allocations[1].id,
owner: Owner::Strategy("B".into(), Some("B2".into())),
claim_id: Some(claims[1].id),
owner: Owner::Strategy("C".into(), Some("B2".into())),
claim_id: Some(claims[2].id),
lot_id: lot.id,
ticker: "AAPL".into(),
shares: Decimal::new(25, 1),
Expand Down

0 comments on commit ce88fb1

Please sign in to comment.