웹스쿨

키움API - 일자별실현손익요청 본문

개인 프로젝트/C# 키움 주식 자동매매

키움API - 일자별실현손익요청

마스터욱 2023. 3. 30. 01:52
반응형
호출함수
  1. private void requestProfitInfo()
  2. {
  3.     fparent.axKHOpenAPI1.SetInputValue("계좌번호", currentAccount);
  4.     fparent.axKHOpenAPI1.SetInputValue("시작일자", DateTime.Now.ToString("yyyyMMdd"));
  5.     fparent.axKHOpenAPI1.SetInputValue("종료일자", DateTime.Now.ToString("yyyyMMdd"));
  6.  
  7.     int result = fparent.axKHOpenAPI1.CommRqData("일자별실현손익요청""opt10074", 0, GetScrNum());
  8. }

"일자별실현손익요청" 으로 호출하여 OnReceiveTrData 에서 callBack 데이터를 받습니다.

아래는 CommRqData 원형입니다.

 

          [CommRqData() 함수]

          

          CommRqData(

          BSTR sRQName,    // 사용자 구분명

          BSTR sTrCode,    // 조회하려는 TR이름

          long nPrevNext,  // 연속조회여부

          BSTR sScreenNo  // 화면번호

          )

          

          조회요청함수이며 빈번하게 조회요청하면 시세과부하 에러값으로 -200이 전달됩니다.

          리턴값

          0이면 조회요청 정상 나머지는 에러

          -200 시세과부하

          -201 조회전문작성 에러

 

 

OnReceiveTrData 의 조건부분 

 

  1. else if (e.sRQName == "일자별실현손익요청")
  2. {
  3.     //string.Format("{0:n0}", row.Value.매수가격);
  4.     /*
  5.     string temp1 = fparent.axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, 0, "총매입금액").TrimStart('0');
  6.    if (!temp1.Equals(""))
  7.     {
  8.         totalBuyTextView.Text = string.Format("{0:n0}"double.Parse(temp1));
  9.     }
  10.  
  11.     string temp2 = fparent.axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, 0, "총평가금액").TrimStart('0');
  12.    if (!temp2.Equals(""))
  13.     {
  14.         totalEstimateTextView.Text = string.Format("{0:n0}"double.Parse(temp2));
  15.         if (double.Parse(temp2) > double.Parse(temp1))
  16.         {
  17.             totalEstimateTextView.ForeColor = Color.Red;
  18.         }
  19.         else
  20.         {
  21.             totalEstimateTextView.ForeColor = Color.Blue;
  22.         }
  23.     }
  24.  
  25.     string temp3 = fparent.axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, 0, "총평가손익금액");
  26.     if (!temp3.Equals(""))
  27.     {
  28.         totalProfitTextView.Text = string.Format("{0:n0}"double.Parse(temp3));
  29.         if (double.Parse(temp3) > 0)
  30.         {
  31.             totalProfitTextView.ForeColor = Color.Red;
  32.         }
  33.         else
  34.         {
  35.             totalProfitTextView.ForeColor = Color.Blue;
  36.         }
  37.     }
  38.  
  39.     string temp4 = fparent.axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, 0, "총수익률(%)").TrimStart('0');
  40.    if (!temp4.Equals(""))
  41.     {
  42.         totalProfitRateTextView.Text = double.Parse(temp4).ToString();
  43.         if (double.Parse(temp4) > 0)
  44.         {
  45.             totalProfitRateTextView.ForeColor = Color.Red;
  46.         }
  47.         else
  48.         {
  49.             totalProfitRateTextView.ForeColor = Color.Blue;
  50.         }
  51.     }
  52.     */
  53.  
  54.     string temp = fparent.axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, 0, "총매수금액").TrimStart('0');
  55.    if (!temp.Equals(""))
  56.     {
  57.         totalBuyTextView.Text = string.Format("{0:n0}"double.Parse(temp));
  58.     }
  59.  
  60.     temp = fparent.axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, 0, "총매도금액").TrimStart('0');
  61.    if (!temp.Equals(""))
  62.     {
  63.         totalEstimateTextView.Text = string.Format("{0:n0}"double.Parse(temp));
  64.     }
  65.  
  66.     temp = fparent.axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, 0, "실현손익").Trim();
  67.     if (string.IsNullOrEmpty(temp) == false)
  68.     {
  69.         Console.WriteLine("실현손익 = " + temp);
  70.         Double 실현손익 = double.Parse(temp);
  71.         totalProfitTextView.Text = string.Format("{0:n0}", 실현손익);
  72.         if (실현손익 > 0)
  73.         {
  74.             totalProfitTextView.ForeColor = Color.Red;
  75.         }
  76.         else if (실현손익 < 0)
  77.         {
  78.             totalProfitTextView.ForeColor = Color.Blue;
  79.         }
  80.         else
  81.         {
  82.             totalProfitTextView.ForeColor = Color.Black;
  83.         }
  84.     }
  85.  
  86.     temp = fparent.axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, 0, "매매수수료").TrimStart('0');
  87.    if (!temp.Equals(""))
  88.     {
  89.         totalProfitRateTextView.Text = string.Format("{0:n0}"double.Parse(temp));
  90.     }
  91.  
  92.     temp = fparent.axKHOpenAPI1.GetCommData(e.sTrCode, e.sRQName, 0, "매매세금").TrimStart('0');
  93.    if (!temp.Equals(""))
  94.     {
  95.         realProfitTextView.Text = string.Format("{0:n0}"double.Parse(temp));
  96.     }
  97. }

 

GetCommData 함수로 각 정보들을 호출합니다.

아래는 GetCommData 함수의 원형입니다.

 

          [GetCommData() 함수]

          

          GetCommData(

          BSTR strTrCode,   // TR 이름

          BSTR strRecordName,   // 레코드이름

          long nIndex,      // TR반복부

          BSTR strItemName) // TR에서 얻어오려는 출력항목이름

          

          OnReceiveTRData()이벤트 함수가 호출될때 조회데이터를 얻어오는 함수입니다.

          이 함수는 반드시 OnReceiveTRData()이벤트 함수가 호출될때 그 안에서 사용해야 합니다.

          

          ------------------------------------------------------------------------------------------------------------------------------------

          

          [OPT10081 : 주식일봉차트조회요청예시]

          

          OnReceiveTrDataKhopenapictrl(...)

          {

            if(strRQName == _T("주식일봉차트"))

            {

              int nCnt = OpenAPI.GetRepeatCnt(sTrcode, strRQName);

              for (int nIdx = 0; nIdx < nCnt; nIdx++)

              {

                strData = OpenAPI.GetCommData(sTrcode, strRQName, nIdx, _T("종목코드"));   strData.Trim();

                strData = OpenAPI.GetCommData(sTrcode, strRQName, nIdx, _T("거래량"));   strData.Trim();

                strData = OpenAPI.GetCommData(sTrcode, strRQName, nIdx, _T("시가"));   strData.Trim();

                strData = OpenAPI.GetCommData(sTrcode, strRQName, nIdx, _T("고가"));   strData.Trim();

                strData = OpenAPI.GetCommData(sTrcode, strRQName, nIdx, _T("저가"));   strData.Trim();

                strData = OpenAPI.GetCommData(sTrcode, strRQName, nIdx, _T("현재가"));   strData.Trim();

              }

            }

          }

          

          ------------------------------------------------------------------------------------------------------------------------------------

 

결과샘플(모의투자입니다.)

 


이 게시글은
https://webschool.kr/?v=board_view&board_key=34&idx=573
에서 작성한 글입니다. 소스코드의 경우 해당 블로그에서 이뿌게 노출이 되지 않을 수 있사오니, 위 링크로 들어오셔서 보시길 바랍니다.

반응형