This commit is contained in:
2024-05-13 12:03:03 -04:00
parent cd51c17a09
commit 1f0a51e3b9

View File

@@ -16,7 +16,7 @@ trait Dumpable {
impl Dumpable for Offer { impl Dumpable for Offer {
fn dump(&self) { fn dump(&self) {
println!("Giving {} to get {}",fxp_to_fp(self.sell_qty),fxp_to_fp(self.buy_qty)); println!("Giving {}/{} to get {}",fxp_to_fp(self.sell_remain),fxp_to_fp(self.sell_qty),fxp_to_fp(self.buy_qty));
} }
} }
@@ -67,38 +67,31 @@ impl Market {
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
let sell_rate=fxp_div(buy_qty,sell_qty); let sell_rate=fxp_div(sell_qty,buy_qty);
let ap=(buy_type,sell_type); let ap=(buy_type,sell_type);
while sell_qty>0 { let mut lc=3;
while sell_qty>0 && lc>0 {
if let Some(asks)=self.offers.get_mut(&ap) { if let Some(asks)=self.offers.get_mut(&ap) {
if asks.v.len()>0 { if asks.v.len()>0 {
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 transact=std::cmp::min(sell_qty,fxp_mult(elt.buy_qty,fxp_div(elt.sell_remain,elt.sell_qty)));
let sell_transact=sell_qty; elt.sell_remain-=fxp_div(transact,ask_rate);
let transact=std::cmp::min(ask_transact,sell_transact); // 48000 USD buy_qty-=fxp_div(transact,ask_rate);
println!("Before: elt.sell_remain={} sell_qty={} transact={}",fxp_to_fp(elt.sell_remain),fxp_to_fp(sell_qty),fxp_to_fp(transact));
elt.sell_remain-=transact;
sell_qty-=transact; sell_qty-=transact;
println!("After: elt.sell_remain={} sell_qty={}",elt.sell_remain,sell_qty); if elt.sell_remain==0 { println!("Popping asks"); asks.pop(); }
if elt.sell_remain==0 {
println!("Popping asks");
asks.pop();
}
} else { break; } } else { break; }
*/
}
else { break; }
} else { break; } } else { break; }
} else { break; }
lc-=1;
} }
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); println!("Inserting new offer with sell_qty {}, buy_qty {}",fxp_to_fp(sell_qty),fxp_to_fp(buy_qty));
// 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, buy_qty:buy_qty } );
}
} }
} }
} }
@@ -115,8 +108,8 @@ impl PartialOrd for Offer {
impl PartialEq for Offer { impl PartialEq for Offer {
fn eq(&self, other:&Self) -> bool { fn eq(&self, other:&Self) -> bool {
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.sell_qty/other.buy_qty;
ord0==ord1 ord0==ord1
} }
} }
@@ -180,21 +173,43 @@ impl<T: Dumpable + std::cmp::PartialOrd + std::fmt::Debug> PQueue<T> {
} }
} }
fn fxp_mult(n0:u64, n1:u64) ->u64 { (n0>>32)*(n1>>32)+(n0>>32)*(n1&0xFFFFFFFF)+(n0&0xFFFFFFFF)*(n1>>32)+(n0&0xFFFFFFFF)*(n1&0xFFFFFFFF) } //fn fxp_mult(n0:u64, n1:u64) ->u64 { (((n0>>32)*(n1>>32))<<64)+(((n0>>32)*(n1&0xFFFFFFFF))<<32)+(((n0&0xFFFFFFFF)*(n1>>32))<<32)+(n0&0xFFFFFFFF)*(n1&0xFFFFFFFF) }
fn fxp_div(n0:u64, n1:u64) -> u64 { fxp_mult(n0,fxp_recip(n1)) } fn fxp_div(n0:u64, n1:u64) -> u64 { fxp_mult(n0,fxp_recip(n1)) }
fn fxp_from_int(n:u32) -> u64 { (n as u64)<<32 } fn fxp_from_int(n:u32) -> u64 { (n as u64)<<32 }
fn fxp_recip(n:u64) -> u64 { 0xFFFFFFFFFFFFFFFFu64/n } fn fxp_recip(n:u64) -> u64 { 0xFFFFFFFFFFFFFFFFu64/n }
fn fxp_to_fp(n:u64) -> f64 { (n as f64)/(1u64<<32) as f64 } fn fxp_to_fp(n:u64) -> f64 { (n as f64)/(1u64<<32) as f64 }
fn fxp_mult(a: u64, b: u64) -> u64 {
let a_high = a >> 32;
let a_low = a & 0xFFFFFFFF;
let b_high = b >> 32;
let b_low = b & 0xFFFFFFFF;
let high_high = a_high * b_high;
let high_low = a_high * b_low;
let low_high = a_low * b_high;
let low_low = a_low * b_low;
let mid_term = high_low + low_high;
let carry = mid_term >> 32;
let result_high = high_high + carry;
let result_low = (mid_term << 32) + low_low;
(result_high << 32) | (result_low >> 32)
}
fn main() { fn main() {
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 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 usd=m.register_asset("USD"); let usd=m.register_asset("USD");
let btc=m.register_asset("BTC"); let btc=m.register_asset("BTC");
let n=64000u64<<32; let n=64000u64<<32;
println!("Testing recip: {} {} {}",fxp_to_fp(n),fxp_recip(n),fxp_to_fp(fxp_recip(fxp_recip(n)))); println!("This should be 64000: {} or in raw form {}",fxp_to_fp(n),n);
m.make_offer(btc,usd,fxp_from_int(1),fxp_from_int(1)); println!("This should be 1/64000: {} or in raw form {}",fxp_to_fp(fxp_recip(n)),fxp_recip(n));
println!("This should be 64000: {} or in raw form {}",fxp_to_fp(fxp_recip(fxp_recip(n))),fxp_recip(fxp_recip(n)));
println!("This should be 1: {} or in raw form ",fxp_to_fp(fxp_mult(fxp_recip(fxp_from_int(64000)),fxp_from_int(64000))));
println!("{}*{}={}",fxp_to_fp(fxp_from_int(3)),fxp_to_fp(fxp_from_int(5)),fxp_to_fp(fxp_mult(fxp_from_int(3),fxp_from_int(5))));
m.make_offer(btc,usd,fxp_from_int(1),fxp_from_int(60000));
m.dump(); m.dump();
m.make_offer(usd,btc,fxp_from_int(64000),fxp_from_int(64000)); // Buy 1 BTC for 64000 USD m.make_offer(usd,btc,fxp_from_int(128000),fxp_from_int(2)); // Buy 1 BTC for 64000 USD
m.dump(); m.dump();
} }