This commit is contained in:
2024-05-19 13:15:58 -04:00
parent da9e907932
commit d65eee0a51

View File

@@ -143,6 +143,7 @@ impl Market {
while buy_qty>FiNum::new(0) && self.offers.contains_key(&ap) && self.offers.get(&ap).unwrap().v.len()>0 {
let elt=&mut self.offers.get_mut(&ap).unwrap().v[0];
if sell_qty_initial/buy_qty_initial>=elt.buy_qty/elt.sell_qty { // Transact at ask_rate
println!("Transacting at {}/{}, not at {}/{}",elt.buy_qty,elt.sell_qty,sell_qty_initial,buy_qty_initial);
let qty=std::cmp::min(elt.sell_remain,buy_qty);
elt.sell_remain-=qty;
buy_qty-=qty;
@@ -202,7 +203,7 @@ impl<T: Dumpable + std::cmp::PartialOrd + std::fmt::Debug> PQueue<T> {
fn bubble_up(&mut self, pos: usize) {
if pos>0 {
let parent=(pos-1)/2;
if self.v[parent]<self.v[pos] {
if self.v[parent]>self.v[pos] {
self.v.swap(parent,pos);
self.bubble_up(parent);
}
@@ -213,9 +214,9 @@ impl<T: Dumpable + std::cmp::PartialOrd + std::fmt::Debug> PQueue<T> {
let child0=pos*2+1;
let child1=pos*2+2;
if child0<self.v.len() {
if self.v[pos]<self.v[child0] { pivot=child0; }
if self.v[pos]>self.v[child0] { pivot=child0; }
if child1<self.v.len() {
if self.v[pivot]<self.v[child1] { pivot=child1; }
if self.v[pivot]>self.v[child1] { pivot=child1; }
}
if pivot!=pos {
self.v.swap(pivot,pos);
@@ -247,6 +248,14 @@ impl<T: Dumpable + std::cmp::PartialOrd + std::fmt::Debug> PQueue<T> {
fn main() {
let mut pq:PQueue<Offer>=PQueue::new();
pq.insert(Offer { owner:1, sell_qty:12.into(), sell_remain:12.into(), buy_qty:4.into() } );
pq.insert(Offer { owner:1, sell_qty:12.into(), sell_remain:12.into(), buy_qty:2.into() } );
pq.insert(Offer { owner:1, sell_qty:12.into(), sell_remain:12.into(), buy_qty:3.into() } );
println!("Pop: {}",pq.pop().unwrap().buy_qty);
println!("Pop: {}",pq.pop().unwrap().buy_qty);
println!("Pop: {}",pq.pop().unwrap().buy_qty);
let mut rng: StdRng=StdRng::seed_from_u64(1u64);
let mut m=Market::new(); // USD type is 1, EUR type is 2, BTC type is 10, ETH type is 11, SOL type is 12
let teppy=m.register_trader("Teppy");
@@ -261,12 +270,12 @@ fn main() {
if rng.gen_bool(0.5) {
sell_type=btc;
buy_type=usd;
sell_qty=rng.gen_range(2..=5).into();
sell_qty=1.into(); //rng.gen_range(2..=5).into();
buy_qty=sell_qty*rng.gen_range(60000..=70000).into();
} else {
sell_type=usd;
buy_type=btc;
buy_qty=rng.gen_range(2..=5).into();
buy_qty=1.into(); //rng.gen_range(2..=5).into();
sell_qty=buy_qty*rng.gen_range(60000..=70000).into();
}
println!("Index {} Sell {} {} to buy {} {} ({})",i,sell_qty,m.number_to_name(sell_type),buy_qty,m.number_to_name(buy_type),m.traders[seller as usize].name);