Beispiel #1
0
 /// <summary>
 /// 计算指标
 /// </summary>
 /// <param name="id">指标ID</param>
 /// <param name="code">代码</param>
 /// <param name="path">路径</param>
 /// <param name="type">类型</param>
 /// <param name="cycle">周期</param>
 /// <param name="subscription">复权方式</param>
 /// <param name="date">日期</param>
 /// <param name="open">开盘价</param>
 /// <param name="high">最高价</param>
 /// <param name="low">最低价</param>
 /// <param name="close">收盘价</param>
 /// <param name="volume">成交量</param>
 /// <param name="amount">成交额</param>
 /// <returns>返回数据</returns>
 public static double[] calculateIndicatorExtern(int id, String code, ref double result)
 {
     if (m_indicators.ContainsKey(id))
     {
         FCScript        indicator  = m_indicators[id];
         List <FCScript> indicators = new List <FCScript>();
         indicators.Add(indicator);
         List <SecurityData> datas = new List <SecurityData>();
         if (SecurityService.m_historyDatas.ContainsKey(code))
         {
             datas = SecurityService.m_historyDatas[code];
             SecurityLatestData latestData = null;
             if (SecurityService.m_latestDatas.ContainsKey(code))
             {
                 latestData = SecurityService.m_latestDatas[code];
             }
             if (latestData != null)
             {
                 SecurityData newData = new SecurityData();
                 getSecurityData(latestData, latestData.m_lastClose, 1440, 0, ref newData);
                 if (datas.Count == 0)
                 {
                     datas.Add(newData);
                 }
                 else
                 {
                     if (newData.m_date > datas[datas.Count - 1].m_date)
                     {
                         datas.Add(newData);
                     }
                     else
                     {
                         datas[datas.Count - 1] = newData;
                     }
                 }
             }
             FCDataTable dataSource = indicator.DataSource;
             int[]       fields     = new int[] { KeyFields.CLOSE_INDEX, KeyFields.HIGH_INDEX, KeyFields.LOW_INDEX, KeyFields.OPEN_INDEX, KeyFields.VOL_INDEX, KeyFields.AMOUNT_INDEX };
             SecurityDataHelper.bindHistoryDatas(m_chart, dataSource, indicators, fields, datas);;
             int      rowsCount     = dataSource.RowsCount;
             int      variablesSize = indicator.MainVariables.Count;
             double[] list          = new double[variablesSize];
             if (rowsCount > 0)
             {
                 int pos = 0;
                 foreach (String name in indicator.MainVariables.Keys)
                 {
                     int    field = indicator.MainVariables[name];
                     double value = dataSource.get2(rowsCount - 1, field);
                     list[pos] = value;
                     pos++;
                 }
             }
             result = indicator.m_result;
             dataSource.clear();
             return(list);
         }
     }
     return(null);
 }
Beispiel #2
0
        /// <summary>
        /// 获取最新数据
        /// </summary>
        /// <param name="code">代码</param>
        /// <param name="latestData">最新数据</param>
        /// <returns>状态</returns>
        public static int getLatestData(String code, ref SecurityLatestData latestData)
        {
            int state = 0;

            lock (m_latestDatas) {
                if (m_latestDatas.ContainsKey(code))
                {
                    latestData.copy(m_latestDatas[code]);
                    state = 1;
                }
            }
            return(state);
        }
Beispiel #3
0
        /// <summary>
        /// 根据字符串获取新浪最新数据
        /// </summary>
        /// <param name="str">数据字符串</param>
        /// <param name="formatType">格式</param>
        /// <param name="datas">最新数据</param>
        /// <returns>状态</returns>
        public static int getLatestDatasBySinaStr(String str, int formatType, List <SecurityLatestData> datas)
        {
            String[] strs   = str.Split(new String[] { ";\n" }, StringSplitOptions.RemoveEmptyEntries);
            int      strLen = strs.Length;

            for (int i = 0; i < strLen; i++)
            {
                SecurityLatestData latestData = new SecurityLatestData();
                String             dataStr    = strs[i];
                getLatestDataBySinaStr(strs[i], formatType, ref latestData);
                if (latestData.m_date > 0)
                {
                    datas.Add(latestData);
                }
            }
            return(1);
        }
Beispiel #4
0
 /// <summary>
 /// 比较是否相同
 /// </summary>
 /// <param name="data">数据</param>
 /// <returns>是否相同</returns>
 public bool Equal(SecurityLatestData data)
 {
     if (data == null)
     {
         return(false);
     }
     if (m_amount == data.m_amount &&
         m_buyVolume1 == data.m_buyVolume1 &&
         m_buyVolume2 == data.m_buyVolume2 &&
         m_buyVolume3 == data.m_buyVolume3 &&
         m_buyVolume4 == data.m_buyVolume4 &&
         m_buyVolume5 == data.m_buyVolume5 &&
         m_buyPrice1 == data.m_buyPrice1 &&
         m_buyPrice2 == data.m_buyPrice2 &&
         m_buyPrice3 == data.m_buyPrice3 &&
         m_buyPrice4 == data.m_buyPrice4 &&
         m_buyPrice5 == data.m_buyPrice5 &&
         m_close == data.m_close &&
         m_date == data.m_date &&
         m_high == data.m_high &&
         m_innerVol == data.m_innerVol &&
         m_lastClose == data.m_lastClose &&
         m_low == data.m_low &&
         m_open == data.m_open &&
         m_openInterest == data.m_openInterest &&
         m_outerVol == data.m_outerVol &&
         m_securityCode == data.m_securityCode &&
         m_sellVolume1 == data.m_sellVolume1 &&
         m_sellVolume2 == data.m_sellVolume2 &&
         m_sellVolume3 == data.m_sellVolume3 &&
         m_sellVolume4 == data.m_sellVolume4 &&
         m_sellVolume5 == data.m_sellVolume5 &&
         m_sellPrice1 == data.m_sellPrice1 &&
         m_sellPrice2 == data.m_sellPrice2 &&
         m_sellPrice3 == data.m_sellPrice3 &&
         m_sellPrice4 == data.m_sellPrice4 &&
         m_sellPrice5 == data.m_sellPrice5 &&
         m_settlePrice == data.m_settlePrice &&
         m_turnoverRate == data.m_turnoverRate &&
         m_volume == data.m_volume)
     {
         return(true);
     }
     return(false);
 }
Beispiel #5
0
 /// <summary>
 /// 复制数据
 /// </summary>
 /// <param name="data">数据</param>
 public void copy(SecurityLatestData data)
 {
     if (data == null)
     {
         return;
     }
     m_amount       = data.m_amount;
     m_buyVolume1   = data.m_buyVolume1;
     m_buyVolume2   = data.m_buyVolume2;
     m_buyVolume3   = data.m_buyVolume3;
     m_buyVolume4   = data.m_buyVolume4;
     m_buyVolume5   = data.m_buyVolume5;
     m_buyPrice1    = data.m_buyPrice1;
     m_buyPrice2    = data.m_buyPrice2;
     m_buyPrice3    = data.m_buyPrice3;
     m_buyPrice4    = data.m_buyPrice4;
     m_buyPrice5    = data.m_buyPrice5;
     m_close        = data.m_close;
     m_date         = data.m_date;
     m_high         = data.m_high;
     m_innerVol     = data.m_innerVol;
     m_lastClose    = data.m_lastClose;
     m_low          = data.m_low;
     m_open         = data.m_open;
     m_openInterest = data.m_openInterest;
     m_outerVol     = data.m_outerVol;
     m_securityCode = data.m_securityCode;
     m_sellVolume1  = data.m_sellVolume1;
     m_sellVolume2  = data.m_sellVolume2;
     m_sellVolume3  = data.m_sellVolume3;
     m_sellVolume4  = data.m_sellVolume4;
     m_sellVolume5  = data.m_sellVolume5;
     m_sellPrice1   = data.m_sellPrice1;
     m_sellPrice2   = data.m_sellPrice2;
     m_sellPrice3   = data.m_sellPrice3;
     m_sellPrice4   = data.m_sellPrice4;
     m_sellPrice5   = data.m_sellPrice5;
     m_settlePrice  = data.m_settlePrice;
     m_turnoverRate = data.m_turnoverRate;
     m_volume       = data.m_volume;
 }
Beispiel #6
0
        /// <summary>
        /// 获取股票历史数据
        /// </summary>
        /// <param name="latestData">最新数据</param>
        /// <param name="lastClose">上一期收盘价</param>
        /// <param name="cycle">周期</param>
        /// <param name="subscription">复权模式</param>
        /// <param name="securityData">历史数据</param>
        /// <returns>历史数据</returns>
        public static void getSecurityData(SecurityLatestData latestData, double lastClose, int cycle, int subscription, ref SecurityData securityData)
        {
            if (cycle <= 60)
            {
                securityData.m_date = getDivideDate(latestData.m_date, 60 * 60);
            }
            else
            {
                securityData.m_date = (long)latestData.m_date / (3600 * 24) * (3600 * 24);
            }
            //前复权计算
            double factor = 1;

            if (lastClose > 0 && latestData.m_lastClose > 0 && subscription == 2)
            {
                factor = lastClose / latestData.m_lastClose;
            }
            securityData.m_close  = latestData.m_close * factor;
            securityData.m_high   = latestData.m_high * factor;
            securityData.m_low    = latestData.m_low * factor;
            securityData.m_open   = latestData.m_open * factor;
            securityData.m_volume = latestData.m_volume;
            securityData.m_amount = latestData.m_amount;
        }
Beispiel #7
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;
                FCStr.getDateByNum(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;
                FCStr.getDateByNum(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,                                                   //
                                                 FCStr.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");
                FCFile.write(String.Format(outputFileTemplate, code), strbuff.ToString());
            }
        }
Beispiel #8
0
 /// <summary>
 /// 开始工作
 /// </summary>
 private static void startWork()
 {
     while (true)
     {
         if (m_codes != null && m_codes.Length > 0)
         {
             if (m_codes.EndsWith(","))
             {
                 m_codes.Remove(m_codes.Length - 1);
             }
             String[] strCodes    = m_codes.Split(new String[] { "," }, StringSplitOptions.RemoveEmptyEntries);
             int      codesSize   = strCodes.Length;
             String   latestCodes = "";
             for (int i = 0; i < codesSize; i++)
             {
                 latestCodes += strCodes[i];
                 if (i == codesSize - 1 || (i > 0 && i % 50 == 0))
                 {
                     String latestDatasResult = StockService.getSinaLatestDatasStrByCodes(latestCodes);
                     if (latestDatasResult != null && latestDatasResult.Length > 0)
                     {
                         List <SecurityLatestData> latestDatas = new List <SecurityLatestData>();
                         StockService.getLatestDatasBySinaStr(latestDatasResult, 0, latestDatas);
                         String[] subStrs         = latestDatasResult.Split(new String[] { ";\n" }, StringSplitOptions.RemoveEmptyEntries);
                         int      latestDatasSize = latestDatas.Count;
                         for (int j = 0; j < latestDatasSize; j++)
                         {
                             SecurityLatestData latestData = latestDatas[j];
                             if (latestData.m_close == 0)
                             {
                                 latestData.m_close = latestData.m_buyPrice1;
                             }
                             if (latestData.m_close == 0)
                             {
                                 latestData.m_close = latestData.m_sellPrice1;
                             }
                             lock (m_latestDatas) {
                                 m_latestDatasStr[latestData.m_code] = subStrs[j];
                                 //bool append = true;
                                 //if (m_latestDatas.ContainsKey(latestData.m_code))
                                 //{
                                 //    if (!m_latestDatas[CStrA.ConvertFileCodeToMemoryCode(latestData.m_code)].Equals(latestData))
                                 //    {
                                 //        append = false;
                                 //    }
                                 //}
                                 //if(append)
                                 //{
                                 //    long today = (long)DateTime.Now.Subtract(DateTime.Parse("1970-1-1")).TotalMilliseconds / 86400000;
                                 //    if (m_today < today)
                                 //    {
                                 //        m_today = today;
                                 //        String nPath = DataCenter.GetAppPath() + "\\tick\\" + DateTime.Now.ToString("yyyy-MM-dd");
                                 //        if (!Directory.Exists(nPath))
                                 //        {
                                 //            Directory.CreateDirectory(nPath);
                                 //        }
                                 //        m_newFileDir = nPath + "\\";
                                 //    }
                                 //    String line = String.Format("{0},{1},{2},{3}\r\n", latestData.m_date,//
                                 //        latestData.m_close, latestData.m_volume, latestData.m_amount);
                                 //    CFileA.Append(m_newFileDir + latestData.m_code + ".txt", line);
                                 //}
                                 if (!m_latestDatas.ContainsKey(latestData.m_code))
                                 {
                                     m_latestDatas[latestData.m_code] = latestData;
                                 }
                                 else
                                 {
                                     m_latestDatas[latestData.m_code].copy(latestData);
                                 }
                                 if (latestData.m_code == "000001.SH")
                                 {
                                     m_shTradeTime = latestData.m_date;
                                 }
                             }
                         }
                         latestDatas.Clear();
                     }
                     latestCodes = "";
                 }
                 else
                 {
                     latestCodes += ",";
                 }
             }
         }
         Thread.Sleep(1);
     }
 }
Beispiel #9
0
        /// <summary>
        /// 平仓
        /// </summary>
        /// <param name="investorPosition">持仓</param>
        /// <param name="code">代码</param>
        /// <param name="direction">方向</param>
        /// <param name="close">最新价</param>
        /// <param name="openPrice">开仓价</param>
        /// <param name="state">state=0为手动平仓,其余为自动</param>
        public static void askOrBidClose(InvestorPosition investorPosition, String code, String direction, double close, double openPrice, int state)
        {
            //如果持仓为0,则不检查
            if (investorPosition.m_position == 0)
            {
                return;
            }
            //获取对应的行情数据
            SecurityLatestData latestData = null;

            lock (m_latestDatas) {
                if (m_latestDatas.ContainsKey(code))
                {
                    latestData = m_latestDatas[code];
                }
            }
            if (latestData != null)
            {
                //获取的码表
                Security security = null;
                lock (m_securities) {
                    if (m_securities.ContainsKey(investorPosition.m_code))
                    {
                        security = m_securities[investorPosition.m_code];
                    }
                }
                if (security != null)
                {
                    bool canTrade = true;
                    //检查冷却时间,一次平仓10秒后才能再次平仓
                    if (state != 0)
                    {
                        lock (m_cd2) {
                            if (m_cd2.ContainsKey(investorPosition.m_code))
                            {
                                if (m_cd2[investorPosition.m_code] > 0)
                                {
                                    canTrade = false;
                                }
                            }
                        }
                    }
                    //获取昨仓
                    int ydPosition = investorPosition.m_ydPosition;
                    //获取今仓
                    int todayPosition = investorPosition.m_position - ydPosition;
                    if (canTrade)
                    {
                        bool isSh = security.m_exchangeID == "SHFE";
                        //买开仓情况下
                        if (direction == "买")
                        {
                            //上期所处理方法
                            if (isSh)
                            {
                                //平昨
                                if (ydPosition > 0)
                                {
                                    CTPDLL.askClose(m_ctpID, CTPDLL.generateReqID(m_ctpID), investorPosition.m_code, security.m_exchangeID, 0, ydPosition, '3', "");
                                }
                                //平今
                                if (todayPosition > 0)
                                {
                                    CTPDLL.askCloseToday(m_ctpID, CTPDLL.generateReqID(m_ctpID), investorPosition.m_code, security.m_exchangeID, 0, todayPosition, '3', "");
                                }
                            }
                            //其他交易所处理方法
                            else
                            {
                                CTPDLL.askClose(m_ctpID, CTPDLL.generateReqID(m_ctpID), investorPosition.m_code, security.m_exchangeID, 0, investorPosition.m_position, '3', "");
                            }
                            //打印日志
                            if (state == 1)
                            {
                                FCStrEx.writeLog(String.Format("自动止盈,开仓价{0},当前价{1},买平仓,代码{2},价格{3},数量{4}\r\n",
                                                               openPrice, close, investorPosition.m_code, latestData.m_bidPrice1, investorPosition.m_position));
                            }
                            else if (state == 2)
                            {
                                FCStrEx.writeLog(String.Format("自动止损,开仓价{0},当前价{1},买平仓,代码{2},价格{3},数量{4}\r\n",
                                                               openPrice, close, investorPosition.m_code, latestData.m_bidPrice1, investorPosition.m_position));
                            }
                            //开仓冷却时间+10
                            lock (m_cd1) {
                                if (m_cd1.ContainsKey(investorPosition.m_code))
                                {
                                    m_cd1[investorPosition.m_code] += 10;
                                }
                                else
                                {
                                    m_cd1[investorPosition.m_code] = 10;
                                }
                            }
                            //平仓冷却时间重置
                            lock (m_cd2) {
                                m_cd2[investorPosition.m_code] = 10;
                            }
                        }
                        //卖开仓情况下
                        else if (direction == "卖")
                        {
                            //上期所处理方法
                            if (isSh)
                            {
                                //平昨
                                if (ydPosition > 0)
                                {
                                    CTPDLL.bidClose(m_ctpID, CTPDLL.generateReqID(m_ctpID), investorPosition.m_code, security.m_exchangeID, 0, ydPosition, '3', "");
                                }
                                //平今
                                if (todayPosition > 0)
                                {
                                    CTPDLL.bidCloseToday(m_ctpID, CTPDLL.generateReqID(m_ctpID), investorPosition.m_code, security.m_exchangeID, 0, todayPosition, '3', "");
                                }
                            }
                            //其他交易所处理方法
                            else
                            {
                                CTPDLL.bidClose(m_ctpID, CTPDLL.generateReqID(m_ctpID), investorPosition.m_code, security.m_exchangeID, 0, investorPosition.m_position, '3', "");
                            }
                            //打印日志
                            if (state == 1)
                            {
                                FCStrEx.writeLog(String.Format("自动止盈,开仓价{0},当前价{1},卖平仓,代码{2},价格{3},数量{4}\r\n",
                                                               openPrice, close, investorPosition.m_code, latestData.m_bidPrice1, investorPosition.m_position));
                            }
                            else if (state == 2)
                            {
                                FCStrEx.writeLog(String.Format("自动止损,开仓价{0},当前价{1},卖平仓,代码{2},价格{3},数量{4}\r\n",
                                                               openPrice, close, investorPosition.m_code, latestData.m_bidPrice1, investorPosition.m_position));
                            }
                            //开仓冷却时间+10
                            lock (m_cd1) {
                                if (m_cd1.ContainsKey(investorPosition.m_code))
                                {
                                    m_cd1[investorPosition.m_code] += 10;
                                }
                                else
                                {
                                    m_cd1[investorPosition.m_code] = 10;
                                }
                            }
                            //平仓冷却时间重置
                            lock (m_cd2) {
                                m_cd2[investorPosition.m_code] = 10;
                            }
                        }
                    }
                }
            }
        }
Beispiel #10
0
 /// <summary>
 /// 触发开仓
 /// </summary>
 /// <param name="data">最新数据</param>
 public static void doOpen(SecurityLatestData data)
 {
     //执行买开仓
     if (data.m_state1 == 1 && data.m_state2 == 1)
     {
         //判断是否有持仓
         bool hasTrade = inTrade(data.m_code);
         if (!hasTrade)
         {
             String key = data.m_code + "买";
             //判断冷却时间
             bool canTrade = true;
             lock (Strategy1.m_cd1) {
                 if (Strategy1.m_cd1.ContainsKey(data.m_code))
                 {
                     if (Strategy1.m_cd1[data.m_code] > 0)
                     {
                         canTrade = false;
                     }
                 }
             }
             if (canTrade)
             {
                 //获取默认交易手数
                 int tradeVol = 1;
                 if (m_tradeVolumes.ContainsKey(key))
                 {
                     tradeVol = m_tradeVolumes[key];
                 }
                 //打印日志
                 FCStrEx.writeLog(String.Format("创近20日新高,均线上翘,买开仓,代码{0},价格{1},数量{2}\r\n",
                                                data.m_code, data.m_askPrice1, tradeVol));
                 Security security = null;
                 lock (m_securities) {
                     if (m_securities.ContainsKey(data.m_code))
                     {
                         security = m_securities[data.m_code];
                     }
                 }
                 //买开仓
                 CTPDLL.bidOpen(Strategy1.m_ctpID, CTPDLL.generateReqID(Strategy1.m_ctpID), data.m_code, security.m_exchangeID, data.m_askPrice1, tradeVol, '3', "");
                 //刷新开仓冷却时间
                 lock (Strategy1.m_cd1) {
                     Strategy1.m_cd1[data.m_code] = 60;
                 }
             }
         }
     }
     //执行卖开仓
     if (data.m_state3 == 1 && data.m_state4 == 1)
     {
         //判断是否有持仓
         bool hasTrade = inTrade(data.m_code);
         if (!hasTrade)
         {
             String key = data.m_code + "卖";
             //判断冷却时间
             bool canTrade = true;
             lock (Strategy1.m_cd1) {
                 if (Strategy1.m_cd1.ContainsKey(data.m_code))
                 {
                     if (Strategy1.m_cd1[data.m_code] > 0)
                     {
                         canTrade = false;
                     }
                 }
             }
             if (canTrade)
             {
                 //获取默认交易手数
                 int tradeVol = 1;
                 if (m_tradeVolumes.ContainsKey(key))
                 {
                     tradeVol = m_tradeVolumes[key];
                 }
                 //打印日志
                 FCStrEx.writeLog(String.Format("创近20日新低,均线下翘,卖开仓,代码{0},价格{1},数量{2}\r\n",
                                                data.m_code, data.m_askPrice1, tradeVol));
                 Security security = null;
                 lock (m_securities) {
                     if (m_securities.ContainsKey(data.m_code))
                     {
                         security = m_securities[data.m_code];
                     }
                 }
                 //卖开仓
                 CTPDLL.askOpen(Strategy1.m_ctpID, CTPDLL.generateReqID(Strategy1.m_ctpID), data.m_code, security.m_exchangeID, data.m_bidPrice1, tradeVol, '3', "");
                 //刷新开仓冷却时间
                 lock (Strategy1.m_cd1) {
                     Strategy1.m_cd1[data.m_code] = 60;
                 }
             }
         }
     }
 }
Beispiel #11
0
        /// <summary>
        /// 检查是否开仓
        /// </summary>
        /// <param name="latestData">最新数据</param>
        /// <param name="datas">数据</param>
        /// <param name="n">创新高参数</param>
        /// <param name="m">均线参数</param>
        /// <returns>是否开仓 1:买开 2:卖开 0:不开仓</returns>
        public static void checkOpen(SecurityLatestData latestData, List <SecurityData> datas, int n, int m)
        {
            int datasSize = datas.Count;

            //依次判断买和卖 direction = 0 买 direction = 1卖
            for (int direction = 0; direction <= 1; direction++)
            {
                //极值
                double extrem = 0;
                //判断是否产生极值
                double lastClose = datas[datasSize - 1].m_close;
                //循环遍历除最新数据外的前n-1条数据
                for (int i = datasSize - n; i < datasSize - 1; i++)
                {
                    SecurityData data = datas[i];
                    if (direction == 0)
                    {
                        //获取阶段最高价
                        if (extrem == 0 || extrem < data.m_close)
                        {
                            extrem = data.m_close;
                        }
                    }
                    else if (direction == 1)
                    {
                        //获取阶段最低价
                        if (extrem == 0 || extrem > data.m_close)
                        {
                            extrem = data.m_close;
                        }
                    }
                }
                //判断是否创新高
                bool newExtrem = false;
                if (direction == 0)
                {
                    //当前价高于前n-1的最高价
                    if (lastClose > extrem)
                    {
                        newExtrem = true;
                    }
                }
                else if (direction == 1)
                {
                    //当前价低于前n-1的最低价
                    if (lastClose < extrem)
                    {
                        newExtrem = true;
                    }
                }
                //循环便利数据,计算60周期均线
                for (int i = 0; i < datasSize; i++)
                {
                    SecurityData data       = datas[i];
                    int          startIndex = i - (m - 1);
                    int          realM      = m;
                    if (startIndex <= 0)
                    {
                        startIndex = 0;
                        realM      = i + 1;
                    }
                    double sum = 0;
                    for (int j = startIndex; j <= i; j++)
                    {
                        sum += datas[j].m_close;
                    }
                    data.m_ma = sum / realM;
                }
                //判断60周期均线是否上翘或下翘
                bool newWards = false;
                if (direction == 0)
                {
                    newWards = datas[datasSize - 1].m_ma > datas[datasSize - 2].m_ma;
                }
                else if (direction == 1)
                {
                    newWards = datas[datasSize - 1].m_ma < datas[datasSize - 2].m_ma;
                }
                //判断是否买开仓
                if (direction == 0)
                {
                    if (newExtrem)
                    {
                        latestData.m_state1 = 1; //state1=1 代表是创新高
                    }
                    if (newWards)
                    {
                        latestData.m_state2 = 1; //state2=1 代表是均线上翘
                    }
                }
                //判断是否卖开仓
                else if (direction == 1)
                {
                    if (newExtrem)
                    {
                        latestData.m_state3 = 1; //state3=1 代表是创新低
                    }
                    if (newWards)
                    {
                        latestData.m_state4 = 1; //state4=1 代表是均线下翘
                    }
                }
            }
        }
Beispiel #12
0
        /// <summary>
        /// 检查平仓
        /// </summary>
        /// <param name="data">最新持仓数据</param>
        /// <param name="ceil">止盈位</param>
        /// <param name="floor">止损位</param>
        public static void checkClose(List <InvestorPosition> data, double ceil, double floor)
        {
            //取小数部分,止盈为正,止损位负
            int dataSize = data.Count;
            Dictionary <String, String> codes = new Dictionary <string, string>();

            //循环遍历数据
            for (int i = 0; i < dataSize; i++)
            {
                InvestorPosition investorPosition = data[i];
                String           key = investorPosition.m_code + investorPosition.m_posiDirection;
                //判断该持仓是新增的还是减少的
                lock (m_investorPositions) {
                    if (m_investorPositions.ContainsKey(key))
                    {
                        if (m_investorPositions[key].m_position != investorPosition.m_position)
                        {
                            codes[investorPosition.m_code] = "";
                        }
                    }
                    else
                    {
                        codes[investorPosition.m_code] = "";
                    }
                    //更新内存
                    m_investorPositions[key] = investorPosition;
                }
                //获取行情数据
                SecurityLatestData latestData = null;
                lock (m_latestDatas) {
                    if (m_latestDatas.ContainsKey(investorPosition.m_code))
                    {
                        latestData = m_latestDatas[investorPosition.m_code];
                    }
                }
                //openPrice字段是0
                if (latestData != null && investorPosition.m_position > 0)
                {
                    double openPrice = investorPosition.m_openCost / investorPosition.m_position;
                    //获取当前价格与开仓价的差额
                    if (openPrice != 0)
                    {
                        //获取代码
                        String code = investorPosition.m_code;
                        //获取方向
                        String direction = investorPosition.m_posiDirection;
                        //处理买开仓
                        if (investorPosition.m_posiDirection == "买")
                        {
                            //获取盈亏比例
                            double ratio    = latestData.m_close / openPrice;
                            String strClose = String.Format("现价/开仓价:{0},止盈位:{1},止损位:{2}", ratio, ceil, floor);
                            //判断止盈
                            if (ratio >= ceil) //如果是止盈,右边应该1.03
                            {
                                askOrBidClose(investorPosition, code, direction, latestData.m_close, openPrice, 1);
                            }
                            //判断止损
                            else if (ratio <= floor) //如果是止损,右边应该是0.99
                            {
                                askOrBidClose(investorPosition, code, direction, latestData.m_close, openPrice, 2);
                            }
                        }
                        //处理卖开仓
                        else if (investorPosition.m_posiDirection == "卖")
                        {
                            //获取当前价格与开仓价的差额,但是方向相反
                            double ratio    = latestData.m_close / openPrice;
                            double subCeil  = ceil - 1;  //也就是0.03
                            double subFloor = 1 - floor; //也就是0.01
                            String strClose = String.Format("现价/开仓价:{0},止盈位:{1},止损位:{2}", ratio, 1 - subCeil, 1 + subFloor);
                            //判断止盈
                            if (ratio <= 1 - subCeil) //如果止盈是0.03,右边应该是0.97
                            {
                                askOrBidClose(investorPosition, code, direction, latestData.m_close, openPrice, 1);
                            }
                            //判断止损
                            else if (ratio >= 1 + subFloor) //如果止损是0.01,右边应该是1.01
                            {
                                askOrBidClose(investorPosition, code, direction, latestData.m_close, openPrice, 2);
                            }
                        }
                    }
                }
            }
        }
Beispiel #13
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 = FCStrEx.convertSinaCodeToDBCode(str2);
                    if (data.m_code.StartsWith("399"))
                    {
                        szIndex = true;
                    }
                    break;

                case 1: {
                    data.m_open = FCStr.convertStrToDouble(str2);
                    break;
                }

                case 2: {
                    data.m_lastClose = FCStr.convertStrToDouble(str2);
                    break;
                }

                case 3: {
                    data.m_close = FCStr.convertStrToDouble(str2);
                    break;
                }

                case 4: {
                    data.m_high = FCStr.convertStrToDouble(str2);
                    break;
                }

                case 5: {
                    data.m_low = FCStr.convertStrToDouble(str2);
                    break;
                }

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

                case 9: {
                    data.m_amount = FCStr.convertStrToDouble(str2);
                    break;
                }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                case 29: {
                    if (formatType == 0)
                    {
                        data.m_sellPrice5 = FCStr.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 = FCStr.getDateNum(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);
        }