示例#1
0
        public static void Test()
        {
            SocketWorker sw = new SocketWorker();
            //      sw.Connect();

            //    SYRequest.QureyCoinSymbolMarket("XDFYX");

            //           SYRequest.QureyBuy(100, "XDFYX", "12", "0.12");

            //            SYRequest.QureySell(100, "XDFYX", "20", "99.9999");

            //            SYRequest.QureyPosition(101, 1, 10);

            //           SYRequest.QureyCancelOrder(101, "");

            //           SYRequest.QureyReturnTrade("", EnumOrderType.购买);

            //     SYRequest.OureyAllOrder(100, "XDFYX");

            //            SYRequest.QureyOrder("11201903151252423176");

            // SocketWorker s = new SocketWorker();

            //SYBase bs = new SYStrategy();
            //bs.Load();
        }
示例#2
0
 //连接行情端
 private bool MarketConnect()
 {
     socketWorker = new SocketWorker();
     socketWorker.Init(this, coinSymbol);
     if (socketWorker.Connect())
     {
         Print("建立行情 socket 连接成功!");
         isConnect = true;
         return(true);
     }
     Print("建立行情 socket 连接失败!");
     runStatus = EnumRunStatus.行情连接异常;
     return(false);
 }
示例#3
0
        public static void ThreadFun(object obj)
        {
            SocketWorker socketWorker = (SocketWorker)obj;
            Socket       socket       = socketWorker.sock;

            try
            {
                byte[] coin = Encoding.UTF8.GetBytes(socketWorker.subJson);
                Head   hed  = new Head();

                hed.length      = 26 + coin.Length;
                hed.sequencedId = 0L;
                hed.code        = 20021;
                hed.version     = 1;
                hed.terminal    = "1001";
                hed.requestId   = 0;

                byte[] sedh     = hed.headToByteArray();
                int    hdlength = sedh.Length + coin.Length;
                byte[] senddata = new byte[hdlength];
                // 包头 包体 合并
                Buffer.BlockCopy(sedh, 0, senddata, 0, sedh.Length);  //这种方法仅适用于字节数组
                Buffer.BlockCopy(coin, 0, senddata, sedh.Length, coin.Length);

                // 发送请求信息
                socket.Send(senddata, senddata.Length, SocketFlags.None);
                // 接收交易信息
                for (; socket.Connected && !socketWorker.tradeStop;)
                {
                    int headBuf    = Head.LENGTH;
                    int headLeft   = Head.LENGTH;
                    int headOffset = 0;

                    int    readLen    = 0;
                    byte[] HeadBuffer = new byte[Head.LENGTH];//数据长度
                    while (socket.Connected && headLeft > 0)
                    {
                        readLen = socket.Receive(HeadBuffer, headOffset, headLeft, SocketFlags.None);
                        if (readLen <= 0)
                        {
                            break;
                        }
                        headOffset += readLen;
                        headLeft   -= readLen;
                    }

                    Head head = Head.ByteToHead(HeadBuffer);
                    if (head == null)
                    {
                        throw new Exception("解析包头信息异常");
                    }
                    //接收包体数据
                    int bodyBuf    = head.length - Head.LENGTH;
                    int bodyLeft   = bodyBuf;
                    int bodyOffset = 0;

                    byte[] bodyBuffer = new byte[head.length - Head.LENGTH];

                    while (socket.Connected && bodyLeft > 0)
                    {
                        readLen = socket.Receive(bodyBuffer, bodyOffset, bodyLeft, SocketFlags.None);//读取socket数据
                        if (readLen <= 0)
                        {
                            break;
                        }
                        bodyLeft   -= readLen;
                        bodyOffset += readLen;
                    }
                    //读取信息后 解析
                    string json = Encoding.UTF8.GetString(bodyBuffer);
                    if (head.code == 20023 || head.code == 20024)
                    {
                        socketWorker.attrbute.OnMarket(head.code, json);
                    }
                }
            }
            catch (ThreadAbortException tae)
            {
                try {
                    socketWorker.attrbute.OnError(EnumExceptionCode.线程被终止, "【SocketWorker】 行情线程被强制关闭,线程即将退出! \r\n 异常信息:" + tae.ToString());
                    return;
                }
                catch { }
            }
            catch (SocketException se)
            {
                try
                {
                    if (socket.Connected)
                    {
                        socket.Shutdown(SocketShutdown.Both);
                        socket.Close();
                    }
                    socketWorker.attrbute.OnError(EnumExceptionCode.socket异常, "【SocketWorker】 Socket 出发异常,线程即将退出! \r\n 异常信息:" + se.ToString());
                    return;
                }
                catch { }
            }
            catch (Exception ex)
            {
                socketWorker.attrbute.OnError(EnumExceptionCode.其他异常, "【SocketWorker】 异常信息:" + ex.ToString());
                return;
            }
        }
示例#4
0
        /// <summary>
        /// 加载交易信息
        /// </summary>
        public sealed override bool Load()
        {
            try
            {
                #region 成员属性赋值

                //用户ID
                userId = 100;
                //用户名
                userName = "";
                //密码
                password = "";

                //设置  开仓倍数 (必成交)
                mustMore = 1;
                //设置 开仓倍数  (非成交)
                OtherMore = 1;

                //真实盘口深度
                maxRealQuoteLen = 10;
                //伪盘口深度
                maxQuoteLen = maxRealQuoteLen + 1;

                //产品编号
                coinSymbol = "XDFYX";
                //交易对
                symbol = coinSymbol + "/CNY";
                //最小变动单位
                minPrice = 0.01;
                //线程休眠等级
                timerGrade = 1;
                //
                mustToBuy = 30;
                //
                mustToSell = 30;
                //
                otherToBuy = 80;
                //
                otherToSell = 80;

                maxOrderCount = 60;

                //设置线程的休眠等级
                SetTimer(timerGrade);

                #endregion

                #region 交易价格信息处理
                //查询当日交易的价格区间
                coinSymbolMarket = SYRequest.QureyCoinSymbolMarket(coinSymbol);
                if (coinSymbolMarket == null)
                {
                    Print("获取当日涨跌停信息失败,策略启动失败!");
                    return(false);
                }
                if (coinSymbolMarket.beginPrice == null || coinSymbolMarket.maxPrice == null || coinSymbolMarket.minPrice == null)
                {
                    Print("获取当日涨跌停信息有误,策略启动失败!");
                    return(false);
                }

                // 开盘价
                openPrice = (double)decimal.Round((decimal)coinSymbolMarket.beginPrice, 2);
                // 最高限价
                upLimitPrice = (double)decimal.Round((decimal)coinSymbolMarket.maxPrice, 2);
                // 最低限价
                lowLimitPrice = (double)decimal.Round((decimal)coinSymbolMarket.minPrice, 2);

                Print("获取当日涨跌停信息成功!");
                #endregion

                #region 更新用户持仓信息
                //查询所有持仓
                position = SYRequest.QureyPosition(userId, coinSymbol);
                if (position == null || position.balance == 0)
                {
                    Print("该账户不存在仓位信息!");
                }
                else
                {
                    Print("获取到所有仓位信息信息!");
                }
                #endregion

                #region 盘口信息更新
                //查询盘口信息
                Quote q = SYRequest.QureyQuote(symbol);
                realQuoteBuy  = q.bid.items.ToArray();
                realQuoteSell = q.ask.items.ToArray();
                int len = maxQuoteLen;
                quoteBuy  = new QuoteItem[len];
                quoteSell = new QuoteItem[len];
                for (int i = 0; i < len; i++)
                {
                    quoteBuy[i]  = new QuoteItem();
                    quoteSell[i] = new QuoteItem();
                }

                productFakeQuote();
                #endregion

                #region 建立行情连接
                socketWorker = new SocketWorker();
                if (!socketWorker.Connect())
                {
                    Print("建立行情 socket 连接失败,策略启动失败!");
                    return(false);
                }
                socketWorker.Init(this, symbol);
                Print("建立行情 socket 连接成功!");
                #endregion

                return(true);
            } catch (Exception ex)
            {
                OnError(EnumExceptionCode.其他异常, ex.ToString());
                return(false);
            }
        }
示例#5
0
        public static void ThreadFun(object obj)
        {
            SocketWorker socketWorker = (SocketWorker)obj;


            Socket socket = socketWorker.sock;

            byte[] coin = Encoding.UTF8.GetBytes("{\"symbol\":\"XDFYX / CNY\"}");
            Head   hed  = new Head();

            hed.length      = 26 + coin.Length;
            hed.sequencedId = 0L;
            hed.code        = 20021;
            hed.version     = 1;
            hed.terminal    = "1001";
            hed.requestId   = 0;

            byte[] sedh = hed.headToByteArray();

            int hdlength = sedh.Length + coin.Length;

            byte[] senddata = new byte[hdlength];

            Buffer.BlockCopy(sedh, 0, senddata, 0, sedh.Length);  //这种方法仅适用于字节数组
            Buffer.BlockCopy(coin, 0, senddata, sedh.Length, coin.Length);

            int sendlen = socket.Send(senddata, senddata.Length, SocketFlags.None);

            try
            {
                for (; socket.Connected;)
                {
                    int headBuf    = Head.LENGTH;
                    int headLeft   = Head.LENGTH;
                    int headOffset = 0;

                    int    readLen    = 0;
                    byte[] HeadBuffer = new byte[Head.LENGTH];//数据长度
                    while (socket.Connected && headLeft > 0)
                    {
                        readLen = socket.Receive(HeadBuffer, headOffset, headLeft, SocketFlags.None);
                        if (readLen <= 0)
                        {
                            break;
                        }
                        headOffset += readLen;
                        headLeft   -= readLen;
                    }

                    Head head = Head.ByteToHead(HeadBuffer);
                    if (head == null)
                    {
                        throw new Exception("解析包头信息异常");
                    }
                    //接收包体数据
                    int bodyBuf    = head.length - Head.LENGTH;
                    int bodyLeft   = bodyBuf;
                    int bodyOffset = 0;

                    byte[] bodyBuffer = new byte[head.length - Head.LENGTH];

                    while (socket.Connected && bodyLeft > 0)
                    {
                        readLen = socket.Receive(bodyBuffer, bodyOffset, bodyLeft, SocketFlags.None);//读取socket数据
                        if (readLen <= 0)
                        {
                            break;
                        }
                        bodyLeft   -= readLen;
                        bodyOffset += readLen;
                    }
                    //读取信息后 解析
                    string message = Encoding.UTF8.GetString(bodyBuffer);
                    socketWorker.op.printmsg(message);
                }
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
        }