Esempio n. 1
0
 static OrderBookAllocator()
 {
     if (!OrderBook.AllowOrderBookHistory)
     {
         return;
     }
     pool = new List <List <OrderBookEntry> >(100000);
     for (int i = 0; i < 100000; i++)
     {
         List <OrderBookEntry> list = new List <OrderBookEntry>(OrderBook.Depth);
         for (int j = 0; j < OrderBook.Depth; j++)
         {
             list[j] = new OrderBookEntry();
         }
         pool.Add(list);
     }
 }
Esempio n. 2
0
        void CalcVolume(List <OrderBookEntry> list, out double volume, out double exp, out double disp, int depth)
        {
            int count = Math.Min(depth, list.Count);
            int index = 0;

            volume = 0;
            exp    = 0;
            disp   = 0;
            for (int i = 0; i < list.Count; i++)
            {
                OrderBookEntry e = list[i];
                volume += e.Amount;
                exp    += e.Amount * e.Value;
                index++;
                if (index == count)
                {
                    break;
                }
            }
            exp /= list.Count;
            if (volume == 0)
            {
                exp  = 0;
                disp = 0;
                return;
            }

            index = 0;
            double invVolume = 1.0 / volume;
            double qexp      = exp * exp;

            for (int i = 0; i < list.Count; i++)
            {
                OrderBookEntry e = list[i];
                disp += (e.Value * e.Value) * e.Amount * invVolume - qexp;
                index++;
                if (index == count)
                {
                    break;
                }
            }
        }
Esempio n. 3
0
        protected void ApplyIncrementalUpdate(string rateString, string amountString, List <OrderBookEntry> list, bool ascending)
        {
            double value  = FastValueConverter.Convert(rateString);
            double amount = FastValueConverter.Convert(amountString);
            int    index  = 0;

            if (amount == 0)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    OrderBookEntry e = list[i];
                    if (IsEqual(e.Value, value))
                    {
                        list.Remove(e);
                        return;
                    }
                }
                if (EnableValidationOnRemove)
                {
                    IsDirty = true;
                }
                return;
            }
            if (ascending)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    OrderBookEntry e = list[i];
                    if (IsEqual(e.Value, value))
                    {
                        e.AmountString = amountString;
                        return;
                    }
                    if (e.Value > value)
                    {
                        OrderBookEntry ee = new OrderBookEntry()
                        {
                            ValueString = rateString, AmountString = amountString
                        };
                        list.Insert(index, ee);
                        return;
                    }
                    index++;
                }
            }
            else
            {
                for (int i = 0; i < list.Count; i++)
                {
                    OrderBookEntry e = list[i];
                    if (IsEqual(e.Value, value))
                    {
                        e.AmountString = amountString;
                        return;
                    }
                    if (e.Value < value)
                    {
                        OrderBookEntry ee = new OrderBookEntry()
                        {
                            ValueString = rateString, AmountString = amountString
                        };
                        list.Insert(index, ee);
                        return;
                    }
                    index++;
                }
            }
            OrderBookEntry addE = new OrderBookEntry()
            {
                ValueString = rateString, AmountString = amountString
            };

            list.Add(addE);
        }
Esempio n. 4
0
        protected void ApplyIncrementalUpdate(long id, OrderBookUpdateType type, string rateString, string amountString, List <OrderBookEntry> list, bool ascending)
        {
            int index = 0;

            if (type == OrderBookUpdateType.Remove)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    OrderBookEntry e = list[i];
                    if (e.Id == id)
                    {
                        list.Remove(e);
                        return;
                    }
                }
                if (EnableValidationOnRemove)
                {
                    IsDirty = true;
                }
                return;
            }
            double amount = FastValueConverter.Convert(amountString);

            if (type == OrderBookUpdateType.Modify)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    OrderBookEntry e = list[i];
                    if (e.Id == id)
                    {
                        e.Amount = amount;
                        return;
                    }
                }
                if (EnableValidationOnRemove)
                {
                    IsDirty = true;
                }
                return;
            }
            double value = FastValueConverter.Convert(rateString);

            if (ascending)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    OrderBookEntry e = list[i];
                    if (e.Value > value)
                    {
                        OrderBookEntry ee = new OrderBookEntry()
                        {
                            ValueString = rateString, AmountString = amountString, Id = id
                        };
                        list.Insert(index, ee);
                        return;
                    }
                    index++;
                }
            }
            else
            {
                for (int i = 0; i < list.Count; i++)
                {
                    OrderBookEntry e = list[i];
                    if (e.Value < value)
                    {
                        OrderBookEntry ee = new OrderBookEntry()
                        {
                            ValueString = rateString, AmountString = amountString, Id = id
                        };
                        list.Insert(index, ee);
                        return;
                    }
                    index++;
                }
            }
            OrderBookEntry addE = new OrderBookEntry()
            {
                ValueString = rateString, AmountString = amountString, Id = id
            };

            list.Add(addE);
        }
Esempio n. 5
0
        public void UpdateEntries()
        {
            LastUpdateTime = DateTime.Now;
            if (Bids.Count == 0 || Asks.Count == 0)
            {
                return;
            }
            HighestBid = Bids[0].Value;
            LowestAsk  = Asks[0].Value;
            if (UpdateEntriesCount == 0)
            {
                return;
            }
            if (!AllowAdditionalCalculations)
            {
                return;
            }
            double MaxVolume = 0;
            double vt        = 0;

            for (int i = 0; i < Bids.Count; i++)
            {
                OrderBookEntry e = Bids[i];
                e.Volume      = e.Value * e.Amount;
                vt           += e.Volume;
                e.VolumeTotal = vt;
                MaxVolume     = Math.Max(MaxVolume, e.Volume);
            }
            vt = 0;
            for (int i = 0; i < Asks.Count; i++)
            {
                OrderBookEntry e = Asks[i];
                e.Volume      = e.Value * e.Amount;
                vt           += e.Volume;
                e.VolumeTotal = vt;
                MaxVolume     = Math.Max(MaxVolume, e.Volume);
            }
            double mv = Asks.Count == 0 ? 0 : Asks.Last().VolumeTotal;

            vt = 0;
            if (AllowInvertedAsks)
            {
                for (int i = 0; i < AsksInverted.Count; i++)
                {
                    OrderBookEntry e = AsksInverted[i];
                    e.Volume      = e.Value * e.Amount;
                    vt           += e.Volume;
                    e.VolumeTotal = mv - vt;
                }
            }
            if (MaxVolume == 0)
            {
                return;
            }
            MaxVolume = 1 / MaxVolume;
            for (int i = 0; i < Bids.Count; i++)
            {
                OrderBookEntry e = Bids[i];
                e.VolumePercent = e.Volume * MaxVolume;
            }
            for (int i = 0; i < Asks.Count; i++)
            {
                OrderBookEntry e = Asks[i];
                e.VolumePercent = e.Volume * MaxVolume;
            }
            if (AllowInvertedAsks)
            {
                for (int i = 0; i < AsksInverted.Count; i++)
                {
                    OrderBookEntry e = AsksInverted[i];
                    e.VolumePercent = e.Volume * MaxVolume;
                }
            }
        }