async하게 수정

This commit is contained in:
2018-06-07 15:33:33 +09:00
parent 663d2977ed
commit 9140673315
11 changed files with 412 additions and 317 deletions

View File

@@ -51,7 +51,7 @@ namespace AutoSellerNS
int m_iPrevTime = 0;
public void OnRecievedPrice()
public async void OnRecievedPrice()
{
bool bOpeningHour = (m_StockCur.GetHeaderValue(19) == '2');
m_iCurPrice = m_StockCur.GetHeaderValue(13);
@@ -117,7 +117,7 @@ namespace AutoSellerNS
if(m_iCheckCount >= m_dCheckCountLimit && m_Listener.IsSelling() == true)
{
m_CybosHelper.SellItem(m_strCode, m_Listener.GetSellableCount(m_strCode), m_aiAskPrice[0]);
await m_CybosHelper.SellItem(m_strCode, m_Listener.GetSellableCount(m_strCode), m_aiAskPrice[0]);
Util.Log(Util.LOG_TYPE.SELL, string.Format("[조건 완료 매도] {0} ({1}회) (현재가 {2:n0}원, 최고가 {3:n0}원)",
m_strCodeName,
Config.GetBidCount(),
@@ -129,7 +129,7 @@ namespace AutoSellerNS
m_iTrailingCount++;
if(m_iTrailingCount >= Config.GetTrailingCnt() && m_Listener.IsSelling() == true)
{
m_CybosHelper.SellItem(m_strCode, m_Listener.GetSellableCount(m_strCode), m_aiAskPrice[0]);
await m_CybosHelper.SellItem(m_strCode, m_Listener.GetSellableCount(m_strCode), m_aiAskPrice[0]);
Util.Log(Util.LOG_TYPE.SELL, string.Format("[트레일링 매도] {0} ({1}% {2}회) (현재가 {3:n0}원, 최고가 {4:n0}원)",
m_strCodeName,
Config.GetTrailingRate(),
@@ -140,7 +140,7 @@ namespace AutoSellerNS
if(iTimeDiff >= Config.GetTimeLimit() && m_Listener.IsSelling() == true)
{
m_CybosHelper.SellItem(m_strCode, m_Listener.GetSellableCount(m_strCode), m_aiAskPrice[0]);
await m_CybosHelper.SellItem(m_strCode, m_Listener.GetSellableCount(m_strCode), m_aiAskPrice[0]);
Util.Log(Util.LOG_TYPE.SELL, string.Format("[시간제한 매도] {0} ({1}초) (현재가 {2:n0}원, 최고가 {3:n0}원)",
m_strCodeName,
iTimeDiff,
@@ -236,6 +236,14 @@ namespace AutoSellerNS
m_CpConclusion.Subscribe();
}
public async Task InitCybosAsync()
{
await Task.Run(() =>
{
InitCybos();
});
}
public void SavePrice(STOCK_CUR_ITEM item)
{
if (Directory.Exists(Util.GetLogPath()) == false)
@@ -259,37 +267,36 @@ namespace AutoSellerNS
public List<AutoSeller.ITEM> UpdateItems()
{
List<AutoSeller.ITEM> aItems = new List<AutoSeller.ITEM>();
if (Config.GetAccount() == "")
return aItems;
m_6033.SetInputValue(0, Config.GetAccount());
m_6033.SetInputValue(1, Config.GetSubAccount());
m_6033.BlockRequest2(1);
m_6033.BlockRequest2(0);
string strCodeList = "";
bool bContinue = true;
while(bContinue == true)
while (bContinue == true)
{
int iCount = m_6033.GetHeaderValue(7);
for(int i = 0; i < iCount; i++)
for (int i = 0; i < iCount; i++)
{
AutoSeller.ITEM Item = new AutoSeller.ITEM();
Item.m_strCodeName = m_6033.GetDataValue(0, i);
if(Item.m_strCodeName != "")
if (Item.m_strCodeName != "")
{
Item.m_strCode = m_6033.GetDataValue(12, i);
Item.m_iItemCnt = m_6033.GetDataValue(7, i);
Item.m_iAssessedValue = m_6033.GetDataValue(9, i);
Item.m_iValuationGains = m_6033.GetDataValue(10, i);
Item.m_iAssessedValue = m_6033.GetDataValue(9, i) / 1000;
Item.m_iValuationGains = m_6033.GetDataValue(10, i) / 1000;
Item.m_dYield = m_6033.GetDataValue(11, i);
Item.m_iAvailableQuantity = m_6033.GetDataValue(15, i);
Item.m_dBookUnitPrice = m_6033.GetDataValue(17, i);
Item.m_iProfitUnitPrice = m_6033.GetDataValue(18, i);
if(strCodeList.Length > 0)
if (strCodeList.Length > 0)
strCodeList += ",";
strCodeList += Item.m_strCode;
@@ -298,15 +305,16 @@ namespace AutoSellerNS
}
bContinue = (m_6033.Continue != 0);
if(bContinue == true)
m_6033.BlockRequest2(1);
bContinue = false;
if (bContinue == true)
m_6033.BlockRequest2(0);
}
if (strCodeList.Length > 0)
{
DSCBO1Lib.StockMst2 StockMst = new DSCBO1Lib.StockMst2();
StockMst.SetInputValue(0, strCodeList);
StockMst.BlockRequest2(1);
StockMst.BlockRequest2(0);
int iCnt = StockMst.GetHeaderValue(0);
for (int i = 0; i < iCnt; i++)
{
@@ -320,13 +328,13 @@ namespace AutoSellerNS
// sync
List<string> aRemoveKeys = new List<string>();
foreach(var Item in m_aStockCur)
foreach (var Item in m_aStockCur)
{
if(aItems.Any(s => s.m_strCode == Item.Key) == false)
if (aItems.Any(s => s.m_strCode == Item.Key) == false)
aRemoveKeys.Add(Item.Key);
}
foreach(var Key in aRemoveKeys)
foreach (var Key in aRemoveKeys)
{
m_aStockCur[Key].m_StockCur.Unsubscribe();
m_aStockCur[Key].m_Jpbid.Unsubscribe();
@@ -337,14 +345,16 @@ namespace AutoSellerNS
return aItems;
}
static int iUpdateNCIdx = 1;
public async Task<List<AutoSeller.ITEM>> UpdateItemsAsync()
{
return await Task<List<AutoSeller.ITEM>>.Run(() =>
{
return UpdateItems();
});
}
public List<AutoSeller.NCITEM> UpdateNC()
{
int iIdx = iUpdateNCIdx++;
Util.Log(Util.LOG_TYPE.DEBUG, string.Format("UpdateNC Start ({0}, {1}, {2}, {3})", iIdx,
GetLimitRemainCountRQ(), GetLimitRemainCountSB(), GetLimitRemainCountTrade()
));
while(GetLimitRemainCountTrade() < 1)
Thread.Sleep(500);
@@ -353,14 +363,12 @@ namespace AutoSellerNS
CPTRADELib.CpTd5339 CP5339 = new CPTRADELib.CpTd5339();
CP5339.SetInputValue(0, Config.GetAccount());
CP5339.SetInputValue(1, Config.GetSubAccount());
CP5339.BlockRequest2(1);
CP5339.BlockRequest2(0);
bool bContinue = true;
while(bContinue)
{
int iCnt = CP5339.GetHeaderValue(5);
Console.WriteLine("icnt : {0}", iCnt);
for(int i = 0; i<iCnt; i++)
{
AutoSeller.NCITEM Item = new AutoSeller.NCITEM();
@@ -373,20 +381,27 @@ namespace AutoSellerNS
Item.m_bAsk = (CP5339.GetDataValue(13, i) == "1");
Item.m_iOrderNo = CP5339.GetDataValue(1, i);
Item.m_iOrgOrderNo = CP5339.GetDataValue(2, i);
if (Item.m_iOrgOrderNo == 0)
Item.m_iOrgOrderNo = Item.m_iOrderNo;
NCItems.Add(Item);
}
bContinue = (CP5339.Continue != 0);
if(bContinue)
CP5339.BlockRequest2(1);
CP5339.BlockRequest2(0);
}
Util.Log(Util.LOG_TYPE.DEBUG, string.Format("UpdateNC End ({0})", iIdx));
return NCItems;
}
public async Task<List<AutoSeller.NCITEM>> UpdateNCAsync()
{
return await Task<List<AutoSeller.NCITEM>>.Run(() => {
return UpdateNC();
});
}
private void CpConclusion_Received()
{
string strType = m_CpConclusion.GetHeaderValue(14);
@@ -416,7 +431,7 @@ namespace AutoSellerNS
switch (iType)
{
case 1: // 체결
m_Listener.UpdateItem(strCode, strCodeName, bBid, iConclusionCnt, iRemainCnt, iBookValue);
m_Listener.UpdateItem();
m_Listener.UpdateNCItem();
Util.Log(bBid?Util.LOG_TYPE.BUY:Util.LOG_TYPE.SELL, string.Format("{0}:{1} {2} 체결 ({3:n0}원 {4}주) - {5}", strCodeName, strCode, strBidOrAsk, iPrice, iConclusionCnt, strOrderCondition));
break;
@@ -432,7 +447,7 @@ namespace AutoSellerNS
case 4: // 접수
m_Listener.UpdateNCItem();
Util.Log(bBid ? Util.LOG_TYPE.BUY : Util.LOG_TYPE.SELL, string.Format("{0}:{1} {2} 접수 ({3:n0}원 {4}주) - {5}", strCodeName, strCode, strBidOrAsk, iPrice, iConclusionCnt, strOrderCondition));
if (bBid == true)
if (bBid == false)
{
if(m_aStockCur.ContainsKey(strCode) == true)
{
@@ -489,7 +504,7 @@ namespace AutoSellerNS
DSCBO1Lib.StockJpbid2 JpBid = new DSCBO1Lib.StockJpbid2();
JpBid.SetInputValue(0, Item.m_strCode);
JpBid.BlockRequest2(1);
JpBid.BlockRequest2(0);
for(int i=0; i<10; i++)
{
StockCur.m_aiAskPrice[i] = JpBid.GetDataValue(0, i);
@@ -539,62 +554,60 @@ namespace AutoSellerNS
return (iValue/iUnit)*iUnit;
}
static int iSellIdx = 1;
public void SellItem(string strCode, int iCnt, int iAskPrice)
public async Task SellItem(string strCode, int iCnt, int iAskPrice)
{
int iIdx = iSellIdx++;
Util.Log(Util.LOG_TYPE.DEBUG, string.Format("SellItem Start ({0})", iIdx));
await Task.Run(() =>
{
int iSellPrice = iAskPrice - GetUnitValue(iAskPrice);
int iSellPrice = iAskPrice-GetUnitValue(iAskPrice);
CPTRADELib.CpTd0311 Td0311 = new CPTRADELib.CpTd0311();
Td0311.SetInputValue(0, "1");
Td0311.SetInputValue(1, Config.GetAccount());
Td0311.SetInputValue(2, Config.GetSubAccount());
Td0311.SetInputValue(3, strCode);
Td0311.SetInputValue(4, iCnt);
Td0311.SetInputValue(5, iSellPrice);
Td0311.SetInputValue(8, "01");
Td0311.BlockRequest2(1);
Util.Log(Util.LOG_TYPE.DEBUG, string.Format("SellItem End ({0})", iIdx));
CPTRADELib.CpTd0311 Td0311 = new CPTRADELib.CpTd0311();
Td0311.SetInputValue(0, "1");
Td0311.SetInputValue(1, Config.GetAccount());
Td0311.SetInputValue(2, Config.GetSubAccount());
Td0311.SetInputValue(3, strCode);
Td0311.SetInputValue(4, iCnt);
Td0311.SetInputValue(5, iSellPrice);
Td0311.SetInputValue(8, "01");
Td0311.BlockRequest2(1);
});
}
static int iCancelIdx = 1;
public void CancelItem(string strCode, int iOrgOrderNo)
public async Task CancelItem(string strCode, int iOrgOrderNo)
{
int iIdx = iCancelIdx++;
Util.Log(Util.LOG_TYPE.DEBUG, string.Format("CancelItem Start ({0})", iIdx));
CPTRADELib.CpTd0314 Td0314 = new CPTRADELib.CpTd0314();
Td0314.SetInputValue(1, iOrgOrderNo);
Td0314.SetInputValue(2, Config.GetAccount());
Td0314.SetInputValue(3, Config.GetSubAccount());
Td0314.SetInputValue(4, strCode);
Td0314.SetInputValue(5, 0);
Td0314.BlockRequest2(1);
Util.Log(Util.LOG_TYPE.DEBUG, string.Format("CancelItem End ({0})", iIdx));
await Task.Run(() =>
{
CPTRADELib.CpTd0314 Td0314 = new CPTRADELib.CpTd0314();
Td0314.SetInputValue(1, iOrgOrderNo);
Td0314.SetInputValue(2, Config.GetAccount());
Td0314.SetInputValue(3, Config.GetSubAccount());
Td0314.SetInputValue(4, strCode);
Td0314.SetInputValue(5, 0);
Td0314.BlockRequest2(1);
});
}
static int iCorrectIdx = 1;
public void CorrectionItem(string strCode, int iOrgOrderNo, int iCnt, int iAskPrice)
public async Task CorrectionItem(string strCode, int iOrgOrderNo, int iCnt, int iAskPrice)
{
int iIdx = iCorrectIdx++;
Util.Log(Util.LOG_TYPE.DEBUG, string.Format("CorrectionItem Start ({0})", iIdx));
if(Config.GetMockTrading() == true)
{
await CancelItem(strCode, iOrgOrderNo);
await SellItem(strCode, iCnt, iAskPrice);
return;
}
int iSellPrice = iAskPrice;
await Task.Run(() =>
{
int iSellPrice = iAskPrice;
CPTRADELib.CpTd0313 Td0313 = new CPTRADELib.CpTd0313();
Td0313.SetInputValue(1, iOrgOrderNo);
Td0313.SetInputValue(2, Config.GetAccount());
Td0313.SetInputValue(3, Config.GetSubAccount());
Td0313.SetInputValue(4, strCode);
Td0313.SetInputValue(5, 0);
Td0313.SetInputValue(6, iSellPrice);
Td0313.BlockRequest2(1);
Util.Log(Util.LOG_TYPE.DEBUG, string.Format("CorrectionItem End ({0})", iIdx));
CPTRADELib.CpTd0313 Td0313 = new CPTRADELib.CpTd0313();
Td0313.SetInputValue(1, iOrgOrderNo);
Td0313.SetInputValue(2, Config.GetAccount());
Td0313.SetInputValue(3, Config.GetSubAccount());
Td0313.SetInputValue(4, strCode);
Td0313.SetInputValue(5, 0);
Td0313.SetInputValue(6, iSellPrice);
Td0313.BlockRequest2(1);
});
}
}
}