Esempio n. 1
0
        private OrderConfirmation PlaceSpreadOrder(string token, enumOrderType orderType, decimal?price, decimal?stop, int refid)
        {
            Order order = InitOrder();

            order.UnderlyingSymbol = "AA";

            OrderLeg leg = new OrderLeg()
            {
                AssetType      = enumAssetType.Equity,
                Symbol         = "AA",
                Action         = enumOrderAction.Sell,
                SpreadRatio    = 100,
                PositionEffect = enumPositionEffect.Close
            };

            order.Legs.Add(leg);

            leg = new OrderLeg()
            {
                AssetType      = enumAssetType.Option,
                Symbol         = "AA 180817C50000",
                Action         = enumOrderAction.Buy,
                SpreadRatio    = 1,
                PositionEffect = enumPositionEffect.Close
            };

            order.Legs.Add(leg);

            leg = new OrderLeg()
            {
                AssetType      = enumAssetType.Option,
                Symbol         = "AA 180817P48000",
                Action         = enumOrderAction.Sell,
                SpreadRatio    = 1,
                PositionEffect = enumPositionEffect.Close
            };

            order.Legs.Add(leg);

            order.ClientRefId = refid;

            order.OrderClass  = enumOrderClass.Multileg;
            order.Quantity    = 1; //this is spread quantity
            order.DebitCredit = enumDebitCredit.Debit;

            order.OrderType = orderType;
            order.Price     = price;
            order.Stop      = stop;
            order.AllOrNone = false;
            order.Duration  = enumOrderDuration.Day;

            var confirm = PlaceNewOrder(token, order);

            return(confirm);
        }
Esempio n. 2
0
 public Order(string symbol, int volume, double price, string userId, enumOrderType orderType, enumSide orderSide)
     : this()
 {
     Symbol = symbol;
     Volume = volume;
     if (orderSide.Equals(enumSide.Buy))
     {
         Price = Math.Abs(price);
     }
     if (orderSide.Equals(enumSide.Sell))
     {
         Price = -Math.Abs(price);
     }
     UserID      = userId;
     OrderType   = orderType;
     OrderSide   = orderSide;
     OrderStatus = enumStatus.Initial;
     //OrderType = enumOrderType.Market;
     //OrderSide = enumSide.Buy;
 }
Esempio n. 3
0
        private OrderConfirmation ModifyOrder(string token, int orderId, enumOrderType orderType, decimal?price, decimal?stop, int referenceId)
        {
            Order order = InitOrder();

            if (AccountId > 0)
            {
                order.AccountId = AccountId; // THIS FIELD IS OPTIONAL. ONLY REQUIRED IF YOU HAVE MORE THAN ONE ACCOUNT
            }
            if (SubaccountId.HasValue && SubaccountId.Value > 0)
            {
                order.SubaccountId = SubaccountId.Value; // THIS FIELD IS OPTIONAL. ONLY REQUIRED IF YOU HAVE MORE THAN ONE SUBACCOUNT
            }
            order.OrderId     = orderId;
            order.ClientRefId = referenceId;

            order.OrderType = orderType;
            order.Price     = price;
            order.Stop      = stop;

            return(PlaceChangeOrder(token, order));
        }
Esempio n. 4
0
        /// <summary>
        /// Calculate the VWAP price
        /// </summary>
        /// <returns></returns>
        private double GetVWAP(string Symbol, enumOrderType orderType, enumSide side)
        {
            double VWAP = 0;

            if (orderType.Equals(enumOrderType.Market))
            {
                VWAP = Convert.ToDouble(Int16.MinValue); // For a market type, return a -32768
            }
            else
            {
                List <ExchangeOrder> liveOrders = null;
                int qty = 0;
                if (side.Equals(enumSide.Buy) && Sells.ContainsKey(Symbol))
                {
                    liveOrders = Sells[Symbol];
                }
                if (side.Equals(enumSide.Sell) && Buys.ContainsKey(Symbol))
                {
                    liveOrders = Buys[Symbol];
                }
                if (liveOrders == null)
                {
                    VWAP = Convert.ToDouble(Int16.MaxValue);
                }
                foreach (ExchangeOrder eo in liveOrders)
                {
                    VWAP += eo.Offer * eo.Volume;
                    qty  += eo.Volume;
                }
                if (!qty.Equals(0.0))
                {
                    VWAP /= qty;
                }
                else
                {
                    throw new DivideByZeroException(string.Format("Quantity becomes ZERO in GetVWAP, how come! Sybmol {0}, Side {1}", Symbol, side));
                }
            }
            return(VWAP);
        }
Esempio n. 5
0
        public string PlaceAnOrder(string UserName, string UserId, string Symbol, string Volume, string Price, string OrderType, string OrderSide)
        {
            User          user            = null;
            string        message         = string.Empty;
            string        userId          = MatchMeDB.Instance.UserTable[Config.AdminUser].Id;
            enumStatus    status          = enumStatus.Unknown;
            enumSide      orderside       = enumSide.Invalid;
            enumOrderType ordertype       = enumOrderType.Invalid;
            double        price           = 0.0;
            int           volume          = 0;
            bool          exist           = false;
            double        requiredbalance = 0.0;
            double        remainbalance   = 0.0;
            string        symbol          = Symbol.ToUpper();

            try
            {
                Symbol = Symbol.ToUpper();
                if (!int.TryParse(Volume, out volume))
                {
                    message = string.Format("Error: Invalid Volume: {0}", volume);
                    status  = enumStatus.OrderPlaceError;
                }
                if (!status.Equals(enumStatus.OrderPlaceError) && !string.IsNullOrEmpty(symbol))
                {
                    List <Order> companies = MatchMeDB.Instance.OrderTable.ContainsKey(userId) ? MatchMeDB.Instance.OrderTable[userId] : new List <Order>();
                    exist = companies.Where(a => a.Symbol.Equals(symbol)).Count() > 0;
                    if (!exist)
                    {
                        message = string.Format("Error: The Company does not exist {0}", symbol);
                        status  = enumStatus.OrderPlaceError;
                    }
                }
                else
                {
                    message = string.Format("Error: The Company does not exist {0}", symbol);
                    status  = enumStatus.OrderPlaceError;
                }
                if (!status.Equals(enumStatus.OrderPlaceError) && !Enum.TryParse(OrderSide, true, out orderside))
                {
                    message = string.Format("Error: Invalid Order Side: {0}", OrderSide);
                    status  = enumStatus.OrderPlaceError;
                }
                if (!status.Equals(enumStatus.OrderPlaceError) && !Enum.TryParse(OrderType, true, out ordertype))
                {
                    message = string.Format("Error: Invalid Order Type: {0}", OrderType);
                    status  = enumStatus.OrderPlaceError;
                }
                if (!status.Equals(enumStatus.OrderPlaceError) && ordertype.Equals(enumOrderType.Market))
                {
                    price = Exchange.Instance.GetMarketPrice(orderside, symbol, volume);
                }
                if (!status.Equals(enumStatus.OrderPlaceError) && ordertype.Equals(enumOrderType.Limit))
                {
                    if (!double.TryParse(Price, out price))
                    {
                        message = string.Format("Error: Invalid Price: {0}", Price);
                        status  = enumStatus.OrderPlaceError;
                    }
                    if (!status.Equals(enumStatus.OrderPlaceError) && orderside.Equals(enumSide.Sell))
                    {
                        price = (-1) * Math.Abs(price);
                    }
                }

                if (!status.Equals(enumStatus.OrderPlaceError) && !string.IsNullOrEmpty(UserName))
                {
                    MatchMeDB.Instance.UserTable.TryGetValue(UserName, out user);
                    if (user == null)
                    {
                        message = string.Format("Error: Can't retrieve the User from the db via the keys. UserName{0}, UserId{1}", UserName, UserId);
                        status  = enumStatus.OrderPlaceError;
                    }
                }
                if (!status.Equals(enumStatus.OrderPlaceError) && string.IsNullOrEmpty(message) && exist)
                {
                    if (price > 0)
                    {
                        //price larger than zero means that it's a buy limit order or an order with valid market price
                        requiredbalance = price * volume + Config.Commission;
                    }
                    if (requiredbalance > user.Balance)
                    {
                        //check if balance is enough
                        remainbalance = requiredbalance - user.Balance;
                        message       = string.Format("Error: No Enough Balance: Remain balance {0}, Requires {1} to place the order", user.Balance, remainbalance);
                        status        = enumStatus.Rejected;
                    }
                    else
                    {
                        Order newOrder = new Order(symbol, volume, price, user.Id, ordertype, orderside);
                        if (orderside.Equals(enumSide.Sell))
                        {
                            status = Exchange.Instance.PlaceSell(newOrder);
                            if (status.Equals(enumStatus.Rejected))
                            {
                                message = string.Format("Error: Short Sell is not allowed. {0}", newOrder);
                            }
                        }
                        else if (orderside.Equals(enumSide.Buy))
                        {
                            status = Exchange.Instance.PlaceBuy(newOrder);
                        }
                        if (!status.Equals(enumStatus.OrderPlaceError) && string.IsNullOrEmpty(message))
                        {
                            message = "New Order Placed:" + MatchMeDB.Instance.Orders[newOrder.Id];
                            status  = enumStatus.OrderPlaced;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ServerLog.LogException(ex, string.Format("Place Order Failed, User {0}, Symbol {1}", user.ToString(), Symbol));
                message = ex.Message;
                status  = enumStatus.OrderPlaceError;
            }
            var result = new Dictionary <string, string> {
                { "status", status.ToString() }, { "message", message }
            };

            return(JsonConvert.SerializeObject(result));
        }
Esempio n. 6
0
        internal OrderConfirmation PlaceSimpleOrder(string token, string symbol, string underlying,
                                                    enumAssetType assetType, enumPositionEffect positionEffect, enumOrderAction action, enumOrderType orderType,
                                                    decimal?price, decimal?stop, int referenceId)
        {
            Order order = InitOrder();

            order.UnderlyingSymbol = underlying;
            if (AccountId > 0)
            {
                order.AccountId = AccountId; // THIS FIELD IS OPTIONAL. ONLY REQUIRED IF YOU HAVE MORE THAN ONE ACCOUNT
            }
            if (SubaccountId.HasValue && SubaccountId.Value > 0)
            {
                order.SubaccountId = SubaccountId.Value; // THIS FIELD IS OPTIONAL. ONLY REQUIRED IF YOU HAVE MORE THAN ONE SUBACCOUNT
            }
            OrderLeg leg = new OrderLeg()
            {
                AssetType      = assetType,
                Symbol         = symbol,
                Action         = action,
                PositionEffect = positionEffect
            };

            order.Legs.Add(leg);

            order.ClientRefId = referenceId;

            order.OrderClass = enumOrderClass.Single;
            order.Quantity   = 10;

            order.OrderType = orderType;
            order.Price     = price;
            order.Stop      = stop;
            order.AllOrNone = false;
            order.Duration  = enumOrderDuration.Day;

            return(PlaceNewOrder(token, order));
        }