This commit is contained in:
2024-05-09 12:08:26 -10:00
parent a396c43e71
commit cd51c17a09

View File

@@ -63,7 +63,7 @@ impl Market {
} }
} }
fn make_offer(&mut self, sell_type:i32, buy_type:i32, sell_qty_initial:u64, buy_qty_initial:u64) // Dollars, Bitcoin, 64000, 1 fn make_offer(&mut self, sell_type:i32, buy_type:i32, sell_qty_initial:u64, buy_qty_initial:u64) // Dollars, Bitcoin, 64000, 1
{ // Comments assume someone is asking 48000 USD for 1 Bitcoin. Then someone comes along and bids $64000 for 1 Bitcoin (sells 64000 USD to buy 1 Bitcoin. { // Comments assume someone is already selling 1 Bitcoin for 48000 USD (Bitcoin, Dollars, 1, 48000). Then someone comes along and bids $64000 for 1 Bitcoin (sells 64000 USD to buy 1 Bitcoin.
println!("Making offer to sell {} {} and get {} {}",fxp_to_fp(sell_qty_initial),self.number_to_name(sell_type),fxp_to_fp(buy_qty_initial),self.number_to_name(buy_type)); println!("Making offer to sell {} {} and get {} {}",fxp_to_fp(sell_qty_initial),self.number_to_name(sell_type),fxp_to_fp(buy_qty_initial),self.number_to_name(buy_type));
let mut sell_qty=sell_qty_initial; // 64000 Dollars let mut sell_qty=sell_qty_initial; // 64000 Dollars
let mut buy_qty=buy_qty_initial; // 1 Bitcoin let mut buy_qty=buy_qty_initial; // 1 Bitcoin
@@ -75,7 +75,7 @@ impl Market {
let elt=&mut asks.v[0]; let elt=&mut asks.v[0];
let ask_rate=fxp_div(elt.buy_qty,elt.sell_qty); let ask_rate=fxp_div(elt.buy_qty,elt.sell_qty);
if sell_rate>=ask_rate { // Transact at ask_rate if sell_rate>=ask_rate { // Transact at ask_rate
let ask_transact =fxp_mult(elt.sell_remain,fxp_div(elt.sell_price,elt.sell_qty)); // 48000 USD /* let ask_transact =fxp_mult(elt.sell_remain,fxp_div(elt.sell_price,elt.sell_qty)); // 48000 USD
let sell_transact=sell_qty; let sell_transact=sell_qty;
let transact=std::cmp::min(ask_transact,sell_transact); // 48000 USD let transact=std::cmp::min(ask_transact,sell_transact); // 48000 USD
println!("Before: elt.sell_remain={} sell_qty={} transact={}",fxp_to_fp(elt.sell_remain),fxp_to_fp(sell_qty),fxp_to_fp(transact)); println!("Before: elt.sell_remain={} sell_qty={} transact={}",fxp_to_fp(elt.sell_remain),fxp_to_fp(sell_qty),fxp_to_fp(transact));
@@ -87,23 +87,26 @@ impl Market {
asks.pop(); asks.pop();
} }
} else { break; } } else { break; }
} else { break; } */
}
else { break; }
} else { break; } } else { break; }
} }
if sell_qty>0 { if sell_qty>0 {
let ap=(sell_type,buy_type); let ap=(sell_type,buy_type);
if let None=self.offers.get_mut(&ap) { self.offers.insert(ap,PQueue::new()); } if let None=self.offers.get_mut(&ap) { self.offers.insert(ap,PQueue::new()); }
let mut bids=self.offers.get_mut(&ap).unwrap(); let mut bids=self.offers.get_mut(&ap).unwrap();
let sell_price=fxp_div(fxp_mult(sell_price,sell_qty),sell_qty_initial); // let sell_price=fxp_div(fxp_mult(sell_price,sell_qty),sell_qty_initial);
bids.insert(Offer { sell_qty:sell_qty, sell_remain:sell_qty, sell_price:sell_price } ); // bids.insert(Offer { sell_qty:sell_qty, sell_remain:sell_qty, sell_price:sell_price } );
}
} }
} }
} }
impl PartialOrd for Offer { impl PartialOrd for Offer {
fn partial_cmp(&self, other:&Self) -> Option<Ordering> { fn partial_cmp(&self, other:&Self) -> Option<Ordering> {
let ord0=self .sell_price/self .sell_qty; let ord0=self .sell_qty/self .buy_qty;
let ord1=other.sell_price/other.sell_qty; let ord1=other.buy_qty /other.sell_qty;
if ord0<ord1 { Some(Ordering::Less) } if ord0<ord1 { Some(Ordering::Less) }
else if ord0>ord1 { Some(Ordering::Greater) } else if ord0>ord1 { Some(Ordering::Greater) }
else { Some(Ordering::Equal) } else { Some(Ordering::Equal) }
@@ -118,6 +121,7 @@ impl PartialEq for Offer {
} }
} }
struct PQueue<T: Dumpable> { struct PQueue<T: Dumpable> {
v: Vec<T>, v: Vec<T>,
} }