Esempio n. 1
0
        public static IBfxOrder Trailing(BfTradeSide side, decimal trailingOffset, decimal size)
        {
            var request = BfParentOrderRequest.Trail(BfProductCode.Unknown, side, trailingOffset, size, 0, BfTimeInForce.NotSpecified);
            var order   = new BfxParentOrder(request);

            return(order);
        }
Esempio n. 2
0
        public static IBfxOrder StopLimit(BfTradeSide side, decimal triggerPrice, decimal orderPrice, decimal size)
        {
            var request = BfParentOrderRequest.StopLimit(BfProductCode.Unknown, side, triggerPrice, orderPrice, size, 0, BfTimeInForce.NotSpecified);
            var order   = new BfxParentOrder(request);

            return(order);
        }
Esempio n. 3
0
        //======================================================================
        // From element of parent order
        //

        public DbChildOrder(BfParentOrderRequest req, BfParentOrderResponse resp, int childOrderIndex)
        {
            ProductCode    = req.Parameters[childOrderIndex].ProductCode;
            Side           = req.Parameters[childOrderIndex].Side;
            OrderType      = req.Parameters[childOrderIndex].ConditionType;
            OrderSize      = req.Parameters[childOrderIndex].Size;
            MinuteToExpire = req.MinuteToExpire;
            TimeInForce    = req.TimeInForce;

            if (OrderType is BfOrderType.Limit or BfOrderType.StopLimit)
            {
                OrderPrice = req.Parameters[childOrderIndex].Price;
            }
            if (OrderType is BfOrderType.Stop or BfOrderType.StopLimit)
            {
                TriggerPrice = req.Parameters[childOrderIndex].TriggerPrice;
            }
            if (OrderType == BfOrderType.Trail)
            {
                Offset = req.Parameters[childOrderIndex].Offset;
            }

            ParentOrderAcceptanceId = resp.ParentOrderAcceptanceId;
            ChildOrderIndex         = childOrderIndex;
        }
Esempio n. 4
0
 public static BfParentOrderRequestParameter ToParameter(this BfParentOrderRequest parent)
 {
     if (parent.Paremters.Count != 1)
     {
         throw new ArgumentException();
     }
     return(parent.Paremters[0]);
 }
Esempio n. 5
0
 // Parent orders
 public void Insert(BfProductCode productCode, BfParentOrderRequest req, BfParentOrderResponse resp)
 {
     ParentOrders.Add(new DbParentOrder(productCode, req, resp));
     for (int childOrderIndex = 0; childOrderIndex < req.Parameters.Count; childOrderIndex++)
     {
         ChildOrders.Add(new DbChildOrder(req, resp, childOrderIndex));
     }
 }
Esempio n. 6
0
        public void SendParentOrder()
        {
            EnableSendOrder(false);
            var order = BfParentOrderRequest.Stop(_productCode, BfTradeSide.Sell, 100000m, 0.1m);
            var resp  = _client.SendParentOrder(order);

            Assert.IsTrue(resp.IsOk);
            Console.WriteLine(GetRequestJson());
        }
Esempio n. 7
0
        public static IBfxOrder IFD(IBfxOrder ifOrder, IBfxOrder doneOrder)
        {
            var request = BfParentOrderRequest.IFD(
                GetParentOrderRequestParameter(ifOrder),
                GetParentOrderRequestParameter(doneOrder),
                0, BfTimeInForce.NotSpecified
                );

            return(new BfxParentOrder(request));
        }
Esempio n. 8
0
        public static IBfxOrder OCO(IBfxOrder firstOrder, IBfxOrder secondOrder)
        {
            var request = BfParentOrderRequest.OCO(
                GetParentOrderRequestParameter(firstOrder),
                GetParentOrderRequestParameter(secondOrder),
                0, BfTimeInForce.NotSpecified
                );

            return(new BfxParentOrder(request));
        }
Esempio n. 9
0
        public BfxParentOrder(BfParentOrderRequest request)
        {
            Request     = request;
            ProductCode = request.Paremters[0].ProductCode;

            foreach (var childRequest in request.Paremters)
            {
                ChildOrders.Add(new BfxChildOrder(childRequest));
            }
        }
Esempio n. 10
0
 public DbParentOrder(BfProductCode productCode, BfParentOrderRequest req, BfParentOrderResponse resp)
 {
     ProductCode = productCode;
     OrderType   = req.OrderMethod;
     TimeInForce = req.TimeInForce;
     if (TimeInForce == BfTimeInForce.NotSpecified)
     {
         TimeInForce = BfTimeInForce.GTC;
     }
     AcceptanceId = resp.ParentOrderAcceptanceId;
 }
Esempio n. 11
0
        public void SendParentOrder()
        {
            var order = new BfParentOrderRequest();
            var resp  = _client.SendParentOrder(order);

            if (CheckUnauthorized(resp))
            {
                return;
            }
            Assert.IsFalse(resp.IsError);
            DumpJson(resp);
        }
Esempio n. 12
0
        public void CheckParentOrderRequestValid(BfParentOrderRequest request)
        {
            foreach (var childRequest in request.Paremters)
            {
                if (request.Paremters[0].ProductCode != childRequest.ProductCode)
                {
                    throw new ArgumentException("Different product code is set to child order");
                }

                CheckChildOrderRequestValid(childRequest);

                switch (request.OrderMethod)
                {
                case BfOrderType.Simple:
                    switch (childRequest.ConditionType)
                    {
                    case BfOrderType.Stop:
                    case BfOrderType.StopLimit:
                    case BfOrderType.Trail:
                        break;

                    default:
                        throw new ArgumentException("Illegal child order type");
                    }
                    break;

                case BfOrderType.IFD:
                case BfOrderType.OCO:
                case BfOrderType.IFDOCO:
                    switch (childRequest.ConditionType)
                    {
                    case BfOrderType.Limit:
                    case BfOrderType.Market:
                    case BfOrderType.Stop:
                    case BfOrderType.StopLimit:
                    case BfOrderType.Trail:
                        break;

                    default:
                        throw new ArgumentException("Illegal child order type");
                    }
                    break;

                default:
                    throw new ArgumentException("Illegal parent order type");
                }
            }
        }
Esempio n. 13
0
        public BfParentOrderRequest CreateStopLimitOrder(BfTradeSide side, decimal size, decimal price, decimal stopTriggerPrice)
        {
            if (size > price || size > stopTriggerPrice)
            {
                throw new ArgumentException();
            }
            var request = new BfParentOrderRequest {
                OrderMethod = BfOrderType.Simple
            };

            request.Paremters.Add(new BfParentOrderRequestParameter {
                ProductCode = _market.ProductCode, ConditionType = BfOrderType.StopLimit, Side = side, Size = size, Price = price, TriggerPrice = stopTriggerPrice
            });
            CheckParentOrderRequestValid(request);
            return(request);
        }
Esempio n. 14
0
        public BfParentOrderRequest CreateTrailOrder(BfTradeSide side, decimal size, decimal trailingStopPriceOffset)
        {
            if (size > trailingStopPriceOffset)
            {
                throw new ArgumentException();
            }
            var request = new BfParentOrderRequest {
                OrderMethod = BfOrderType.Simple
            };

            request.Paremters.Add(new BfParentOrderRequestParameter {
                ProductCode = _market.ProductCode, ConditionType = BfOrderType.Trail, Side = side, Size = size, Offset = trailingStopPriceOffset
            });
            CheckParentOrderRequestValid(request);
            return(request);
        }
Esempio n. 15
0
        public BfParentOrderRequest CreateOCO(BfParentOrderRequestParameter first, BfParentOrderRequestParameter second, TimeSpan?minuteToExpire = null, BfTimeInForce timeInForce = BfTimeInForce.NotSpecified)
        {
            var request = new BfParentOrderRequest {
                OrderMethod = BfOrderType.OCO
            };

            if (minuteToExpire.HasValue)
            {
                request.MinuteToExpire = (int)minuteToExpire.Value.TotalMinutes;
            }
            request.TimeInForce = timeInForce;
            request.Paremters.Add(first);
            request.Paremters.Add(second);
            CheckParentOrderRequestValid(request);
            return(request);
        }
Esempio n. 16
0
        public BfxParentOrder(BfParentOrderRequest request)
            : this(request.OrderMethod)
        {
            Request      = request;
            _orderMethod = request.OrderMethod;
            _childOrders = request.Parameters.Select(e => new BfxChildOrder(e)).ToArray();

            ProductCode = _childOrders[0].ProductCode;
            if (request.OrderMethod == BfOrderType.Simple)
            {
                var childOrder = _childOrders[0];
                OrderType    = childOrder.OrderType;
                Side         = childOrder.Side;
                OrderSize    = childOrder.OrderSize;
                OrderPrice   = childOrder.OrderPrice;
                TriggerPrice = childOrder.TriggerPrice;
                TrailOffset  = childOrder.TrailOffset;
            }
            else
            {
                OrderType = request.OrderMethod;
            }
        }
Esempio n. 17
0
        public Task <BfxParentOrderTransaction> PlaceOrder(BfParentOrderRequest request, object tag = null)
        {
            DebugEx.Trace();
            return(Task.Run(() =>
            {
                DebugEx.Trace();
                var trans = new BfxParentOrderTransaction(this, request);
                trans.ChildOrderChanged += OnChildOrderChanged;
                trans.ParentOrderStateChanged += OnParentOrderTransactionStateChanged;
                trans.ParentOrderChanged += OnParentOrderChanged;
                trans.Tag = tag;

                for (var retry = 0; retry <= Config.OrderRetryMax; retry++)
                {
                    DebugEx.Trace();
                    if (!trans.IsOrderable())
                    {
                        DebugEx.Trace();
                        trans.Dispose();
                        return null; // order is unacceptable
                    }

                    if (trans.SendOrder())
                    {
                        DebugEx.Trace();
                        return trans;
                    }

                    DebugEx.Trace("Trying retry...");
                    Thread.Sleep(Config.OrderRetryInterval);
                }

                DebugEx.Trace();
                trans.Dispose();
                return null; // Retried out
            }));
        }
Esempio n. 18
0
 void IBfOrderSource.OpenParentOrder(BfParentOrderRequest req, BfParentOrderResponse resp)
 {
 }
 public BfxParentOrderTransaction(BfTradingMarket market, BfParentOrderRequest request)
 {
     DebugEx.EnterMethod();
     _market = market;
     State   = new BfxParentOrderTransactionState(_market, request);
 }
Esempio n. 20
0
 public BfxParentOrderTransactionState(BfTradingMarket market, BfParentOrderRequest request)
     : base(request)
 {
     _market = market;
 }
        static void ParentOrderMain()
        {
            while (true)
            {
                Console.WriteLine("======== Parent order operations");
                Console.WriteLine("S)top        Stop L)imit     T)rail");
                Console.WriteLine("I)FD         O)CO            IFD)OCO");
                Console.WriteLine("");
                Console.WriteLine("Time in F)orce");
                Console.WriteLine("M)inutes to expire");
                Console.WriteLine("");
                Console.WriteLine("C) Cancel parent order");
                Console.WriteLine("G)et parent orders");
                Console.WriteLine("");
                Console.WriteLine("R) Return to main");

                try
                {
                    switch (GetCh())
                    {
                    case 'S':
                    {
                        var request = BfParentOrderRequest.Stop(ProductCode, BfTradeSide.Buy, _ticker.BestAsk + UnexecuteGap, OrderSize);
                        var content = _client.SendParentOrder(request).GetContent();
                        _parentOrderAcceptanceIds.Enqueue(content.ParentOrderAcceptanceId);
                    }
                    break;

                    case 'L':
                    {
                        var request = BfParentOrderRequest.StopLimit(ProductCode, BfTradeSide.Buy, _ticker.BestAsk + UnexecuteGap, _ticker.BestAsk + UnexecuteGap, OrderSize);
                        var content = _client.SendParentOrder(request).GetContent();
                        _parentOrderAcceptanceIds.Enqueue(content.ParentOrderAcceptanceId);
                    }
                    break;

                    case 'T':
                    {
                        var request = BfParentOrderRequest.Trail(ProductCode, BfTradeSide.Buy, UnexecuteGap, OrderSize);
                        var content = _client.SendParentOrder(request).GetContent();
                        _parentOrderAcceptanceIds.Enqueue(content.ParentOrderAcceptanceId);
                    }
                    break;

                    case 'I':
                    {
                        var request = BfParentOrderRequest.IFD(
                            BfParentOrderRequestParameter.Limit(ProductCode, BfTradeSide.Sell, _ticker.BestAsk + UnexecuteGap, OrderSize),
                            BfParentOrderRequestParameter.Limit(ProductCode, BfTradeSide.Buy, _ticker.BestBid - UnexecuteGap, OrderSize)
                            );
                        var content = _client.SendParentOrder(request).GetContent();
                        _parentOrderAcceptanceIds.Enqueue(content.ParentOrderAcceptanceId);
                    }
                    break;

                    case 'O':
                    {
                        var request = BfParentOrderRequest.OCO(
                            BfParentOrderRequestParameter.Limit(ProductCode, BfTradeSide.Sell, _ticker.BestAsk + UnexecuteGap, OrderSize),
                            BfParentOrderRequestParameter.Limit(ProductCode, BfTradeSide.Buy, _ticker.BestBid - UnexecuteGap, OrderSize)
                            );
                        var content = _client.SendParentOrder(request).GetContent();
                        _parentOrderAcceptanceIds.Enqueue(content.ParentOrderAcceptanceId);
                    }
                    break;

                    case 'D':
                    {
                        var request = BfParentOrderRequest.IFDOCO(
                            BfParentOrderRequestParameter.Limit(ProductCode, BfTradeSide.Sell, _ticker.BestAsk + UnexecuteGap, OrderSize),
                            BfParentOrderRequestParameter.Limit(ProductCode, BfTradeSide.Sell, _ticker.BestAsk + UnexecuteGap, OrderSize),
                            BfParentOrderRequestParameter.Limit(ProductCode, BfTradeSide.Buy, _ticker.BestBid - UnexecuteGap, OrderSize)
                            );
                        var content = _client.SendParentOrder(request).GetContent();
                        _parentOrderAcceptanceIds.Enqueue(content.ParentOrderAcceptanceId);
                    }
                    break;

                    case 'F':
                        break;

                    case 'M':
                        break;

                    case 'C':
                    {
                        var resp = _client.CancelParentOrder(ProductCode, parentOrderAcceptanceId: _parentOrderAcceptanceIds.Dequeue());
                        if (!resp.IsErrorOrEmpty)
                        {
                            // Cancel order sent
                        }
                    }
                    break;

                    case 'G':
                    {
                        var resp = _client.GetParentOrders(ProductCode);
                        if (resp.IsOk)
                        {
                            var jobj = JsonConvert.DeserializeObject(resp.Json);
                            var json = JsonConvert.SerializeObject(jobj, Formatting.Indented);
                            Console.WriteLine(json);
                        }
                    }
                    break;

                    case 'R':
                        return;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"{ex.Message}");
                }
            }
        }