Ejemplo n.º 1
0
        public TrdPlaceOrder.Response PlaceOrderSync(TrdPlaceOrder.C2S c2s)
        {
            ReqInfo reqInfo   = null;
            Object  syncEvent = new Object();

            lock (syncEvent)
            {
                lock (trdLock)
                {
                    if (trdConnStatus != ConnStatus.READY)
                    {
                        return(null);
                    }
                    TrdPlaceOrder.Request req = TrdPlaceOrder.Request.CreateBuilder().SetC2S(c2s).Build();
                    uint sn = trd.PlaceOrder(req);
                    if (sn == 0)
                    {
                        return(null);
                    }
                    reqInfo = new ReqInfo(ProtoID.TrdPlaceOrder, syncEvent);
                    trdReqInfoMap.Add(sn, reqInfo);
                }
                Monitor.Wait(syncEvent);
                return((TrdPlaceOrder.Response)reqInfo.Rsp);
            }
        }
Ejemplo n.º 2
0
        void Buy(QotCommon.Security sec, ulong trdAcc, TrdCommon.TrdMarket trdMarket,
                 TrdCommon.TrdEnv trdEnv)
        {
            TrdCommon.TrdSecMarket secMarket = TrdCommon.TrdSecMarket.TrdSecMarket_Unknown;
            if (sec.Market == (int)QotCommon.QotMarket.QotMarket_HK_Security)
            {
                secMarket = TrdCommon.TrdSecMarket.TrdSecMarket_HK;
            }
            else
            {
                secMarket = TrdCommon.TrdSecMarket.TrdSecMarket_US;
            }

            TrdGetFunds.Response getFundsRsp = GetFundsSync(trdAcc, trdMarket, trdEnv, false, TrdCommon.Currency.Currency_Unknown);
            if (getFundsRsp.RetType != (int)Common.RetType.RetType_Succeed)
            {
                Console.Error.Write("getFundsSync err; retType={0} msg={1}\n", getFundsRsp.RetType, getFundsRsp.RetMsg);
                return;
            }
            QotGetSecuritySnapshot.Response snapshotRsp = GetSecuritySnapshotSync(new List <QotCommon.Security>()
            {
                sec
            });
            if (snapshotRsp.RetType != (int)Common.RetType.RetType_Succeed)
            {
                Console.Error.Write("getSecuritySnapshotSync err; retType={0} msg={1}\n", snapshotRsp.RetType, snapshotRsp.RetMsg);
                return;
            }
            int    lotSize  = snapshotRsp.S2C.SnapshotListList[0].Basic.LotSize;
            double curPrice = snapshotRsp.S2C.SnapshotListList[0].Basic.CurPrice;
            double cash     = getFundsRsp.S2C.Funds.Cash;
            int    qty      = (int)Math.Floor(cash / curPrice);

            qty = qty / lotSize * lotSize;
            TrdCommon.TrdHeader trdHeader = MakeTrdHeader(trdEnv, trdAcc, trdMarket);
            TrdPlaceOrder.C2S   c2s       = TrdPlaceOrder.C2S.CreateBuilder()
                                            .SetPacketID(trd.NextPacketID())
                                            .SetHeader(trdHeader)
                                            .SetTrdSide((int)TrdCommon.TrdSide.TrdSide_Buy)
                                            .SetOrderType((int)TrdCommon.OrderType.OrderType_Normal)
                                            .SetCode(sec.Code)
                                            .SetQty(qty)
                                            .SetPrice(curPrice)
                                            .SetAdjustPrice(true)
                                            .SetSecMarket((int)secMarket)
                                            .Build();
            TrdPlaceOrder.Response placeOrderRsp = PlaceOrderSync(c2s);
            Console.WriteLine(placeOrderRsp);
        }
Ejemplo n.º 3
0
        void Sell(QotCommon.Security sec, TrdCommon.Position pstn, ulong trdAcc, TrdCommon.TrdMarket trdMarket,
                  TrdCommon.TrdEnv trdEnv)
        {
            QotGetSecuritySnapshot.Response snapshotRsp = GetSecuritySnapshotSync(new List <QotCommon.Security>()
            {
                sec
            });
            if (snapshotRsp.RetType != (int)Common.RetType.RetType_Succeed)
            {
                Console.Error.Write("getSecuritySnapshotSync err; retType={0} msg={1}\n", snapshotRsp.RetType, snapshotRsp.RetMsg);
                return;
            }
            double price = snapshotRsp.S2C.SnapshotListList[0].Basic.CurPrice;

            TrdCommon.TrdSecMarket secMarket = TrdCommon.TrdSecMarket.TrdSecMarket_Unknown;
            if (sec.Market == (int)QotCommon.QotMarket.QotMarket_HK_Security)
            {
                secMarket = TrdCommon.TrdSecMarket.TrdSecMarket_HK;
            }
            else
            {
                secMarket = TrdCommon.TrdSecMarket.TrdSecMarket_US;
            }
            TrdCommon.TrdHeader trdHeader = MakeTrdHeader(trdEnv, trdAcc, trdMarket);
            TrdPlaceOrder.C2S   c2s       = TrdPlaceOrder.C2S.CreateBuilder()
                                            .SetPacketID(trd.NextPacketID())
                                            .SetHeader(trdHeader)
                                            .SetTrdSide((int)TrdCommon.TrdSide.TrdSide_Sell)
                                            .SetOrderType((int)TrdCommon.OrderType.OrderType_Normal)
                                            .SetCode(sec.Code)
                                            .SetQty(pstn.CanSellQty)
                                            .SetPrice(price)
                                            .SetAdjustPrice(true)
                                            .SetSecMarket((int)secMarket)
                                            .Build();
            TrdPlaceOrder.Response placeOrderRsp = PlaceOrderSync(c2s);
            Console.WriteLine(placeOrderRsp);
        }
Ejemplo n.º 4
0
        public void TradeHkTest()
        {
            bool ret = InitConnectTrdSync(Config.OpendIP, Config.OpendPort);

            if (!ret)
            {
                Console.Error.WriteLine("Fail to connect opend");
                return;
            }
            else
            {
                Console.WriteLine("trd connected");
            }

            TrdUnlockTrade.Response unlockTradeRsp = UnlockTradeSync(Config.UnlockTradePwdMd5, true);
            if (unlockTradeRsp.RetType != (int)Common.RetType.RetType_Succeed)
            {
                Console.Error.Write("unlockTradeSync err; retType={0} msg={1}\n", unlockTradeRsp.RetType, unlockTradeRsp.RetMsg);
            }
            else
            {
                Console.WriteLine("unlock succeed");
            }

            TrdGetFunds.Response getFundsRsp = GetFundsSync(Config.TrdAcc, TrdCommon.TrdMarket.TrdMarket_HK, TrdCommon.TrdEnv.TrdEnv_Real,
                                                            false, TrdCommon.Currency.Currency_Unknown);
            Console.Write("getFundsSync: {0}\n", getFundsRsp);

            TrdGetAccList.Response getAccListRsp = GetAccListSync(Config.UserID);
            Console.Write("getAccList: {0}\n", getAccListRsp);

            {
                TrdGetPositionList.Response getPositionListRsp = GetPositionListSync(Config.TrdAcc,
                                                                                     TrdCommon.TrdMarket.TrdMarket_HK,
                                                                                     TrdCommon.TrdEnv.TrdEnv_Real, null,
                                                                                     -50.0, 50.0, false);
                Console.Write("getPositionList: {0}\n", getPositionListRsp);
            }

            {
                TrdGetOrderList.Response getOrderListRsp = GetOrderListSync(Config.TrdAcc, TrdCommon.TrdMarket.TrdMarket_HK,
                                                                            TrdCommon.TrdEnv.TrdEnv_Real, false, null,
                                                                            new List <TrdCommon.OrderStatus> {
                    TrdCommon.OrderStatus.OrderStatus_Submitted
                });
                Console.Write("getOrderList: {0}\n", getOrderListRsp);
            }

            {
                TrdCommon.TrdHeader header = TrdCommon.TrdHeader.CreateBuilder()
                                             .SetTrdEnv((int)TrdCommon.TrdEnv.TrdEnv_Real)
                                             .SetAccID(Config.TrdAcc)
                                             .SetTrdMarket((int)TrdCommon.TrdMarket.TrdMarket_HK)
                                             .Build();
                TrdPlaceOrder.C2S c2s = TrdPlaceOrder.C2S.CreateBuilder()
                                        .SetPacketID(trd.NextPacketID())
                                        .SetHeader(header)
                                        .SetTrdSide((int)TrdCommon.TrdSide.TrdSide_Sell)
                                        .SetOrderType((int)TrdCommon.OrderType.OrderType_Normal)
                                        .SetCode("00700")
                                        .SetQty(100)
                                        .SetPrice(700)
                                        .SetAdjustPrice(true)
                                        .SetSecMarket((int)TrdCommon.TrdSecMarket.TrdSecMarket_HK)
                                        .Build();
                TrdPlaceOrder.Response placeOrderRsp = PlaceOrderSync(c2s);
                Console.Write("placeOrder: {0}\n", placeOrderRsp);
            }

            {
                TrdCommon.TrdFilterConditions filterConditions = TrdCommon.TrdFilterConditions.CreateBuilder()
                                                                 .AddCodeList("00700")
                                                                 .Build();
                TrdGetOrderFillList.Response getOrderFillListRsp = GetOrderFillListSync(Config.TrdAcc,
                                                                                        TrdCommon.TrdMarket.TrdMarket_HK,
                                                                                        TrdCommon.TrdEnv.TrdEnv_Real, false, filterConditions);
                Console.Write("getOrderFillList: {0}\n", getOrderFillListRsp);
            }
        }
Ejemplo n.º 5
0
        void smartSell(String code, QotCommon.QotMarket qotMarket, TrdCommon.TrdSecMarket secMarket,
                       int volume, TrdCommon.OrderType orderType,
                       TrdCommon.TrdEnv trdEnv,
                       ulong accID,
                       TrdCommon.TrdMarket trdMarket,
                       String trdPwdMD5)
        {
            int lotSize = 0;

            QotCommon.Security sec = MakeSec(qotMarket, code);
            while (true)
            {
                Thread.Sleep(1000);
                if (lotSize == 0)
                {
                    List <QotCommon.Security> secList = new List <QotCommon.Security>();
                    secList.Add(sec);
                    QotGetSecuritySnapshot.Response rsp = GetSecuritySnapshotSync(secList);
                    if (rsp.RetType != (int)Common.RetType.RetType_Succeed)
                    {
                        Console.Error.Write("getSecuritySnapshotSync err; retType={} msg={1}\n", rsp.RetType,
                                            rsp.RetMsg);
                        return;
                    }
                    lotSize = rsp.S2C.SnapshotListList[0].Basic.LotSize;
                    if (lotSize <= 0)
                    {
                        Console.Error.Write("invalid lot size; code={0} lotSize={1}\n", code, lotSize);
                        return;
                    }
                }
                int qty = (volume / lotSize) * lotSize; // 将数量调整为整手的股数

                QotSub.Response subRsp = SubSync(new List <QotCommon.Security>()
                {
                    sec
                },
                                                 new List <QotCommon.SubType>()
                {
                    QotCommon.SubType.SubType_OrderBook
                },
                                                 true,
                                                 false);
                if (subRsp.RetType != (int)Common.RetType.RetType_Succeed)
                {
                    Console.Error.Write("subSync er; retType={0}; msg={1}\n", subRsp.RetType, subRsp.RetMsg);
                    return;
                }

                QotGetOrderBook.Response getOrderBookRsp = GetOrderBookSync(MakeSec(qotMarket, code), 1);
                if (getOrderBookRsp.RetType != (int)Common.RetType.RetType_Succeed)
                {
                    Console.Error.Write("getOrderBookSync er; retType={0}; msg={1}\n", subRsp.RetType, subRsp.RetMsg);
                    return;
                }
                double bid1Price = getOrderBookRsp.S2C.OrderBookBidListList[0].Price;
                TrdCommon.TrdHeader trdHeader = MakeTrdHeader(trdEnv, accID, trdMarket);
                TrdPlaceOrder.C2S   c2s       = TrdPlaceOrder.C2S.CreateBuilder()
                                                .SetHeader(trdHeader)
                                                .SetPacketID(trd.NextPacketID())
                                                .SetTrdSide((int)TrdCommon.TrdSide.TrdSide_Sell)
                                                .SetOrderType((int)orderType)
                                                .SetCode(code)
                                                .SetQty(qty)
                                                .SetPrice(bid1Price)
                                                .SetAdjustPrice(true)
                                                .SetSecMarket((int)secMarket)
                                                .Build();
                TrdPlaceOrder.Response placeOrderRsp = PlaceOrderSync(c2s);
                if (placeOrderRsp.RetType != (int)Common.RetType.RetType_Succeed)
                {
                    Console.Error.Write("placeOrderSync err; retType={0} msg={1}\n", placeOrderRsp.RetType, placeOrderRsp.RetMsg);
                    return;
                }
                else
                {
                    Console.Write("下单成功: {0}\n", placeOrderRsp.S2C);
                }
            }
        }
Ejemplo n.º 6
0
        void simpleSell(String code, QotCommon.QotMarket qotMarket, TrdCommon.TrdSecMarket secMarket,
                        double price, int volume, TrdCommon.OrderType orderType,
                        TrdCommon.TrdEnv trdEnv,
                        ulong accID,
                        TrdCommon.TrdMarket trdMarket,
                        String trdPwdMD5)
        {
            bool ret = InitConnectQotSync(Config.OpendIP, Config.OpendPort);

            if (!ret)
            {
                Console.Error.WriteLine("Fail to connect opend");
                return;
            }
            ret = InitConnectTrdSync(Config.OpendIP, Config.OpendPort);
            if (!ret)
            {
                Console.Error.WriteLine("Fail to connect opend");
                return;
            }
            int lotSize = 0;

            QotCommon.Security sec = MakeSec(qotMarket, code);
            while (true)
            {
                Thread.Sleep(1000);
                if (lotSize == 0)
                {
                    List <QotCommon.Security> secList = new List <QotCommon.Security>();
                    secList.Add(sec);
                    QotGetSecuritySnapshot.Response rsp = GetSecuritySnapshotSync(secList);
                    if (rsp.RetType != (int)Common.RetType.RetType_Succeed)
                    {
                        Console.Error.Write("getSecuritySnapshotSync err; retType={0} msg={1}\n", rsp.RetType,
                                            rsp.RetMsg);
                        return;
                    }
                    lotSize = rsp.S2C.SnapshotListList[0].Basic.LotSize;
                    if (lotSize <= 0)
                    {
                        Console.Error.Write("invalid lot size; code={0} lotSize={1}\n", code, lotSize);
                        return;
                    }
                }

                int qty = (volume / lotSize) * lotSize; // 将数量调整为整手的股数
                TrdCommon.TrdHeader trdHeader = MakeTrdHeader(trdEnv, accID, trdMarket);
                TrdPlaceOrder.C2S   c2s       = TrdPlaceOrder.C2S.CreateBuilder()
                                                .SetHeader(trdHeader)
                                                .SetPacketID(trd.NextPacketID())
                                                .SetTrdSide((int)TrdCommon.TrdSide.TrdSide_Sell)
                                                .SetOrderType((int)orderType)
                                                .SetCode(code)
                                                .SetQty(qty)
                                                .SetPrice(price)
                                                .SetAdjustPrice(true)
                                                .SetSecMarket((int)secMarket)
                                                .Build();
                TrdPlaceOrder.Response placeOrderRsp = PlaceOrderSync(c2s);
                if (placeOrderRsp.RetType != (int)Common.RetType.RetType_Succeed)
                {
                    Console.Error.Write("placeOrderSync err; retType={0} msg={1}\n", placeOrderRsp.RetType, placeOrderRsp.RetMsg);
                    return;
                }
                else
                {
                    Console.Write("下单成功: {0}\n", placeOrderRsp.S2C);
                }
            }
        }