예제 #1
0
        protected override void OnPush(ProtoHeader protoHeader, byte[] data)
        {
            ProtoID   protoID     = (ProtoID)protoHeader.nProtoID;
            uint      serialNo    = protoHeader.nSerialNo;
            FTSPI_Trd trdCallback = null;

            lock (trdCallbackLock)
            {
                trdCallback = this.trdCallback;
            }

            if (trdCallback == null)
            {
                return;
            }

            switch (protoID)
            {
            case ProtoID.TrdUpdateOrder:    //订单状态变动通知(推送)
            {
                Futu.OpenApi.Pb.TrdUpdateOrder.Response rsp;
                try
                {
                    rsp = Futu.OpenApi.Pb.TrdUpdateOrder.Response.ParseFrom(data);
                }
                catch (InvalidProtocolBufferException)
                {
                    rsp = Futu.OpenApi.Pb.TrdUpdateOrder.Response.CreateBuilder().SetRetType((int)Common.RetType.RetType_Invalid).Build();
                }
                trdCallback.OnReply_UpdateOrder(this, serialNo, rsp);
            }
            break;

            case ProtoID.TrdUpdateOrderFill:    //成交通知(推送)
            {
                Futu.OpenApi.Pb.TrdUpdateOrderFill.Response rsp;
                try
                {
                    rsp = Futu.OpenApi.Pb.TrdUpdateOrderFill.Response.ParseFrom(data);
                }
                catch (InvalidProtocolBufferException)
                {
                    rsp = Futu.OpenApi.Pb.TrdUpdateOrderFill.Response.CreateBuilder().SetRetType((int)Common.RetType.RetType_Invalid).Build();
                }
                trdCallback.OnReply_UpdateOrderFill(this, serialNo, rsp);
            }
            break;
            }
        }
예제 #2
0
        public FTAPI_Conn()
        {
            this.channel = FTCAPI.CreateFTAPIChannel();
            FTCAPI.FTAPIChannel_SetProgrammingLanguage(this.channel, "C#");
            this.initConnectCallback = (IntPtr pChannel, long errCode, string desc) =>
            {
                this.OnInitConnect(errCode, desc);
            };
            this.replyCallback = (IntPtr pChannel, int enReqReplyType, IntPtr protoHeaderPtr, IntPtr protoData, int nDataLen) =>
            {
                ProtoHeader protoHeader = (ProtoHeader)Marshal.PtrToStructure(protoHeaderPtr, typeof(ProtoHeader));
                byte[]      data        = null;
                if (nDataLen > 0 && protoData != IntPtr.Zero)
                {
                    data = new byte[nDataLen];
                    Marshal.Copy(protoData, data, 0, nDataLen);
                }

                this.OnReply((ReqReplyType)enReqReplyType, protoHeader, data);
            };
            this.pushCallback = (IntPtr pChannel, IntPtr protoHeaderPtr, IntPtr protoData, int nDataLen) =>
            {
                ProtoHeader protoHeader = (ProtoHeader)Marshal.PtrToStructure(protoHeaderPtr, typeof(ProtoHeader));
                byte[]      data        = new byte[nDataLen];
                Marshal.Copy(protoData, data, 0, nDataLen);
                this.OnPush(protoHeader, data);
            };
            this.disConnCallback = (IntPtr pChannel, long errCode) =>
            {
                this.OnDisConnect(errCode);
            };

            FTCAPI.FTAPIChannel_SetOnInitConnectCallback(channel, initConnectCallback);
            FTCAPI.FTAPIChannel_SetOnDisconnectCallback(channel, disConnCallback);
            FTCAPI.FTAPIChannel_SetOnReplyCallback(channel, replyCallback);
            FTCAPI.FTAPIChannel_SetOnPushCallback(channel, pushCallback);
        }
예제 #3
0
        protected override void OnReply(ReqReplyType replyType, ProtoHeader protoHeader, byte[] data)
        {
            ProtoID protoID  = (ProtoID)protoHeader.nProtoID;
            int     serialNo = protoHeader.nSerialNo;

            lock (this)
            {
                if (this.trdCallback == null)
                {
                    return;
                }

                switch (protoID)
                {
                case ProtoID.TrdGetAccList:    //获取交易账户列表
                {
                    Futu.OpenApi.Pb.TrdGetAccList.Response rsp;
                    if (replyType == ReqReplyType.SvrReply)
                    {
                        try
                        {
                            rsp = Futu.OpenApi.Pb.TrdGetAccList.Response.ParseFrom(data);
                        }
                        catch (InvalidProtocolBufferException)
                        {
                            rsp = Futu.OpenApi.Pb.TrdGetAccList.Response.CreateBuilder().SetRetType((int)Common.RetType.RetType_Invalid).Build();
                        }
                    }
                    else
                    {
                        rsp = Futu.OpenApi.Pb.TrdGetAccList.Response.CreateBuilder().SetRetType((int)replyType).Build();
                    }

                    this.trdCallback.OnReply_GetAccList(this, serialNo, rsp);
                }
                break;

                case ProtoID.TrdUnlockTrade:    //解锁
                {
                    Futu.OpenApi.Pb.TrdUnlockTrade.Response rsp;
                    if (replyType == ReqReplyType.SvrReply)
                    {
                        try
                        {
                            rsp = Futu.OpenApi.Pb.TrdUnlockTrade.Response.ParseFrom(data);
                        }
                        catch (InvalidProtocolBufferException)
                        {
                            rsp = Futu.OpenApi.Pb.TrdUnlockTrade.Response.CreateBuilder().SetRetType((int)Common.RetType.RetType_Invalid).Build();
                        }
                    }
                    else
                    {
                        rsp = Futu.OpenApi.Pb.TrdUnlockTrade.Response.CreateBuilder().SetRetType((int)replyType).Build();
                    }

                    this.trdCallback.OnReply_UnlockTrade(this, serialNo, rsp);
                }
                break;

                case ProtoID.TrdSubAccPush:    //订阅接收推送数据的交易账户
                {
                    Futu.OpenApi.Pb.TrdSubAccPush.Response rsp;
                    if (replyType == ReqReplyType.SvrReply)
                    {
                        try
                        {
                            rsp = Futu.OpenApi.Pb.TrdSubAccPush.Response.ParseFrom(data);
                        }
                        catch (InvalidProtocolBufferException)
                        {
                            rsp = Futu.OpenApi.Pb.TrdSubAccPush.Response.CreateBuilder().SetRetType((int)Common.RetType.RetType_Invalid).Build();
                        }
                    }
                    else
                    {
                        rsp = Futu.OpenApi.Pb.TrdSubAccPush.Response.CreateBuilder().SetRetType((int)replyType).Build();
                    }

                    this.trdCallback.OnReply_SubAccPush(this, serialNo, rsp);
                }
                break;

                case ProtoID.TrdGetFunds:    //获取账户资金
                {
                    Futu.OpenApi.Pb.TrdGetFunds.Response rsp;
                    if (replyType == ReqReplyType.SvrReply)
                    {
                        try
                        {
                            rsp = Futu.OpenApi.Pb.TrdGetFunds.Response.ParseFrom(data);
                        }
                        catch (InvalidProtocolBufferException)
                        {
                            rsp = Futu.OpenApi.Pb.TrdGetFunds.Response.CreateBuilder().SetRetType((int)Common.RetType.RetType_Invalid).Build();
                        }
                    }
                    else
                    {
                        rsp = Futu.OpenApi.Pb.TrdGetFunds.Response.CreateBuilder().SetRetType((int)replyType).Build();
                    }

                    this.trdCallback.OnReply_GetFunds(this, serialNo, rsp);
                }
                break;

                case ProtoID.TrdGetPositionList:    //获取账户持仓
                {
                    Futu.OpenApi.Pb.TrdGetPositionList.Response rsp;
                    if (replyType == ReqReplyType.SvrReply)
                    {
                        try
                        {
                            rsp = Futu.OpenApi.Pb.TrdGetPositionList.Response.ParseFrom(data);
                        }
                        catch (InvalidProtocolBufferException)
                        {
                            rsp = Futu.OpenApi.Pb.TrdGetPositionList.Response.CreateBuilder().SetRetType((int)Common.RetType.RetType_Invalid).Build();
                        }
                    }
                    else
                    {
                        rsp = Futu.OpenApi.Pb.TrdGetPositionList.Response.CreateBuilder().SetRetType((int)replyType).Build();
                    }

                    this.trdCallback.OnReply_GetPositionList(this, serialNo, rsp);
                }
                break;

                case ProtoID.TrdGetMaxTrdQtys:    //获取最大交易数量
                {
                    Futu.OpenApi.Pb.TrdGetMaxTrdQtys.Response rsp;
                    if (replyType == ReqReplyType.SvrReply)
                    {
                        try
                        {
                            rsp = Futu.OpenApi.Pb.TrdGetMaxTrdQtys.Response.ParseFrom(data);
                        }
                        catch (InvalidProtocolBufferException)
                        {
                            rsp = Futu.OpenApi.Pb.TrdGetMaxTrdQtys.Response.CreateBuilder().SetRetType((int)Common.RetType.RetType_Invalid).Build();
                        }
                    }
                    else
                    {
                        rsp = Futu.OpenApi.Pb.TrdGetMaxTrdQtys.Response.CreateBuilder().SetRetType((int)replyType).Build();
                    }

                    this.trdCallback.OnReply_GetMaxTrdQtys(this, serialNo, rsp);
                }
                break;

                case ProtoID.TrdGetOrderList:    //获取当日订单列表
                {
                    Futu.OpenApi.Pb.TrdGetOrderList.Response rsp;
                    if (replyType == ReqReplyType.SvrReply)
                    {
                        try
                        {
                            rsp = Futu.OpenApi.Pb.TrdGetOrderList.Response.ParseFrom(data);
                        }
                        catch (InvalidProtocolBufferException)
                        {
                            rsp = Futu.OpenApi.Pb.TrdGetOrderList.Response.CreateBuilder().SetRetType((int)Common.RetType.RetType_Invalid).Build();
                        }
                    }
                    else
                    {
                        rsp = Futu.OpenApi.Pb.TrdGetOrderList.Response.CreateBuilder().SetRetType((int)replyType).Build();
                    }

                    this.trdCallback.OnReply_GetOrderList(this, serialNo, rsp);
                }
                break;

                case ProtoID.TrdPlaceOrder:    //下单
                {
                    Futu.OpenApi.Pb.TrdPlaceOrder.Response rsp;
                    if (replyType == ReqReplyType.SvrReply)
                    {
                        try
                        {
                            rsp = Futu.OpenApi.Pb.TrdPlaceOrder.Response.ParseFrom(data);
                        }
                        catch (InvalidProtocolBufferException)
                        {
                            rsp = Futu.OpenApi.Pb.TrdPlaceOrder.Response.CreateBuilder().SetRetType((int)Common.RetType.RetType_Invalid).Build();
                        }
                    }
                    else
                    {
                        rsp = Futu.OpenApi.Pb.TrdPlaceOrder.Response.CreateBuilder().SetRetType((int)replyType).Build();
                    }

                    this.trdCallback.OnReply_PlaceOrder(this, serialNo, rsp);
                }
                break;

                case ProtoID.TrdModifyOrder:    //修改订单
                {
                    Futu.OpenApi.Pb.TrdModifyOrder.Response rsp;
                    if (replyType == ReqReplyType.SvrReply)
                    {
                        try
                        {
                            rsp = Futu.OpenApi.Pb.TrdModifyOrder.Response.ParseFrom(data);
                        }
                        catch (InvalidProtocolBufferException)
                        {
                            rsp = Futu.OpenApi.Pb.TrdModifyOrder.Response.CreateBuilder().SetRetType((int)Common.RetType.RetType_Invalid).Build();
                        }
                    }
                    else
                    {
                        rsp = Futu.OpenApi.Pb.TrdModifyOrder.Response.CreateBuilder().SetRetType((int)replyType).Build();
                    }

                    this.trdCallback.OnReply_ModifyOrder(this, serialNo, rsp);
                }
                break;

                case ProtoID.TrdGetOrderFillList:    //获取当日成交列表
                {
                    Futu.OpenApi.Pb.TrdGetOrderFillList.Response rsp;
                    if (replyType == ReqReplyType.SvrReply)
                    {
                        try
                        {
                            rsp = Futu.OpenApi.Pb.TrdGetOrderFillList.Response.ParseFrom(data);
                        }
                        catch (InvalidProtocolBufferException)
                        {
                            rsp = Futu.OpenApi.Pb.TrdGetOrderFillList.Response.CreateBuilder().SetRetType((int)Common.RetType.RetType_Invalid).Build();
                        }
                    }
                    else
                    {
                        rsp = Futu.OpenApi.Pb.TrdGetOrderFillList.Response.CreateBuilder().SetRetType((int)replyType).Build();
                    }

                    this.trdCallback.OnReply_GetOrderFillList(this, serialNo, rsp);
                }
                break;

                case ProtoID.TrdGetHistoryOrderList:    //获取历史订单列表
                {
                    Futu.OpenApi.Pb.TrdGetHistoryOrderList.Response rsp;
                    if (replyType == ReqReplyType.SvrReply)
                    {
                        try
                        {
                            rsp = Futu.OpenApi.Pb.TrdGetHistoryOrderList.Response.ParseFrom(data);
                        }
                        catch (InvalidProtocolBufferException)
                        {
                            rsp = Futu.OpenApi.Pb.TrdGetHistoryOrderList.Response.CreateBuilder().SetRetType((int)Common.RetType.RetType_Invalid).Build();
                        }
                    }
                    else
                    {
                        rsp = Futu.OpenApi.Pb.TrdGetHistoryOrderList.Response.CreateBuilder().SetRetType((int)replyType).Build();
                    }

                    this.trdCallback.OnReply_GetHistoryOrderList(this, serialNo, rsp);
                }
                break;

                case ProtoID.TrdGetHistoryOrderFillList:    //获取历史成交列表
                {
                    Futu.OpenApi.Pb.TrdGetHistoryOrderFillList.Response rsp;
                    if (replyType == ReqReplyType.SvrReply)
                    {
                        try
                        {
                            rsp = Futu.OpenApi.Pb.TrdGetHistoryOrderFillList.Response.ParseFrom(data);
                        }
                        catch (InvalidProtocolBufferException)
                        {
                            rsp = Futu.OpenApi.Pb.TrdGetHistoryOrderFillList.Response.CreateBuilder().SetRetType((int)Common.RetType.RetType_Invalid).Build();
                        }
                    }
                    else
                    {
                        rsp = Futu.OpenApi.Pb.TrdGetHistoryOrderFillList.Response.CreateBuilder().SetRetType((int)replyType).Build();
                    }

                    this.trdCallback.OnReply_GetHistoryOrderFillList(this, serialNo, rsp);
                }
                break;
                }
            }
        }
예제 #4
0
 protected virtual void OnPush(ProtoHeader protoHeader, byte[] data)
 {
 }
예제 #5
0
 protected virtual void OnReply(ReqReplyType replyType, ProtoHeader protoHeader, byte[] data)
 {
 }