Example #1
0
        /// <summary>
        /// 连接到同花顺
        /// </summary>
        public void Attach()
        {
            AutoTradeService.InitSysTreeView32Handle();

            DataCenter.ThsDealService.RegisterListener(5, ThsDealCallBack);
            DataCenter.ThsDealService.RegisterListener(6, ThsDealCallBack);
            DataCenter.ThsDealService.RegisterListener(7, ThsDealCallBack);
            DataCenter.ThsDealService.RegisterListener(8, ThsDealCallBack);

            ButtonA btnStart = m_mainFrame.GetButton("btnStart");

            if (btnStart == null)
            {
                return;
            }

            if (!m_isStart)
            {
                QueryAll();
                m_isStart     = true;
                btnStart.Text = "停止";
            }
            else
            {
                m_isStart     = false;
                btnStart.Text = "启动";
            }
        }
Example #2
0
        /// <summary>
        /// 同花顺交易线程处理函数
        /// </summary>
        public static void OnTimer()
        {
            THSDealInfo req = DataCenter.ThsDealService.GetTHSDealRequest();

            if (req != null)
            {
                String result = "";
                // 交易类型
                // 0:默认值
                // 1:买入
                // 2:卖出
                // 3:撤销买入
                // 4:撤销卖出
                // 5:查询持仓
                // 6:查询成交
                // 7:查询资金
                // 8:查询委托
                switch (req.m_operateType)
                {
                case 0:
                    break;

                case 1:
                    break;

                case 2:
                    break;

                case 3:
                    AutoTradeService.CancelBuy();
                    break;

                case 4:
                    AutoTradeService.CancelSell();
                    break;

                case 5:
                    result = AutoTradeService.GetSecurityPosition();
                    break;

                case 6:
                    result = AutoTradeService.GetSecurityTrade();
                    break;

                case 7:
                    result = AutoTradeService.GetSecurityCaptial();
                    break;

                case 8:
                    result = AutoTradeService.GetSecurityCommission();
                    break;
                }
                DataCenter.ThsDealService.OnReceive(req.m_operateType, req.m_reqID, result);
                DataCenter.ThsDealService.RemoveThsDealRequest(req);
            }
        }
Example #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="param"></param>
        public void DealStrategyThread(object param)
        {
            SecurityLatestData latestData = param as SecurityLatestData;

            if (latestData == null)
            {
                return;
            }

            if (m_securityStrategySettingCurrnet == null || m_securityStrategySettingCurrnet.m_strategyType != 0)
            {
                return;
            }

            SecurityRangeTradeCondition securityRangeTradeCondition
                = JsonConvert.DeserializeObject <SecurityRangeTradeCondition>(m_securityStrategySettingCurrnet.m_strategySettingInfo);

            if (securityRangeTradeCondition == null)
            {
                return;
            }

            bool  isInitBuild      = securityRangeTradeCondition.m_initBuildFlag;
            float bottomRangePrice = securityRangeTradeCondition.m_bottomRangePrice;
            float topRangePrice    = securityRangeTradeCondition.m_topRangePrice;

            // 当前价格超过了区间的上限价格或者低于区间的下限,则不做处理
            if (latestData.m_close > topRangePrice || latestData.m_close < bottomRangePrice)
            {
                return;
            }
            // 已经建仓完成
            if (isInitBuild)
            {
                bool  isBasePrice      = securityRangeTradeCondition.m_isBasePrice;
                float lastDealPrice    = securityRangeTradeCondition.m_lastDealPrice;
                int   buyCount         = securityRangeTradeCondition.m_buyCount;
                int   sellCount        = securityRangeTradeCondition.m_sellCount;
                float lowLastDealBuy   = securityRangeTradeCondition.m_lowLastDealBuy;
                float overLastDealSell = securityRangeTradeCondition.m_overLastDealSell;
                bool  isCrossBuy       = securityRangeTradeCondition.m_isCrossBuy;
                bool  isCrossSell      = securityRangeTradeCondition.m_isCrossSell;

                // 当前价格和上次委托未成交价格的比较
                double diffPrice1 = latestData.m_close - m_lastCommissionNoTradePrice;
                if (diffPrice1 == 0)
                {
                    return;
                }
                if (diffPrice1 > 0 && diffPrice1 < overLastDealSell)
                {
                    return;
                }
                if (diffPrice1 < 0 && Math.Abs(diffPrice1) < lowLastDealBuy)
                {
                    return;
                }

                // 计算当前价格和上次成交价格的差值
                double diffPrice = latestData.m_close - lastDealPrice;
                if (diffPrice > 0)
                {
                    SecurityPosition postion = null;
                    if (!m_dictSecurityPositions.TryGetValue(latestData.m_code, out postion))
                    {
                        // 没有持仓信息,不做处理
                        return;
                    }

                    // 高于上次成交价格
                    int readSellCount = 0;
                    int sepaCount     = (int)(diffPrice / overLastDealSell);
                    if (sepaCount < 1)
                    {
                        // 价格没有达到预期值
                        return;
                    }
                    if (isCrossSell)
                    {
                        readSellCount = sepaCount * sellCount;
                    }
                    else
                    {
                        readSellCount = sellCount;
                    }

                    if (readSellCount < 100)
                    {
                        return;
                    }

                    // 股票余额小于可卖数量
                    if (postion.m_stockBalance < readSellCount)
                    {
                        readSellCount = (int)postion.m_stockBalance;
                    }

                    OrderInfo info = new OrderInfo();
                    info.m_code  = CStrA.ConvertDBCodeToDealCode(latestData.m_code);
                    info.m_price = (float)Math.Round(latestData.m_close, 2);
                    info.m_qty   = readSellCount;
                    m_lastCommissionNoTradePrice = info.m_price;
                    AutoTradeService.Sell(info);
                    Thread.Sleep(3000);
                    THSDealInfo req = new THSDealInfo();
                    req.m_operateType = 4;
                    req.m_reqID       = DataCenter.ThsDealService.GetRequestID();
                    DataCenter.ThsDealService.AddTHSDealReq(req);
                }
                else if (diffPrice < 0)
                {
                    if (m_securityTradingAccount == null)
                    {
                        // 没有资金账户信息
                        return;
                    }

                    // 低于上次成交价格
                    int readBuyCount = 0;
                    int sepaCount    = (int)(Math.Abs(diffPrice) / overLastDealSell);
                    if (sepaCount < 1)
                    {
                        // 价格没有达到预期值
                        return;
                    }
                    if (isCrossBuy)
                    {
                        readBuyCount = sepaCount * buyCount;
                    }
                    else
                    {
                        readBuyCount = buyCount;
                    }

                    if (readBuyCount < 100)
                    {
                        return;
                    }

                    int capitalAllowBuyCount = (int)((m_securityTradingAccount.m_capitalBalance - m_securityTradingAccount.m_frozenCash) / latestData.m_close) / 100 * 100;
                    // 资金余额小于可买的数量
                    if (capitalAllowBuyCount < readBuyCount)
                    {
                        readBuyCount = capitalAllowBuyCount;
                    }

                    OrderInfo info = new OrderInfo();
                    info.m_code  = CStrA.ConvertDBCodeToDealCode(latestData.m_code);
                    info.m_price = (float)Math.Round(latestData.m_close, 2);
                    info.m_qty   = readBuyCount;
                    m_lastCommissionNoTradePrice = info.m_price;
                    AutoTradeService.Buy(info);
                    Thread.Sleep(3000);
                    THSDealInfo req = new THSDealInfo();
                    req.m_operateType = 3;
                    req.m_reqID       = DataCenter.ThsDealService.GetRequestID();
                    DataCenter.ThsDealService.AddTHSDealReq(req);
                }
            }
            else
            {
            }
        }