Exemplo n.º 1
0
        /// <summary>
        /// 字符串转换成成交对象
        /// </summary>
        /// <param name="tradeResult"></param>
        /// <returns></returns>
        public static SecurityTrade ConvertToStockTrade(String tradeResult)
        {
            if (tradeResult == null || tradeResult.Length == 0)
            {
                return(null);
            }
            String[] tradeFields = tradeResult.Split("	".ToCharArray());
            if (tradeFields == null || tradeFields.Length < 11)
            {
                return(null);
            }
            int           index = 0;
            SecurityTrade trade = new SecurityTrade();

            trade.m_tradeDate     = tradeFields[index++];
            trade.m_stockCode     = tradeFields[index++];
            trade.m_stockName     = tradeFields[index++];
            trade.m_operate       = tradeFields[index++];
            trade.m_tradeVolume   = CStrA.ConvertStrToDouble(tradeFields[index++]);
            trade.m_tradeAvgPrice = CStrA.ConvertStrToDouble(tradeFields[index++]);
            trade.m_tradeAmount   = CStrA.ConvertStrToDouble(tradeFields[index++]);
            trade.m_orderSysID    = tradeFields[index++];
            trade.m_orderTradeID  = tradeFields[index++];
            trade.m_cancelVolume  = CStrA.ConvertStrToDouble(tradeFields[index++]);
            trade.m_stockBalance  = CStrA.ConvertStrToDouble(tradeFields[index++]);
            return(trade);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 根据股票代码获取新浪最新数据
        /// </summary>
        /// <param name="codes">股票代码列表</param>
        /// <returns>字符串</returns>
        public static String GetSinaLatestDatasStrByCodes(String codes)
        {
            String[]      strs      = codes.Split(new String[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            int           strLen    = strs.Length;
            List <String> sinaCodes = new List <String>();
            List <String> dcCodes   = new List <String>();

            for (int i = 0; i < strLen; i++)
            {
                String postCode = strs[i];
                sinaCodes.Add(CStrA.ConvertDBCodeToSinaCode(postCode));
            }
            String requestCode   = "";
            int    sinaCodesSize = sinaCodes.Count;

            for (int i = 0; i < sinaCodesSize; i++)
            {
                String postCode = sinaCodes[i];
                requestCode += postCode;
                if (i != strLen - 1)
                {
                    requestCode += ",";
                }
            }
            String result = "";

            if (sinaCodesSize > 0)
            {
                String url = "http://hq.sinajs.cn/list=" + requestCode.ToLower();
                result = HttpGetService.Get(url);
            }
            return(result);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 字符串转换成持仓对象
        /// </summary>
        /// <param name="positionResult"></param>
        /// <returns></returns>
        public static SecurityPosition ConvertToStockPosition(String positionResult)
        {
            if (positionResult == null || positionResult.Length == 0)
            {
                return(null);
            }
            String[] positionFields = positionResult.Split("	".ToCharArray());
            if (positionFields == null || positionFields.Length < 14)
            {
                return(null);
            }
            int index = 0;
            SecurityPosition position = new SecurityPosition();

            position.m_stockCode           = positionFields[index++];
            position.m_stockName           = positionFields[index++];
            position.m_stockBalance        = CStrA.ConvertStrToDouble(positionFields[index++]);
            position.m_availableBalance    = CStrA.ConvertStrToDouble(positionFields[index++]);
            position.m_volume              = CStrA.ConvertStrToDouble(positionFields[index++]);
            position.m_frozenVolume        = CStrA.ConvertStrToDouble(positionFields[index++]);
            position.m_positionProfit      = CStrA.ConvertStrToDouble(positionFields[index++]);
            position.m_positionCost        = CStrA.ConvertStrToDouble(positionFields[index++]);
            position.m_positionProfitRatio = CStrA.ConvertStrToDouble(positionFields[index++]);
            position.m_marketPrice         = CStrA.ConvertStrToDouble(positionFields[index++]);
            position.m_marketValue         = CStrA.ConvertStrToDouble(positionFields[index++]);
            position.m_redemptionVolume    = CStrA.ConvertStrToDouble(positionFields[index++]);
            position.m_marketName          = positionFields[index++];
            position.m_investorAccount     = positionFields[index++];
            return(position);
        }
Exemplo n.º 4
0
        /// <summary>
        /// 字符串转换成委托对象
        /// </summary>
        /// <param name="commissionResult"></param>
        /// <returns></returns>
        public static SecurityCommission ConvertToSecurityCommission(String commissionResult)
        {
            if (commissionResult == null || commissionResult.Length == 0)
            {
                return(null);
            }
            String[] orderFields = commissionResult.Split("	".ToCharArray());
            if (orderFields == null || orderFields.Length < 12)
            {
                return(null);
            }
            int index = 0;
            SecurityCommission commission = new SecurityCommission();

            commission.m_orderDate     = orderFields[index++];
            commission.m_stockCode     = orderFields[index++];
            commission.m_stockName     = orderFields[index++];
            commission.m_operate       = orderFields[index++];
            commission.m_remark        = orderFields[index++];
            commission.m_orderVolume   = CStrA.ConvertStrToDouble(orderFields[index++]);
            commission.m_tradeVolume   = CStrA.ConvertStrToDouble(orderFields[index++]);
            commission.m_cancelVolume  = CStrA.ConvertStrToDouble(orderFields[index++]);
            commission.m_orderPrice    = CStrA.ConvertStrToDouble(orderFields[index++]);
            commission.m_orderType     = orderFields[index++];
            commission.m_tradeAvgPrice = CStrA.ConvertStrToDouble(orderFields[index++]);
            commission.m_orderSysID    = orderFields[index++];
            return(commission);
        }
Exemplo n.º 5
0
        /// <summary>
        /// 获取通达信的历史数据
        /// </summary>
        /// <param name="str">数据字符串</param>
        /// <param name="datas">历史数据</param>
        /// <returns>状态</returns>
        public static int GetHistoryDatasByTdxStr(String str, List <SecurityData> datas)
        {
            String[] strs   = str.Split(new String[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            int      strLen = strs.Length;

            for (int i = 2; i < strLen - 1; i++)
            {
                String[] strs2     = strs[i].Split(',');
                int      strs2Size = strs2.Length;
                if (strs2Size >= 7)
                {
                    String[] dateStrs = strs2[0].Split('-');
                    int      year = 0, month = 0, day = 0, hour = 0, minute = 0;
                    if (dateStrs.Length == 3)
                    {
                        year  = CStrA.ConvertStrToInt(dateStrs[0]);
                        month = CStrA.ConvertStrToInt(dateStrs[1]);
                        day   = CStrA.ConvertStrToInt(dateStrs[2]);
                    }
                    int si = 1;
                    if (strs2Size == 8)
                    {
                        si     = 2;
                        hour   = CStrA.ConvertStrToInt(strs2[1].Substring(0, 2));
                        minute = CStrA.ConvertStrToInt(strs2[1].Substring(2, 2));
                    }
                    double open   = CStrA.ConvertStrToDouble(strs2[si]);
                    double high   = CStrA.ConvertStrToDouble(strs2[si + 1]);
                    double low    = CStrA.ConvertStrToDouble(strs2[si + 2]);
                    double close  = CStrA.ConvertStrToDouble(strs2[si + 3]);
                    double volume = CStrA.ConvertStrToDouble(strs2[si + 4]);
                    double amount = CStrA.ConvertStrToDouble(strs2[si + 5]);
                    if (volume == 0)
                    {
                        continue;
                    }
                    if (year != 0 && month != 0 && day != 0)
                    {
                        SecurityData securityData = new SecurityData();
                        if (hour != 0 || minute != 0)
                        {
                            securityData.m_date = CStrA.M129(year, month, day, hour, minute, 0, 0) - 300;
                        }
                        else
                        {
                            securityData.m_date = CStrA.M129(year, month, day, 0, 0, 0, 0);
                        }
                        securityData.m_open   = open;
                        securityData.m_high   = high;
                        securityData.m_low    = low;
                        securityData.m_close  = close;
                        securityData.m_volume = volume;
                        securityData.m_amount = amount;
                        datas.Add(securityData);
                    }
                }
            }
            return(1);
        }
Exemplo n.º 6
0
        /// <summary>
        /// 获取周线的历史数据
        /// </summary>
        /// <param name="weekDatas">周线历史数据</param>
        /// <param name="dayDatas">日线历史数据</param>
        /// <returns>状态</returns>
        public static int GetHistoryWeekDatas(List <SecurityData> weekDatas, List <SecurityData> dayDatas)
        {
            int weekDatasSize = dayDatas.Count;

            if (weekDatasSize > 0)
            {
                SecurityData weekData = new SecurityData();
                weekData.Copy(dayDatas[0]);
                int lDayOfWeek = 0, lDays = 0;
                for (int i = 0; i < weekDatasSize; i++)
                {
                    SecurityData dayData = new SecurityData();
                    dayData.Copy(dayDatas[i]);
                    int year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0, ms = 0, days = (int)dayData.m_date / (3600 * 24);
                    CStrA.M130(dayData.m_date, ref year, ref month, ref day, ref hour, ref minute, ref second, ref ms);
                    int  dayOfWeek  = DayOfWeek(year, month, day);
                    bool isNextWeek = true;
                    bool add        = false;
                    if (days - lDays <= 5)
                    {
                        if (days != lDays)
                        {
                            isNextWeek = dayOfWeek <= lDayOfWeek;
                        }
                    }
                    if (isNextWeek || i == weekDatasSize - 1)
                    {
                        add = true;
                    }
                    if (!isNextWeek)
                    {
                        weekData.m_close   = dayData.m_close;
                        weekData.m_amount += dayData.m_amount;
                        weekData.m_volume += dayData.m_volume;
                        if (weekData.m_high < dayData.m_high)
                        {
                            weekData.m_high = dayData.m_high;
                        }
                        if (weekData.m_low > dayData.m_low)
                        {
                            weekData.m_low = dayData.m_low;
                        }
                    }
                    if (add)
                    {
                        weekDatas.Add(weekData);
                        weekData = dayData;
                    }
                    if (isNextWeek && i == weekDatasSize - 1)
                    {
                        weekData = dayData;
                        weekDatas.Add(weekData);
                    }
                    lDayOfWeek = dayOfWeek;
                    lDays      = days;
                }
            }
            return(1);
        }
Exemplo n.º 7
0
        /// <summary>
        /// 获取通达信的历史数据的字符串
        /// </summary>
        /// <param name="code">股票代码</param>
        /// <param name="path">本地文件路径</param>
        /// <returns>数据字符串</returns>
        public static String GetTdxHistoryDatasStrByCode(String code, String path)
        {
            String fileName = CStrA.ConvertDBCodeToFileName(code);
            String result   = "";
            String filePath = path + fileName;

            if (CFileA.IsFileExist(filePath))
            {
                CFileA.Read(filePath, ref result);
            }
            return(result);
        }
Exemplo n.º 8
0
        /// <summary>
        /// 获取月线的历史数据
        /// </summary>
        /// <param name="weekDatas">月线历史数据</param>
        /// <param name="dayDatas">日线历史数据</param>
        /// <returns>状态</returns>
        public static int GetHistoryMonthDatas(List <SecurityData> monthDatas, List <SecurityData> dayDatas)
        {
            int monthDatasSize = dayDatas.Count;

            if (monthDatasSize > 0)
            {
                SecurityData monthData = new SecurityData();
                monthData.Copy(dayDatas[0]);
                int lYear = 0, lMonth = 0, lDay = 0;
                for (int i = 0; i < monthDatasSize; i++)
                {
                    SecurityData dayData = new SecurityData();
                    dayData.Copy(dayDatas[i]);
                    int year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0, ms = 0;
                    CStrA.M130(dayData.m_date, ref year, ref month, ref day, ref hour, ref minute, ref second, ref ms);
                    bool isNextMonth = year * 12 + month > lYear * 12 + lMonth;
                    bool add         = false;
                    if (i == monthDatasSize - 1 || (i > 0 && isNextMonth))
                    {
                        add = true;
                    }
                    if (!isNextMonth)
                    {
                        monthData.m_close   = dayData.m_close;
                        monthData.m_amount += dayData.m_amount;
                        monthData.m_volume += dayData.m_volume;
                        if (monthData.m_high < dayData.m_high)
                        {
                            monthData.m_high = dayData.m_high;
                        }
                        if (monthData.m_low > dayData.m_low)
                        {
                            monthData.m_low = dayData.m_low;
                        }
                    }
                    if (add)
                    {
                        monthDatas.Add(monthData);
                        monthData = dayData;
                    }
                    if (isNextMonth && i == monthDatasSize - 1)
                    {
                        monthData = dayData;
                        monthDatas.Add(monthData);
                    }
                    lYear  = year;
                    lMonth = month;
                    lDay   = day;
                }
            }
            return(1);
        }
Exemplo n.º 9
0
        /// <summary>
        /// 更新会话
        /// </summary>
        /// <param name="cookie">会话</param>
        /// <returns>状态</returns>
        public int UpdateCookie(UserCookie cookie)
        {
            String sql = String.Format("UPDATE USERCOOKIE SET VALUE = '{0}' WHERE USERID = {1} AND KEY = '{2}'",
                                       CStrA.GetDBString(cookie.m_value), m_userID, CStrA.GetDBString(cookie.m_key));
            SQLiteConnection conn = new SQLiteConnection(m_connectStr);
            SQLiteCommand    cmd  = conn.CreateCommand();

            cmd.CommandText = sql;
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
            return(1);
        }
Exemplo n.º 10
0
        public static double SumHistoryData(CTable dataSource, double date, int cycle, int field)
        {
            double sumValue = 0;
            double value = 0;
            int    year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0, ms = 0;

            CStrA.M130(date, ref year, ref month, ref day, ref hour, ref minute, ref second, ref ms);
            if (cycle == CYCLE_WEEK)
            {
                int dayOfWeek = DayOfWeek(year, month, day);
                if (dayOfWeek >= 5)
                {
                    dayOfWeek = 4;
                }
                for (int i = 1; i <= dayOfWeek; i++)
                {
                    double calcDate = CStrA.M129(year, month, day - i, 0, 0, 0, 0);
                    value = dataSource.Get(calcDate, field);
                    if (!double.IsNaN(value))
                    {
                        sumValue += value;
                    }
                }
            }
            else if (cycle == CYCLE_MONTH)
            {
                for (int i = 1; i < day; i++)
                {
                    double calcDate = CStrA.M129(year, month, i, 0, 0, 0, 0);
                    value = dataSource.Get(calcDate, field);
                    if (!double.IsNaN(value))
                    {
                        sumValue += value;
                    }
                }
            }
            else if (cycle == 0)
            {
                int rowCount = dataSource.RowsCount;
                for (int i = 0; i < rowCount; i++)
                {
                    value = dataSource.Get2(i, field);
                    if (!double.IsNaN(value))
                    {
                        sumValue += value;
                    }
                }
            }
            return(sumValue);
        }
Exemplo n.º 11
0
        /// <summary>
        /// 获取分时数据
        /// </summary>
        public static void GetMinuteDatas()
        {
            if (m_minuteDatas.Count > 0)
            {
                return;
            }
            String appPath = DataCenter.GetAppPath();

            foreach (String code in m_codedMap.Keys)
            {
                String fileName = m_newFileDir + CStrA.ConvertDBCodeToFileName(code);
                if (!CFileA.IsFileExist(fileName))
                {
                    fileName = m_newFileDir + CStrA.ConvertDBCodeToSinaCode(code).ToUpper() + ".txt";
                }
                if (CFileA.IsFileExist(fileName))
                {
                    String text = "";
                    CFileA.Read(fileName, ref text);
                    List <SecurityData> datas = new List <SecurityData>();
                    StockService.GetHistoryDatasByMinuteStr(text, datas);
                    if (datas.Count > 0)
                    {
                        int rindex   = 0;
                        int dataSize = datas.Count;
                        while (rindex < dataSize)
                        {
                            SecurityData d = datas[rindex];
                            if (rindex == 0)
                            {
                                d.m_avgPrice = d.m_close;
                            }
                            else
                            {
                                SecurityData ld = datas[rindex - 1];
                                d.m_avgPrice = (ld.m_avgPrice * rindex + d.m_close) / (rindex + 1);
                            }
                            rindex++;
                        }
                        m_minuteDatas[code] = datas;
                    }
                }
            }
        }
Exemplo n.º 12
0
        public static double GetMinuteVol(CTable dataSource, double date, int field, double volume)
        {
            int year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0, ms = 0;

            CStrA.M130(date, ref year, ref month, ref day, ref hour, ref minute, ref second, ref ms);
            double startDate = 0;
            double endDate   = date;

            if (hour >= 6)
            {
                startDate = CStrA.M129(year, month, day, 6, 0, 0, 0);
            }
            else
            {
                double preDate = date - 86400;
                int    lyear = 0, lmonth = 0, lday = 0, lhour = 0, lminute = 0, lsecond = 0, lms = 0;
                CStrA.M130(preDate, ref lyear, ref lmonth, ref lday, ref lhour, ref lminute, ref lsecond, ref lms);
                startDate = CStrA.M129(lyear, lmonth, lday, 6, 0, 0, 0);
            }
            int  dataSize    = dataSource.RowsCount;
            bool containsFlg = false;

            for (int i = dataSize - 1; i >= 0; i--)
            {
                containsFlg = false;
                double ldate = dataSource.GetXValue(i);
                if (startDate <= ldate && ldate <= endDate)
                {
                    containsFlg = true;
                }
                if (!containsFlg)
                {
                    break;
                }
                else
                {
                    double vol = dataSource.Get2(i, KeyFields.VOL_INDEX);
                    volume = volume - vol;
                }
            }
            return(volume);
        }
Exemplo n.º 13
0
        /// <summary>
        /// 添加用户Cookie
        /// </summary>
        /// <param name="cookie">消息</param>
        /// <returns>状态</returns>
        public int AddCookie(UserCookie cookie)
        {
            UserCookie oldCookie = new UserCookie();

            if (GetCookie(cookie.m_key, ref oldCookie) > 0)
            {
                UpdateCookie(cookie);
            }
            else
            {
                String sql = String.Format("INSERT INTO USERCOOKIE(USERID, KEY, VALUE, MODIFYTIME, CREATETIME) values ({0}, '{1}', '{2}','1970-1-1','1970-1-1')",
                                           m_userID, CStrA.GetDBString(cookie.m_key), CStrA.GetDBString(cookie.m_value));
                SQLiteConnection conn = new SQLiteConnection(m_connectStr);
                conn.Open();
                SQLiteCommand cmd = conn.CreateCommand();
                cmd.CommandText = sql;
                cmd.ExecuteNonQuery();
                conn.Close();
            }
            return(1);
        }
Exemplo n.º 14
0
 /// <summary>
 /// 加载历史数据
 /// </summary>
 /// <param name="history"></param>
 public static void LoadHistoryDatas()
 {
     if (m_historyDatas.Count > 0)
     {
         return;
     }
     foreach (String code in m_codedMap.Keys)
     {
         String fileName = DataCenter.GetAppPath() + "\\day\\" + CStrA.ConvertDBCodeToSinaCode(code).ToUpper() + ".txt";
         if (File.Exists(fileName))
         {
             StreamReader        sra   = new StreamReader(fileName, Encoding.Default);
             String              text  = sra.ReadToEnd();
             List <SecurityData> datas = new List <SecurityData>();
             StockService.GetHistoryDatasByTdxStr(text, datas);
             if (datas.Count > 0)
             {
                 m_historyDatas[code] = datas;
             }
         }
     }
 }
Exemplo n.º 15
0
        /// <summary>
        /// 字符串转换成持仓对象
        /// </summary>
        /// <param name="tradingAccountResult"></param>
        /// <returns></returns>
        public static SecurityTradingAccount ConvertToStockTradingAccount(String tradingAccountResult)
        {
            if (tradingAccountResult == null || tradingAccountResult.Length == 0)
            {
                return(null);
            }
            String[] tradingAccountFields = tradingAccountResult.Split(Environment.NewLine.ToCharArray(),
                                                                       StringSplitOptions.RemoveEmptyEntries);
            if (tradingAccountFields == null || tradingAccountFields.Length < 6)
            {
                return(null);
            }
            int index = 0;
            SecurityTradingAccount stockTradingAccount = new SecurityTradingAccount();

            stockTradingAccount.m_capitalBalance = CStrA.ConvertStrToDouble(tradingAccountFields[index++]);
            stockTradingAccount.m_frozenCash     = CStrA.ConvertStrToDouble(tradingAccountFields[index++]);
            stockTradingAccount.m_available      = CStrA.ConvertStrToDouble(tradingAccountFields[index++]);
            stockTradingAccount.m_withdrawQuota  = CStrA.ConvertStrToDouble(tradingAccountFields[index++]);
            stockTradingAccount.m_stockValue     = CStrA.ConvertStrToDouble(tradingAccountFields[index++]);
            stockTradingAccount.m_totalCapital   = CStrA.ConvertStrToDouble(tradingAccountFields[index++]);
            return(stockTradingAccount);
        }
Exemplo n.º 16
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
            {
            }
        }
Exemplo n.º 17
0
        /// <summary>
        /// 获取用户Cookie
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="cookie">会话</param>
        /// <returns>状态</returns>
        public int GetCookie(String key, ref UserCookie cookie)
        {
            int              state = 0;
            String           sql   = String.Format("SELECT * FROM USERCOOKIE WHERE USERID = {0} AND KEY = '{1}'", m_userID, CStrA.GetDBString(key));
            SQLiteConnection conn  = new SQLiteConnection(m_connectStr);
            SQLiteCommand    cmd   = conn.CreateCommand();

            cmd.CommandText = sql;
            conn.Open();
            SQLiteDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                cookie.m_userID = reader.GetInt32(0);
                cookie.m_key    = reader.GetString(1);
                cookie.m_value  = reader.GetString(2);
                state           = 1;
            }
            reader.Close();
            conn.Close();
            return(state);
        }
Exemplo n.º 18
0
        /// <summary>
        /// 获取分时线的历史数据
        /// </summary>
        /// <param name="str">数据字符串</param>
        /// <param name="datas">历史数据</param>
        /// <returns>状态</returns>
        public static int GetHistoryDatasByMinuteStr(String str, List <SecurityData> datas)
        {
            String[] strs = str.Split(new String[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            int      strLen = strs.Length;
            double   lClose = 0, lHigh = 0, lLow = 0, lOpen = 0;
            double   lVolume = 0, lAmount = 0, fVolume = 0, fAmount = 0, sum = 0;
            int      lYear = 0, lMonth = 0, lDay = 0, lHour = 0, lMinute = 0;
            int      startIndex = 0;

            for (int i = startIndex; i < strLen; i++)
            {
                String[] strs2 = strs[i].Split(',');
                if (strs2.Length == 4)
                {
                    double date = CStrA.ConvertStrToDouble(strs2[0]);
                    double close = CStrA.ConvertStrToDouble(strs2[1]);
                    double volume = CStrA.ConvertStrToDouble(strs2[2]);
                    double amount = CStrA.ConvertStrToDouble(strs2[3]);
                    int    year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0, ms = 0;
                    CStrA.M130(date, ref year, ref month, ref day, ref hour, ref minute, ref second, ref ms);
                    if (hour * 60 + minute >= 899)
                    {
                        hour   = 14;
                        minute = 59;
                    }
                    if (i == startIndex)
                    {
                        lClose  = close;
                        lHigh   = close;
                        lLow    = close;
                        lOpen   = close;
                        lVolume = volume;
                        lAmount = amount;
                        lYear   = year;
                        lMonth  = month;
                        lDay    = day;
                        lHour   = hour;
                        lMinute = minute;
                    }
                    bool inSameTime = false;
                    if (hour == lHour && minute == lMinute)
                    {
                        inSameTime = true;
                        if (close > lHigh)
                        {
                            lHigh = close;
                        }
                        if (close < lLow)
                        {
                            lLow = close;
                        }
                    }
                    if (!inSameTime || i == strLen - 1)
                    {
                        SecurityData data = new SecurityData();
                        data.m_date  = CStrA.M129(lYear, lMonth, lDay, lHour, lMinute, 0, 0);
                        data.m_close = lClose;
                        if (lHigh != 0)
                        {
                            data.m_high = lHigh;
                        }
                        else
                        {
                            data.m_high = lClose;
                        }
                        if (lLow != 0)
                        {
                            data.m_low = lLow;
                        }
                        else
                        {
                            data.m_low = lClose;
                        }
                        if (lOpen != 0)
                        {
                            data.m_open = lOpen;
                        }
                        else
                        {
                            data.m_open = lClose;
                        }
                        data.m_volume = lVolume - fVolume;
                        data.m_amount = lAmount - fAmount;
                        if (data.m_close != 0 && data.m_volume != 0)
                        {
                            sum            += data.m_close;
                            data.m_avgPrice = sum / (datas.Count + 1);
                            datas.Add(data);
                        }
                        fVolume = lVolume;
                        fAmount = lAmount;
                    }
                    if (!inSameTime)
                    {
                        lHigh   = close;
                        lLow    = close;
                        lOpen   = close;
                        lYear   = year;
                        lMonth  = month;
                        lDay    = day;
                        lHour   = hour;
                        lMinute = minute;
                    }
                    lClose  = close;
                    lVolume = volume;
                    lAmount = amount;
                }
            }
            return(1);
        }
Exemplo n.º 19
0
        /// <summary>
        /// 根据字符串获取新浪的最新数据
        /// </summary>
        /// <param name="str">数据字符串</param>
        /// <param name="formatType">格式</param>
        /// <param name="data">最新数据</param>
        /// <returns>状态</returns>
        public static int GetLatestDataBySinaStr(String str, int formatType, ref SecurityLatestData data)
        {
            //分析数据
            String date = "";

            String[] strs2   = str.Split(new String[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            int      strLen2 = strs2.Length;
            bool     szIndex = false;

            for (int j = 0; j < strLen2; j++)
            {
                String str2 = strs2[j];
                switch (j)
                {
                case 0:
                    data.m_code = CStrA.ConvertSinaCodeToDBCode(str2);
                    if (data.m_code.StartsWith("399"))
                    {
                        szIndex = true;
                    }
                    break;

                case 1:
                {
                    data.m_open = CStrA.ConvertStrToDouble(str2);
                    break;
                }

                case 2:
                {
                    data.m_lastClose = CStrA.ConvertStrToDouble(str2);
                    break;
                }

                case 3:
                {
                    data.m_close = CStrA.ConvertStrToDouble(str2);
                    break;
                }

                case 4:
                {
                    data.m_high = CStrA.ConvertStrToDouble(str2);
                    break;
                }

                case 5:
                {
                    data.m_low = CStrA.ConvertStrToDouble(str2);
                    break;
                }

                case 8:
                {
                    data.m_volume = CStrA.ConvertStrToDouble(str2);
                    if (szIndex)
                    {
                        data.m_volume /= 100;
                    }
                    break;
                }

                case 9:
                {
                    data.m_amount = CStrA.ConvertStrToDouble(str2);
                    break;
                }

                case 10:
                {
                    if (formatType == 0)
                    {
                        data.m_buyVolume1 = (int)CStrA.ConvertStrToDouble(str2);
                    }
                    break;
                }

                case 11:
                {
                    if (formatType == 0)
                    {
                        data.m_buyPrice1 = CStrA.ConvertStrToDouble(str2);
                    }
                    break;
                }

                case 12:
                {
                    if (formatType == 0)
                    {
                        data.m_buyVolume2 = (int)CStrA.ConvertStrToDouble(str2);
                    }
                    break;
                }

                case 13:
                {
                    if (formatType == 0)
                    {
                        data.m_buyPrice2 = CStrA.ConvertStrToDouble(str2);
                    }
                    break;
                }

                case 14:
                {
                    if (formatType == 0)
                    {
                        data.m_buyVolume3 = (int)CStrA.ConvertStrToDouble(str2);
                    }
                    break;
                }

                case 15:
                {
                    if (formatType == 0)
                    {
                        data.m_buyPrice3 = CStrA.ConvertStrToDouble(str2);
                    }
                    break;
                }

                case 16:
                {
                    if (formatType == 0)
                    {
                        data.m_buyVolume4 = (int)CStrA.ConvertStrToDouble(str2);
                    }
                    break;
                }

                case 17:
                {
                    if (formatType == 0)
                    {
                        data.m_buyPrice4 = CStrA.ConvertStrToDouble(str2);
                    }
                    break;
                }

                case 18:
                {
                    if (formatType == 0)
                    {
                        data.m_buyVolume5 = (int)CStrA.ConvertStrToDouble(str2);
                    }
                    break;
                }

                case 19:
                {
                    if (formatType == 0)
                    {
                        data.m_buyPrice5 = CStrA.ConvertStrToDouble(str2);
                    }
                    break;
                }

                case 20:
                {
                    if (formatType == 0)
                    {
                        data.m_sellVolume1 = (int)CStrA.ConvertStrToDouble(str2);
                    }
                    break;
                }

                case 21:
                {
                    if (formatType == 0)
                    {
                        data.m_sellPrice1 = CStrA.ConvertStrToDouble(str2);
                    }
                    break;
                }

                case 22:
                {
                    if (formatType == 0)
                    {
                        data.m_sellVolume2 = (int)CStrA.ConvertStrToDouble(str2);
                    }
                    break;
                }

                case 23:
                {
                    if (formatType == 0)
                    {
                        data.m_sellPrice2 = CStrA.ConvertStrToDouble(str2);
                    }
                    break;
                }

                case 24:
                {
                    if (formatType == 0)
                    {
                        data.m_sellVolume3 = (int)CStrA.ConvertStrToDouble(str2);
                    }
                    break;
                }

                case 25:
                {
                    if (formatType == 0)
                    {
                        data.m_sellPrice3 = CStrA.ConvertStrToDouble(str2);
                    }
                    break;
                }

                case 26:
                {
                    if (formatType == 0)
                    {
                        data.m_sellVolume4 = (int)CStrA.ConvertStrToDouble(str2);
                    }
                    break;
                }

                case 27:
                {
                    if (formatType == 0)
                    {
                        data.m_sellPrice4 = CStrA.ConvertStrToDouble(str2);
                    }
                    break;
                }

                case 28:
                {
                    if (formatType == 0)
                    {
                        data.m_sellVolume5 = (int)CStrA.ConvertStrToDouble(str2);
                    }
                    break;
                }

                case 29:
                {
                    if (formatType == 0)
                    {
                        data.m_sellPrice5 = CStrA.ConvertStrToDouble(str2);
                    }
                    break;
                }

                case 30:
                    date = str2;
                    break;

                case 31:
                    date += " " + str2;
                    break;
                }
            }
            //获取时间
            if (date != null && date.Length > 0)
            {
                DateTime dateTime = Convert.ToDateTime(date);
                data.m_date = CStrA.M129(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, dateTime.Second, 0);
            }
            //价格修正
            if (data.m_close != 0)
            {
                if (data.m_open == 0)
                {
                    data.m_open = data.m_close;
                }
                if (data.m_high == 0)
                {
                    data.m_high = data.m_close;
                }
                if (data.m_low == 0)
                {
                    data.m_low = data.m_close;
                }
            }
            return(0);
        }
Exemplo n.º 20
0
        /// <summary>
        /// 数据落地线程工作
        /// </summary>
        public static void StartWork3()
        {
            //复制数据
            LoadHistoryDatas();
            GetMinuteDatas();
            //新旧数据合并
            foreach (String oCode in m_historyDatas.Keys)
            {
                if (!m_latestDatas.ContainsKey(oCode) || !m_historyDatas.ContainsKey(oCode))
                {
                    continue;
                }
                SecurityLatestData  securityLatestData = m_latestDatas[oCode];
                List <SecurityData> oldSecurityDatas = m_historyDatas[oCode];
                SecurityData        oldSecurityData = oldSecurityDatas[oldSecurityDatas.Count - 1];
                int myear = 0, mmonth = 0, mday = 0, mhour = 0, mmin = 0, msec = 0, mmsec = 0;
                CStrA.M130(oldSecurityData.m_date, ref myear, ref mmonth, ref mday, ref mhour, ref mmin, ref msec, ref mmsec);
                int year = 0, month = 0, day = 0, hour = 0, min = 0, sec = 0, msec2 = 0;
                CStrA.M130(securityLatestData.m_date, ref year, ref month, ref day, ref hour, ref min, ref sec, ref msec2);
                if (year >= myear && month >= mmonth && day >= mday)
                {
                    SecurityData nSecurityData = new SecurityData();
                    nSecurityData.m_amount = securityLatestData.m_amount;
                    nSecurityData.m_close  = securityLatestData.m_close;
                    nSecurityData.m_date   = securityLatestData.m_date;
                    nSecurityData.m_high   = securityLatestData.m_high;
                    nSecurityData.m_low    = securityLatestData.m_low;
                    nSecurityData.m_open   = securityLatestData.m_open;
                    nSecurityData.m_volume = securityLatestData.m_volume;
                    if (day == mday)
                    {
                        m_historyDatas[oCode].RemoveAt(m_historyDatas[oCode].Count - 1);
                    }
                    m_historyDatas[oCode].Add(nSecurityData);
                }
            }
            String outputFileTemplate = DataCenter.GetAppPath() + "\\day\\{0}.txt";
            String fileInfo           = "{0} {1} 日线 前复权\r\n";
            String title         = "      日期	    开盘	    最高	    最低	    收盘	    成交量	    成交额\r\n";
            String lineTemp      = "{0},{1},{2},{3},{4},{5},{6}\r\n";
            String timeFormatStr = "yyyy-MM-dd";

            //写入文件
            foreach (String code in m_historyDatas.Keys)
            {
                List <SecurityData> temp3   = m_historyDatas[code];
                StringBuilder       strbuff = new StringBuilder();
                strbuff.Append(String.Format(fileInfo, m_codedMap[code].m_code, m_codedMap[code].m_name));
                strbuff.Append(title);
                foreach (SecurityData sdt in temp3)
                {
                    strbuff.Append(String.Format(lineTemp,                                                  //
                                                 CStr.ConvertNumToDate(sdt.m_date).ToString(timeFormatStr), //
                                                 sdt.m_open,                                                //
                                                 sdt.m_high,                                                //
                                                 sdt.m_low,                                                 //
                                                 sdt.m_close,                                               //
                                                 sdt.m_volume,                                              //
                                                 sdt.m_amount));
                }
                strbuff.Append("数据来源:通达信\r\n");
                CFileA.Write(String.Format(outputFileTemplate, code), strbuff.ToString());
            }
        }
Exemplo n.º 21
0
        /// <summary>
        /// 删除用户Cookie
        /// </summary>
        /// <param name="key">键</param>
        /// <returns>状态</returns>
        public int DeleteCookie(String key)
        {
            String           sql  = String.Format("DELETE FROM USERCOOKIE WHERE USERID = {0} AND KEY = '{1}'", m_userID, CStrA.GetDBString(key));
            SQLiteConnection conn = new SQLiteConnection(m_connectStr);
            SQLiteCommand    cmd  = conn.CreateCommand();

            cmd.CommandText = sql;
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
            return(1);
        }