Ejemplo n.º 1
0
        public static Dish CreateDish(EOrderType orderType, int input)
        {
            if (orderType == EOrderType.Morning)
            {
                switch (input)
                {
                case 1: return(new EggsDish());

                case 2: return(new ToastDish());

                case 3: return(new CoffeeDish());

                case 4: return(new NaDish());

                default: return(new NaDish());
                }
            }
            if (orderType == EOrderType.Night)
            {
                switch (input)
                {
                case 1: return(new SteakDish());

                case 2: return(new PotatoDish());

                case 3: return(new WineDish());

                case 4: return(new CakeDish());

                default: return(new NaDish());
                }
            }
            return(null);
        }
Ejemplo n.º 2
0
        // Notes:
        //  - A 'Trade' is a description of a trade that *could* be placed. It is different
        //    to an 'Order' which is live on an exchange, waiting to be filled.
        //  - AmountIn * Price does not have to equal AmountOut, because 'Trade' is used
        //    with the order book to calculate the best price for trading a given amount.
        //    The price will represent the best price that covers all of the amount, not
        //    the spot price.
        //  - Don't implicitly change amounts/prices based on order type for the same reason.
        //    Instead, allow any values to be set and use validate to check they're correct.
        //    The 'EditTradeUI' should be used to modify properties and ensure correct behaviour
        //    w.r.t to order type.
        // Rounding issues:
        //  - Quantisation doesn't help, that just makes the discrepancies larger.
        //  - Instead, maintain separate values for amount in and amount out. These imply the
        //    price and allow control over which side of the trade gets rounded.

        /// <summary>Create a trade on 'pair' to convert 'amount_in' of 'coin_in' to 'amount_out'</summary>
        public Trade(Fund fund, TradePair pair, EOrderType order_type, ETradeType trade_type, Unit <decimal> amount_in, Unit <decimal> amount_out, Unit <decimal>?price_q2b = null, string creator = null)
        {
            // Check trade amounts and units
            if (amount_in < 0m._(trade_type.CoinIn(pair)))
            {
                throw new Exception("Invalid trade 'in' amount");
            }
            if (amount_out < 0m._(trade_type.CoinOut(pair)))
            {
                throw new Exception("Invalid trade 'out' amount");
            }
            if (amount_out != 0 && amount_in != 0 && trade_type.PriceQ2B(amount_out / amount_in) < 0m._(pair.RateUnits))
            {
                throw new Exception("Invalid trade price");
            }

            CreatorName = creator ?? string.Empty;
            Fund        = fund;
            Pair        = pair;
            OrderType   = order_type;
            TradeType   = trade_type;
            AmountIn    = amount_in;
            AmountOut   = amount_out;
            PriceQ2B    =
                price_q2b != null ? price_q2b.Value :
                amount_out != 0 && amount_in != 0 ? TradeType.PriceQ2B(amount_out / amount_in) :
                SpotPriceQ2B;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 应用排序
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <param name="property"></param>
        /// <param name="methodName"></param>
        /// <param name="orderType"></param>
        /// <returns></returns>
        public static IOrderedQueryable <T> ApplyOrder <T>(this IQueryable <T> source, string property,
                                                           EOrderType orderType)
        {
            var methodName = orderType.ToString();

            var        props = property.Split('.');
            var        type  = typeof(T);
            var        arg   = Expression.Parameter(type, "x");
            Expression expr  = arg;

            foreach (var prop in props)
            {
                // use reflection (not ComponentModel) to mirror LINQ
                var pi = type.GetProperty(prop);
                expr = Expression.Property(expr, pi);
                type = pi.PropertyType;
            }

            var delegateType = typeof(Func <,>).MakeGenericType(typeof(T), type);
            var lambda       = Expression.Lambda(delegateType, expr, arg);
            var result       = typeof(Queryable).GetMethods().Single(method => method.Name == methodName &&
                                                                     method.IsGenericMethodDefinition &&
                                                                     method.GetGenericArguments().Length == 2 &&
                                                                     method.GetParameters().Length == 2)
                               .MakeGenericMethod(typeof(T), type)
                               .Invoke(null, new object[] { source, lambda });

            return((IOrderedQueryable <T>)result);
        }
Ejemplo n.º 4
0
 public String ToString(EOrderType type)
 {
     if (type == EOrderType.name)
     {
         return("name");
     }
     else if (type == EOrderType.set)
     {
         return("set");
     }
     else if (type == EOrderType.released)
     {
         return("released");
     }
     else if (type == EOrderType.rarity)
     {
         return("rarity");
     }
     else if (type == EOrderType.color)
     {
         return("color");
     }
     else if (type == EOrderType.usd)
     {
         return("usd");
     }
     else if (type == EOrderType.tix)
     {
         return("tix");
     }
     else if (type == EOrderType.eur)
     {
         return("eur");
     }
     else if (type == EOrderType.cmc)
     {
         return("cmc");
     }
     else if (type == EOrderType.power)
     {
         return("power");
     }
     else if (type == EOrderType.toughness)
     {
         return("toughness");
     }
     else if (type == EOrderType.edhrec)
     {
         return("edhrec");
     }
     else if (type == EOrderType.artist)
     {
         return("artist");
     }
     else
     {
         return(String.Empty);
     }
 }
Ejemplo n.º 5
0
 private void LoadOrders()
 {
     foreach (var element in EOrderType.GetValues(typeof(EOrderType)))
     {
         cboOrder.Items.Add(element);
     }
     cboOrder.SelectedIndex = 0;
 }
Ejemplo n.º 6
0
 public OrderParams(EOrderSide side, EOrderType type, ETimeInForce tif, decimal amount_base, decimal?price_q2b, decimal?stop_price)
 {
     Side              = side;
     Type              = type;
     TimeInForce       = tif;
     AmountBase        = amount_base;
     PriceQ2B          = price_q2b;
     StopPriceQ2B      = stop_price;
     IcebergAmountBase = null;
 }
Ejemplo n.º 7
0
        public SELECT ORDERBY(Expression expression, EOrderType orderType)
        {
            if (this.orderBy == null)
            {
                this.orderBy = new OrderBy();
            }

            this.orderBy.addOrderby(expression, orderType);
            return(this);
        }
Ejemplo n.º 8
0
        public static long ToOrderType(EOrderType orderType)
        {
            switch (orderType)
            {
            case EOrderType.Limit:
                return(Limit);

            default:
                throw new BinanceDexException($"Unhandled orderType: {orderType}");
            }
        }
Ejemplo n.º 9
0
        public static string ToString(EOrderType order_type)
        {
            switch (order_type)
            {
            default: throw new ArgumentException("order_type");

            case EOrderType.Buy: return("buylimit");

            case EOrderType.Sell: return("selllimit");
            }
        }
Ejemplo n.º 10
0
        /// <summary>Get the server to update the available balance</summary>
        public decimal CalcAvailableBalance(CurrencyPair pair, EOrderType tt, decimal price_q2b, CancellationToken?cancel = null)
        {
            var reply = PostData("v2/auth/calc/order/avail", cancel,
                                 new KV("symbol", pair.Id),
                                 new KV("dir", tt == EOrderType.Buy ? +1 : tt == EOrderType.Sell ? -1 : throw new Exception("Unknown order type")),
                                 new KV("rate", price_q2b),
                                 new KV("type", "EXCHANGE"));
            var avail = ParseJsonReply <List <decimal> >(reply);

            return(avail[0]);
        }
Ejemplo n.º 11
0
        // Notes:
        //  - An Order is a request to buy/sell that has been sent to an exchange and
        //    should exist somewhere in their order book. When an Order is filled it
        //    becomes a 'OrderCompleted'

        public Order(long order_id, Fund fund, TradePair pair, EOrderType ot, ETradeType tt, Unit <decimal> amount_in, Unit <decimal> amount_out, Unit <decimal> remaining_in, DateTimeOffset created, DateTimeOffset updated)
            : base(fund, pair, ot, tt, amount_in, amount_out)
        {
            if (created < Misc.CryptoCurrencyEpoch)
            {
                throw new Exception("Invalid creation time");
            }

            OrderId     = order_id;
            UniqueKey   = Guid.NewGuid();
            RemainingIn = remaining_in;
            Created     = created;
            Updated     = updated;
        }
Ejemplo n.º 12
0
        public static Order CreateOrder(EOrderType orderType, int?errorIndex, params Dish[] dishes)
        {
            var order = new Order(orderType);

            if (errorIndex.HasValue)
            {
                order.FirstError = errorIndex.Value;
            }
            foreach (var dish in dishes)
            {
                order.Dishes.Add(dish);
            }
            return(order);
        }
Ejemplo n.º 13
0
        public static global::Binance.API.EOrderType ToBinanceOT(this EOrderType order_type)
        {
            // In CoinFlip, a stop order is just a limit order on the wrong side of the spot price.
            // In Binance speak, these are stop loss limits regardless of the trade side.
            switch (order_type)
            {
            default: throw new Exception("Unknown trade type");

            case EOrderType.Market: return(global::Binance.API.EOrderType.MARKET);

            case EOrderType.Limit: return(global::Binance.API.EOrderType.LIMIT);

            case EOrderType.Stop: return(global::Binance.API.EOrderType.STOP_LOSS_LIMIT);
            }
        }
Ejemplo n.º 14
0
        public void AddUnitOrder(EOrderType type, int team, int sector, int[] selectedUnitIds, Point mousePos, Keys key)
        {
            if (GameComplete || !RecordingEnabled)
            {
                return;
            }

            AddOrder(new ReplayOrder
            {
                OrderType   = type,
                Team        = team,
                SectorId    = sector,
                MousePos    = mousePos,
                Key         = key,
                SelectedIds = selectedUnitIds
            });
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Places a team order.
        /// </summary>
        /// <param name="theTeam">The team which made the order.</param>
        /// <param name="orderTypeValue">The order type.</param>
        /// <param name="parameters">The parameters of the order.</param>
        public void PlaceTeamOrder(Team theTeam, EOrderType orderTypeValue, string parameters)
        {
            var orderTypeRepo = this.container.Resolve<IOrderTypeRepository>();
            OrderType orderType = orderTypeRepo.GetByOrderTypeValue(orderTypeValue);

            var uow = this.container.Resolve<IUnitOfWork>();
            Order order = new Order()
            {
                Team = theTeam,
                OrderType = orderType,
                Parameters = parameters,
                DateTime = DateTime.Now,
                CountdownToProcess = orderType.TimeToProcess
            };

            orderRepo.Save(order);
            uow.Commit();
        }
Ejemplo n.º 16
0
        public static LinkedList <T> SortList <T>(LinkedList <T> items, EOrderType type = EOrderType.Asc) where T : IComparable
        {
            void AddItemToList(T item, LinkedList <T> collection)
            {
                var temp = collection.First;

                while (temp != null)
                {
                    if (type == EOrderType.Asc
                                                ? temp.Value.CompareTo(item) > 0
                                                : temp.Value.CompareTo(item) < 0)
                    {
                        collection.AddBefore(temp, item);
                        break;
                    }

                    if (temp.Next == null)
                    {
                        collection.AddLast(item);
                        break;
                    }

                    temp = temp.Next;
                }
            }

            var result = new LinkedList <T>();

            foreach (var item in items)
            {
                if (result.Count == 0)
                {
                    result.AddLast(item);
                }
                else
                {
                    AddItemToList(item, result);
                }
            }

            return(result);
        }
        public double GetMultiplier(EDivision division, int points, EOrderType orderType)
        {
            double multiplier = 1.25;

            multiplier -= ((int)division * 0.10);
            multiplier += points * 0.001;

            switch (orderType)
            {
            case EOrderType.GamesBoost:
            {
                multiplier *= 0.80;
                break;
            }

            case EOrderType.DuoQWinsBoost:
            {
                multiplier *= 1.25;
                break;
            }
            }
            return(multiplier);
        }
Ejemplo n.º 18
0
        public static Dictionary <EDishType, CountValidation> Create(EOrderType orderType)
        {
            var result = new Dictionary <EDishType, CountValidation>();

            switch (orderType)
            {
            case EOrderType.Morning:
                result.Add(EDishType.Entree, new CountValidation(1));
                result.Add(EDishType.Side, new CountValidation(1));
                result.Add(EDishType.Drink, new CountValidation(int.MaxValue));
                result.Add(EDishType.Desert, new CountValidation(0));
                result.Add(EDishType.NotAvailable, new CountValidation(0));
                break;

            case EOrderType.Night:
                result.Add(EDishType.Entree, new CountValidation(1));
                result.Add(EDishType.Side, new CountValidation(int.MaxValue));
                result.Add(EDishType.Drink, new CountValidation(1));
                result.Add(EDishType.Desert, new CountValidation(1));
                result.Add(EDishType.NotAvailable, new CountValidation(0));
                break;
            }
            return(result);
        }
Ejemplo n.º 19
0
 public Order(EOrderType orderType)
 {
     OrderType = orderType;
     Dishes    = new List <Dish>();
 }
Ejemplo n.º 20
0
 /// <summary>
 /// Places a team order.
 /// </summary>
 /// <param name="theTeam">The team which made the order.</param>
 /// <param name="orderTypeValue">The order type.</param>
 /// <param name="parameters">The parameters of the order.</param>
 public void PlaceTeamOrder(Team theTeam, EOrderType orderTypeValue, string parameters)
 {
     OrderingServices orderingSvc = new OrderingServices();
     orderingSvc.PlaceTeamOrder(theTeam, orderTypeValue, parameters);
 }
Ejemplo n.º 21
0
 public void addOrderby(Expression expression, EOrderType orderType)
 {
     this.orderObjs.Add(new OrderByObj(expression, orderType));
 }
Ejemplo n.º 22
0
        /// <summary>Attempt to make a trade on 'pair' for the given 'price' and base 'amount'</summary>
        private void TryFillOrder(TradePair pair, Fund fund, long order_id, ETradeType tt, EOrderType ot, Unit <decimal> amount_in, Unit <decimal> amount_out, Unit <decimal> remaining_in, out Order ord, out OrderCompleted his)
        {
            // The order can be filled immediately, filled partially, or not filled and remain as an 'Order'.
            // Also, exchanges use the base currency as the amount to fill, so for Q2B trades it's possible
            // that 'amount_in' is less than the trade asked for.
            var market = m_depth[pair];

            // Consume orders
            var price_q2b   = tt.PriceQ2B(amount_out / amount_in);
            var amount_base = tt.AmountBase(price_q2b, amount_in: remaining_in);
            var filled      = market.Consume(pair, tt, ot, price_q2b, amount_base, out var remaining_base);

            // The order is partially or completely filled...
            Debug.Assert(Misc.EqlAmount(amount_base, filled.Sum(x => x.AmountBase) + remaining_base));
            ord = remaining_base != 0 ? new Order(order_id, fund, pair, ot, tt, amount_in, amount_out, tt.AmountIn(remaining_base, price_q2b), Model.UtcNow, Model.UtcNow) : null;
            his = remaining_base != amount_base ? new OrderCompleted(order_id, fund, pair, tt) : null;

            // Add 'TradeCompleted' entries for each order book offer that was filled
            foreach (var fill in filled)
            {
                his.Trades.AddOrUpdate(new TradeCompleted(his.OrderId, ++m_history_id, pair, tt, fill.AmountIn(tt), fill.AmountOut(tt), Exchange.Fee * fill.AmountOut(tt), tt.CoinOut(pair), Model.UtcNow, Model.UtcNow));
            }
        }
Ejemplo n.º 23
0
 public OrderType(EOrderType value)
 {
     this.Value = value;
 }
Ejemplo n.º 24
0
 public OrderParams(EOrderSide side, EOrderType type, ETimeInForce tif = ETimeInForce.GTC)
     : this(side, type, tif, 0, null, null)
 {
 }
Ejemplo n.º 25
0
        /// <summary>
        /// Consume offers up to 'price_q2b' or 'amount_base' (based on order type).
        /// 'pair' is the trade pair that this market depth data is associated with.
        /// Returns the offers that were consumed. 'amount_remaining' is what remains unfilled</summary>
        public IList <Offer> Consume(TradePair pair, ETradeType tt, EOrderType ot, Unit <decimal> price_q2b, Unit <decimal> amount_base, out Unit <decimal> remaining_base)
        {
            // Notes:
            //  - 'remaining_base' should only be non zero if the order book is empty
            //  - Handling 'dust' amounts:
            //     If the amount to fill almost matches an offer, where the difference is an amount too small to trade,
            //     the offer amount is adjusted to exactly match. The small difference is absorbed by the exchange.
            //  - This function cannot use 'amount_in' + 'amount_out' parameters because the price to consume up to
            //    is not necessarity 'amount_out/amount_in'. 'amount_in' may be the partial remaining amount of a trade.

            var order_book = this[tt];

            remaining_base = amount_base;

            // Stop orders become market orders when the price reaches the stop level
            if (ot == EOrderType.Stop)
            {
                if (order_book.Count != 0 && tt.Sign() * price_q2b.CompareTo(order_book[0].PriceQ2B) <= 0)
                {
                    ot = EOrderType.Market;
                }
                else
                {
                    return(new List <Offer>());
                }
            }

            var count  = 0;
            var offers = order_book.Offers;

            foreach (var offer in offers)
            {
                // Price is too high/low to fill 'offer', stop.
                if (ot != EOrderType.Market && tt.Sign() * price_q2b.CompareTo(offer.PriceQ2B) < 0)
                {
                    break;
                }

                var rem = remaining_base - offer.AmountBase;

                // The remaining amount is large enough to consider the next offer
                if (rem < pair.AmountRangeBase.Beg || rem * offer.PriceQ2B < pair.AmountRangeQuote.Beg)
                {
                    break;
                }

                remaining_base = rem;
                ++count;
            }

            // Remove the orders that have been filled
            var consumed = offers.GetRange(0, count);

            offers.RemoveRange(0, count);

            // Remove any remaining amount from the top remaining offer (if the price is right)
            if (remaining_base != 0 && offers.Count != 0 && (ot == EOrderType.Market || tt.Sign() * price_q2b.CompareTo(offers[0].PriceQ2B) >= 0))
            {
                var offer = offers[0];

                var rem = offer.AmountBase - remaining_base;
                if (pair.AmountRangeBase.Contains(rem) && pair.AmountRangeQuote.Contains(rem * offer.PriceQ2B))
                {
                    offers[0] = new Offer(offers[0].PriceQ2B, rem);
                }
                else
                {
                    offers.RemoveAt(0);
                }

                consumed.Add(new Offer(offer.PriceQ2B, remaining_base));
                remaining_base -= remaining_base;
            }
            return(consumed);
        }
Ejemplo n.º 26
0
 public OrderByObj(Expression expression, EOrderType orderType)
 {
     this.expression = expression;
     this.orderType  = orderType;
 }
Ejemplo n.º 27
0
 /// <summary>True if binance considers this order type an algorithm order type</summary>
 public static bool IsAlgo(this EOrderType ot)
 {
     return(ot == EOrderType.STOP_LOSS || ot == EOrderType.STOP_LOSS_LIMIT || ot == EOrderType.TAKE_PROFIT || ot == EOrderType.TAKE_PROFIT_LIMIT);
 }