コード例 #1
0
        private COrderStateRecord ParseRecord(string stOneRecord)
        {
            string pat = @"\s*([\w\s]*)\s@\s([0-9\.]*)\(([0-9\.]*)\)";



            Regex reg = new Regex(pat);
            Match m   = reg.Match(stOneRecord);

            if (m.Groups.Count != 4)
            {
                throw new ApplicationException("COrderStatusTracker.ParseRecord");
            }

            string  status = m.Groups[1].ToString();
            decimal price  = Convert.ToDecimal(m.Groups[2].ToString().Replace('.', ','));
            decimal amount = Convert.ToDecimal(m.Groups[3].ToString().Replace('.', ','));



            COrderStateRecord ordStatusRec = new COrderStateRecord();

            ordStatusRec.Price       = price;
            ordStatusRec.Amount      = amount;
            ordStatusRec.OrderStatus = CBfxUtils.GetOrderStatus(status);
            ordStatusRec.Dir         = amount > 0 ? EnmDealDir.Buy : EnmDealDir.Sell;
            return(ordStatusRec);
        }
コード例 #2
0
        private void ProcessOrderBookUpdate(CBookParams bookParam, JArray jArrOrderBookUpdate)
        {
            _perf.StartOrdeBookUpd();

            decimal price  = (decimal)jArrOrderBookUpdate[0];
            long    count  = (long)jArrOrderBookUpdate[1];
            decimal amount = (decimal)jArrOrderBookUpdate[2];

            /*
             * _dictStockStor[bookParam.Instrument].Update(CBfxUtils.GetPrecInt(bookParam.Precision),
             *                                                 price,count, amount);
             *
             */

            _dictStockStor[bookParam.Instrument].Update(
                new CBfxStockStorUpdStock
            {
                amount = amount,
                count  = count,
                prec   = CBfxUtils.GetPrecInt(bookParam.Precision),
                price  = price
            }
                );

            _perf.EndOrderBookUpd();
        }
コード例 #3
0
        protected override ResponseOrders JArrayToTradingTicker(JArray array)
        {
            ResponseOrders ro = new ResponseOrders();

            ro.Id         = ConvLong(array[0]);
            ro.Gid        = (long?)array[1];
            ro.Cid        = ConvLong((long)array[2]);
            ro.Symbol     = (string)array[3];
            ro.MtsCreate  = (long?)array[4];
            ro.MtsUpdate  = (long?)array[5];
            ro.Amount     = ConvDbl(array[6]);
            ro.AmountOrig = (double?)array[7];
            ro.Type       = CBfxUtils.GetOrderType((string)array[8]);
            ro.TypePrev   = CBfxUtils.GetOrderType((string)array[9]);
            // 10
            // 11
            ro.Flags       = (int?)array[12];
            ro.OrderStatus = (string)array[13];
            // 14
            // 15
            ro.Price         = (double?)array[16];
            ro.PriceAvg      = (double?)array[17];
            ro.PriceTrailing = (double?)array[18];
            ro.PriceAuxLimit = (double?)array[19];
            // 20
            // 21
            // 22
            ro.Notify   = (int?)array[23];
            ro.Hidden   = (int?)array[24];
            ro.PlacedId = (int?)array[25];

            return(ro);
        }
コード例 #4
0
        public void TestGetPriceFromVolume()
        {
            Assert.AreEqual(10908.0m,
                            CBfxUtils.GetDealPriceFromStatus("EXECUTED @ 10908.0(-0.0)"));


            Assert.AreEqual(10904.0m,
                            CBfxUtils.GetDealPriceFromStatus("EXECUTED @ 10904.0(0.0)"));


            Assert.AreEqual(586.86m,
                            CBfxUtils.GetDealPriceFromStatus("EXECUTED @ 586.86(0.05)"));



            Assert.AreEqual(586.86m,
                            CBfxUtils.GetDealPriceFromStatus("EXECUTED @ 586.86(0.05)"));


            Assert.AreEqual(586.06m,
                            CBfxUtils.GetDealPriceFromStatus("PARTIALLY FILLED @ 586.06(0.85)"));

            Assert.AreEqual(586.86m,
                            CBfxUtils.GetDealPriceFromStatus("EXECUTED @ 586.86(0.05): was PARTIALLY FILLED @ 586.06(0.85)"));
        }
コード例 #5
0
        private void ProcessTradeExecute(string inpInstrWithPrefix, JArray jarrTe)
        {
            string instrument = CBfxUtils.RemoveFirstT(inpInstrWithPrefix);


            int decimalVolume = GetDecimalVolume(instrument);

            long    lngMilis      = (long)jarrTe[1];
            decimal dcmlAmountRaw = (decimal)jarrTe[2];
            decimal dcmlAmount    = Math.Abs(dcmlAmountRaw);
            decimal dcmlPrice     = (decimal)jarrTe[3];



            long amount = CUtilConv.GetIntVolume(dcmlAmount, decimalVolume);

            //2018-02-22
            //Amount is too small. Not possible to understand how it
            //could be. Was seen on LTC. For now just ignore these trades
            if (amount == 0)
            {
                return;
            }



            DateTime dt = CUtilTime.DateTimeFromUnixTimestampMillis(lngMilis);


            CRawDeal rd = new CRawDeal {
                Amount = amount,
                Price  = dcmlPrice,
                Moment = dt
            };



            if (dcmlAmountRaw > 0)
            {
                rd.Id_ord_buy = 1;
            }
            else
            {
                rd.Id_ord_sell = 1;
            }

            _client.UpdateDeal(instrument, rd);

            /*  DateTime dtCurr = DateTime.Now;
             * double ddt = (dtCurr - dt).TotalSeconds;
             *
             * if (ddt != 00)
             *    Thread.Sleep(0);
             *
             */
        }
コード例 #6
0
        public void TestGetOrderStatus()
        {
            Assert.AreEqual(EnmBfxOrderStatus.Executed,
                            CBfxUtils.GetOrderStatus("EXECUTED @ 586.86(0.05): was PARTIALLY FILLED @ 586.06(0.85)"));

            Assert.AreEqual(EnmBfxOrderStatus.Executed,
                            CBfxUtils.GetOrderStatus("EXECUTED @ 10904.0"));


            Assert.AreEqual(EnmBfxOrderStatus.Canceled,
                            CBfxUtils.GetOrderStatus("CANCELED"));
        }
コード例 #7
0
 public override string ToString()
 {
     return(String.Format("Gid={0} Symbol={1} id={2} type={3} price={4} amount={5} amount_orig={6} statusRow={7} statusParsed={8}",
                          this.Gid,                                //0
                          this.Symbol,                             //1
                          this.Id,                                 //2
                          this.Type,                               //3
                          this.Price,                              //4
                          this.Amount,                             //5
                          this.AmountOrig,                         //6
                          this.OrderStatus,                        //7
                          CBfxUtils.GetOrderStatus(OrderStatus))); //8
 }
コード例 #8
0
        private void ProcessOrderBookSnapshot(CBookParams bookParam, JArray jArrOrderBook)
        {
            _perf.StartOrdeBookSnapshot();

            /*
             * _dictStockStor[bookParam.Instrument].UpdateBySnapshot(CBfxUtils.GetPrecInt(bookParam.Precision),
             *                                                          jArrOrderBook);
             */


            _dictStockStor[bookParam.Instrument].UpdateBySnapshot
                (new CBfxStockStorMsgUpdSnap
            {
                prec          = CBfxUtils.GetPrecInt(bookParam.Precision),
                jArrOrderBook = jArrOrderBook
            });

            _perf.EndOrderBookSnapshot();
        }
コード例 #9
0
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            if (!(value is RequestNewOrder))
            {
                throw new ApplicationException("Can't serialize order");
            }


            RequestNewOrder order = (RequestNewOrder)value;

            writer.WriteStartArray();
            writer.WriteValue(0);
            writer.WriteValue("on");
            writer.WriteValue((object)null);

            writer.WriteStartObject();

            if (order.Gid.HasValue)
            {
                writer.WritePropertyName("gid");
                writer.WriteValue(order.Gid.Value);
            }

            writer.WritePropertyName("cid");
            writer.WriteValue(order.Cid);

            writer.WritePropertyName("type");
            //TODO remove from OrderConverter !
            writer.WriteValue(CBfxUtils.GetOrderTypeString(order.Type));

            writer.WritePropertyName("symbol");
            writer.WriteValue(order.Symbol);

            writer.WritePropertyName("amount");
            writer.WriteValue(order.Amount.ToString(CultureInfo.InvariantCulture));


            writer.WritePropertyName("price");
            writer.WriteValue(order.Price.ToString(CultureInfo.InvariantCulture));


            if (order.PriceTrailing.HasValue)
            {
                writer.WritePropertyName("price_trailing");
                writer.WriteValue(order.PriceTrailing.Value.ToString(CultureInfo.InvariantCulture));
            }

            if (order.PriceAuxLimit.HasValue)
            {
                writer.WritePropertyName("price_aux_limit");
                writer.WriteValue(order.PriceAuxLimit.Value.ToString(CultureInfo.InvariantCulture));
            }

            writer.WritePropertyName("hidden");
            writer.WriteValue(order.Hidden);

            if (order.Postonly.HasValue)
            {
                writer.WritePropertyName("postonly");
                writer.WriteValue(order.Postonly.Value.ToString(CultureInfo.InvariantCulture));
            }

            if (order.Close.HasValue)
            {
                writer.WritePropertyName("Close");
                writer.WriteValue(order.Close.Value);
            }


            writer.WriteEndObject();
            writer.WriteEndArray();
        }
コード例 #10
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);
            }
        }