From 5714cb33886d62ad61467dd7dfebde28bc89a7d5 Mon Sep 17 00:00:00 2001 From: mjjo Date: Tue, 9 Aug 2016 14:42:50 +0900 Subject: [PATCH] =?UTF-8?q?=EA=B0=9C=EB=B0=9C=EC=A4=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MainForm.Designer.cs | 31 ++++-- MainForm.cs | 93 ++++++++++++++---- ULBalanceDlg.cs | 224 ++++++++++++++++++++++++++----------------- ULDataMgr.cs | 78 ++++++++++----- ULMonitorDlg.cs | 12 +-- ULTrader.cs | 77 ++++++--------- UlUtil.cs | 15 +++ 7 files changed, 336 insertions(+), 194 deletions(-) diff --git a/MainForm.Designer.cs b/MainForm.Designer.cs index 20bf970..5e22dee 100644 --- a/MainForm.Designer.cs +++ b/MainForm.Designer.cs @@ -50,6 +50,7 @@ this.splitContainer1 = new System.Windows.Forms.SplitContainer(); this.splitContainer2 = new System.Windows.Forms.SplitContainer(); this.splitContainer3 = new System.Windows.Forms.SplitContainer(); + this.btLogDump = new System.Windows.Forms.Button(); this.btMATest = new System.Windows.Forms.Button(); this.btTimeSync = new System.Windows.Forms.Button(); this.cbStart = new System.Windows.Forms.CheckBox(); @@ -62,7 +63,7 @@ this.statusBar1 = new System.Windows.Forms.StatusBar(); this.statusBarPanel1 = new System.Windows.Forms.StatusBarPanel(); this.statusBarPanel2 = new System.Windows.Forms.StatusBarPanel(); - this.btLogDump = new System.Windows.Forms.Button(); + this.button1 = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit(); this.splitContainer1.Panel1.SuspendLayout(); this.splitContainer1.Panel2.SuspendLayout(); @@ -288,6 +289,7 @@ // // splitContainer2.Panel2 // + this.splitContainer2.Panel2.Controls.Add(this.button1); this.splitContainer2.Panel2.Controls.Add(this.btLogDump); this.splitContainer2.Panel2.Controls.Add(this.btMATest); this.splitContainer2.Panel2.Controls.Add(this.btTimeSync); @@ -330,6 +332,16 @@ this.splitContainer3.SplitterDistance = 256; this.splitContainer3.TabIndex = 0; // + // btLogDump + // + this.btLogDump.Location = new System.Drawing.Point(21, 555); + this.btLogDump.Name = "btLogDump"; + this.btLogDump.Size = new System.Drawing.Size(75, 35); + this.btLogDump.TabIndex = 25; + this.btLogDump.Text = "Dump Log"; + this.btLogDump.UseVisualStyleBackColor = true; + this.btLogDump.Click += new System.EventHandler(this.btLogDump_Click); + // // btMATest // this.btMATest.Location = new System.Drawing.Point(122, 421); @@ -443,15 +455,15 @@ this.statusBarPanel2.Name = "statusBarPanel2"; this.statusBarPanel2.Width = 120; // - // btLogDump + // button1 // - this.btLogDump.Location = new System.Drawing.Point(22, 555); - this.btLogDump.Name = "btLogDump"; - this.btLogDump.Size = new System.Drawing.Size(75, 23); - this.btLogDump.TabIndex = 25; - this.btLogDump.Text = "Dump Log"; - this.btLogDump.UseVisualStyleBackColor = true; - this.btLogDump.Click += new System.EventHandler(this.btLogDump_Click); + this.button1.Location = new System.Drawing.Point(118, 555); + this.button1.Name = "button1"; + this.button1.Size = new System.Drawing.Size(75, 35); + this.button1.TabIndex = 25; + this.button1.Text = "Dump Simulation"; + this.button1.UseVisualStyleBackColor = true; + this.button1.Click += new System.EventHandler(this.button1_Click); // // MainForm // @@ -516,6 +528,7 @@ private System.Windows.Forms.Label lbCacheBalance; private System.Windows.Forms.Label lbEvalProfit; private System.Windows.Forms.Button btLogDump; + private System.Windows.Forms.Button button1; } } diff --git a/MainForm.cs b/MainForm.cs index c71645c..eba80ca 100644 --- a/MainForm.cs +++ b/MainForm.cs @@ -4,6 +4,7 @@ using System.Collections; using System.Reflection; using System.Diagnostics; using System.Collections.Generic; +using System.Threading; namespace upper_limit_crawler { @@ -23,7 +24,7 @@ namespace upper_limit_crawler { ArrayList m_TraceList = new ArrayList(); - Timer m_MainTimer = new Timer(); + System.Windows.Forms.Timer m_MainTimer = new System.Windows.Forms.Timer(); int m_iLastTime = 0; static ULDataMgr m_DataMgr = new ULDataMgr(); @@ -233,20 +234,71 @@ namespace upper_limit_crawler List aCodeList = new List { - "064240", - "090410", - "208860", - "044180", - "127160", - "123570", +"A064240", +"A090410", +"A208860", +"A044180", +"A127160", +"A123570", +"A039420", +"A015020", +"A090850", +"A007660", +"A017180", +"A036810", +"A049950", +"A044480", +"A047310", +"A115610", +"A086060", +"A118000", +"A037400", +"A027580", +"A100090", +"A103130", +"A032790", +"A226350", +"A024800", +"A002630", +"A109740", +"A077970", +"A040350", +"A092870", +"A001140", +"A065130", +"A043340", +"A038010", +"A099410", +"A115440", +"A115960", +"A098120", +"A004380", +"A007120", +"A025820", +"A005980", +"A011810", +"A131100", +"A059090", +"A083470", +"A051170", +"A096350", +"A131400", +"A122800", +"A043910", +"A089590", +"A067990", +"A091970", +"A058450", +"A104040", +"A003070", }; - string strPrevDate = "20160729"; - string strDate = "20160801"; + string strPrevDate = "20160801"; + string strDate = "20160802"; int iTotalProfit = 0; foreach (string code in aCodeList) { - string strCode = "A" + code; + string strCode = code[0] == 'A' ? code : "A"+code; string strCodeName = ULUtil.GetCodeName(strCode); Console.WriteLine("{0}({1})", strCodeName, strCode); @@ -263,7 +315,7 @@ namespace upper_limit_crawler stockChart.SetInputValue(4, 1); stockChart.SetInputValue(5, new int[] { 0, 1, 2, 3, 4, 5, 6, 10, 37 }); stockChart.SetInputValue(6, 'T'); - stockChart.BlockRequest2(1); + ULUtil.BlockRequest(stockChart); int iTimeTest = (int)stockChart.GetDataValue(1, 0); int iPrevClosing = stockChart.GetDataValue(5, 0); @@ -274,7 +326,7 @@ namespace upper_limit_crawler stockChart.SetInputValue(4, 0); stockChart.SetInputValue(5, new int[] { 0, 1, 2, 3, 4, 5, 6, 10, 37 }); stockChart.SetInputValue(6, 'T'); - stockChart.BlockRequest2(1); + ULUtil.BlockRequest(stockChart); int iFieldCnt = stockChart.GetHeaderValue(1); string[] astrFieldName = stockChart.GetHeaderValue(2); @@ -293,8 +345,8 @@ namespace upper_limit_crawler DataList.Add(new chart_data(iTime, iCurPrice)); } - if (stockChart.Continue == 1) - stockChart.BlockRequest2(1); + if(stockChart.Continue == 1) + ULUtil.BlockRequest(stockChart); else break; } @@ -352,7 +404,7 @@ namespace upper_limit_crawler Console.WriteLine("[{0}] [{1}] 조건 매수 {2}원 ({3}) (5MA slop:{4})", iTime, WatchItem.m_strCodeName, WatchItem.m_iCurPrice, fCompRate.ToString("0.00%"), f5MASlope.ToString("0.00%")); ULUtil.TraceCSV(iTime, "[시뮬레이션] 조건 매수", WatchItem.m_strCodeName, WatchItem.m_iCurPrice, fCompRate.ToString("0.00%"), f5MASlope.ToString("0.00%")); - m_DataMgr.AddBidLog(strCode, WatchItem.m_strCodeName, iTime, WatchItem.m_iCurPrice, f5MASlope); + m_DataMgr.AddBidLog(strCode, WatchItem.m_strCodeName, iTime, WatchItem.m_iCurPrice, f5MASlope, false); int iBidCnt = (int)m_DataMgr.m_Setting.m_fBidAmount / iCurPrice; @@ -379,7 +431,7 @@ namespace upper_limit_crawler Console.WriteLine("[{0}] [{1}] 손절 {2}원 ({3}:{4})", iTime, OwnItem.m_strCodeName, iCurPrice, iProfit, fProfitRate.ToString("0.00%")); ULUtil.TraceCSV(iTime, "[시뮬레이션] 손절", OwnItem.m_strCodeName, iCurPrice, fProfitRate.ToString("0.00%")); - m_DataMgr.AddAskLog(strCode, WatchItem.m_strCodeName, iTime, iCurPrice, float.NaN, true, iProfit, fProfitRate); + m_DataMgr.AddAskLog(strCode, WatchItem.m_strCodeName, iTime, iCurPrice, float.NaN, true, iProfit, fProfitRate, false); iTotalAskPrice += iCurPrice * OwnItem.m_iConclusionBalanceCnt; OwnItem.m_iUnitBEP = 0; @@ -408,7 +460,7 @@ namespace upper_limit_crawler Console.WriteLine("[{0}] [{1}] 트레일링 매도 {2}원 ({3}:{4}) (5MA slop:{5})", iTime, OwnItem.m_strCodeName, iCurPrice, iProfit, fProfitRate.ToString("0.00%"), f5MASlope.ToString("0.00%")); ULUtil.TraceCSV(iTime, "[시뮬레이션] 트레일링 매도", OwnItem.m_strCodeName, iCurPrice, iProfit, fProfitRate.ToString("0.00%"), f5MASlope.ToString("0.00%")); - m_DataMgr.AddAskLog(strCode, WatchItem.m_strCodeName, iTime, iCurPrice, f5MASlope, false, iProfit, fProfitRate); + m_DataMgr.AddAskLog(strCode, WatchItem.m_strCodeName, iTime, iCurPrice, f5MASlope, false, iProfit, fProfitRate, false); iTotalAskPrice += iCurPrice * OwnItem.m_iConclusionBalanceCnt; OwnItem.m_iUnitBEP = 0; @@ -462,7 +514,12 @@ namespace upper_limit_crawler private void btLogDump_Click(object sender, EventArgs e) { - m_DataMgr.DumpLog(); + m_DataMgr.DumpLog(true); + } + + private void button1_Click(object sender, EventArgs e) + { + m_DataMgr.DumpLog(false); } } } diff --git a/ULBalanceDlg.cs b/ULBalanceDlg.cs index 79b45f2..a930a1d 100644 --- a/ULBalanceDlg.cs +++ b/ULBalanceDlg.cs @@ -38,7 +38,7 @@ namespace upper_limit_crawler List m_OwnList = new List(); - Timer m_MainTimer = new Timer(); + System.Windows.Forms.Timer m_MainTimer = new System.Windows.Forms.Timer(); int m_iLastTime = 0; int m_iBalanceDelay = BALANCE_DELAY; @@ -81,7 +81,7 @@ namespace upper_limit_crawler { m_Td6033.SetInputValue(0, m_DataMgr.GetAccount()); m_Td6033.SetInputValue(2, 50); - m_Td6033.BlockRequest2(1); + ULUtil.BlockRequest(m_Td6033); //object[] datalist = new object[] @@ -177,98 +177,143 @@ namespace upper_limit_crawler private void RefreshCurPrice() { Dictionary WatchList = m_DataMgr.GetWatchList(); - foreach (ListViewItem lvItem in lvBalance.Items) + + foreach(KeyValuePair WatchPair in WatchList) { - string strCode = lvItem.SubItems[chCode.Index].Text; - int iTime = ULUtil.GetCurTimeInt(); + string strCode = WatchPair.Key; + ULWatchItem WatchItem = WatchPair.Value; + OWN_ITEM OwnItem = m_OwnList.Find(itemFind => itemFind.m_strCode == strCode); + if(OwnItem == null) + continue; - if (WatchList.ContainsKey(strCode) == true) + int iTime = WatchItem.m_iCurTime; + int iCurPrice = WatchItem.m_iCurPrice; + int iComp = iCurPrice - (int)OwnItem.m_iUnitBEP; // or OwnItem.m_dConclusionUnitPrice + + OwnItem.m_iCurPrice = iCurPrice; + OwnItem.m_iEvaluationPrice = iCurPrice * OwnItem.m_iConclusionBalanceCnt; + OwnItem.m_iEvaluationProfit = iComp * OwnItem.m_iConclusionBalanceCnt; + OwnItem.m_dProfitRate = iComp / (float)OwnItem.m_iUnitBEP; + OwnItem.m_iMaxPrice = Math.Max(iCurPrice, OwnItem.m_iMaxPrice); + + + // loss cut + if(iCurPrice <= OwnItem.m_iUnitBEP * (1.0f - m_DataMgr.m_Setting.m_fLossCut)) { - ULWatchItem WatchItem = WatchList[strCode]; - OWN_ITEM OwnItem = m_OwnList.Find(itemFind => itemFind.m_strCode == strCode); - if (OwnItem == null) - continue; + m_DataMgr.GetTrader().SellCurPrice(OwnItem.m_strCode, OwnItem.m_iPayBalance); + m_OwnList.Remove(OwnItem); + m_DataMgr.RemoveWatch(strCode); + m_DataMgr.AddLosscutItem(strCode); - int iDisplayCurPrice = 0; - if (lvItem.SubItems[chCurPrice.Index].Text.Length > 0) - iDisplayCurPrice = int.Parse(Regex.Replace(lvItem.SubItems[chCurPrice.Index].Text, @"\D", "")); + int iProfit = iCurPrice-(int)OwnItem.m_iUnitBEP; + float fProfitRate = iCurPrice/(float)OwnItem.m_iUnitBEP-1.0f; - if (WatchItem.m_iCurPrice != iDisplayCurPrice) + ULUtil.Trace("[{0}] 손절 {1}원 ({2}) {3}", OwnItem.m_strCodeName, iCurPrice, fProfitRate.ToString("0.00%"), WatchItem.m_iHighestPrice); + ULUtil.TraceCSV("손절", OwnItem.m_strCodeName, iCurPrice, fProfitRate.ToString("0.00%"), WatchItem.m_iHighestPrice); + m_DataMgr.AddAskLog(strCode, WatchItem.m_strCodeName, iTime, iCurPrice, float.NaN, true, iProfit, fProfitRate, true); + + // own에서 삭제하고 미체결 리스트에 넣고, watch에서도 뺀다 + + // 미체결 잔량 취소 + } + // trailing + else if(iCurPrice <= OwnItem.m_iMaxPrice - OwnItem.m_iUnitBEP * m_DataMgr.m_Setting.m_fTrailing) + { + float f5MASlope = WatchItem.GetPrev5MASlope(iTime); + if(float.IsNaN(f5MASlope) || f5MASlope > -0.0100f) { - int iCurPrice = WatchItem.m_iCurPrice; - int iComp = iCurPrice - (int)OwnItem.m_iUnitBEP; // or OwnItem.m_dConclusionUnitPrice - - OwnItem.m_iCurPrice = iCurPrice; - OwnItem.m_iEvaluationPrice = iCurPrice * OwnItem.m_iConclusionBalanceCnt; - OwnItem.m_iEvaluationProfit = iComp * OwnItem.m_iConclusionBalanceCnt; - OwnItem.m_dProfitRate = iComp / (float)OwnItem.m_iUnitBEP; - - lvItem.SubItems[chCurPrice.Index].Text = iCurPrice.ToString("###,###,##0"); - lvItem.SubItems[chEvaluationPrice.Index].Text = OwnItem.m_iEvaluationPrice.ToString("###,###,##0"); - lvItem.SubItems[chEvaluationProfit.Index].Text = OwnItem.m_iEvaluationProfit.ToString("###,###,##0"); - if (OwnItem.m_iEvaluationProfit > 0) - lvItem.SubItems[chEvaluationProfit.Index].ForeColor = Color.Red; - else - lvItem.SubItems[chEvaluationProfit.Index].ForeColor = Color.Blue; - lvItem.SubItems[chEvaluationPrice.Index].Text = OwnItem.m_iEvaluationPrice.ToString("###,###,##0"); - lvItem.SubItems[chProfitRate.Index].Text = OwnItem.m_dProfitRate.ToString("#,##0.00%"); - lvItem.SubItems[chHighestPrice.Index].Text = WatchItem.m_iHighestPrice.ToString("#,##0.00%"); - if (OwnItem.m_dProfitRate > 0) - lvItem.SubItems[chProfitRate.Index].ForeColor = Color.Red; - else - lvItem.SubItems[chProfitRate.Index].ForeColor = Color.Blue; - - - OwnItem.m_iMaxPrice = Math.Max(iCurPrice, OwnItem.m_iMaxPrice); - - // loss cut - if (iCurPrice <= OwnItem.m_iUnitBEP * (1.0f - m_DataMgr.m_Setting.m_fLossCut)) - { - m_DataMgr.GetTrader().SellCurPrice(OwnItem.m_strCode, OwnItem.m_iPayBalance); - m_OwnList.Remove(OwnItem); - m_DataMgr.RemoveWatch(strCode); - m_DataMgr.AddLosscutItem(strCode); - - int iProfit = iCurPrice-(int)OwnItem.m_iUnitBEP; - float fProfitRate = iCurPrice/(float)OwnItem.m_iUnitBEP-1.0f; - - ULUtil.Trace("[{0}] 손절 {1}원 ({2}) {3}", OwnItem.m_strCodeName, iCurPrice, fProfitRate.ToString("0.00%"), WatchItem.m_iHighestPrice); - ULUtil.TraceCSV("손절", OwnItem.m_strCodeName, iCurPrice, fProfitRate.ToString("0.00%"), WatchItem.m_iHighestPrice); - m_DataMgr.AddAskLog(strCode, WatchItem.m_strCodeName, iTime, iCurPrice, float.NaN, true, iProfit, fProfitRate); - - // own에서 삭제하고 미체결 리스트에 넣고, watch에서도 뺀다 - - // 미체결 잔량 취소 - } - // trailing - else if (iCurPrice <= OwnItem.m_iMaxPrice - OwnItem.m_iUnitBEP * m_DataMgr.m_Setting.m_fTrailing) - { - float f5MASlope = WatchItem.GetPrev5MASlope(iTime); - if (float.IsNaN(f5MASlope) || f5MASlope > -0.0100f) - { - //Console.WriteLine(string.Format("[{0}] [{1}] 트레일링 매도 대기. 5ma 상승 중 {2}원 ({3})", iTime, OwnItem.m_strCodeName, iCurPrice, (iCurPrice / (float)OwnItem.m_iUnitBEP - 1.0f).ToString("0.00%"))); - continue; - } - - m_DataMgr.GetTrader().SellCurPrice(OwnItem.m_strCode, OwnItem.m_iPayBalance); - m_OwnList.Remove(OwnItem); - m_DataMgr.RemoveWatch(strCode); - - int iProfit = iCurPrice-(int)OwnItem.m_iUnitBEP; - float fProfitRate = iCurPrice/(float)OwnItem.m_iUnitBEP-1.0f; - - ULUtil.Trace("[{0}] 트레일링 매도 {1}원 ({2}:{3}) (5MA slop:{4}) {5}", - OwnItem.m_strCodeName, - iCurPrice, - iCurPrice - OwnItem.m_iUnitBEP, - (iCurPrice/(float)OwnItem.m_iUnitBEP-1.0f).ToString("0.00%"), - f5MASlope.ToString("0.00%"), - WatchItem.m_iHighestPrice); - ULUtil.TraceCSV("트레일링 매도", OwnItem.m_strCodeName, iCurPrice, iCurPrice - OwnItem.m_iUnitBEP, fProfitRate.ToString("0.00%"), f5MASlope.ToString("0.00%"), WatchItem.m_iHighestPrice); - m_DataMgr.AddAskLog(strCode, WatchItem.m_strCodeName, iTime, iCurPrice, f5MASlope, false, iProfit, fProfitRate); - } + Console.WriteLine(string.Format("[{0}] [{1}] 트레일링 매도 대기. 5ma 상승 중 {2}원 ({3})", iTime, OwnItem.m_strCodeName, iCurPrice, (iCurPrice / (float)OwnItem.m_iUnitBEP - 1.0f).ToString("0.00%"))); + continue; } + m_DataMgr.GetTrader().SellCurPrice(OwnItem.m_strCode, OwnItem.m_iPayBalance); + m_OwnList.Remove(OwnItem); + m_DataMgr.RemoveWatch(strCode); + + int iProfit = iCurPrice-(int)OwnItem.m_iUnitBEP; + float fProfitRate = iCurPrice/(float)OwnItem.m_iUnitBEP-1.0f; + + ULUtil.Trace("[{0}] 트레일링 매도 {1}원 ({2}:{3}) (5MA slop:{4}) {5}", + OwnItem.m_strCodeName, + iCurPrice, + iCurPrice - OwnItem.m_iUnitBEP, + (iCurPrice/(float)OwnItem.m_iUnitBEP-1.0f).ToString("0.00%"), + f5MASlope.ToString("0.00%"), + WatchItem.m_iHighestPrice); + ULUtil.TraceCSV("트레일링 매도", OwnItem.m_strCodeName, iCurPrice, iCurPrice - OwnItem.m_iUnitBEP, fProfitRate.ToString("0.00%"), f5MASlope.ToString("0.00%"), WatchItem.m_iHighestPrice); + m_DataMgr.AddAskLog(strCode, WatchItem.m_strCodeName, iTime, iCurPrice, f5MASlope, false, iProfit, fProfitRate, true); + } + + + + ListViewItem lvItem = lvBalance.FindItemWithText(strCode); + if(lvItem != null) + { + lvItem.SubItems[chCurPrice.Index].Text = iCurPrice.ToString("###,###,##0"); + lvItem.SubItems[chEvaluationPrice.Index].Text = OwnItem.m_iEvaluationPrice.ToString("###,###,##0"); + lvItem.SubItems[chEvaluationProfit.Index].Text = OwnItem.m_iEvaluationProfit.ToString("###,###,##0"); + if(OwnItem.m_iEvaluationProfit > 0) + lvItem.SubItems[chEvaluationProfit.Index].ForeColor = Color.Red; + else + lvItem.SubItems[chEvaluationProfit.Index].ForeColor = Color.Blue; + lvItem.SubItems[chEvaluationPrice.Index].Text = OwnItem.m_iEvaluationPrice.ToString("###,###,##0"); + lvItem.SubItems[chProfitRate.Index].Text = OwnItem.m_dProfitRate.ToString("#,##0.00%"); + lvItem.SubItems[chHighestPrice.Index].Text = WatchItem.m_iHighestPrice.ToString("###,###,##0"); + if(OwnItem.m_dProfitRate > 0) + lvItem.SubItems[chProfitRate.Index].ForeColor = Color.Red; + else + lvItem.SubItems[chProfitRate.Index].ForeColor = Color.Blue; + } + } + + + //foreach (ListViewItem lvItem in lvBalance.Items) + //{ + // string strCode = lvItem.SubItems[chCode.Index].Text; + // int iTime = ULUtil.GetCurTimeInt(); + + // if (WatchList.ContainsKey(strCode) == true) + // { + // ULWatchItem WatchItem = WatchList[strCode]; + // OWN_ITEM OwnItem = m_OwnList.Find(itemFind => itemFind.m_strCode == strCode); + // if (OwnItem == null) + // continue; + + // int iDisplayCurPrice = 0; + // if (lvItem.SubItems[chCurPrice.Index].Text.Length > 0) + // iDisplayCurPrice = int.Parse(Regex.Replace(lvItem.SubItems[chCurPrice.Index].Text, @"\D", "")); + + // if (WatchItem.m_iCurPrice != iDisplayCurPrice) + // { + // int iCurPrice = WatchItem.m_iCurPrice; + // int iComp = iCurPrice - (int)OwnItem.m_iUnitBEP; // or OwnItem.m_dConclusionUnitPrice + + // OwnItem.m_iCurPrice = iCurPrice; + // OwnItem.m_iEvaluationPrice = iCurPrice * OwnItem.m_iConclusionBalanceCnt; + // OwnItem.m_iEvaluationProfit = iComp * OwnItem.m_iConclusionBalanceCnt; + // OwnItem.m_dProfitRate = iComp / (float)OwnItem.m_iUnitBEP; + + // lvItem.SubItems[chCurPrice.Index].Text = iCurPrice.ToString("###,###,##0"); + // lvItem.SubItems[chEvaluationPrice.Index].Text = OwnItem.m_iEvaluationPrice.ToString("###,###,##0"); + // lvItem.SubItems[chEvaluationProfit.Index].Text = OwnItem.m_iEvaluationProfit.ToString("###,###,##0"); + // if (OwnItem.m_iEvaluationProfit > 0) + // lvItem.SubItems[chEvaluationProfit.Index].ForeColor = Color.Red; + // else + // lvItem.SubItems[chEvaluationProfit.Index].ForeColor = Color.Blue; + // lvItem.SubItems[chEvaluationPrice.Index].Text = OwnItem.m_iEvaluationPrice.ToString("###,###,##0"); + // lvItem.SubItems[chProfitRate.Index].Text = OwnItem.m_dProfitRate.ToString("#,##0.00%"); + // lvItem.SubItems[chHighestPrice.Index].Text = WatchItem.m_iHighestPrice.ToString("#,##0.00%"); + // if (OwnItem.m_dProfitRate > 0) + // lvItem.SubItems[chProfitRate.Index].ForeColor = Color.Red; + // else + // lvItem.SubItems[chProfitRate.Index].ForeColor = Color.Blue; + + + // OwnItem.m_iMaxPrice = Math.Max(iCurPrice, OwnItem.m_iMaxPrice); + + + // } + // steadiness //if (WatchItem.GetAvgDiff(iTime) < WatchItem.m_iPrevClosing*0.01) //{ @@ -284,8 +329,8 @@ namespace upper_limit_crawler // WatchItem.m_iHighestPrice); // ULUtil.TraceCSV("보합 처리", OwnItem.m_strCodeName, WatchItem.m_iCurPrice, WatchItem.m_iCurPrice - OwnItem.m_iUnitBEP, (WatchItem.m_iCurPrice / (float)OwnItem.m_iUnitBEP - 1.0f).ToString("0.00%"), WatchItem.m_iHighestPrice); //} - } - } + // } + //} } private void RefreshData(object sender, EventArgs e) @@ -299,8 +344,7 @@ namespace upper_limit_crawler int iDeltaT = iCurTime-m_iLastTime; m_iBalanceDelay-=iDeltaT; - if((m_DataMgr.GetTrader().HasConclusion() == true || m_iBalanceDelay<=0) - && ULUtil.GetLimitRemainCountRQ() > 20) + if(m_DataMgr.GetTrader().HasConclusion() == true || m_iBalanceDelay<=0) { RefreshBalance(); m_iBalanceDelay=BALANCE_DELAY; diff --git a/ULDataMgr.cs b/ULDataMgr.cs index 785e5c3..31b48ad 100644 --- a/ULDataMgr.cs +++ b/ULDataMgr.cs @@ -73,6 +73,9 @@ namespace upper_limit_crawler List m_BidLogs = new List(); List m_AskLogs = new List(); + List m_BidLogsSimul = new List(); + List m_AskLogsSimul = new List(); + long m_iCacheBalance = 0; long m_iEvalProfit = 0; @@ -86,9 +89,11 @@ namespace upper_limit_crawler else m_AvailableList = new List(); - m_BidLogs = GetBidLog(m_strToday); - m_AskLogs = GetAskLog(m_strToday); - } + m_BidLogs = GetBidLog(m_strToday, true); + m_AskLogs = GetAskLog(m_strToday, true); + m_BidLogsSimul = GetBidLog(m_strToday, false); + m_AskLogsSimul = GetAskLog(m_strToday, false); + } public void Init() { @@ -223,9 +228,9 @@ namespace upper_limit_crawler } } - public void AddBidLog(string strCode, string strCodeName, int iTime, int iBidPrice, float f5MAslope) + public void AddBidLog(string strCode, string strCodeName, int iTime, int iBidPrice, float f5MAslope, bool bReal) { - string strKey = "bid-"+m_strToday; + string strKey = bReal ? "bid-"+m_strToday : "bid-simul-"+m_strToday; BID_LOG log = new BID_LOG(); log.m_strCode = strCode; @@ -233,23 +238,31 @@ namespace upper_limit_crawler log.m_iTime = iTime; log.m_iBidPrice = iBidPrice; log.m_f5MAslope = f5MAslope; - m_BidLogs.Add(log); - m_DB.Insert(strKey, m_BidLogs); + if(bReal == true) + { + m_BidLogs.Add(log); + m_DB.Insert(strKey, m_BidLogs); + } + else + { + m_BidLogsSimul.Add(log); + m_DB.Insert(strKey, m_BidLogsSimul); + } } - List GetBidLog(string strDate) + List GetBidLog(string strDate, bool bReal) { - string strKey = "bid-"+strDate; + string strKey = bReal ? "bid-"+m_strToday : "bid-simul-"+m_strToday; if(m_DB.IsExist(strKey) == true) return m_DB.Get>(strKey); else return new List(); } - public void AddAskLog(string strCode, string strCodeName, int iTime, int iAskPrice, float f5MAslope, bool bLosscut, int iProfit, float fProfitRate) + public void AddAskLog(string strCode, string strCodeName, int iTime, int iAskPrice, float f5MAslope, bool bLosscut, int iProfit, float fProfitRate, bool bReal) { - string strKey = "ask-"+m_strToday; + string strKey = bReal ? "ask-"+m_strToday : "ask-simul-"+m_strToday; ASK_LOG log = new ASK_LOG(); log.m_strCode = strCode; @@ -260,33 +273,52 @@ namespace upper_limit_crawler log.m_bLosscut = bLosscut; log.m_iProfit = iProfit; log.m_fProfitRate = fProfitRate; - m_AskLogs.Add(log); - m_DB.Insert(strKey, m_AskLogs); + if(bReal == true) + { + m_AskLogs.Add(log); + m_DB.Insert(strKey, m_AskLogs); + } + else + { + m_AskLogsSimul.Add(log); + m_DB.Insert(strKey, m_AskLogsSimul); + } } - List GetAskLog(string strDate) + List GetAskLog(string strDate, bool bReal) { - string strKey = "ask-"+strDate; + string strKey = bReal ? "ask-"+m_strToday : "ask-simul-"+m_strToday; + if(m_DB.IsExist(strKey) == true) return m_DB.Get>(strKey); else return new List(); } - public void DumpLog() + public void DumpLog(bool bReal) { StringBuilder sb = new StringBuilder(); foreach(string strCode in m_AvailableList) - sb.AppendLine(strCode+","); + sb.AppendLine("\"" +strCode+ "\","); - foreach(BID_LOG log in m_BidLogs) - sb.AppendFormat("매수, {0}, {1}, {2}, {3}"+Environment.NewLine, log.m_iTime, log.m_strCodeName, log.m_iBidPrice, log.m_f5MAslope.ToString("0.00%")); - foreach(ASK_LOG log in m_AskLogs) - sb.AppendFormat("매도, {0}, {1}, {2}, {3}, {4}, {5}"+Environment.NewLine, log.m_iTime, log.m_strCodeName, log.m_iAskPrice, log.m_f5MAslope.ToString("0.00%"), log.m_iProfit, log.m_fProfitRate.ToString("0.00%")); - - System.IO.File.WriteAllText(ULUtil.GetLogDir()+"dblog-"+m_strToday+".csv", sb.ToString(), Encoding.UTF8); + if(bReal == true) + { + foreach(BID_LOG log in m_BidLogs) + sb.AppendFormat("매수, {0}, {1}, {2}, {3}"+Environment.NewLine, log.m_iTime, log.m_strCodeName, log.m_iBidPrice, log.m_f5MAslope.ToString("0.00%")); + foreach(ASK_LOG log in m_AskLogs) + sb.AppendFormat("매도, {0}, {1}, {2}, {3}, {4}, {5}"+Environment.NewLine, log.m_iTime, log.m_strCodeName, log.m_iAskPrice, log.m_f5MAslope.ToString("0.00%"), log.m_iProfit, log.m_fProfitRate.ToString("0.00%")); + System.IO.File.WriteAllText(ULUtil.GetLogDir()+"dblog-"+m_strToday+".csv", sb.ToString(), Encoding.UTF8); + } + else + { + foreach(BID_LOG log in m_BidLogsSimul) + sb.AppendFormat("매수, {0}, {1}, {2}, {3}"+Environment.NewLine, log.m_iTime, log.m_strCodeName, log.m_iBidPrice, log.m_f5MAslope.ToString("0.00%")); + foreach(ASK_LOG log in m_AskLogsSimul) + sb.AppendFormat("매도, {0}, {1}, {2}, {3}, {4}, {5}"+Environment.NewLine, log.m_iTime, log.m_strCodeName, log.m_iAskPrice, log.m_f5MAslope.ToString("0.00%"), log.m_iProfit, log.m_fProfitRate.ToString("0.00%")); + System.IO.File.WriteAllText(ULUtil.GetLogDir()+"dblog-simul-"+m_strToday+".csv", sb.ToString(), Encoding.UTF8); + } } } } diff --git a/ULMonitorDlg.cs b/ULMonitorDlg.cs index 64eb1b8..7819ad9 100644 --- a/ULMonitorDlg.cs +++ b/ULMonitorDlg.cs @@ -17,7 +17,7 @@ namespace upper_limit_crawler ULDataMgr m_DataMgr; CPSYSDIBLib.CpSvrNew7043 m_7043 = null; - Timer m_Timer = new Timer(); + System.Windows.Forms.Timer m_Timer = new System.Windows.Forms.Timer(); int m_iMonitorDelay = 0; int m_iLastTime = 0; bool m_bRequesting = false; @@ -62,13 +62,13 @@ namespace upper_limit_crawler bool RefreshMonitorItem() { - if (m_bRequesting == true || ULUtil.GetLimitRemainCountRQ() < 30) + if (m_bRequesting == true) 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); + int iResult = ULUtil.BlockRequest(m_7043); lvWatch.Items.Clear(); int iIdx = 1; @@ -121,8 +121,8 @@ namespace upper_limit_crawler } bContinue = (m_7043.Continue == 1); - if (bContinue == true && ULUtil.GetLimitRemainCountRQ() >= 30) - iResult = m_7043.BlockRequest2(1); + if (bContinue == true) + iResult = ULUtil.BlockRequest(m_7043); } //foreach (ColumnHeader col in lvWatch.Columns) @@ -181,7 +181,7 @@ namespace upper_limit_crawler // bid and add to black list m_DataMgr.GetTrader().Buy(strCode, item.m_iCurPrice, (int)m_DataMgr.m_Setting.m_fBidAmount); - m_DataMgr.AddBidLog(strCode, item.m_strCodeName, iTime, item.m_iCurPrice, f5MASlope); + m_DataMgr.AddBidLog(strCode, item.m_strCodeName, iTime, item.m_iCurPrice, f5MASlope, true); m_DataMgr.AddPostphoneItem(iTime, strCode); ULUtil.Trace("[{0}] 조건 매수 {1}원 ({2}) (5MA slop:{3})", diff --git a/ULTrader.cs b/ULTrader.cs index 5cc5b06..4522c36 100644 --- a/ULTrader.cs +++ b/ULTrader.cs @@ -143,69 +143,50 @@ namespace upper_limit_crawler public void Buy(string strCode, int iUnitPrice, int iBidAmount) { - //lock(lockBuy) - { - while (ULUtil.GetLimitRemainCountRQ() <= 0) ; + int iCnt = iBidAmount / iUnitPrice; - int iCnt = iBidAmount / iUnitPrice; + CPTRADELib.CpTd0311 Td0311Bid = new CPTRADELib.CpTd0311(); + Td0311Bid.SetInputValue(0, "2"); + Td0311Bid.SetInputValue(1, m_astrAccounts[0]); + Td0311Bid.SetInputValue(3, strCode); + Td0311Bid.SetInputValue(4, iCnt); // 수량 + Td0311Bid.SetInputValue(5, 0); // 단가 + Td0311Bid.SetInputValue(7, "0"); + Td0311Bid.SetInputValue(8, "03"); - CPTRADELib.CpTd0311 Td0311Bid = new CPTRADELib.CpTd0311(); - Td0311Bid.SetInputValue(0, "2"); - Td0311Bid.SetInputValue(1, m_astrAccounts[0]); - Td0311Bid.SetInputValue(3, strCode); - Td0311Bid.SetInputValue(4, iCnt); // 수량 - Td0311Bid.SetInputValue(5, 0); // 단가 - Td0311Bid.SetInputValue(7, "0"); - Td0311Bid.SetInputValue(8, "03"); - - Td0311Bid.BlockRequest2(1); - } + ULUtil.BlockRequest(Td0311Bid); } public void Sell(string strCode, int iUnitPrice, int iCnt) { - //lock (lockSell) - { - //long iCnt = (long)Math.Floor(fAskAmount/fCurPrice); - //iCnt=1; + while (ULUtil.GetLimitRemainCountRQ() <= 0) ; - while (ULUtil.GetLimitRemainCountRQ() <= 0) ; + CPTRADELib.CpTd0311 Td0311Ask = new CPTRADELib.CpTd0311(); - CPTRADELib.CpTd0311 Td0311Ask = new CPTRADELib.CpTd0311(); + Td0311Ask.SetInputValue(0, "1"); + Td0311Ask.SetInputValue(1, m_astrAccounts[0]); + Td0311Ask.SetInputValue(3, strCode); + Td0311Ask.SetInputValue(4, iCnt); // 수량 + Td0311Ask.SetInputValue(5, iUnitPrice); // 단가 + Td0311Ask.SetInputValue(7, "0"); + Td0311Ask.SetInputValue(8, "03"); - Td0311Ask.SetInputValue(0, "1"); - Td0311Ask.SetInputValue(1, m_astrAccounts[0]); - Td0311Ask.SetInputValue(3, strCode); - Td0311Ask.SetInputValue(4, iCnt); // 수량 - Td0311Ask.SetInputValue(5, iUnitPrice); // 단가 - Td0311Ask.SetInputValue(7, "0"); - Td0311Ask.SetInputValue(8, "03"); - - Td0311Ask.BlockRequest2(1); - } + ULUtil.BlockRequest(Td0311Ask); } public void SellCurPrice(string strCode, int iCnt) { - //lock (lockSell) - { - //long iCnt = (long)Math.Floor(fAskAmount/fCurPrice); - //iCnt=1; + CPTRADELib.CpTd0311 Td0311Ask = new CPTRADELib.CpTd0311(); - while (ULUtil.GetLimitRemainCountRQ() <= 0); + Td0311Ask.SetInputValue(0, "1"); + Td0311Ask.SetInputValue(1, m_astrAccounts[0]); + Td0311Ask.SetInputValue(3, strCode); + Td0311Ask.SetInputValue(4, iCnt); // 수량 + Td0311Ask.SetInputValue(5, 0); // 단가 + Td0311Ask.SetInputValue(7, "0"); + Td0311Ask.SetInputValue(8, "03"); - CPTRADELib.CpTd0311 Td0311Ask = new CPTRADELib.CpTd0311(); - - Td0311Ask.SetInputValue(0, "1"); - Td0311Ask.SetInputValue(1, m_astrAccounts[0]); - Td0311Ask.SetInputValue(3, strCode); - Td0311Ask.SetInputValue(4, iCnt); // 수량 - Td0311Ask.SetInputValue(5, 0); // 단가 - Td0311Ask.SetInputValue(7, "0"); - Td0311Ask.SetInputValue(8, "03"); - - Td0311Ask.BlockRequest2(1); - } + ULUtil.BlockRequest(Td0311Ask); } } } diff --git a/UlUtil.cs b/UlUtil.cs index 1611d07..7f94abc 100644 --- a/UlUtil.cs +++ b/UlUtil.cs @@ -5,6 +5,7 @@ using System.IO; using System.Linq; using System.Net; using System.Text; +using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; @@ -282,5 +283,19 @@ namespace upper_limit_crawler return (iTotalSec2 >= iTotalSec1 && iTotalSec2 - iTotalSec1 <= iMinDiff * 60); } + + public static short BlockRequest(CPTRADELib.ICpTdDib obj) + { + while(GetLimitRemainCountRQ() < 1) + Thread.Sleep(500); + return obj.BlockRequest2(1); + } + + public static short BlockRequest(CPSYSDIBLib.ISysDib obj) + { + while(GetLimitRemainCountRQ() < 1) + Thread.Sleep(500); + return obj.BlockRequest2(1); + } } }