private void axKHOpenAPI_OnReceiveTrCondition(object sender, AxKHOpenAPILib._DKHOpenAPIEvents_OnReceiveTrConditionEvent e) { if (e.strCodeList == "") { return; } var codeNameList = e.strCodeList.Split(';').Where(x => x != "").ToList(); if (e.strConditionName == ConditionName.최근결산PER30) { var longTermBuySetInfoList = new Dictionary <string, BuyStockInfo>(); foreach (var codeName in codeNameList) { var longTermBuySetInfo = new BuyStockInfo(); longTermBuySetInfo.code = codeName; longTermBuySetInfoList.Add(codeName, longTermBuySetInfo); } var serializeCodeList = JsonConvert.SerializeObject(longTermBuySetInfoList); // LongTermBuyListKey 에 코드 리스트 저장. RedisConnector.SetString(LongTermBuyListKey, serializeCodeList, IsRealServer); foreach (var codeName in codeNameList) { KHConnector.Instance.GetStockBasicInfo(codeName); System.Threading.Thread.Sleep(400); } } }
public void WriteLog(string log) { var logText = MakeLogText(DateTime.Now, log); WriteToLogBox(logText); var newLogs = new List <Log>(); var serializedLogs = RedisConnector.GetString(LastLogsKey, Form1.IsRealServer); if (serializedLogs != null) { var lastLogs = JsonConvert.DeserializeObject <List <Log> >(serializedLogs); newLogs = lastLogs.Where(x => Util.DateTimeToTotalSec(x.time) + MaxLogSavingSec > Util.DateTimeToTotalSec(DateTime.Now)).ToList(); } newLogs.Add(new Log(DateTime.Now, log)); var newSerializeLogs = JsonConvert.SerializeObject(newLogs); RedisConnector.SetString(LastLogsKey, newSerializeLogs, Form1.IsRealServer); }
private void axKHOpenAPI_OnReceiveTrData(object sender, AxKHOpenAPILib._DKHOpenAPIEvents_OnReceiveTrDataEvent e) { // 30초마다 리프래쉬 될 구문 if (e.sRQName == "계좌평가현황요청") { listBox1.Items.Clear(); listBox1.Items.Add("==계좌평가현황== 최종 갱신:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); AccountMoney = Int32.Parse(axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, 0, "예수금").Trim()); textBox4.Text = AccountMoney.ToString(); listBox1.Items.Add($"D+2추정예수금 = " + Int32.Parse(axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, 0, "D+2추정예수금").Trim())); listBox1.Items.Add($"유가잔고평가액 = " + Int32.Parse(axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, 0, "유가잔고평가액").Trim())); listBox1.Items.Add($"예탁자산평가액 = " + Int32.Parse(axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, 0, "예탁자산평가액").Trim())); listBox1.Items.Add($"총매입금액 = " + Int32.Parse(axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, 0, "총매입금액").Trim())); listBox1.Items.Add($"추정예탁자산 = " + Int32.Parse(axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, 0, "추정예탁자산").Trim())); listBox1.Items.Add($"당일투자원금 = " + Int32.Parse(axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, 0, "당일투자원금").Trim())); listBox1.Items.Add($"당월투자원금 = " + Int32.Parse(axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, 0, "당월투자원금").Trim())); listBox1.Items.Add($"누적투자원금 = " + Int32.Parse(axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, 0, "누적투자원금").Trim())); listBox1.Items.Add($"당일투자손익 = " + Int32.Parse(axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, 0, "당일투자손익").Trim())); listBox1.Items.Add($"당월투자손익 = " + Int32.Parse(axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, 0, "당월투자손익").Trim())); listBox1.Items.Add($"당일손익율 = " + Int32.Parse(axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, 0, "당일손익율").Trim())); listBox1.Items.Add($"당월손익율 = " + Int32.Parse(axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, 0, "당월손익율").Trim())); listBox1.Items.Add($"누적손익율 = " + Int32.Parse(axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, 0, "누적손익율").Trim())); var count = axKHOpenAPI1.GetRepeatCnt(e.sTrCode, e.sRQName); dataGridView1.Rows.Clear(); var savingStockInfoList = new List <AutoUpdateStockInfo>(); for (int i = 0; i < count; ++i) { var stockName = axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, i, "종목명").Trim(); var stockCode = axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, i, "종목코드").Trim(); var stockCount = Int32.Parse(axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, i, "보유수량").Trim()).ToString(); var currentPrice = Int32.Parse(axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, i, "현재가").Trim()).ToString(); var evaluationAmount = Int32.Parse(axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, i, "평가금액").Trim()).ToString(); var profitPrice = Int32.Parse(axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, i, "손익금액").Trim()); var profitPercent = Double.Parse(axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, i, "손익율").Trim()); var purchaseAmount = Int32.Parse(axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, i, "매입금액").Trim()).ToString(); var purchasePrice = (Int32.Parse(axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, i, "매입금액").Trim()) / Int32.Parse(axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, i, "보유수량").Trim())).ToString(); var paymentBalance = Int32.Parse(axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, i, "결제잔고").Trim()).ToString(); var todayBuyCount = Int32.Parse(axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, i, "금일매수수량").Trim()).ToString(); var todaySellCount = Int32.Parse(axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, i, "금일매도수량").Trim()).ToString(); dataGridView1.Rows.Add( stockName, stockCode, stockCount, currentPrice, evaluationAmount, profitPrice, profitPercent, purchasePrice, paymentBalance, todayBuyCount, todaySellCount ); var savingStockInfo = new AutoUpdateStockInfo(stockName, stockCode, Int32.Parse(stockCount), Int32.Parse(currentPrice), Int32.Parse(evaluationAmount), profitPrice, profitPercent, Int32.Parse(purchasePrice), Int32.Parse(paymentBalance), Int32.Parse(todayBuyCount) , Int32.Parse(todaySellCount), ""); savingStockInfoList.Add(savingStockInfo); } var savingInfo = JsonConvert.SerializeObject(savingStockInfoList); RedisConnector.SetString(HavingStockInfoKey, savingInfo, IsRealServer); groupBox3.Text = TextCollection.GroupBox3 + " " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); } else if (e.sRQName == "매수미체결") { dataGridView2.Rows.Clear(); int nCnt = axKHOpenAPI1.GetRepeatCnt(e.sTrCode, e.sRQName); var savingStockInfoList = new List <AutoUpdateNotConcludedStockInfo>(); for (int i = 0; i < nCnt; i++) { var stockName = axKHOpenAPI1.CommGetData(e.sTrCode, "", e.sRQName, i, "종목명").Trim(); var stockCode = axKHOpenAPI1.CommGetData(e.sTrCode, "", e.sRQName, i, "종목코드").Trim(); var currentPrice = Int32.Parse(axKHOpenAPI1.CommGetData(e.sTrCode, "", e.sRQName, i, "현재가").Trim()).ToString(); var orderPrice = Int32.Parse(axKHOpenAPI1.CommGetData(e.sTrCode, "", e.sRQName, i, "주문가격").Trim()).ToString(); var notConcludedCount = Int32.Parse(axKHOpenAPI1.CommGetData(e.sTrCode, "", e.sRQName, i, "미체결수량").Trim()).ToString(); var orderCount = Int32.Parse(axKHOpenAPI1.CommGetData(e.sTrCode, "", e.sRQName, i, "주문수량").Trim()).ToString(); var originalOrderNumber = axKHOpenAPI1.CommGetData(e.sTrCode, "", e.sRQName, i, "원주문번호").Trim().ToString(); dataGridView2.Rows.Add( stockName, stockCode, currentPrice, orderPrice, notConcludedCount, orderPrice, originalOrderNumber ); var savingStockInfo = new AutoUpdateNotConcludedStockInfo(stockCode, notConcludedCount, originalOrderNumber); savingStockInfoList.Add(savingStockInfo); } var savingInfo = JsonConvert.SerializeObject(savingStockInfoList); RedisConnector.SetString(NotConcludedBuyKey, savingInfo, IsRealServer); groupBox4.Text = TextCollection.GroupBox4 + " " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); } else if (e.sRQName == "매도미체결") { dataGridView3.Rows.Clear(); int nCnt = axKHOpenAPI1.GetRepeatCnt(e.sTrCode, e.sRQName); var savingStockInfoList = new List <AutoUpdateNotConcludedStockInfo>(); for (int i = 0; i < nCnt; i++) { var stockName = axKHOpenAPI1.CommGetData(e.sTrCode, "", e.sRQName, i, "종목명").Trim(); var stockCode = axKHOpenAPI1.CommGetData(e.sTrCode, "", e.sRQName, i, "종목코드").Trim(); var currentPrice = Int32.Parse(axKHOpenAPI1.CommGetData(e.sTrCode, "", e.sRQName, i, "현재가").Trim()).ToString(); var orderPrice = Int32.Parse(axKHOpenAPI1.CommGetData(e.sTrCode, "", e.sRQName, i, "주문가격").Trim()).ToString(); var notConcludedCount = Int32.Parse(axKHOpenAPI1.CommGetData(e.sTrCode, "", e.sRQName, i, "미체결수량").Trim()).ToString(); var orderCount = Int32.Parse(axKHOpenAPI1.CommGetData(e.sTrCode, "", e.sRQName, i, "주문수량").Trim()).ToString(); var originalOrderNumber = axKHOpenAPI1.CommGetData(e.sTrCode, "", e.sRQName, i, "원주문번호").Trim().ToString(); dataGridView3.Rows.Add( stockName, stockCode, currentPrice, orderPrice, notConcludedCount, orderPrice, originalOrderNumber ); var savingStockInfo = new AutoUpdateNotConcludedStockInfo(stockCode, notConcludedCount, originalOrderNumber); savingStockInfoList.Add(savingStockInfo); } var savingInfo = JsonConvert.SerializeObject(savingStockInfoList); RedisConnector.SetString(NotConcludedSellKey, savingInfo, IsRealServer); groupBox5.Text = TextCollection.GroupBox5 + " " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); } else if (e.sRQName == "고저PER요청") { // 알수없는 오류남. } else if (e.sRQName == "상하한가요청") { // 필요없음 } else if (e.sRQName == "코스피200지수요청") { int nCnt = axKHOpenAPI1.GetRepeatCnt(e.sTrCode, e.sRQName); var day = axKHOpenAPI1.CommGetData(e.sTrCode, "", e.sRQName, 0, "일자").Trim(); var stockCode = axKHOpenAPI1.CommGetData(e.sTrCode, "", e.sRQName, 0, "코스피200").Trim(); } else if (e.sRQName == "주식기본정보요청") { // LongTermBuyListKey 에 코드 리스트 저장. var serializeCodeInfos = RedisConnector.GetString(LongTermBuyListKey, IsRealServer); var codeInfos = JsonConvert.DeserializeObject <Dictionary <string, BuyStockInfo> >(serializeCodeInfos); var eCode = axKHOpenAPI1.CommGetData(e.sTrCode, "", e.sRQName, 0, "종목코드").Trim(); if (codeInfos.TryGetValue(eCode, out var value)) { codeInfos[eCode].name = axKHOpenAPI1.CommGetData(e.sTrCode, "", e.sRQName, 0, "종목명").Trim(); codeInfos[eCode].per = double.Parse(axKHOpenAPI1.CommGetData(e.sTrCode, "", e.sRQName, 0, "PER").Trim()); codeInfos[eCode].price = Math.Abs(Int32.Parse(axKHOpenAPI1.CommGetData(e.sTrCode, "", e.sRQName, 0, "현재가").Trim().ToString())); codeInfos[eCode].count = Config.LongTermBuyPer / value.price; } var newSerializeCodeInfos = JsonConvert.SerializeObject(codeInfos); RedisConnector.SetString(LongTermBuyListKey, newSerializeCodeInfos, IsRealServer); // LongTermBuySetKey 플래그를 켠다. RedisConnector.FlagOn(LongTermBuySetFlagKey, IsRealServer); } }