using System; using System.ComponentModel; namespace sharp.trading { public abstract class OrderBook : MarshalByRefObject { public VolumeRate[] Bids { get; protected set; } public VolumeRate[] Asks { get; protected set; } public Market Market { get; private set; } public OrderBook(Market market) { Market = market; } public abstract void Refresh(); public string MarketName { get; private set; } public string MarketCurrency { get; private set; } public string BaseCurrency { get; private set; } public double CurrentBid { get { return this.Bids[this.Bids.Length - 1].Price; } } public double CurrentAsk { get { return this.Asks[0].Price; } } public Tuple getVolumePrice(double volume){ double volbuy = volume; double volsell = volume; double pricebuy = 0; double pricesell = 0; for (int n = 0; ;n++){ double avail; if (volbuy > 0) { avail = volbuy < Asks[n].Volume ? volbuy : Asks[n].Volume; pricebuy += avail * Asks[n].Price; volbuy -= avail; } if (volsell > 0) { avail = volsell < Bids[Bids.Length - 1 - n].Volume ? volsell : Bids[Bids.Length - 1 - n].Volume; pricesell += avail * Bids[Bids.Length - 1 - n].Price; volsell -= avail; } if ((volbuy <= 0) && (volsell <= 0)) { break; } } return new Tuple(pricebuy, pricesell); } public OrderbookVolume[] calculateVolumes(double[] volumes) { OrderbookVolume[] ovl = new OrderbookVolume[volumes.Length]; int bidend = Bids.Length - 1; for (int vn = 0; vn < volumes.Length; vn++){ ovl[vn] = new OrderbookVolume(); } for (int n = 0; ; n++) { bool done = true; for (int vn = 0; vn < volumes.Length; vn++) { double toBuy = volumes[vn] - ovl[vn].VolumeBuy; double toSell = volumes[vn] - ovl[vn].VolumeSell; if ((toBuy <= 0) && (toSell <= 0)){ continue; } if (toBuy > Asks[n].Volume){ toBuy = Asks[n].Volume; } if (toSell > Bids[bidend - n].Volume){ toSell = Bids[bidend - n].Volume; } ovl[vn].PriceBuy += Asks[n].Price * toBuy; ovl[vn].VolumeBuy += toBuy; ovl[vn].PriceSell += Bids[bidend - n].Price * toSell; ovl[vn].VolumeSell += toSell; done = false; } if (done){ break; } } return ovl; } } }