using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows.Forms; namespace upper_limit_crawler { public partial class ULMonitorDlg : Form { const int MONITOR_DELAY = 15000; ULDataMgr m_DataMgr; CPSYSDIBLib.CpSvrNew7043 m_7043 = null; Timer m_Timer = new Timer(); int m_iMonitorDelay = 0; int m_iLastTime = 0; bool m_bRequesting = false; public ULMonitorDlg(ULDataMgr DataMgr) { InitializeComponent(); MainForm.SetDoubleBuffered(lvWatch); this.TopLevel = false; m_DataMgr = DataMgr; m_Timer.Interval = 100; m_Timer.Tick += Refresh; } public void Start() { m_7043 = new CPSYSDIBLib.CpSvrNew7043(); m_7043.SetInputValue(0, '0'); m_7043.SetInputValue(1, '2'); m_7043.SetInputValue(2, '1'); m_7043.SetInputValue(3, 21); m_7043.SetInputValue(4, '1'); m_7043.SetInputValue(5, '0'); m_7043.SetInputValue(6, '0'); m_7043.SetInputValue(7, (short)(m_DataMgr.m_Setting.m_fSearchMin * 100)); m_7043.SetInputValue(8, (short)((m_DataMgr.m_Setting.m_fSearchMax + 0.009) * 100)); RefreshMonitorItem(); m_Timer.Start(); } public void Stop() { m_Timer.Stop(); } bool RefreshMonitorItem() { if (m_bRequesting == true || ULUtil.GetLimitRemainCountRQ() < 30) return false; m_bRequesting = true; m_7043.SetInputValue(7, (short)(m_DataMgr.m_Setting.m_fSearchMin * 100)); m_7043.SetInputValue(8, (short)((m_DataMgr.m_Setting.m_fSearchMax + 0.009) * 100)); int iResult = m_7043.BlockRequest2(1); lvWatch.Items.Clear(); int iIdx = 1; bool bContinue = true; while (bContinue) { int iCnt = m_7043.GetHeaderValue(0); for (int i = 0; i < iCnt; i++) { string strCode = m_7043.GetDataValue(0, i); string strCodeName = m_7043.GetDataValue(1, i); int iCurPrice = m_7043.GetDataValue(2, i); object flag = m_7043.GetDataValue(3, i); int iComp = m_7043.GetDataValue(4, i); float fCompRate = m_7043.GetDataValue(5, i); int iVolume = m_7043.GetDataValue(6, i); int iStartPrice = m_7043.GetDataValue(7, i); int iStartPriceComp = m_7043.GetDataValue(8, i); float fStartPriceCompRate = m_7043.GetDataValue(9, i); int iSerialDay = m_7043.GetDataValue(10, i); ListViewItem searchItem = null; if(lvWatch.Items.Count > 0) searchItem = lvWatch.FindItemWithText(strCode, true, 0, false); if(searchItem != null) { searchItem.SubItems[chOrder.Index].Text = iIdx.ToString(); searchItem.SubItems[chCurPrice.Index].Text = iCurPrice.ToString("###,###,###,###"); searchItem.SubItems[chComp.Index].Text = iComp.ToString("###,###,###,###"); searchItem.SubItems[chCompRate.Index].Text = fCompRate.ToString("#,##0.00"); searchItem.SubItems[chVolume.Index].Text = iVolume.ToString("###,###,###,###"); searchItem.SubItems[chPrevClosing.Index].Text = (iCurPrice - iComp).ToString("###,###,###,###"); } else { ListViewItem listViewItem = new ListViewItem(new string[] { iIdx.ToString(), strCode.ToString(), strCodeName.ToString(), iCurPrice.ToString("###,###,###,###"), iComp.ToString("###,###,###,###"), fCompRate.ToString("#,##0.00")+"%", iVolume.ToString("###,###,###,###"), 0.ToString(), 0.ToString(), (iCurPrice - iComp).ToString("###,###,###,###") }); lvWatch.Items.Add(listViewItem); } iIdx++; if (fCompRate >= m_DataMgr.m_Setting.m_fSearchMin * 100 && fCompRate <= m_DataMgr.m_Setting.m_fSearchMax * 100) { m_DataMgr.AddWatch(strCode, strCodeName, iCurPrice, iCurPrice-iComp); } } bContinue = (m_7043.Continue == 1); if (bContinue == true && ULUtil.GetLimitRemainCountRQ() >= 30) iResult = m_7043.BlockRequest2(1); } //foreach (ColumnHeader col in lvWatch.Columns) // col.Width = -2; m_bRequesting = false; return true; } void RefreshData() { Dictionary WatchList = m_DataMgr.GetWatchList(); foreach (ListViewItem lvItem in lvWatch.Items) { string strCode = lvItem.SubItems[chCode.Index].Text; if (WatchList.ContainsKey(strCode) == true) { ULWatchItem item = WatchList[strCode]; int iDisplayCurPrice = 0; if (lvItem.SubItems[chCurPrice.Index].Text.Length > 0) iDisplayCurPrice = int.Parse(Regex.Replace(lvItem.SubItems[chCurPrice.Index].Text, @"\D", "")); if (item.m_iCurPrice != iDisplayCurPrice && item.m_iCurPrice != 0) { int iComp = item.m_iCurPrice - item.m_iPrevClosing; float fCompRate = item.m_iCurPrice / (float)item.m_iPrevClosing - 1.0f; lvItem.SubItems[chCurPrice.Index].Text = item.m_iCurPrice.ToString("###,###,###,###"); lvItem.SubItems[chComp.Index].Text = iComp.ToString("###,###,###,###"); lvItem.SubItems[chCompRate.Index].Text = fCompRate.ToString("#,##0.00%") + "%"; lvItem.SubItems[chVolume.Index].Text = item.m_iVolume.ToString("###,###,###,###"); lvItem.SubItems[chBidPrice.Index].Text = item.m_iBidPrice.ToString("###,###,###,###"); if (fCompRate >= m_DataMgr.m_Setting.m_fBidMin && fCompRate <= m_DataMgr.m_Setting.m_fBidMax) { int iTime = ULUtil.GetCurTimeInt(); if (m_DataMgr.IsInBlackList(iTime, strCode) == true) { //ULUtil.Trace("[{0}] 조건 매수 실패. black list {1}원 ({2})", item.m_strCodeName, item.m_iCurPrice, fCompRate.ToString("0.00%")); continue; } float f5MASlope = item.GetPrev5MASlope(iTime); if (f5MASlope <= 0) { //ULUtil.Trace("[{0}] 조건 매수 실패. 5ma 상승하지 않음 {1}원 ({2})", item.m_strCodeName, item.m_iCurPrice, fCompRate.ToString("0.00%")); continue; } // bid and add to black list m_DataMgr.GetTrader().Buy(strCode, item.m_iCurPrice, (int)m_DataMgr.m_Setting.m_fBidAmount); m_DataMgr.AddBlackList(iTime, strCode); ULUtil.Trace("[{0}] 조건 매수 {1}원 ({2}) (5MA slop:{3:0.00})", item.m_strCodeName, item.m_iCurPrice, fCompRate.ToString("0.00%"), f5MASlope); ULUtil.TraceCSV("조건 매수", item.m_strCodeName, item.m_iCurPrice, fCompRate.ToString("0.00%"), f5MASlope); } } } } } private void Refresh(object sender, EventArgs e) { int iCurTime = Environment.TickCount; if (m_iLastTime == 0) { m_iLastTime = iCurTime; return; } int iDeltaT = iCurTime - m_iLastTime; m_iMonitorDelay -= iDeltaT; if (m_iMonitorDelay <= 0) { bool bRequest = RefreshMonitorItem(); m_iMonitorDelay = (bRequest == true) ? MONITOR_DELAY : 1000; } RefreshData(); m_iLastTime = iCurTime; } } }