using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; namespace upper_limit_crawler { public struct SETTING { public float m_fSearchMin; public float m_fSearchMax; public float m_fBidMin; public float m_fBidMax; public float m_fBidAmount; public float m_fTimeout; public float m_fTrailing; public float m_fLossCut; } public struct POSTPONE_ITEM { public POSTPONE_ITEM(string strCode, int iTime) { m_strCode = strCode; m_iTime = iTime; } public string m_strCode; public int m_iTime; } [Serializable] struct BID_LOG { public string m_strCode { get; set; } public string m_strCodeName { get; set; } public int m_iTime { get; set; } public int m_iBidPrice { get; set; } public float m_f5MAslope { get; set; } } [Serializable] struct ASK_LOG { public string m_strCode { get; set; } public string m_strCodeName { get; set; } public int m_iTime { get; set; } public int m_iAskPrice { get; set; } public float m_f5MAslope { get; set; } public bool m_bLosscut { get; set; } public int m_iProfit { get; set; } public float m_fProfitRate { get; set; } } public class ULDataMgr { public SETTING m_Setting = new SETTING(); public ULDB m_DB = new ULDB(); ULTrader m_Trader = new ULTrader(); Dictionary m_WatchList = new Dictionary(); List m_PostponeList = new List(); Dictionary m_LossCutList = new Dictionary(); List m_BlackList = new List(); string m_AvailableListKey; List m_AvailableList = new List(); 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; string m_strToday = ULUtil.GetCurTime().ToString("yyyy-MM-dd"); public ULDataMgr() { m_AvailableListKey= "available-"+m_strToday; if(m_DB.IsExist(m_AvailableListKey) == true) m_AvailableList = m_DB.Get>(m_AvailableListKey); else m_AvailableList = new List(); 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() { m_Trader.Init(); } public string GetAccount() { return m_Trader.GetAccount(); } public ULTrader GetTrader() { return m_Trader; } public void AddWatch(string strCode, string strCodeName) { if (m_WatchList.ContainsKey(strCode)) return; ULWatchItem item = new ULWatchItem(); item.m_strCode = strCode; item.m_strCodeName = strCodeName; item.FillPrice(); item.m_StockCur = new DSCBO1Lib.StockCur(); item.m_StockCur.SetInputValue(0, strCode); item.m_StockCur.Received += item.Received; item.m_StockCur.SubscribeLatest(); m_WatchList.Add(strCode, item); } public void RemoveWatch(string strCode) { if (m_WatchList.ContainsKey(strCode)) return; m_WatchList[strCode].m_StockCur.Unsubscribe(); m_WatchList.Remove(strCode); } public void StartAll() { foreach (KeyValuePair item in m_WatchList) { item.Value.m_StockCur = new DSCBO1Lib.StockCur(); item.Value.m_StockCur.SetInputValue(0, item.Value.m_strCode); item.Value.m_StockCur.Received += item.Value.Received; item.Value.m_StockCur.SubscribeLatest(); } } public void StopAll() { foreach (KeyValuePair item in m_WatchList) { item.Value.m_StockCur.Unsubscribe(); } } public void AddPostphoneItem(int iTime, string strCode) { POSTPONE_ITEM postpone = new POSTPONE_ITEM(strCode, iTime); m_PostponeList.Add(postpone); } public bool IsInPostponeList(int iTime, string strCode) { return m_PostponeList.Any(r => r.m_strCode == strCode && ULUtil.IsInTime(r.m_iTime, iTime, 4)); } public void AddLosscutItem(string strCode) { if(m_LossCutList.ContainsKey(strCode) == false) { m_LossCutList.Add(strCode, 1); } else { m_LossCutList[strCode] += 1; if(m_LossCutList[strCode] >= 2) m_BlackList.Add(strCode); } } public bool IsInBlackList(string strCode) { return m_BlackList.Any(r => r == strCode); } public Dictionary GetWatchList() { return m_WatchList; } public void SetCacheBalance(long iBalance) { m_iCacheBalance = iBalance; } public long GetCacheBalance() { return m_iCacheBalance; } public void SetEvalProfit(long iProfit) { m_iEvalProfit = iProfit; } public long GetEvalProfit() { return m_iEvalProfit; } public ULDB GetDB() { return m_DB; } public void AddAvailableItem(string strCode) { if(m_AvailableList.Any(r => r==strCode)==false) { m_AvailableList.Add(strCode); m_DB.Insert(m_AvailableListKey, m_AvailableList); } } public void AddBidLog(string strCode, string strCodeName, int iTime, int iBidPrice, float f5MAslope, bool bReal) { string strKey = bReal ? "bid-"+m_strToday : "bid-simul-"+m_strToday; BID_LOG log = new BID_LOG(); log.m_strCode = strCode; log.m_strCodeName = strCodeName; log.m_iTime = iTime; log.m_iBidPrice = iBidPrice; log.m_f5MAslope = f5MAslope; 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, bool bReal) { 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, bool bReal) { string strKey = bReal ? "ask-"+m_strToday : "ask-simul-"+m_strToday; ASK_LOG log = new ASK_LOG(); log.m_strCode = strCode; log.m_strCodeName = strCodeName; log.m_iTime = iTime; log.m_iAskPrice = iAskPrice; log.m_f5MAslope = f5MAslope; log.m_bLosscut = bLosscut; log.m_iProfit = iProfit; log.m_fProfitRate = fProfitRate; 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, bool bReal) { 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 int GetBidTime(string strCode) { int iTime = 0; List BidLog = m_BidLogs.Where(r => r.m_strCode==strCode).ToList(); BidLog.Reverse(); foreach(BID_LOG bid in BidLog) { if(m_AskLogs.Any(r => r.m_strCode==strCode && r.m_iTime>bid.m_iTime)==false) iTime=bid.m_iTime; else break; } return iTime; } public void DumpLog(bool bReal) { StringBuilder sb = new StringBuilder(); foreach(string strCode in m_AvailableList) sb.AppendLine("\"" +strCode+ "\","); 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); } } } }