본문 바로가기
KHOpenAPI

[KHOpenAPI] 조회횟수 제한

by Falto 2023. 10. 8.

3초 간격으로 조회 요청을 반복한 결과 1001번째 조회 요청에서 조회가 차단되었음을 알 수 있다. 총 테스트 소요 시간은 약 50분이다.

또한 0.6초 간격으로 반복해도 1001번째 조회에서 차단된다.

테스트 프로그램의 소스 코드는 아래와 같다. C#이며 .NET Framework 4.8 32bit Console Application으로 작성되었다.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using AxKHOpenAPILib;

namespace KiwoomDelayTester
{
    internal static class Program
    {

        static AxKHOpenAPI api;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            api = new AxKHOpenAPI();
            new Control().Controls.Add(api);
            Console.WriteLine(api.AccessibilityObject);
            api.EndInit();
            api.OnEventConnect += Api_OnEventConnect;
            api.CommConnect();
            Application.Run();
        }


        private static void Api_OnEventConnect(object sender, _DKHOpenAPIEvents_OnEventConnectEvent e)
        {
            if (e.nErrCode == 0)
            {
                Console.WriteLine("Enter the delay in milliseconds:");
                int delay = int.Parse(Console.ReadLine());
                if(delay < 200) throw new Exception("Delay must be greater than 200ms");
                Stopwatch sw = Stopwatch.StartNew();

                for(uint count = 0;count < 10000; count++)
                {
                    api.SetInputValue("종목코드", "005930");
                    int i = api.CommRqData("test", "opt10001", 0, "1000");
                    if(i != 0)
                    {
                        Console.WriteLine("The error code is {0}", i);
                        Console.WriteLine("Press any key to continue...");
                        Console.ReadKey();
                        api.Dispose();
                        Application.Exit();
                        return;
                    }
                    else
                    {
                        long elapsed = sw.ElapsedMilliseconds;
                        Console.WriteLine("elms: {0}, count: {1}, average delay: {2}", elapsed, count, (elapsed+delay)/(count+1));
                        Thread.Sleep(delay);
                    }
                }
            }
        }
    }
}

댓글