Match fractional share lots and preserve order quantities across restart
This commit is contained in:
@@ -8,12 +8,17 @@ pub(crate) fn rate(db: &Connection, market: &str) -> Result<i64> {
|
||||
|
||||
// Price and rate both have six decimals. Round the fee only after the
|
||||
// complete fill, to the nearest ten micros, with the published minimum.
|
||||
pub(crate) fn micros(rate: i64, price_micros: i64, shares: i64) -> i64 {
|
||||
let numerator = i128::from(shares)
|
||||
#[cfg(test)]
|
||||
fn micros(rate: i64, price_micros: i64, shares: i64) -> i64 {
|
||||
lot_fee(rate, price_micros, shares * 100)
|
||||
}
|
||||
|
||||
pub(crate) fn lot_fee(rate: i64, price_micros: i64, lots: i64) -> i64 {
|
||||
let numerator = i128::from(lots)
|
||||
* i128::from(rate)
|
||||
* i128::from(price_micros)
|
||||
* i128::from(1_000_000 - price_micros);
|
||||
let denominator = 10_000_000_000_000;
|
||||
let denominator = 1_000_000_000_000_000;
|
||||
if numerator < denominator {
|
||||
return 0;
|
||||
}
|
||||
@@ -23,7 +28,7 @@ pub(crate) fn micros(rate: i64, price_micros: i64, shares: i64) -> i64 {
|
||||
pub(crate) fn total(rate: i64, fills: &[Fill]) -> i64 {
|
||||
fills
|
||||
.iter()
|
||||
.map(|f| micros(rate, f.price * 10000, f.quantity))
|
||||
.map(|f| lot_fee(rate, f.price * 10000, f.quantity))
|
||||
.sum()
|
||||
}
|
||||
|
||||
@@ -37,8 +42,8 @@ pub(crate) fn fit_budget(rate: i64, fills: &mut Vec<Fill>, budget: i64) {
|
||||
let mut count = 0;
|
||||
for f in fills.iter_mut() {
|
||||
let cost = |qty: i64| {
|
||||
i128::from(qty) * i128::from(f.price) * 10000
|
||||
+ i128::from(micros(rate, f.price * 10000, qty))
|
||||
i128::from(qty) * i128::from(f.price) * 100
|
||||
+ i128::from(lot_fee(rate, f.price * 10000, qty))
|
||||
};
|
||||
if cost(f.quantity) <= i128::from(remaining) {
|
||||
remaining -= cost(f.quantity) as i64;
|
||||
|
||||
Reference in New Issue
Block a user