Пример #1
0
        protected override TraderResult internalBuyStock(string code, float price, int amount)
        {
            const int ID_TXT_CODE  = 0x0408;
            const int ID_TXT_PRICE = 0x0409;
            const int ID_TXT_NUM   = 0x040A;
            const int ID_BTN_BUY   = 0x3EE;

            ClickBuyTreeViewItem();

            // 设定代码,价格,数量
            IntPtr hBuySell = GetDetailPanel();

            Win32API.SendDlgItemMessage(hBuySell, ID_TXT_CODE, Win32Code.WM_SETTEXT, 0, code);
            PeekAndDelay(50);
            Win32API.SendDlgItemMessage(hBuySell, ID_TXT_PRICE, Win32Code.WM_SETTEXT, 0, price.ToString());
            Win32API.SendDlgItemMessage(hBuySell, ID_TXT_NUM, Win32Code.WM_SETTEXT, 0, amount.ToString());

            // 点击买入按钮
            Win32API.PostMessage(hBuySell, Win32Code.WM_COMMAND, ID_BTN_BUY, 0);

            int no = waitAndGetTradeID(hBuySell, code, price, amount);

            TraderResult result = new TraderResult();

            result.Code      = TraderResultEnum.SUCCESS;
            result.EntrustNo = no;
            return(result);
        }
Пример #2
0
        //private List<EntrustRecord> GetEntrustList(String[] ps)
        //{
        //    List<EntrustRecord> records = new List<EntrustRecord>();
        //    for (int i = 1; i < ps.Length; i++)
        //    {
        //        Console.WriteLine(ps[i]);
        //        String[] stocks = ps[i].Split(new string[] { "\t" }, StringSplitOptions.None);
        //        EntrustRecord rec = new EntrustRecord
        //        {
        //            Date = DateTime.Parse(stocks[0] + " " + stocks[6]),
        //            StockCode = stocks[1],
        //            StockName = stocks[2],
        //            OperType = stocks[3],
        //            Price = float.Parse(stocks[4]),
        //            Amount = int.Parse(stocks[5]),
        //            TransactAmount = int.Parse(stocks[7]),
        //            TransactPrice = float.Parse(stocks[8]),
        //            No = int.Parse(stocks[1]),
        //        };
        //        records.Add(rec);
        //    }

        //    return records;
        //}

        //private List<TransactionRecord> GetTransactionList(String[] ps)
        //{
        //    List<TransactionRecord> records = new List<TransactionRecord>();
        //    for (int i = 1; i < ps.Length; i++)
        //    {
        //        Console.WriteLine(ps[i]);
        //        String[] stocks = ps[i].Split(new string[] { "\t" }, StringSplitOptions.None);
        //        TransactionRecord rec = new TransactionRecord
        //        {
        //            Date = DateTime.ParseExact(stocks[0], "yyyyMMdd", null),
        //            EntrustNo = int.Parse(stocks[1]),
        //            StockCode = stocks[2],
        //            StockName = stocks[3],
        //            OperType = stocks[4],
        //            Amount = int.Parse(stocks[5]),
        //            Price = float.Parse(stocks[6]),
        //            Turnover = float.Parse(stocks[7]),
        //            TransactionNo = int.Parse(stocks[10])
        //        };
        //        records.Add(rec);
        //    }

        //    return records;
        //}

        //private void GetStocks(TradingAccount account, String[] ps)
        //{
        //    for (int i = 1; i < ps.Length; i++)
        //    {
        //        String[] stocks = ps[i].Split(new string[] { "\t" }, StringSplitOptions.None);
        //        StockHolderInfo shi = new StockHolderInfo
        //        {
        //            StockCode = stocks[0],
        //            StockName = stocks[1],
        //            CurrentAmount = int.Parse(stocks[2]),
        //            EnableAmount = int.Parse(stocks[3]),
        //            IncomeAmount = int.Parse(stocks[4]),
        //            CostPrice = float.Parse(stocks[5]),
        //            KeepCostPrice = float.Parse(stocks[6]),
        //            LastPrice = float.Parse(stocks[7]),
        //            MarketValue = float.Parse(stocks[10]),
        //            ExchangeName = stocks[11],
        //            StockAccount = stocks[12]
        //        };
        //        account.AddStockHolder(shi);
        //    }
        //}

        /// <summary>
        ///
        /// </summary>
        /// <param name="hWndTrade"></param>
        /// <param name="code"></param>
        /// <param name="price"></param>
        /// <param name="amount"></param>
        /// <returns></returns>
        private int waitAndGetTradeID(IntPtr hWndTrade, String code, float price, int amount)
        {
            const int ID_TXT_CODE  = 0x0408;
            const int ID_TXT_PRICE = 0x0409;
            const int ID_TXT_NUM   = 0x040A;

            uint dwTick    = Win32API.GetTickCount();
            int  entrustNo = 0;

            #region callback

            /*
             *          Win32API.EnumWindowsProc EnumWindowsProc = delegate (IntPtr hTip, int lParam)
             *          {
             *              String clazz = GetClassName(hTip);
             *
             *              // 不是对话框的基类,继续扫描
             *              if ("#32770" != clazz)
             *                  return true;
             *
             *              // 不含有0x0555 组件,就不用管
             *              IntPtr hCaption = Win32API.GetDlgItem(hTip, 0x0555);
             *              if (hCaption == IntPtr.Zero)
             *                  return true;
             *
             *              //
             *              IntPtr hCap = Win32API.GetDlgItem(hTip, 0x0555);
             *              String sCap = GetWindowText(hCap, 10);
             *              switch (sCap.Substring(0, 1))
             *              {
             *                  case "委":
             *                      Win32API.SendMessage(hTip, Win32Code.WM_COMMAND, 0x0006, 0);
             *                      break;
             *                  case "提":
             *                      if ("提示" == sCap)
             *                      {
             *                          String s = GetDlgItemTextEx(hTip, 0x03EC);
             *                          Console.WriteLine("提示内容:" + s);
             *                          if (s.Contains("成功提交"))
             *                          {
             *                              int i = s.IndexOf("。");
             *                              entrustNo = int.Parse(s.Substring(17, i - 17));
             *
             *                              Win32API.SendMessage(hTip, Win32Code.WM_COMMAND, 0X0002, 0);
             *                              return false;
             *                          }
             *                          else if (s == "请输入委托价格")
             *                          {
             *                              Win32API.SendMessage(hTip, Win32Code.WM_COMMAND, 0X0002, 0);
             *
             *                              Win32API.SendDlgItemMessage(hWndTrade, 0x0409, Win32Code.WM_SETTEXT, 0, price.ToString());
             *                              Win32API.PostMessage(hWndTrade, Win32Code.WM_COMMAND, 0x03EE, 0);
             *                          }
             *                          else if (s == "请输入委托数量")
             *                          {
             *                              Win32API.SendMessage(hTip, Win32Code.WM_COMMAND, 0X0002, 0);
             *
             *                              Win32API.SendDlgItemMessage(hWndTrade, 0x040A, Win32Code.WM_SETTEXT, 0, amount.ToString());
             *                              Win32API.PostMessage(hWndTrade, Win32Code.WM_COMMAND, 0x03EE, 0);
             *                          }
             *                          else if (s == "提交失败")
             *                          {
             *                              Win32API.SendMessage(hTip, Win32Code.WM_COMMAND, 0X0002, 0);
             *                              entrustNo = 0;
             *                              return false;
             *                          }
             *                          else if (s == "正在连接委托主站...")
             *                          {
             *                              PeekAndDelay(1000);
             *                          }
             *                          else if (s == "正在验证用户身份...")
             *                          {
             *                              PeekAndDelay(1000);
             *                          }
             *                          else
             *                          {
             *                              Console.WriteLine("未获取到值");
             *                          }
             *                      }
             *                      else if ("提示信息" == sCap)
             *                      {
             *                          // 委托价格不在
             *                          // TODO: 确定0x006是否正确
             *                          Win32API.SendMessage(hTip, Win32Code.WM_COMMAND, 0X0006, 0);
             *                      }
             *                      break;
             *                  default:
             *                      break;
             *              }
             *
             *              return true;
             *          };
             */
            #endregion callback

            while (true)
            {
                if (Win32API.GetTickCount() - dwTick > 3 * 1000 || entrustNo != 0)
                {
                    break;
                }
                PeekAndDelay(40);
                IntPtr hTip = IntPtr.Zero;
                // IntPtr hTip = Win32API.EnumWindows(EnumWindowsProc, ref ptr);
                Win32API.EnumWindows(enumTipWindowProc, ref hTip);

                // 获取窗口标题
                IntPtr hCaption = Win32API.GetDlgItem(hTip, 0x0555);
                if (hCaption == IntPtr.Zero)
                {
                    continue;
                }

                String sCap = GetWindowText(hCaption, 10);
                switch (sCap.Substring(0, 1))
                {
                case "委":
                    Win32API.SendMessage(hTip, Win32Code.WM_COMMAND, 0x0006, 0);
                    break;

                case "提":
                    if ("提示" == sCap)
                    {
                        String s = GetDlgItemTextEx(hTip, 0x03EC);
                        Console.WriteLine("提示内容:" + s);
                        if (s.Contains("成功提交"))
                        {
                            int i = s.IndexOf("。");
                            entrustNo = int.Parse(s.Substring(17, i - 17));

                            Win32API.SendMessage(hTip, Win32Code.WM_COMMAND, 0X0002, 0);

                            return(entrustNo);
                        }
                        else if (s == "请输入委托价格")
                        {
                            Win32API.SendMessage(hTip, Win32Code.WM_COMMAND, 0X0002, 0);

                            Win32API.SendDlgItemMessage(hWndTrade, 0x0409, Win32Code.WM_SETTEXT, 0, price.ToString());
                            Win32API.PostMessage(hWndTrade, Win32Code.WM_COMMAND, 0x03EE, 0);
                        }
                        else if (s == "请输入委托数量")
                        {
                            Win32API.SendMessage(hTip, Win32Code.WM_COMMAND, 0X0002, 0);

                            Win32API.SendDlgItemMessage(hWndTrade, 0x040A, Win32Code.WM_SETTEXT, 0, amount.ToString());
                            Win32API.PostMessage(hWndTrade, Win32Code.WM_COMMAND, 0x03EE, 0);
                        }
                        else if (s == "提交失败")
                        {
                            Win32API.SendMessage(hTip, Win32Code.WM_COMMAND, 0X0002, 0);
                            entrustNo = 0;
                            return(entrustNo);
                        }
                        else if (s == "正在连接委托主站...")
                        {
                            //LogHelper.Instance.WriteLog(null, s, true);
                            PeekAndDelay(1000);
                        }
                        else if (s == "正在验证用户身份...")
                        {
                            //LogHelper.Instance.WriteLog(null, s, true);
                            PeekAndDelay(1000);
                        }
                        else if (s.StartsWith("提交失败"))
                        {
                            Console.WriteLine("发生提交失败{0}", s);
                            Win32API.SendMessage(hTip, Win32Code.WM_COMMAND, 0X0002, 0);

                            Win32API.SendDlgItemMessage(hWndTrade, ID_TXT_CODE, Win32Code.WM_SETTEXT, 0, code);
                            PeekAndDelay(50);
                            Win32API.SendDlgItemMessage(hWndTrade, ID_TXT_PRICE, Win32Code.WM_SETTEXT, 0, price.ToString());
                            Win32API.SendDlgItemMessage(hWndTrade, ID_TXT_NUM, Win32Code.WM_SETTEXT, 0, amount.ToString());
                            Win32API.PostMessage(hWndTrade, Win32Code.WM_COMMAND, 0x03EE, 0);
                        }
                    }
                    else if ("提示信息" == sCap)
                    {
                        // 委托价格不在
                        // TODO: 确定0x006是否正确
                        Win32API.SendMessage(hTip, Win32Code.WM_COMMAND, 0X0006, 0);
                    }
                    break;

                default:
                    break;
                }
            }
            return(entrustNo);
        }