diff --git a/src/main.rs b/src/main.rs index ae8ae5b..9c2d91f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 - { // 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)); let mut sell_qty=sell_qty_initial; // 64000 Dollars let mut buy_qty=buy_qty_initial; // 1 Bitcoin @@ -75,7 +75,7 @@ impl Market { let elt=&mut asks.v[0]; let ask_rate=fxp_div(elt.buy_qty,elt.sell_qty); 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 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)); @@ -87,23 +87,26 @@ impl Market { asks.pop(); } } else { break; } - } else { break; } + */ + } + else { break; } } else { break; } - } + } if sell_qty>0 { let ap=(sell_type,buy_type); if let None=self.offers.get_mut(&ap) { self.offers.insert(ap,PQueue::new()); } let mut bids=self.offers.get_mut(&ap).unwrap(); - 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 } ); - } - } - } +// 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 } ); + } + } + } +} impl PartialOrd for Offer { fn partial_cmp(&self, other:&Self) -> Option { - let ord0=self .sell_price/self .sell_qty; - let ord1=other.sell_price/other.sell_qty; + let ord0=self .sell_qty/self .buy_qty; + let ord1=other.buy_qty /other.sell_qty; if ord0ord1 { Some(Ordering::Greater) } else { Some(Ordering::Equal) } @@ -118,6 +121,7 @@ impl PartialEq for Offer { } } + struct PQueue { v: Vec, }