Exemple #1
0
        public DualPositionRecord GetPositionRecord(Instrument instrument)
        {
            DualPositionRecord value;

            if (!Positions.TryGetValue(instrument.Id, out value))
            {
                value            = new DualPositionRecord();
                value.Instrument = instrument;
                Positions.Add(instrument.Id, value);
            }
            return(value);
        }
Exemple #2
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);
        }
Exemple #3
0
 public MonoPositionRecord GetPositionRecord(DualPositionRecord record, SmartQuant.OrderSide Side, OpenCloseType OpenClose)
 {
     return(record.GetPositionRecord(Side, OpenClose));
 }