private void pushDataQuotes(RealtimeDataCenter.RealtimeData rd) { lock (lockData) { this.ltDataQuotes.Add(rd); } }
protected override void recv(RealtimeDataCenter.RealtimeData rd) { if (DateTime.Now.Date != rd.dt.Date) { return; } //expired data //Console.Write(1); CInstruction[] inss = getInstructionFromWaitingByCode(rd.code); if (inss != null) { foreach (var instruction in inss) { //:open or close instruction preprocess string szTime = DateTime.Now.ToString("HHmmss"); if (string.Compare(szTime, CInstruction.szExecOpenTimeBegin) < 0 && instruction.priceType == (short)EPriceType.OPEN) { if (!this.dcCodeLastRdForOpen.ContainsKey(rd.code)) { this.dcCodeLastRdForOpen.Add(rd.code, rd); } else { this.dcCodeLastRdForOpen[rd.code] = rd; } setTimer(10); continue; } if (string.Compare(szTime, CInstruction.szExecCloseTimeBegin) < 0 && instruction.priceType == (short)EPriceType.CLOSE) { if (!this.dcCodeLastRdForClose.ContainsKey(rd.code)) { this.dcCodeLastRdForClose.Add(rd.code, rd); } else { this.dcCodeLastRdForClose[rd.code] = rd; } setTimer(10); continue; } ///:~ /* * if (rd.C == 0) //call auction in open * { * } */ if (instruction.bingo(rd)) { decimal range = 0.01m; EEntryOrExit ee = (EEntryOrExit)instruction.entryOrExit; if (ee == EEntryOrExit.entry) { range = 0.01m; } //谨慎entry else if (ee == EEntryOrExit.exit) { range = 0.03m; } //确保exit //:send ECategory category = (ECategory)instruction.category; switch (category) { case ECategory.BUY: case ECategory.RZ_BUY: case ECategory.RQ_RETURN: if (instruction.price == null) { instruction.price = rd.sell5c[0]; } if (instruction.price == 0) //limit-up, sell none { instruction.price = rd.buy5c[0]; } instruction.price *= (1m + range); decimal limitUp = Limit.getInstance().getLimitUp(rd.code, rd.lastC); if (instruction.price > limitUp) { instruction.price = limitUp; } if (instruction.priceDesc != null && instruction.priceDesc != 0) { if (instruction.price > instruction.priceDesc) { instruction.price = instruction.priceDesc; } } break; case ECategory.SELL: case ECategory.RQ_SELL: case ECategory.RZ_RETURN: if (instruction.price == null) { instruction.price = rd.buy5c[0]; } if (instruction.price == 0) //limit-down, buy none { instruction.price = rd.sell5c[0]; } instruction.price *= (1m - range); decimal limitDown = Limit.getInstance().getLimitDown(rd.code, rd.lastC); if (instruction.price < limitDown) { instruction.price = limitDown; } if (instruction.priceAsc != null && instruction.priceAsc != 0) { if (instruction.price < instruction.priceAsc) { instruction.price = instruction.priceAsc; } } break; case ECategory.RQ_Q2Q: break; default: break; } instruction.state = (short)EInstructionState.SENT; instruction.sendTime = DateTime.Now; instruction.price = Math.Floor((decimal)instruction.price * 100m) / 100m; //exec //...SendOrder //update DB long maybeOldId = instruction.id; if (instruction.id > 0) { var vs = (from n in db.tsInstructions where n.id == instruction.id select n).Single(); instruction.modifyDBObj(ref vs); db.SubmitChanges(); } else { if (instruction.sso != null) { string sqlSSO = instruction.sso.getInsertSql(); string sqlInstruction = instruction.getInsertSql(); long? idSSO = instruction.sso.id; long? idInstruction = instruction.id; try { db.spAddSSOAndInstruction(sqlSSO, sqlInstruction, ref idSSO, ref idInstruction); instruction.id = (long)idInstruction; instruction.sso.id = (long)idSSO; } catch (Exception e) { Console.WriteLine(e); Console.WriteLine(sqlSSO); Console.WriteLine(sqlInstruction); WriteLog.getInstance().pushData("error2DB: " + sqlSSO + "; " + sqlInstruction); } } } Console.Write(2); string ifs = ""; if (instruction.sso != null) { ifs = instruction.sso.idForStrategies.ToString(); } Console.WriteLine("(" + rd.code + "," + ifs + "," + instruction.desc + "," + instruction.sendTime + "," + instruction.price + ")"); //write log WriteLog.getInstance().pushData(instruction.getLog()); addInstruction2Sent(rd.code, instruction); removeInstructionFromWating(rd.code, maybeOldId); ///:~ } } } inss = getInstructionFromSentByCode(rd.code); if (inss != null) { foreach (var instruction in inss) { //...QueryDatas(Category=2) bool bSuccesful = false; ECategory category = (ECategory)instruction.category; if (category == ECategory.BUY) { if (instruction.price > rd.C || (instruction.price == rd.C && DateTime.Now.Subtract((DateTime)instruction.sendTime).TotalSeconds > 5)) { bSuccesful = true; } } else if (category == ECategory.SELL) { if (instruction.price < rd.C || (instruction.price == rd.C && DateTime.Now.Subtract((DateTime)instruction.sendTime).TotalSeconds > 5)) { bSuccesful = true; } } if (bSuccesful) { instruction.price = rd.C; //...QueryDatas(Category=2)' price instruction.state = (short)EInstructionState.SUCCESSFUL; instruction.resultTime = DateTime.Now; //exec //update DB if (instruction.id > 0) { var vs = (from n in db.tsInstructions where n.id == instruction.id select n).Single(); instruction.modifyDBObj(ref vs); db.SubmitChanges(); } Console.Write(3); Console.WriteLine("(" + rd.code + "," + instruction.resultTime + "," + instruction.price + ")"); //write log WriteLog.getInstance().pushData(instruction.getLog()); removeInstructionFromSent(rd.code, instruction.id); } } } }
public bool bingo(RealtimeDataCenter.RealtimeData rd) { if (rd.C == 0) //call auction in open { switch ((ECategory)(category)) { case ECategory.BUY: case ECategory.RZ_BUY: case ECategory.RQ_RETURN: rd.C = rd.sell5c[0]; break; case ECategory.SELL: case ECategory.RQ_SELL: case ECategory.RZ_RETURN: rd.C = rd.buy5c[0]; break; case ECategory.RQ_Q2Q: return(false); default: return(false); } } bool bAsc = true, bDesc = true, bType = true; if (null != priceAsc && rd.C < priceAsc) { bAsc = false; } if (null != priceDesc && rd.C > priceDesc) { bDesc = false; } switch ((EPriceType)priceType) { case EPriceType.REALTIME: bType = string.Compare(rd.dt.ToString("HHmmss"), szExecOpenTimeBegin) >= 0; break; case EPriceType.OPEN: bType = isOpenTime(rd.dt); if (!bType && entryOrExit == (short)(EEntryOrExit.exit) && string.Compare(rd.dt.ToString("HHmmss"), szExecOpenTimeBegin) >= 0) //未能在OPEN时段exit的确保在OPEN之后也能退出 { bType = true; } break; case EPriceType.CLOSE: bType = isCloseTime(rd.dt); break; case EPriceType.REALTIME_OPEN: bType = !isCloseTime(rd.dt); break; case EPriceType.REALTIME_CLOSE: bType = !isOpenTime(rd.dt); break; default: bType = false; break; } if (bAsc && bDesc && bType) { return(true); } return(false); }
protected override void recv(RealtimeDataCenter.RealtimeData rd) { if (DateTime.Now.Date != rd.dt.Date) { return; } //expired data if (this.dcWindow.ContainsKey(rd.code)) { Tuple <decimal, decimal> wind = this.dcWindow[rd.code]; if (rd.L <= wind.Item2) //当日将不再会形成跳空式的关闭窗口(即RI) { removeObj(rd.code); } else { if (rd.C >= wind.Item1) { bool bCreate = false; Reactor.EPriceType pt = Reactor.EPriceType.REALTIME; if ((rd.C / rd.lastC - 1) * 100 > 9.0m) //推测其将limit-up,先买入(此点需继续验证) { bCreate = true; pt = Reactor.EPriceType.REALTIME; } else if (string.Compare(rd.dt.ToString("HHmmss"), "145940") > 0) { bCreate = true; pt = Reactor.EPriceType.CLOSE; } if (bCreate) { Reactor.CInstruction instruction = new Reactor.CInstruction(); instruction.id = 0; instruction.priceAsc = wind.Item2 + 0.01m; instruction.priceDesc = Limit.getInstance().getLimitUp(rd.code, rd.lastC) - 0.01m; //-0.01m, don't buy when limit-up instruction.generateTime = DateTime.Now; instruction.priceType = (short)pt; instruction.category = (short)Reactor.ECategory.BUY; instruction.state = (short)Reactor.EInstructionState.WATING; instruction.desc = "entry"; instruction.quantity = (int)(Math.Round(CASH_SIZE / (decimal)instruction.priceAsc / 100m) * 100m); instruction.codeReference = rd.code; if (instruction.quantity > 0) { instruction.sso = new Reactor.CSimplifiedStrategiesObject() { id = 0, idForStrategies = (long)Reactor.EIFS.RI, code = rd.code, state = (short)Reactor.ESSOState.WAIT_CREATE, planQuantity = instruction.quantity, lastStopPrice = (decimal)instruction.priceAsc, createTime = instruction.generateTime, direction = (short)Reactor.EDirection.LONG, }; instruction.analyseEntryOrExit((Reactor.EDirection)(instruction.sso.direction)); removeObj(rd.code); Reactor.getInstance().addInstruction2Wating(rd.code, instruction); } } } } } }
private void GetQuotesFromTradeXB() { StringBuilder sErrInfo = new StringBuilder(256); StringBuilder sResult = new StringBuilder(1024 * 1024); int idConn = -1; while (true) { if (idConn < 0) { idConn = ThreadGetDataFromTradeX.connectTDX(); } if (idConn < 0) { Console.WriteLine("没有可用的TDX行情服务器! 请检查网络...... 重连......" + DateTime.Now); System.Threading.Thread.Sleep(1000); continue; } List <string> ltCode = new List <string>(); List <byte> ltMarket = new List <byte>(); List <short> ltGetCounts = new List <short>(); bool bRet = true; string code = ""; short getCounts = 0; while (getCode(ref code, ref getCounts)) { ltCode.Add(code); ltMarket.Add(ThreadGetDataFromTradeX.getMarketType(code)); ltGetCounts.Add(getCounts); if (ltCode.Count >= ThreadGetDataFromTradeX.MAX_COUNT_PER_TIMES) { break; } } short count = (short)ltCode.Count; if (count > 0) { sResult.Clear(); sErrInfo.Clear(); string[] arryCode = new string[count]; byte[] arryMarket = new byte[count]; ltCode.CopyTo(arryCode); ltMarket.CopyTo(arryMarket); short c = count; bRet = TradeXB.TdxHq_GetSecurityQuotes(idConn, arryMarket, arryCode, ref c, sResult, sErrInfo); if (bRet) { Console.Write(0); string[] rows = sResult.ToString().Split(new char[] { '\n' }); foreach (var row in rows) { RealtimeDataCenter.RealtimeData rd = ThreadGetDataFromTradeX.analyseRD(row); if (null != rd) { pushDataQuotes(rd); } } } else { Console.Write(0); for (int i = 0; i < ltCode.Count; i++) { if (ltGetCounts[i] < 3) { pushCode(ltCode[i], ++ltGetCounts[i]); } } string szErrorInfo = sErrInfo.ToString(); Console.WriteLine(szErrorInfo); if (szErrorInfo.Contains("连接断开")) { System.Threading.Thread.Sleep(100); idConn = -1; break; } else { bRet = true; } } } if (bRet && count <= 0) { if (idConn >= 0) { TradeXB.TdxHq_Disconnect(idConn); } break; } } subtractReferenced(); }
protected override void recv(RealtimeDataCenter.RealtimeData rd) { if (DateTime.Now.Date != rd.dt.Date) { return; } //expired data if (this.dcTendency.ContainsKey(rd.code)) { decimal[] dcps = this.dcTendency[rd.code].ToArray(); foreach (var dcp in dcps) { if (rd.C >= dcp) { bool bCreate = false; Reactor.EPriceType pt = Reactor.EPriceType.REALTIME; if ((rd.C / rd.lastC - 1) * 100 < 3.0m) { bCreate = true; pt = Reactor.EPriceType.REALTIME; } if (bCreate) { Reactor.CInstruction instruction = new Reactor.CInstruction(); instruction.id = 0; instruction.priceAsc = dcp + 0.01m; instruction.priceDesc = Limit.getInstance().getLimitUp(rd.code, rd.lastC) - 0.01m; //-0.01m, don't buy when limit-up instruction.generateTime = DateTime.Now; instruction.priceType = (short)pt; instruction.category = (short)Reactor.ECategory.BUY; instruction.state = (short)Reactor.EInstructionState.WATING; instruction.desc = "entry"; instruction.quantity = (int)(Math.Round(CASH_SIZE / (decimal)instruction.priceAsc / 100m) * 100m); instruction.codeReference = rd.code; if (instruction.quantity > 0) { instruction.sso = new Reactor.CSimplifiedStrategiesObject() { id = 0, idForStrategies = (long)Reactor.EIFS.RT, code = rd.code, state = (short)Reactor.ESSOState.WAIT_CREATE, planQuantity = instruction.quantity, lastStopPrice = (decimal)instruction.priceAsc, createTime = instruction.generateTime, direction = (short)Reactor.EDirection.LONG, }; instruction.analyseEntryOrExit((Reactor.EDirection)(instruction.sso.direction)); //直接removeObj,以及下面的break (foreach),而不使用下面的注释部分,原因在于:Reactor.EIFS.RT中,同一对象有多个tendency line时,当日只进入一次 removeObj(rd.code); /* * this.dcTendency[rd.code].Remove(dcp); * if (this.dcTendency[rd.code].Count == 0) * { removeObj(rd.code); } */ Reactor.getInstance().addInstruction2Wating(rd.code, instruction); break; } } } } } }
protected virtual void recv(RealtimeDataCenter.RealtimeData rd) { }
public static RealtimeDataCenter.RealtimeData analyseRD(string row) { string[] cols = row.Split(new char[] { '\t' }); if (cols.Count() > 10 && (cols[0] == "0" || cols[0] == "1")) { RealtimeDataCenter.RealtimeData rd = new RealtimeDataCenter.RealtimeData() { /* * 0 市场 * 1 代码 * 2 活跃度 * 3 现价 * 4 昨收 * 5 开盘 * 6 最高 * 7 最低 * 8 保留 (Time, format as 'HHMMSSAA', 'AA' is 10 ms) * 9 保留 * 10 总量 * 11 现量 * 12 总金额 * 13 内盘 * 14 外盘 * 15 保留 * 16 保留 * 17 买一价 * 18 卖一价 * 19 买一量 * 20 卖一量 * 21 买二价 * 22 卖二价 * 23 买二量 * 24 卖二量 * 25 买三价 * 26 卖三价 * 27 买三量 * 28 卖三量 * 29 买四价 * 30 卖四价 * 31 买四量 * 32 卖四量 * 33 买五价 * 34 卖五价 * 35 买五量 * 36 卖五量 * 37 保留 * 38 保留 * 39 保留 * 40 保留 * 41 保留 * 42 涨速 * 43 活跃度 */ code = cols[1], C = Math.Round(Convert.ToDecimal(cols[3]), 2, MidpointRounding.AwayFromZero), lastC = Math.Round(Convert.ToDecimal(cols[4]), 2, MidpointRounding.AwayFromZero), O = Convert.ToDecimal(cols[5]), H = Convert.ToDecimal(cols[6]), L = Convert.ToDecimal(cols[7]), V = Convert.ToDecimal(cols[10]), vm = Convert.ToDecimal(cols[12]), // fd = null, }; rd.buy5c[0] = Convert.ToDecimal(cols[17]); rd.buy5c[1] = Convert.ToDecimal(cols[21]); rd.buy5c[2] = Convert.ToDecimal(cols[25]); rd.buy5c[3] = Convert.ToDecimal(cols[29]); rd.buy5c[4] = Convert.ToDecimal(cols[33]); rd.sell5c[0] = Convert.ToDecimal(cols[18]); rd.sell5c[1] = Convert.ToDecimal(cols[22]); rd.sell5c[2] = Convert.ToDecimal(cols[26]); rd.sell5c[3] = Convert.ToDecimal(cols[30]); rd.sell5c[4] = Convert.ToDecimal(cols[34]); rd.buy5v[0] = Convert.ToDecimal(cols[19]); rd.buy5v[1] = Convert.ToDecimal(cols[23]); rd.buy5v[2] = Convert.ToDecimal(cols[27]); rd.buy5v[3] = Convert.ToDecimal(cols[31]); rd.buy5v[4] = Convert.ToDecimal(cols[35]); rd.sell5v[0] = Convert.ToDecimal(cols[20]); rd.sell5v[1] = Convert.ToDecimal(cols[24]); rd.sell5v[2] = Convert.ToDecimal(cols[28]); rd.sell5v[3] = Convert.ToDecimal(cols[32]); rd.sell5v[4] = Convert.ToDecimal(cols[36]); rd.dt = DateTime.Now; return(rd); } return(null); }
public void GetData() { List <RealtimeDataCenter.RealtimeData> ltRd = new List <RealtimeDataCenter.RealtimeData>(); StringBuilder sErrInfo = new StringBuilder(256); StringBuilder sResult = new StringBuilder(1024 * 1024); while (true) { if (!isTradingTime(DateTime.Now)) { System.Threading.Thread.Sleep(1000); continue; } int idConn = ThreadGetDataFromTradeX.connectTDX(); if (idConn < 0) { Console.WriteLine("没有可用的TDX行情服务器! 请检查网络...... 重连......" + DateTime.Now); System.Threading.Thread.Sleep(1000); continue; } bool bRet = true; while (bRet) { if (!isTradingTime(DateTime.Now)) { bRet = false; TradeXB.TdxHq_Disconnect(idConn); Console.WriteLine("is not trading time"); break; } if (this.bChanged) { generateCodeMarketGrp(); } foreach (var item in ltCodeMarket) { sResult.Clear(); sErrInfo.Clear(); short count = item.Item3[0]; bRet = TradeXB.TdxHq_GetSecurityQuotes(idConn, item.Item2, item.Item1, ref count, sResult, sErrInfo); if (bRet) { string[] rows = sResult.ToString().Split(new char[] { '\n' }); foreach (var row in rows) { RealtimeDataCenter.RealtimeData rd = analyseRD(row); if (null != rd) { ltRd.Add(rd); } } if (ltRd.Count > 0) { this.rdc.recv(ltRd); ltRd.Clear(); } } else { string szErrorInfo = sErrInfo.ToString(); Console.WriteLine(szErrorInfo); if (szErrorInfo.Contains("连接断开")) { TradeXB.TdxHq_Disconnect(idConn); break; } } } if (bRet) { System.Threading.Thread.Sleep(800); } } TradeXB.TdxHq_Disconnect(idConn); System.Threading.Thread.Sleep(1000); } }
public void GetData() { GetFinancialDataFromHttp(); string serviceAddr = "http://hq.sinajs.cn/list=" + this.codes; List <RealtimeDataCenter.RealtimeData> ltRd = new List <RealtimeDataCenter.RealtimeData>(); while (true) { try { WebClient client = new WebClientTimeout(); Stream strm = client.OpenRead(serviceAddr); StreamReader sr = new StreamReader(strm); string line = null; while (null != (line = sr.ReadLine())) { string[] objs = line.Split(new char[] { '\r' }, StringSplitOptions.RemoveEmptyEntries); foreach (var item in objs) { string[] obj = item.Split(new char[] { '=', ',' }); if (obj.Length == 34) { string codeTdx = getSubtractPrefixCode(obj[0]); RealtimeDataCenter.RealtimeData rd = new RealtimeDataCenter.RealtimeData() { code = codeTdx, dt = Convert.ToDateTime(obj[31] + " " + obj[32]), lastC = Convert.ToDecimal(obj[3]), H = Convert.ToDecimal(obj[5]), L = Convert.ToDecimal(obj[6]), O = Convert.ToDecimal(obj[2]), C = Convert.ToDecimal(obj[4]), V = Convert.ToDecimal(obj[9]), vm = Convert.ToDecimal(obj[10]), fd = null, }; if (rd.C == 0 && Convert.ToDecimal(obj[12]) != 0) { rd.C = rd.H = rd.L = rd.O = Convert.ToDecimal(obj[12]); } if (this.dcCodeFD.ContainsKey(codeTdx)) { rd.fd = this.dcCodeFD[codeTdx]; } else { this.codes_i += (getPlusPrefixCode(codeTdx) + "_i,"); } if (rd.code.StartsWith("39900")) //399001, 399006 { rd.V = Convert.ToInt64(rd.V / 100); } ltRd.Add(rd); } } } sr.Close(); strm.Close(); client.Dispose(); if (ltRd.Count > 0) { this.rdc.recv(ltRd); ltRd.Clear(); } } catch (Exception e) { if (e.GetType() != typeof(System.Threading.ThreadAbortException)) //&& e.GetType() != typeof(System.IO.IOException)) { //ctrl.ShowInfo(no.ToString() + ":" + e.ToString()); } } if ("" != this.codes_i) { GetFinancialDataFromHttp(); } System.Threading.Thread.Sleep(CYCLE); } }