public ProposedOrderLeg(ThreadSafeAsk ask, int legLevel, bool doLock = false, ThreadSafeAskLeg leg = null) { Ask = ask; LegLevel = legLevel; DoLock = doLock; Leg = leg; }
public long SetSellQuantity(long targetValue, bool doLock = false, ThreadSafeAskLeg leg = null, long buyCap = long.MaxValue) { lock (sync) { long sellRatio = leg != null ? leg.SellRatio : SellRatio.Value; long buyRatio = leg != null ? leg.BuyRatio : BuyRatio.Value; long aQ = IsBuy ? availableQuantity : (((double)buyRatio / (double)sellRatio) * availableQuantity).ToFlooredInt(); long available = IsBuy ? (((double)sellRatio / (double)buyRatio) * availableQuantity).ToFlooredInt() : availableQuantity; if (leg != null && leg.AvailableQuantity != null) { if (leg.IsBuy) { aQ = leg.AvailableQuantity < aQ ? leg.AvailableQuantity.Value : aQ; } else { aQ = leg.AvailableQuantity < available ? (((double)buyRatio / (double)sellRatio) * leg.AvailableQuantity.Value).ToFlooredInt() : aQ; } available = (((double)sellRatio / (double)buyRatio) * aQ).ToFlooredInt(); } long sellCap = (((double)buyRatio / (double)sellRatio) * available).ToFlooredInt(); available = available > sellCap ? sellCap : available; long value = available >= targetValue ? targetValue : available; long buyValue = (((double)buyRatio / (double)sellRatio) * value).ToCeilingInt(); if (value > 0 && (value < MinSellQuantity || buyValue < MinBuyQuantity || (!AllowPartialFill && ((IsBuy && Ask.BuyQuantity != buyValue) || (!IsBuy && Ask.SellQuantity != value))) || buyValue <= 0)) { value = 0; buyValue = 0; } if (doLock) { lockQuantity += IsBuy ? buyValue : value; if (leg != null) { leg.SetLock(leg.IsBuy ? buyValue : value); } if (targetValue < 0 && PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("AvailableQuantity")); } } return(value); } }
public void RealizeLock(long quanity, ThreadSafeAskLeg leg = null) { lock (sync) { if (IsBuy) { Ask.BuyQuantity -= quanity; } else { Ask.SellQuantity -= quanity; } lockQuantity -= quanity; if (leg != null) { double ratio = leg.IsBuy ? (double)leg.BuyRatio / (double)leg.SellRatio : (double)leg.SellRatio / (double)leg.BuyRatio; long legLock = leg.IsBuy ? (ratio * quanity).ToCeilingInt() : (ratio * quanity).ToFlooredInt(); leg.RealizeLock(legLock); } } }