public void DBGProcessOrderAdded(CUserOrder userOrder)
        {
            string msg = "[ORDBOX_OrderAdded] ";

            msg += GetUserOrderBoxString(userOrder);

            Log(msg);
        }
        public void DBGExecPartFilled(EnmOrderAction ordAct, CUserOrder userOrder)
        {
            string msg = "[ORDBOX_Exec_or_PartFilled] ";

            msg += String.Format(@" [{0}] ", ordAct);
            msg += GetUserOrderBoxString(userOrder);

            Log(msg);
        }
 private string GetUserOrderBoxString(CUserOrder userOrder)
 {
     return(String.Format("Amount={0} BotID={1} Instrument={2} OrderActionLast={3} OrderId={4}",
                          userOrder.Amount,          //0
                          userOrder.BotId,           //1
                          userOrder.Instrument,      //2
                          userOrder.OrderActionLast, //3
                          userOrder.OrderId          //4
                          ));
 }
        public void Test()
        {
            Stopwatch sw1 = new Stopwatch();
            Stopwatch sw2 = new Stopwatch();


            List <string> lstInstruments = new List <string>();

            lstInstruments.Add("tBTCUSD");
            lstInstruments.Add("tLTCUSD");
            lstInstruments.Add("tETHUSD");
            lstInstruments.Add("tZECUSD");
            lstInstruments.Add("tXMRUSD");
            //_lstInstruments.Add("tDASHUSD");
            //_lstInstruments.Add("tIOTAUSD");
            lstInstruments.Add("tEOSUSD");
            lstInstruments.Add("tSANUSD");
            lstInstruments.Add("tOMGUSD");
            lstInstruments.Add("tBCHUSD");
            lstInstruments.Add("tNEOUSD");
            //  _lstInstruments.Add("tUTPUSD");
            //_lstInstruments.Add("tQTUMUSD");
            lstInstruments.Add("tEDOUSD");
            lstInstruments.Add("tAVTUSD");



            sw1.Restart();


            for (int i = 1; i < 1000; i++)
            {
                int botId = _random.Next(100, 10000);

                int iInstr = _random.Next(0, lstInstruments.Count - 1);


                CUserOrder userOrder = new CUserOrder
                {
                    Amount     = 1234,
                    BotId      = botId,
                    OrderId    = _random.Next(10000000, 20000000),
                    Instrument = lstInstruments[iInstr]
                };

                _userOrderLog.Update(userOrder);
            }


            sw1.Stop();

            long ordIdCtrl = _random.Next(10000000, 20000000);

            CUserOrder userOrderCntrl = new CUserOrder
            {
                Amount     = 1234,
                BotId      = 5000,
                OrderId    = ordIdCtrl,
                Instrument = "tBTCUSD"
            };

            _userOrderLog.Update(userOrderCntrl);



            sw2.Restart();
            int botIdTest = _userOrderLog.GetBotId("tBTCUSD", ordIdCtrl);

            sw2.Stop();

            if (botIdTest != 0)
            {
                System.Threading.Thread.Sleep(0);
            }
        }
Esempio n. 5
0
        /// <summary>
        ///
        ///
        /// Algorithm is described here:
        /// https://github.com/bitfinexcom/bitfinex-api-node/issues/153
        /// </summary>
        /// <param name="respOrders"></param>
        /// <param name="orderAction"></param>
        public void ProcessOrder(ResponseOrders respOrders, EnmOrderAction orderAction)
        {
            //TODO normal

            string instrument       = respOrders.Symbol.Remove(0, 1);
            int    decimalsOfVolume = _client.GetDecimalVolume(instrument);
            //int iAmount = (int)Math.Abs(CUtilConv.GetIntVolume((decimal)respOrders.Amount, decimalsOfVolume));
            //int iAmountOrig = (int)Math.Abs(CUtilConv.GetIntVolume((decimal)respOrders.AmountOrig, decimalsOfVolume));
            decimal amount     = Convert.ToDecimal(Math.Abs((double)respOrders.Amount));
            decimal amountOrig = Convert.ToDecimal(Math.Abs((double)respOrders.AmountOrig));

            //int iAmountRest = (int) CBfxUtils.GetIntVolume( Math.Abs((decimal)respOrders.AmountOrig) -  Math.Abs((decimal)respOrders.Amount),decimalsOfVolume);
            DateTime momentOrderCreate = CUtilTime.DateTimeFromUnixTimestampMillis((long)respOrders.MtsCreate);
            DateTime momentOrderUpdate = CUtilTime.DateTimeFromUnixTimestampMillis((long)respOrders.MtsUpdate);

            //check advance data in status field
            //Could by like "Cancelled" or  more complex "EXECUTED @ 10908.0(-0.0)
            EnmBfxOrderStatus orderStatus = CBfxUtils.GetOrderStatus(respOrders.OrderStatus);

            //common parametters same for all order update type
            CRawOrdersLogStruct userOrdLogStruct = new CRawOrdersLogStruct
            {
                Id_ord = respOrders.Id,
                Ext_id = (int)respOrders.Gid,

                Price = Math.Abs((decimal)respOrders.Price),
                //	Amount = (int)iAmount,
                //    Amount_rest = iAmountRest,
                Instrument = instrument,
                Action     = (sbyte)BfxOrdStatusToEnmOrderAction(orderStatus),
                //    Moment = momentOrderCreate
            };



            //case of add order
            if (orderAction == EnmOrderAction.Added)
            {
                //2018-04-04 NOTE. Added order could be two cases:
                //1) Jast Added limit order - we do use amount
                //2) Put limit order by market and it is partialy filled - we alse do
                //   use amount as we initial order in bot with rest of order.
                //   One important thing - we do override Action from "Partialy filled" to "OrderAccepted"


                userOrdLogStruct.Amount = amount;
                userOrdLogStruct.Dir    = respOrders.Amount > 0 ? (sbyte)EnmOrderDir.Buy : (sbyte)EnmOrderDir.Sell;

                //Override to "OrderAccepted" for "partially filled" case
                if (orderStatus == EnmBfxOrderStatus.PartiallyFilled)
                {
                    userOrdLogStruct.Action = (sbyte)EnmOrderAction.Added;
                }


                _client.TriggerRecalculateBot(userOrdLogStruct.Ext_id,
                                              userOrdLogStruct.Instrument,
                                              EnmBotEventCode.OnOrderAccepted,
                                              userOrdLogStruct);
                //added 2018-06-06
                //first process deal if exist
                //old place of CheckForDealsWithNoBotId before 2018-08-03

                CUserOrder userOrder = new CUserOrder
                {
                    Amount          = amount,
                    BotId           = userOrdLogStruct.Ext_id,
                    Instrument      = userOrdLogStruct.Instrument,
                    OrderActionLast = EnmOrderAction.Added,
                    OrderId         = userOrdLogStruct.Id_ord,
                    Status          = respOrders.OrderStatus
                };
                _userOrderLog.Update(userOrder);
                //2018-06-03 move AFTER adding order (as is _userOrderLog later)
                _client.CheckForDealsWithNoBotId(userOrdLogStruct.Id_ord, userOrdLogStruct.Ext_id);

                _dbg.DBGProcessOrderAdded(userOrder);
            }
            //case of fully deleted order
            else if (orderAction == EnmOrderAction.Deleted && orderStatus == EnmBfxOrderStatus.Canceled)
            {
                //just tell bot to cancell order
                _client.TriggerRecalculateBot(userOrdLogStruct.Ext_id,
                                              userOrdLogStruct.Instrument,
                                              EnmBotEventCode.OnOrderCancel,
                                              userOrdLogStruct);


                //added 2018-05-13
                //first process deal if exist
                //old place of CheckForDealsWithNoBotId before 2018-08-03


                //no order is not need even for "very late" update.
                //2018-05-31 removed (user cancel before "te" and "tu"- having a problem)
                //_userOrderLog.Delete(userOrdLogStruct.Ext_id,
                //				  userOrdLogStruct.Instrument,
                //			  userOrdLogStruct.Id_ord);


                //2018-05-31 set order deleted
                CUserOrder userOrder = new CUserOrder
                {
                    Amount          = amount,
                    BotId           = userOrdLogStruct.Ext_id,
                    Instrument      = userOrdLogStruct.Instrument,
                    OrderActionLast = EnmOrderAction.Deleted,
                    OrderId         = userOrdLogStruct.Id_ord,
                    Status          = respOrders.OrderStatus
                };

                _userOrderLog.Update(userOrder);
                //2018-06-03 move AFTER adding order (as is _userOrderLog later)
                _client.CheckForDealsWithNoBotId(userOrdLogStruct.Id_ord, userOrdLogStruct.Ext_id);

                _dbg.DBGDeleted(userOrdLogStruct);



                //TODO check not processed deals
            }
            //case of full or partial executed order
            else if ((orderAction == EnmOrderAction.Deleted && orderStatus == EnmBfxOrderStatus.Executed) ||
                     //case of partial filled
                     (orderAction == EnmOrderAction.Update && orderStatus == EnmBfxOrderStatus.PartiallyFilled))

            {
                decimal amountUse = amountOrig - amount;
                //check for partially filled
                userOrdLogStruct.Dir = respOrders.AmountOrig > 0 ? (sbyte)EnmOrderDir.Buy : (sbyte)EnmOrderDir.Sell;

                //2018-04-04 possible cases:
                //1) Deleted and executed order by market when limit order was not set yet, on that case amount=0
                //   but as no order exist in BotBase.MonitorOrders - nothing happens
                //2) Deal for order that exists, we do encrease amounts of orders on that case -NOT TESTED-
                userOrdLogStruct.Amount = amount;

                //first tell bot order deal
                _client.TriggerRecalculateBot(userOrdLogStruct.Ext_id,
                                              userOrdLogStruct.Instrument,
                                              EnmBotEventCode.OnOrderDeal,
                                              userOrdLogStruct);


                //_userOrderTracker.Update(userOrdLogStruct.Ext_id, userOrdLogStruct.Id_ord, respOrders.OrderStatus);

                //2018-06-13 the same as in all two other conditions
                //old place of CheckForDealsWithNoBotId before 2018-08-03


                EnmOrderAction ordActUse = EnmOrderAction.Unknown;

                if (orderStatus == EnmBfxOrderStatus.Executed)
                {
                    ordActUse = EnmOrderAction.Deal;
                }
                else if (orderStatus == EnmBfxOrderStatus.PartiallyFilled)
                {
                    ordActUse = EnmOrderAction.PartialFilled;
                }



                CUserOrder userOrder = new CUserOrder
                {
                    Amount          = amount,
                    BotId           = userOrdLogStruct.Ext_id,
                    Instrument      = userOrdLogStruct.Instrument,
                    OrderActionLast = ordActUse,
                    OrderId         = userOrdLogStruct.Id_ord,
                    Status          = respOrders.OrderStatus
                };

                _userOrderLog.Update(userOrder);

                _client.CheckForDealsWithNoBotId(userOrdLogStruct.Id_ord, userOrdLogStruct.Ext_id);


                _dbg.DBGExecPartFilled(ordActUse, userOrder);
            }
        }