[C#/WPF 키움 API] 키움 API_조건 검색 (6)
(1) 조건 검색 다운로드
khAPI.GetConditionLoad(); //조건 검색 리스트 다운로드(프로그램 종료시 삭제 됩니다.)
khAPI.OnReceiveConditionVer += khAPI_OnReceiveConditionVer; //조건 검색 리스트를 다운로드하면 발생 되는 이벤트(해당 이벤트에서 검색식 리스트를 받는다)
//생성자에서 이벤트를 등록 한다.
khAPI.OnReceiveConditionVer += khAPI_OnReceiveConditionVer;
private void khAPI_OnReceiveConditionVer(object sender, AxKHOpenAPILib._DKHOpenAPIEvents_OnReceiveConditionVerEvent e)
{
string strs = khAPI.GetConditionNameList();
if (strs.ExNotNullOrEmpty())
{
List strConditions = strs.ExSplit(";");
if (strConditions.ExNotNull())
{
List rtn = new List();
foreach (string str in strConditions)
{
List items = str.ExSplit("^");
if (items.ExNotNull() && items.Count == 2)
{
KHCondition condition = KHCondition.GetSingInstance(items[0].ExInt());
condition.Name = items[1];
rtn.Add(condition);
}
}
if (OnConditionListHandler.ExNotNull())
{
OnConditionListHandler(rtn);
}
}
}
}
(2) 조건 검색 시작 하기
khAPI.OnReceiveTrCondition += khAPI_OnReceiveTrCondition; //SendCondition실행하면 해당 이벤트에서 결과를 받는다.
khAPI.OnReceiveRealCondition += khAPI_OnReceiveRealCondition; //검색 조건 1일경우 실시간으로 조건에 맞는 종목을 받을수 있다.
khAPI.SendCondition(스크린넘버, 조건 검색명, 조건 검색인덱스, 검색 조건) //리턴: 1 이면 성공, 검색 조건 (0 → 일반 검색, 1 → 실시간 검색, 2 → 연속 검색)
khAPI.SendConditionStop(스크린넘버, 조건 검색명, 조건 검색인덱스);//실시간 조건식이 멈춘다.
KHCtrl.cs
private void khAPI_OnReceiveTrCondition(object sender, _DKHOpenAPIEvents_OnReceiveTrConditionEvent e)
{
ScreenNumber sn = ScreenNumber.GetSingInstance(e.sScrNo);
if (sn.ExNotNull() && sn.OnCompleted.ExNotNull())
sn.OnCompleted(e);
}
private void khAPI_OnReceiveRealCondition(object sender, AxKHOpenAPILib._DKHOpenAPIEvents_OnReceiveRealConditionEvent e)
{
KHCondition condition = KHCondition.GetSingInstance(e.strConditionIndex.ExInt());
if (condition.ExNotNull())
{
condition.SetRealTimeData(e.strType , e.sTrCode );
}
}
public bool RunRealCondition(KHCondition condition)
{
ScreenNumber sn = ScreenNumber.GetNext();
sn.OnCompleted = (p) =>
{
if (p is _DKHOpenAPIEvents_OnReceiveTrConditionEvent)
{
_DKHOpenAPIEvents_OnReceiveTrConditionEvent e = p as _DKHOpenAPIEvents_OnReceiveTrConditionEvent;
if (condition.ExNotNull())
{
condition.SetRealTimeData("I", e.strCodeList );
}
}
};
//검색 조건 (0 → 일반 검색, 1 → 실시간 검색, 2 → 연속 검색)
condition.RealTime = khAPI.SendCondition(sn.No, condition.Name, condition.Index, 1) == 1;
return condition.RealTime;
}
public void StopRealCondition(KHCondition condition)
{
ScreenNumber sn = ScreenNumber.GetNext();
khAPI.SendConditionStop(sn.No, condition.Name, condition.Index);
condition.RealTime = false;
}
KHCondition.cs
public void SetRealTimeData(string type, string str)
{
lock (lockGetFindList)
{
if (str.ExNotNullOrEmpty())
{
List<string> codeList = str.ExSplit(";");
foreach (string code in codeList)
{
KHStock stock = KHStock.Find(code);
if (stock.ExNotNull())
{
if (StockList.Count(x => x.Code == code) > 0)
{
if (type == "D")
{
StockList.Remove(stock);
}
}
else
{
if (type == "I")
{
StockList.Add(stock);
}
}
StringBuilder sb = new StringBuilder();
sb.AppendLine("========= 조건조회 실시간 편입/이탈 ==========");
sb.AppendLine("[종목코드] : " + code);
sb.AppendLine("[종목명] : " + stock.Name);
sb.AppendLine("[실시간타입] : " + type);
sb.AppendLine("[조건명] : " + Name);
KHLog.Instance.Write(sb.ToString());
}
}
if (OnRealTimeData.ExNotNull())
{
OnRealTimeData(type, codeList);
}
}
}
}