Beispiel #1
0
        private void OnOrderFilled(SmartQuant.Order order)
        {
            int    index     = order.Reports.Count - 1;
            double LastQty   = order.Reports[index].LastQty;
            double LastPrice = order.Reports[index].LastPx;
            bool   IsDone    = order.IsDone;

            OpenCloseType      OpenClose = OpenCloseHelper.GetOpenClose(order);
            MonoPositionRecord record    = GetPositionRecord(order.Side, OpenClose);

            switch (OpenClose)
            {
            case OpenCloseType.Open:
                record.FilledOpen(LastQty, LastPrice);
                break;

            case OpenCloseType.Close:
                record.FilledClose(LastQty, LastPrice);
                break;

            case OpenCloseType.CloseToday:
                record.FilledCloseToday(LastQty, LastPrice);
                break;

            default:
                MessageBox.Show("Filled");
                break;
            }
        }
Beispiel #2
0
        private void OnPendingNewOrder(Order order)
        {
            double Qty = order.Qty;

            OpenCloseType      OpenClose = OpenCloseHelper.GetOpenClose(order);
            MonoPositionRecord record    = GetPositionRecord(order.Side, OpenClose);

            switch (OpenClose)
            {
            case OpenCloseType.Open:
                record.NewOrderOpen(Qty);
                break;

            case OpenCloseType.Close:
                record.NewOrderClose(Qty);
                break;

            case OpenCloseType.CloseToday:
                record.NewOrderCloseToday(Qty);
                break;

            default:
                MessageBox.Show("PendingNewOrder");
                break;
            }
        }
Beispiel #3
0
        private OpenCloseType OnOrderRejected(Order order)
        {
            OpenCloseType      OpenClose = OpenCloseHelper.GetOpenClose(order);
            MonoPositionRecord record    = GetPositionRecord(order.Side, OpenClose);

            double LeavesQty = order.LeavesQty;

            switch (OpenClose)
            {
            case OpenCloseType.Open:
                record.OrderRejectedOpen(LeavesQty);
                break;

            case OpenCloseType.Close:
                record.OrderRejectedClose(LeavesQty);
                break;

            case OpenCloseType.CloseToday:
                record.OrderRejectedCloseToday(LeavesQty);
                break;

            default:
                MessageBox.Show("OrderRejected");
                break;
            }

            return(OpenClose);
        }
Beispiel #4
0
        private OpenCloseType OnOrderCancelled(Order order)
        {
            OpenCloseType      OpenClose = OpenCloseHelper.GetOpenClose(order);
            MonoPositionRecord record    = GetPositionRecord(order.Side, OpenClose);

            ++record.CumCancelCnt;

            OnOrderRejected(order);

            return(OpenClose);
        }
Beispiel #5
0
 public DualPositionRecord()
 {
     Long  = new MonoPositionRecord();
     Short = new MonoPositionRecord();
 }
Beispiel #6
0
        public Order RebuildOrder(DualPositionRecord dualRecord, Order order)
        {
            if (order == null)
            {
                return(null);
            }

            List <Order> orders = new List <Order>();

            MonoPositionRecord record = dualRecord.GetPositionRecord(order.Side, OpenCloseType.Close);

            // 先分析是否有仓要平
            double QtyToday     = 0;
            double QtyYesterday = 0;

            record.GetCanCloseQty(out QtyToday, out QtyYesterday);

            // 非上海要处理
            if (order.Instrument.Exchange != "SHFE")
            {
                // 如果是上海就不动,非上海有两方案,全转今仓或全转昨仓
                if (DefaultClose == DefaultClose.CloseToday)
                {
                    // 全转今仓
                    QtyToday    += QtyYesterday;
                    QtyYesterday = 0;
                }
                else
                {
                    // 全转昨仓
                    QtyYesterday += QtyToday;
                    QtyToday      = 0;
                }
            }

            // 1.拆单
            // 3.全开
            double leave = order.Qty;

            if (SeparateOrder == SeparateOrder.SeparateCloseOpen)
            {
                // 拆单,先平今,再平昨,最后开仓
                if (QtyToday > 0 && leave > 0)
                {
                    double Qty = Math.Min(QtyToday, leave);
                    leave -= Qty;

                    Order _order = strategy.Order(order.Instrument, order.Type, order.Side, order.Qty, order.StopPx, order.Price, order.Text);
                    order.Fields.CopyTo(_order.Fields);
                    AddOpenClose(_order, Qty, OpenCloseType.CloseToday);
                    orders.Add(_order);
                }

                if (QtyYesterday > 0 && leave > 0)
                {
                    double Qty = Math.Min(QtyYesterday, leave);
                    leave -= Qty;

                    Order _order = strategy.Order(order.Instrument, order.Type, order.Side, order.Qty, order.StopPx, order.Price, order.Text);
                    order.Fields.CopyTo(_order.Fields);
                    AddOpenClose(_order, Qty, OpenCloseType.Close);
                    orders.Add(_order);
                }

                if (leave > 0)
                {
                    double Qty = leave;
                    leave -= Qty;

                    AddOpenClose(order, Qty, OpenCloseType.Open);
                    orders.Add(order);
                }
            }
            else
            {
                // 发现可平数不够,直接开仓
                if (leave <= QtyToday && leave > 0)
                {
                    double Qty = leave;
                    leave -= Qty;
                    AddOpenClose(order, Qty, OpenCloseType.CloseToday);
                    orders.Add(order);
                }
                else if (leave <= QtyYesterday && leave > 0)
                {
                    double Qty = leave;
                    leave -= Qty;
                    AddOpenClose(order, Qty, OpenCloseType.Close);
                    orders.Add(order);
                }
                else if (leave > 0)
                {
                    double Qty = leave;
                    leave -= Qty;

                    AddOpenClose(order, Qty, OpenCloseType.Open);
                    orders.Add(order);
                }
            }


            // 让每个Order串起来,这样可以自己查找到下一个Order
            for (int i = orders.Count - 1; i > 0; --i)
            {
                orders[i - 1].SetNextTimeOrder(orders[i]);
            }

            if (orders.Count > 0)
            {
                return(orders[0]);
            }

            return(null);
        }