Esempio n. 1
0
        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);
                }
            }
        }
Esempio n. 2
0
        public Form1()
        {
            InitializeComponent();
            RedisConnector.Initialize("127.0.0.1", 6379);

            axKHOpenAPI1.OnEventConnect        += axKHOpenAPI_OnEventConnect;
            axKHOpenAPI1.OnReceiveTrData       += axKHOpenAPI_OnReceiveTrData;
            axKHOpenAPI1.OnReceiveChejanData   += axKHOpenAPI_OnReceiveChejanData;
            axKHOpenAPI1.OnReceiveMsg          += axKHOpenAPI_OnReceiveMsg;
            axKHOpenAPI1.OnReceiveConditionVer += axKHOpenAPI_OnReceiveConditionVer;
            axKHOpenAPI1.OnReceiveTrCondition  += axKHOpenAPI_OnReceiveTrCondition;

            new KHConnector(axKHOpenAPI1);
            new LogUtil(MessageLogBox);

            KHConnector.Instance.Connect();

            // 메인 로직 업데이트
            Thread progressThread = new Thread(Progress);

            progressThread.Start();
            threads.Add(progressThread);

            // 계정 정보(머니, 주식, 등등..) 업데이트
            Thread moneyUpdateThread = new Thread(Account.ReqestToServer);

            moneyUpdateThread.Start();
            threads.Add(moneyUpdateThread);

            // Queue 에 들어있는 것 백그라운드 작업
            Thread sendOrder = new Thread(RequestQueue.SendOrderBackgroundJob);

            sendOrder.Start();
            threads.Add(sendOrder);
        }
Esempio n. 3
0
        public void WriteLastLogs()
        {
            var serializedLogs = RedisConnector.GetString(LastLogsKey, Form1.IsRealServer);

            if (serializedLogs != null)
            {
                var lastLogs = JsonConvert.DeserializeObject <List <Log> >(serializedLogs);
                var logs     = lastLogs.Where(x => Util.DateTimeToTotalSec(x.time) + MaxLogSavingSec > Util.DateTimeToTotalSec(DateTime.Now)).ToList();

                foreach (var log in logs)
                {
                    var logText = MakeLogText(log.time, log.message);
                    WriteToLogBox(logText);
                }
            }
        }
Esempio n. 4
0
        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);
        }
Esempio n. 5
0
        public static void Progress()
        {
            // 여기부터 로직 구현
            //var test = KHConnector.Instance.RequestPERData("1");
            //KHConnector.Instance.RequestUpperLimitList();
            //KHConnector.Instance.GetKospi200Point();

            // 조건식을 가져온다.
            KHConnector.Instance.GetConditionExpression();

            // 최근 로그를 메세지에 쓴다.
            LogUtil.Instance.WriteLastLogs();

            while (true)
            {
                // 내일은 일단 다 팔자
                // TODO : kingkakps 2019/01/15 테스트 판매 코드.
                if (090000 < Util.GetCurrentTimeInt64() && Util.GetCurrentTimeInt64() < 093000)
                {
                    //var serializeInfo = RedisConnector.GetString(HavingStockInfoKey, IsRealServer);
                    //if (serializeInfo != null)
                    //{
                    //    var havingStockInfoList = JsonConvert.DeserializeObject<List<AutoUpdateStockInfo>>(serializeInfo);
                    //    foreach (var info in havingStockInfoList)
                    //    {
                    //        var order = new SendOrder("주식주문", Util.GetScrNum(), 2, info.code, Int32.Parse(info.count), 0, "03", "");
                    //        order.Send();
                    //    }
                    //}
                }


                if (093000 < Util.GetCurrentTimeInt64() && Util.GetCurrentTimeInt64() < 100000)
                {
                    // LongTermBuySetKey 플래그가 켜져있다면
                    if (RedisConnector.IsFlagOn(Form1.LongTermBuySetFlagKey, Form1.IsRealServer))
                    {
                        // 키 리스트 가져와서.
                        var buyCodes = JsonConvert.DeserializeObject <Dictionary <string, BuyStockInfo> >(RedisConnector.GetString(Form1.LongTermBuyListKey, Form1.IsRealServer));

                        foreach (var buyCode in buyCodes.Values)
                        {
                            // 구매하고.
                            var order = new Order("주식주문", Util.GetScrNum(), 1, buyCode.code, buyCode.count, 0, "03", "");
                            order.Send();
                        }

                        // 키 삭제.
                        RedisConnector.KeyDelete(Form1.LongTermBuyListKey, Form1.IsRealServer);

                        // LongTermBuySetKey 플래그를 끈다.
                        RedisConnector.FlagOff(Form1.LongTermBuySetFlagKey, Form1.IsRealServer);
                    }
                }

                // 장 종료될때쯤 주문 잘 안된애들 취소.
                if (160000 < Util.GetCurrentTimeInt64() && Util.GetCurrentTimeInt64() < 161000)
                {
                    {
                        var serializeInfo = RedisConnector.GetString(Form1.NotConcludedBuyKey, Form1.IsRealServer);
                        if (serializeInfo != null)
                        {
                            var NotConcludedBuyInfoList = JsonConvert.DeserializeObject <List <AutoUpdateNotConcludedStockInfo> >(serializeInfo);
                            foreach (var info in NotConcludedBuyInfoList)
                            {
                                var order = new Order("주식주문", Util.GetScrNum(), 3, info.code, int.Parse(info.count), 0, "", info.originalOrderNo);
                                order.Send();
                            }
                        }
                    }
                    {
                        var serializeInfo = RedisConnector.GetString(Form1.NotConcludedSellKey, Form1.IsRealServer);
                        if (serializeInfo != null)
                        {
                            var NotConcludedSellInfoList = JsonConvert.DeserializeObject <List <AutoUpdateNotConcludedStockInfo> >(serializeInfo);
                            foreach (var info in NotConcludedSellInfoList)
                            {
                                var order = new Order("주식주문", Util.GetScrNum(), 4, info.code, int.Parse(info.count), 0, "", info.originalOrderNo);
                                order.Send();
                            }
                        }
                    }
                }

                {// 리스트 뽑아내기
                    if (180000 < Util.GetCurrentTimeInt64() && Util.GetCurrentTimeInt64() < 181000)
                    {
                        var isUnderProfitBuySet = RedisConnector.IsFlagOn(Form1.UnderProfitBuySetFlagKey, Form1.IsRealServer);
                        if (!isUnderProfitBuySet)
                        {
                            var serializeInfo = RedisConnector.GetString(Form1.HavingStockInfoKey, Form1.IsRealServer);
                            if (serializeInfo != null)
                            {
                                var UnderProfitBuyList  = new List <BuyStockInfo>();
                                var havingStockInfoList = JsonConvert.DeserializeObject <List <AutoUpdateStockInfo> >(serializeInfo);
                                foreach (var info in havingStockInfoList)
                                {
                                    if (info.profitPercent < Form1.BuyLossPercent)
                                    {
                                        //var savingStockInfo = new BuyStockInfo(info.code, info.profitPercent, info.price, BuyMoneyLoss/info.price, "");
                                        //UnderProfitBuyList.Add(savingStockInfo);
                                    }
                                    //var savingInfo = JsonConvert.SerializeObject(savingStockInfoList);
                                    //RedisConnector.SetString(HavingStockInfoKey, savingInfo, IsRealServer);
                                }

                                RedisConnector.FlagOn(Form1.UnderProfitBuySetFlagKey, Form1.IsRealServer);
                            }
                        }
                    }

                    if (181000 < Util.GetCurrentTimeInt64() && Util.GetCurrentTimeInt64() < 182000)
                    {
                        var isLongTermBuySet = RedisConnector.IsFlagOn(Form1.LongTermBuySetFlagKey, Form1.IsRealServer);
                        if (!isLongTermBuySet)
                        {
                            var ret = KHConnector.Instance.SendCondition(ConditionName.최근결산PER30, 1, 0);

                            if (ret == 1)
                            {
                                LogUtil.Instance.WriteLog($"{ConditionName.최근결산PER30} 조건식 조건 셋팅 실행...");
                            }
                            else
                            {
                                LogUtil.Instance.WriteLog($"{ConditionName.최근결산PER30} 조건식 조건 셋팅 실행 실패");
                            }
                        }
                    }
                }

                System.Threading.Thread.Sleep(60000);
            }
        }
Esempio n. 6
0
        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);
            }
        }
Esempio n. 7
0
 public void ResetLastLog()
 {
     RedisConnector.KeyDelete(LastLogsKey, Form1.IsRealServer);
 }