Ejemplo n.º 1
0
 public Order(PlaceMessage msg)
 {
     asset1   = msg.asset1;
     asset2   = msg.asset2;
     user     = msg.user_id;
     price    = msg.price;
     quantity = msg.quantity;
     buy      = msg.buy;
     id       = last_id++;
 }
Ejemplo n.º 2
0
 public Order(PlaceMessage msg)
 {
     asset1 = msg.asset1;
     asset2 = msg.asset2;
     user = msg.user_id;
     price = msg.price;
     quantity = msg.quantity;
     buy = msg.buy;
     id = last_id++;
 }
Ejemplo n.º 3
0
        internal Tuple <byte[], List <byte[]> > Place(PlaceMessage pm)
        {
            Order o = new Order(pm);
            Tuple <byte[], List <byte[]> > ans = new Tuple <byte[], List <byte[]> >(BitConverter.GetBytes(o.id), new List <byte[]>());

            ans.Item2.Add(o.AddedString());
            orders[o.id] = o;

            if (!users.ContainsKey(pm.user_id))
            {
                //ошибка, юзера нет
            }

            if (pm.buy) //покупаем asset2 за asset1
            {
                long q1, q2;
                User.MUL(pm.price, pm.quantity, out q1, out q2);

                //проверить хватит ли денег
                if (users[pm.user_id].eq[User.num[pm.asset1]].CompareTo(new MyTuple <long, long>(q1, q2)) < 0)
                { //денег не хватило
                }



                if (!buy.ContainsKey(pm.asset1))
                {
                    buy[pm.asset1] = new Dictionary <string, SortedDictionary <MyTuple <long, long>, List <Order> > >();
                }
                if (!buy[pm.asset1].ContainsKey(pm.asset2))
                {
                    buy[pm.asset1][pm.asset2] = new SortedDictionary <MyTuple <long, long>, List <Order> >();
                }
                if (!buy[pm.asset1][pm.asset2].ContainsKey(pm.price))
                {
                    buy[pm.asset1][pm.asset2][pm.price] = new List <Order>();
                }
                buy[pm.asset1][pm.asset2][pm.price].Add(o);
            }
            else //продаем asset2 за asset1
            {
                //проверить хватит ли денег
                MyTuple <long, long> T = new MyTuple <long, long>(pm.quantity / User.MAGIC, pm.quantity % User.MAGIC);
                if (users[pm.user_id].eq[User.num[pm.asset2]].CompareTo(T) < 0)
                {
                    ;
                }
                { //денег не хватило
                }
                if (pm.quantity <= 0 || pm.price.Item1 < 0 || pm.price.Item2 < 0 || (pm.price.Item1) == 0 && (pm.price.Item2) == 0)
                {
                    //проверка на отрицательную смму и количество
                }
                if (!buy.ContainsKey(pm.asset1))
                {
                    buy[pm.asset1] = new Dictionary <string, SortedDictionary <MyTuple <long, long>, List <Order> > >();
                }
                if (!buy[pm.asset1].ContainsKey(pm.asset2))
                {
                    buy[pm.asset1][pm.asset2] = new SortedDictionary <MyTuple <long, long>, List <Order> >();
                }
                if (!buy[pm.asset1][pm.asset2].ContainsKey(pm.price))
                {
                    buy[pm.asset1][pm.asset2][pm.price] = new List <Order>();
                }
                buy[pm.asset1][pm.asset2][pm.price].Add(o);
            }
            while (sell[pm.asset1][pm.asset2].First().Key.CompareTo(buy[pm.asset1][pm.asset2].Last().Key) <= 0)
            {
                MyTuple <long, long> price = pm.buy ? sell[pm.asset1][pm.asset2].First().Key : buy[pm.asset1][pm.asset2].First().Key;
                List <Order>         l1    = sell[pm.asset1][pm.asset2].First().Value;
                List <Order>         l2    = buy[pm.asset1][pm.asset2].Last().Value;
                while (l1.Count > 0 && l2.Count > 0)
                {
                    long q = Math.Min(l1[0].quantity, l2[0].quantity);
                    users[l1[0].user].AddAsset(pm.asset1, -q);
                    users[l2[0].user].AddAsset(pm.asset1, q);
                    users[l1[0].user].AddCurrency(pm.asset2, q, price);
                    users[l2[0].user].AddCurrency(pm.asset2, -q, price);
                    l1[0].quantity -= q;
                    l2[0].quantity -= q;
                    List <byte> tick = new List <byte>();
                    tick.AddRange(System.Text.Encoding.ASCII.GetBytes(pm.asset1 + "/" + pm.asset2));
                    tick.Add(2);
                    tick.AddRange(BitConverter.GetBytes(l1[0].id));
                    tick.AddRange(BitConverter.GetBytes(l2[0].id));
                    tick.AddRange(BitConverter.GetBytes(price.Item1));
                    tick.AddRange(BitConverter.GetBytes(price.Item2));
                    tick.AddRange(BitConverter.GetBytes(q));
                    ans.Item2.Add(tick.ToArray());
                    if (l1[0].quantity == 0)
                    {
                        l1.RemoveAt(0);
                    }
                    if (l2[0].quantity == 0)
                    {
                        l2.RemoveAt(0);
                    }
                }
                if (l1.Count == 0)
                {
                    sell[pm.asset1][pm.asset2].Remove(sell[pm.asset1][pm.asset2].First().Key);
                }
                if (l2.Count == 0)
                {
                    buy[pm.asset1][pm.asset2].Remove(buy[pm.asset1][pm.asset2].Last().Key);
                }
            }
            return(ans);
        }
Ejemplo n.º 4
0
        internal Tuple<byte[], List<byte[]>> Place(PlaceMessage pm)
        {
            Order o = new Order(pm);
            Tuple<byte[], List<byte[]>> ans = new Tuple<byte[], List<byte[]>>(BitConverter.GetBytes(o.id), new List<byte[]>());
            ans.Item2.Add(o.AddedString());
            orders[o.id] = o;

            if (!users.ContainsKey(pm.user_id))
            {
                //ошибка, юзера нет
            }

            if (pm.buy) //покупаем asset2 за asset1
            {
                long q1, q2;
                User.MUL(pm.price, pm.quantity, out q1, out q2);

                //проверить хватит ли денег
                if (users[pm.user_id].eq[User.num[pm.asset1]].CompareTo(new MyTuple<long, long>(q1, q2)) < 0)
                { //денег не хватило
                }

                if (!buy.ContainsKey(pm.asset1))
                {
                    buy[pm.asset1] = new Dictionary<string, SortedDictionary<MyTuple<long, long>, List<Order>>>();
                }
                if (!buy[pm.asset1].ContainsKey(pm.asset2))
                {
                    buy[pm.asset1][pm.asset2] = new SortedDictionary<MyTuple<long, long>, List<Order>>();
                }
                if (!buy[pm.asset1][pm.asset2].ContainsKey(pm.price))
                {
                    buy[pm.asset1][pm.asset2][pm.price] = new List<Order>();
                }
                buy[pm.asset1][pm.asset2][pm.price].Add(o);
            }
            else //продаем asset2 за asset1
            {
               //проверить хватит ли денег
                MyTuple<long, long> T = new MyTuple<long, long>(pm.quantity/User.MAGIC, pm.quantity%User.MAGIC);
                if (users[pm.user_id].eq[User.num[pm.asset2]].CompareTo(T)<0) ;
                { //денег не хватило
                }
                if (pm.quantity<=0 || pm.price.Item1<0 || pm.price.Item2<0 || (pm.price.Item1)==0 && (pm.price.Item2)==0)
                {
                    //проверка на отрицательную смму и количество
                }
                if (!buy.ContainsKey(pm.asset1))
                {
                    buy[pm.asset1] = new Dictionary<string, SortedDictionary<MyTuple<long, long>, List<Order>>>();
                }
                if (!buy[pm.asset1].ContainsKey(pm.asset2))
                {
                    buy[pm.asset1][pm.asset2] = new SortedDictionary<MyTuple<long, long>, List<Order>>();
                }
                if (!buy[pm.asset1][pm.asset2].ContainsKey(pm.price))
                {
                    buy[pm.asset1][pm.asset2][pm.price] = new List<Order>();
                }
                buy[pm.asset1][pm.asset2][pm.price].Add(o);
            }
            while (sell[pm.asset1][pm.asset2].First().Key.CompareTo(buy[pm.asset1][pm.asset2].Last().Key) <= 0)
            {
                MyTuple<long, long> price = pm.buy ? sell[pm.asset1][pm.asset2].First().Key : buy[pm.asset1][pm.asset2].First().Key;
                List<Order> l1 = sell[pm.asset1][pm.asset2].First().Value;
                List<Order> l2 = buy[pm.asset1][pm.asset2].Last().Value;
                while (l1.Count > 0 && l2.Count > 0)
                {
                    long q = Math.Min(l1[0].quantity, l2[0].quantity);
                    users[l1[0].user].AddAsset(pm.asset1, -q);
                    users[l2[0].user].AddAsset(pm.asset1, q);
                    users[l1[0].user].AddCurrency(pm.asset2, q, price);
                    users[l2[0].user].AddCurrency(pm.asset2, -q, price);
                    l1[0].quantity -= q;
                    l2[0].quantity -= q;
                    List<byte> tick = new List<byte>();
                    tick.AddRange(System.Text.Encoding.ASCII.GetBytes(pm.asset1 + "/" + pm.asset2));
                    tick.Add(2);
                    tick.AddRange(BitConverter.GetBytes(l1[0].id));
                    tick.AddRange(BitConverter.GetBytes(l2[0].id));
                    tick.AddRange(BitConverter.GetBytes(price.Item1));
                    tick.AddRange(BitConverter.GetBytes(price.Item2));
                    tick.AddRange(BitConverter.GetBytes(q));
                    ans.Item2.Add(tick.ToArray());
                    if (l1[0].quantity == 0)
                    {
                        l1.RemoveAt(0);
                    }
                    if (l2[0].quantity == 0)
                    {
                        l2.RemoveAt(0);
                    }
                }
                if (l1.Count == 0)
                {
                    sell[pm.asset1][pm.asset2].Remove(sell[pm.asset1][pm.asset2].First().Key);
                }
                if (l2.Count == 0)
                {
                    buy[pm.asset1][pm.asset2].Remove(buy[pm.asset1][pm.asset2].Last().Key);
                }
            }
            return ans;
        }