private void cancelOrder(int orderType, string code, string orgOrderNo) { //주문취소(종목코드, 주문구분.Equals("매수") ? 3 : 4, 주문번호); int result = kiwoomApi.SendOrder("ORDER", SCREEN_NO_ORDER, getSeletedAccountNo(), orderType, code, 0, 0, "", orgOrderNo); if (result < 0) { log(LogMode.TRADE, "[ERROR] 주문 취소 실패 " + result); return; } HoldStock holding = holdings.SingleOrDefault(item => item.StockNo.Contains(code)); if (holding != null && orderType.Equals("매수취소")) { holding.BuyCnt = holding.BuyCnt - 1; } else if (holding != null && orderType.Equals("매도취소")) { holding.Ordered = "대기"; } }
private void updateHoldings() { string stockNo = kiwoomApi.GetChejanData(9001).Trim().Replace("A", ""); int qty = int.Parse(kiwoomApi.GetChejanData(930)); long buyPrice = long.Parse(kiwoomApi.GetChejanData(931)); long totalBuyPrice = long.Parse(kiwoomApi.GetChejanData(932)); HoldStock holding = holdings.SingleOrDefault(item => item.StockNo.Contains(stockNo)); if (holding != null) { if (qty == 0) { holdings.Remove(holding); requestTrEstimatedAssets(); requestTrAccountEvaluationStatus(); } else { holding.Qty = String.Format("{0:#,###0}", qty); holding.BuyPrice = String.Format("{0:#,###0}", buyPrice); holding.TotalBuyPrice = String.Format("{0:#,###0}", totalBuyPrice); if (holding.Ordered.Equals("주문")) { holding.Ordered = "대기"; } } } else { string stockName = kiwoomApi.GetChejanData(302).Trim(); long currentPrice = long.Parse(kiwoomApi.GetChejanData(10)); holdings.Add(new HoldStock(stockNo, stockName, currentPrice, qty, buyPrice, totalBuyPrice, loginInfo.getServerGubun)); requestRealtimeQuote(SCREEN_NO_ACCOUNT_INFO, stockNo, REALTIME_ADD); } }
private void updateOrders() { // 접수 : 주문번호,종목코드,주문상태,종목명,주문수량,주문가격,미체결수량,주문시간,화면번호 // 체결 : 주문번호,종목코드,주문상태,종목명,주문수량,주문가격,미체결수량,체결시간,체결번호,체결가,체결량,화면번호 string orderNo = kiwoomApi.GetChejanData(9203).Trim(); string stockNo = kiwoomApi.GetChejanData(9001).Trim().Replace("A", ""); string orderStatus = kiwoomApi.GetChejanData(913); string stockName = kiwoomApi.GetChejanData(302).Trim(); int qty = int.Parse(kiwoomApi.GetChejanData(900)); long orderPrice = long.Parse(kiwoomApi.GetChejanData(901)); int unclosedQty = int.Parse(kiwoomApi.GetChejanData(902)); string orderType = kiwoomApi.GetChejanData(905); string orderTime = kiwoomApi.GetChejanData(908); //주문/체결시간:095744 string orgOrderNo = kiwoomApi.GetChejanData(904); OrderStock order = orders.SingleOrDefault(item => item.OrderNo.Contains(orderNo)); // nullpointer exception이 뜨네? // 매도 정정 했다가 매도하니까 뜸 if (orderStatus.Equals("접수")) { // 접수 if (order == null) { orders.Add(new OrderStock(orderNo, stockNo, orderStatus, stockName, qty, orderPrice, unclosedQty, orderType, orderTime)); } else if (order != null && unclosedQty == 0) { orders.Remove(order); HoldStock holding = holdings.SingleOrDefault(item => item.StockNo.Contains(stockNo)); if (holding != null && orderType.Equals("매수취소")) { holding.BuyCnt = holding.BuyCnt - 1; } else if (holding != null && orderType.Equals("매도취소")) { holding.Ordered = "대기"; } } } else if (orderStatus.Equals("확인")) { // 확인 // 원주문번호가 있으면 정정 or 취소 // 매수정정, 매수취소 if (order != null) { if (orderType.Contains("취소")) { orders.Remove(order); } else if (orderType.Contains("정정")) { orders.Add(new OrderStock(orderNo, stockNo, orderStatus, stockName, qty, orderPrice, unclosedQty, orderType, orderTime, orgOrderNo)); } } } else { // 체결 if (order != null) { if (unclosedQty == 0) { orders.Remove(order); } else if (orderType.Contains("취소")) { Console.WriteLine("주문번호 " + orderNo + " " + orderType); orders.Remove(order); } else { order.OrderStatus = orderStatus; order.UnclosedQty = String.Format("{0:#,###0}", unclosedQty); } } } }
private void kiwoomApi_OnReceiveRealData(object sender, AxKHOpenAPILib._DKHOpenAPIEvents_OnReceiveRealDataEvent e) { if (!isRunning) { return; } if (DateTime.Now.Hour > 13) { buttonStop.PerformClick(); } //Console.WriteLine("[DEBUG] OnReceiveRealData - e.sRealKey : " + e.sRealKey + ", e.sRealType : " + e.sRealType + ", e.sRealData : " + e.sRealData); // 보유 종목에 있을 경우 값 업데이트 string market = kiwoomApi.GetCommRealData(e.sRealKey, 290).Trim(); if (market.Equals("2")) { if (orders.Count > 0) { foreach (OrderStock order in orders) { if (order.AfterOrder > ORDER_TIMEOUT && !order.Canceled) { log(LogMode.TRADE, "주문번호 " + order.OrderNo + "의 주문이 오래되어 해당 주문을 취소합니다. (" + order.AfterOrder + "초)"); order.Canceled = true; cancelOrder(order.OrderType.Equals("매수") ? ORDER_TYPE_BUY_CANCEL : ORDER_TYPE_SELL_CANCEL, order.StockNo, order.OrderNo); } } } HoldStock holding = holdings.SingleOrDefault(item => item.StockNo.Contains(e.sRealKey)); if (holding != null) { long currentPrice = long.Parse(Regex.Replace(kiwoomApi.GetCommRealData(e.sRealKey, 10).Trim(), @"[^0-9]", "")); holding.CurrentPrice = String.Format("{0:#,###0}", currentPrice); long dayHighPrice = long.Parse(Regex.Replace(kiwoomApi.GetCommRealData(e.sRealKey, 17).Trim(), @"[^0-9]", "")); holding.DayHighPrice = String.Format("{0:#,###0}", dayHighPrice); long dayLowPrice = long.Parse(Regex.Replace(kiwoomApi.GetCommRealData(e.sRealKey, 18).Trim(), @"[^0-9]", "")); holding.DayLowPrice = String.Format("{0:#,###0}", dayLowPrice); if (holding.isProfit) { log(LogMode.TRADE, holding.StockName + "(" + holding.StockNo + ") 종목이 익절가(" + holding.TargetLine + ")에 도달하여 익절 합니다. (매수가 : " + holding.BuyPrice + ", 매수 횟수 : " + holding.BuyCnt + ", 수익 : " + float.Parse(holding.ProfitRate) + "%)"); holding.Ordered = "주문"; if (!order(ORDER_TYPE_SELL, holding.StockNo, int.Parse(holding.Qty, System.Globalization.NumberStyles.AllowThousands), int.Parse(currentPrice.ToString()), ORDER_HOGA_MARKET)) { holding.Ordered = "대기"; } } else if (holding.Ordered.Equals("대기") && long.Parse(holding.SecondBuyPrice, System.Globalization.NumberStyles.AllowThousands) > currentPrice && holding.BuyCnt == 1) { // 2차 매수가에 도달하면 손절 log(LogMode.TRADE, holding.StockName + "(" + holding.StockNo + ") 종목이 손절가(" + holding.SecondBuyPrice + ")에 도달하여 손절 합니다. (매수가 : " + holding.BuyPrice + ", 손익 : " + float.Parse(holding.ProfitRate) + "%)"); holding.Ordered = "주문"; if (!order(ORDER_TYPE_SELL, holding.StockNo, int.Parse(holding.Qty, System.Globalization.NumberStyles.AllowThousands), int.Parse(currentPrice.ToString()), ORDER_HOGA_MARKET)) { holding.Ordered = "대기"; } /*} else if (holding.Ordered.Equals("대기") && long.Parse(holding.SecondBuyPrice, System.Globalization.NumberStyles.AllowThousands) > currentPrice && holding.BuyCnt == 1) * { * // 2차 매수가 주문 * holding.Ordered = "주문"; * holding.BuyCnt = holding.BuyCnt + 1; * log(LogMode.TRADE, holding.StockName + "(" + holding.StockNo + ") 종목이 2차 매수가에 도달하여 주문 합니다. (" + holding.CurrentPrice + "원)"); * if(!order(ORDER_TYPE_BUY, holding.StockNo, (oneTimeAmount / int.Parse(currentPrice.ToString())), int.Parse(currentPrice.ToString()), ORDER_HOGA_LIIMIT)) * { * holding.BuyCnt = holding.BuyCnt - 1; * } * } else if (holding.Ordered.Equals("대기") && long.Parse(holding.ThirdBuyPrice, System.Globalization.NumberStyles.AllowThousands) > currentPrice && holding.BuyCnt == 2) * { * // 3차 매수가 주문. 기본 2배 물량 * holding.Ordered = "주문"; * holding.BuyCnt = holding.BuyCnt + 1; * log(LogMode.TRADE, holding.StockName + "(" + holding.StockNo + ") 종목이 3차 매수가에 도달하여 주문 합니다. (" + holding.CurrentPrice + "원)"); * if(!order(ORDER_TYPE_BUY, holding.StockNo, (oneTimeAmount / int.Parse(currentPrice.ToString())) * 2, int.Parse(currentPrice.ToString()), ORDER_HOGA_LIIMIT)) * { * holding.Ordered = "대기"; * holding.BuyCnt = holding.BuyCnt - 1; * }*/ } } DetectionStock conditionStock = conditionStocks.SingleOrDefault(item => item.StockNo.Contains(e.sRealKey)); if (conditionStock != null) { long currentPrice = long.Parse(Regex.Replace(kiwoomApi.GetCommRealData(e.sRealKey, 10).Trim(), @"[^0-9]", "")); conditionStock.CurrentPrice = String.Format("{0:#,###0}", currentPrice); //conditionStock.NewTransferPrice = String.Format("{0:#,###0}", currentPrice); long dayHighPrice = long.Parse(Regex.Replace(kiwoomApi.GetCommRealData(e.sRealKey, 17).Trim(), @"[^0-9]", "")); conditionStock.DayHighPrice = String.Format("{0:#,###0}", dayHighPrice); long dayLowPrice = long.Parse(Regex.Replace(kiwoomApi.GetCommRealData(e.sRealKey, 18).Trim(), @"[^0-9]", "")); conditionStock.DayLowPrice = String.Format("{0:#,###0}", dayLowPrice); if (string.IsNullOrEmpty(conditionStock.TransferPrice) || long.Parse(conditionStock.TransferPrice, System.Globalization.NumberStyles.AllowThousands) < 1) { conditionStock.TransferPrice = conditionStock.CurrentPrice; } float fluctuationRate = float.Parse(kiwoomApi.GetCommRealData(e.sRealKey, 12).Trim()); conditionStock.FluctuationRate = String.Format("{0:f2}", fluctuationRate); // 1차 매수가에 도달하면 매수 -> 1차 매수 도달 시 매수 대기했다가 돌파하면 매수하는 방식은? if (currentPrice < oneTimeAmount && !conditionStock.Status.Equals("이탈") && conditionStock.Ordered.Equals("대기") && long.Parse(conditionStock.TargetPrice, System.Globalization.NumberStyles.AllowThousands) > 0 && long.Parse(conditionStock.TargetPrice, System.Globalization.NumberStyles.AllowThousands) > currentPrice && long.Parse(conditionStock.TargetPrice, System.Globalization.NumberStyles.AllowThousands) < long.Parse(conditionStock.TransferPrice, System.Globalization.NumberStyles.AllowThousands)) { /*if (long.Parse(conditionStock.TransferPrice, System.Globalization.NumberStyles.AllowThousands) > long.Parse(conditionStock.NewTransferPrice, System.Globalization.NumberStyles.AllowThousands)) * { * return; * }*/ log(LogMode.TRADE, conditionStock.StockName + "(" + conditionStock.StockNo + ")의 1차 매수가에 도달하여 매수 합니다. (1차 매수가 : " + conditionStock.TargetPrice + ", 진입횟수 : " + conditionStock.TransferCnt + ")"); conditionStock.Ordered = "주문"; //if (!order(ORDER_TYPE_BUY, conditionStock.StockNo, (oneTimeAmount / int.Parse(currentPrice.ToString())), int.Parse(currentPrice.ToString()), ORDER_HOGA_LIIMIT)) // 시장가로 매수 if (!order(ORDER_TYPE_BUY, conditionStock.StockNo, (oneTimeAmount / int.Parse(currentPrice.ToString())), 0, ORDER_HOGA_MARKET)) { conditionStock.Ordered = "대기"; } } } } }