269 lines
8.3 KiB
C#
269 lines
8.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace AutoSellerNS
|
|
{
|
|
class CybosHelper
|
|
{
|
|
public class STOCK_CUR_ITEM
|
|
{
|
|
public AutoSeller m_Listener = null;
|
|
public CybosHelper m_CybosHelper = null;
|
|
public string m_strCode;
|
|
public DSCBO1Lib.StockCur m_StockCur = null;
|
|
public DSCBO1Lib.StockJpbid m_Jpbid = null;
|
|
public int[] m_aiBidPrice = new int[10];
|
|
public int[] m_aiBidCount = new int[10];
|
|
public int[] m_aiAskPrice = new int[10];
|
|
public int[] m_aiAskCount = new int[10];
|
|
|
|
public int m_iCurPrice = 0;
|
|
public int m_iMaxPrice = 0;
|
|
public int m_iCheckCount = 0;
|
|
|
|
public void OnRecievedPrice()
|
|
{
|
|
m_iCurPrice = m_StockCur.GetHeaderValue(13);
|
|
m_iMaxPrice = Math.Max(m_iCurPrice, m_iMaxPrice);
|
|
|
|
int iAskPrice = m_StockCur.GetHeaderValue(7);
|
|
int iBidPrice = m_StockCur.GetHeaderValue(8);
|
|
if (m_iCurPrice <= iBidPrice)
|
|
m_iCheckCount++;
|
|
else
|
|
m_iCheckCount = 0;
|
|
|
|
if (m_iCheckCount >= Config.GetBidCount() && m_Listener.IsSelling() == true)
|
|
m_CybosHelper.SellItem(m_strCode, m_iCurPrice, m_Listener.GetSellableCount(m_strCode));
|
|
|
|
m_Listener.OnReceivedCurPrice(m_strCode, m_iCurPrice, m_iMaxPrice, m_iCheckCount);
|
|
}
|
|
|
|
public void OnReceivedCall()
|
|
{
|
|
for(int i=0; i<5; i++)
|
|
{
|
|
m_aiBidPrice[i] = m_Jpbid.GetHeaderValue(4 + 4*i);
|
|
m_aiBidCount[i] = m_Jpbid.GetHeaderValue(6 + 4*i);
|
|
m_aiAskPrice[i] = m_Jpbid.GetHeaderValue(3 + 4*i);
|
|
m_aiAskCount[i] = m_Jpbid.GetHeaderValue(5 + 4*i);
|
|
}
|
|
|
|
for (int i = 0; i < 5; i++)
|
|
{
|
|
m_aiBidPrice[i+5] = m_Jpbid.GetHeaderValue(28 + 4*i);
|
|
m_aiBidCount[i+5] = m_Jpbid.GetHeaderValue(30 + 4*i);
|
|
m_aiAskPrice[i+5] = m_Jpbid.GetHeaderValue(27 + 4*i);
|
|
m_aiAskCount[i+5] = m_Jpbid.GetHeaderValue(29 + 4*i);
|
|
}
|
|
|
|
m_Listener.OnReceivedCall(m_strCode, m_aiBidPrice, m_aiBidCount, m_aiAskPrice, m_aiAskCount);
|
|
}
|
|
}
|
|
|
|
AutoSeller m_Listener = null;
|
|
|
|
CPUTILLib.CpCybos m_CPCybos = new CPUTILLib.CpCybos();
|
|
CPFORETRADELib.CpForeTdUtil m_CPUtil = new CPFORETRADELib.CpForeTdUtil();
|
|
DSCBO1Lib.CpConclusion m_CpConclusion = new DSCBO1Lib.CpConclusion();
|
|
|
|
CPTRADELib.CpTd6033 m_6033 = new CPTRADELib.CpTd6033();
|
|
Dictionary<string, STOCK_CUR_ITEM> m_aStockCur = new Dictionary<string, STOCK_CUR_ITEM>();
|
|
|
|
public CybosHelper(AutoSeller Listener)
|
|
{
|
|
m_Listener = Listener;
|
|
}
|
|
|
|
public void InitCybos()
|
|
{
|
|
short iResult = m_CPUtil.TradeInit();
|
|
switch (iResult)
|
|
{
|
|
case -1:
|
|
Util.Log(Util.LOG_TYPE.ERROR, "[TradeInit] 오류");
|
|
break;
|
|
|
|
case 0:
|
|
Util.Log(Util.LOG_TYPE.VERVOSE, "[TradeInit] 로그인 되었습니다");
|
|
break;
|
|
|
|
case 1:
|
|
Util.Log(Util.LOG_TYPE.ERROR, "[TradeInit] 업무 키 입력 잘못됨");
|
|
break;
|
|
|
|
case 2:
|
|
Util.Log(Util.LOG_TYPE.ERROR, "[TradeInit] 계좌 비밀번호가 잘못되었습니다");
|
|
break;
|
|
|
|
case 3:
|
|
Util.Log(Util.LOG_TYPE.ERROR, "[TradeInit] 취소되었습니다");
|
|
break;
|
|
}
|
|
|
|
m_Listener.SetAccountList(m_CPUtil.AccountNumber);
|
|
m_CpConclusion.Received += CpConclusion_Received;
|
|
m_CpConclusion.Subscribe();
|
|
}
|
|
|
|
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);
|
|
|
|
string strCodeList = "";
|
|
|
|
int iCount = m_6033.GetHeaderValue(7);
|
|
for (int i = 0; i < iCount; i++)
|
|
{
|
|
AutoSeller.ITEM Item = new AutoSeller.ITEM();
|
|
|
|
Item.m_strCodeName = m_6033.GetDataValue(0, i);
|
|
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_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);
|
|
Item.m_iSellableCnt = m_6033.GetDataValue(15, i);
|
|
|
|
if (strCodeList.Length > 0)
|
|
strCodeList += ",";
|
|
strCodeList += Item.m_strCode;
|
|
|
|
aItems.Add(Item);
|
|
}
|
|
|
|
if (strCodeList.Length > 0)
|
|
{
|
|
DSCBO1Lib.StockMst2 StockMst = new DSCBO1Lib.StockMst2();
|
|
StockMst.SetInputValue(0, strCodeList);
|
|
StockMst.BlockRequest2(0);
|
|
int iCnt = StockMst.GetHeaderValue(0);
|
|
for (int i = 0; i < iCnt; i++)
|
|
{
|
|
string strCode = StockMst.GetDataValue(0, i);
|
|
int iPrice = StockMst.GetDataValue(3, i);
|
|
AutoSeller.ITEM Item = aItems.FirstOrDefault(s => s.m_strCode == strCode);
|
|
if (Item != default(AutoSeller.ITEM))
|
|
Item.m_iCurPrice = iPrice;
|
|
}
|
|
}
|
|
|
|
|
|
return aItems;
|
|
}
|
|
|
|
private void CpConclusion_Received()
|
|
{
|
|
string strType = m_CpConclusion.GetHeaderValue(14);
|
|
int iType = 0;
|
|
int.TryParse(strType, out iType);
|
|
bool bBid = (m_CpConclusion.GetHeaderValue(12) == "2");
|
|
|
|
string strCode = m_CpConclusion.GetHeaderValue(9);
|
|
string strCodeName = m_CpConclusion.GetHeaderValue(2);
|
|
int iConclusionCnt = m_CpConclusion.GetHeaderValue(3);
|
|
int iPrice = m_CpConclusion.GetHeaderValue(4);
|
|
string strCancel = m_CpConclusion.GetHeaderValue(16);
|
|
int iBookValue = m_CpConclusion.GetHeaderValue(21);
|
|
int iRemainCnt = m_CpConclusion.GetHeaderValue(23);
|
|
string strBidOrAsk = bBid ? "매수" : "매도";
|
|
|
|
switch (iType)
|
|
{
|
|
case 1: // 체결
|
|
m_Listener.UpdateItem(strCode, strCodeName, bBid, iConclusionCnt, iRemainCnt, iBookValue);
|
|
Util.Log(bBid?Util.LOG_TYPE.BUY:Util.LOG_TYPE.SELL, string.Format("{0}:{1} {2} 체결 ({3:n0}원 {4}주)", strCodeName, strCode, strBidOrAsk, iPrice, iConclusionCnt));
|
|
break;
|
|
|
|
case 2: // 확인
|
|
Util.Log(Util.LOG_TYPE.VERVOSE, string.Format("{0}:{1} {2} 확인", strCodeName, strCode, strBidOrAsk));
|
|
break;
|
|
|
|
case 3: // 거부
|
|
Util.Log(Util.LOG_TYPE.VERVOSE, string.Format("{0}:{1} {2} 거부", strCodeName, strCode, strBidOrAsk));
|
|
break;
|
|
|
|
case 4: // 접수
|
|
Util.Log(bBid ? Util.LOG_TYPE.BUY : Util.LOG_TYPE.SELL, string.Format("{0}:{1} {2} 접수 ({3:n0}원 {4}주)", strCodeName, strCode, strBidOrAsk, iPrice, iConclusionCnt));
|
|
break;
|
|
}
|
|
}
|
|
|
|
public int GetLimitRemainCountTrade()
|
|
{
|
|
return m_CPCybos.GetLimitRemainCount(CPUTILLib.LIMIT_TYPE.LT_TRADE_REQUEST);
|
|
}
|
|
|
|
public int GetLimitRemainCountRQ()
|
|
{
|
|
return m_CPCybos.GetLimitRemainCount(CPUTILLib.LIMIT_TYPE.LT_NONTRADE_REQUEST);
|
|
}
|
|
|
|
public int GetLimitRemainCountSB()
|
|
{
|
|
return m_CPCybos.GetLimitRemainCount(CPUTILLib.LIMIT_TYPE.LT_SUBSCRIBE);
|
|
}
|
|
|
|
public bool IsConnected()
|
|
{
|
|
return (m_CPCybos.IsConnect==1);
|
|
}
|
|
|
|
public STOCK_CUR_ITEM GetItem(string strCode)
|
|
{
|
|
if (m_aStockCur.ContainsKey(strCode) == false)
|
|
return null;
|
|
|
|
return m_aStockCur[strCode];
|
|
}
|
|
|
|
public void Subscribe(AutoSeller.ITEM Item)
|
|
{
|
|
if(m_aStockCur.Any(s => s.Key == Item.m_strCode) == false)
|
|
{
|
|
STOCK_CUR_ITEM StockCur = new STOCK_CUR_ITEM();
|
|
StockCur.m_Listener = m_Listener;
|
|
StockCur.m_CybosHelper = this;
|
|
StockCur.m_strCode = Item.m_strCode;
|
|
StockCur.m_StockCur = new DSCBO1Lib.StockCur();
|
|
StockCur.m_StockCur.SetInputValue(0, Item.m_strCode);
|
|
StockCur.m_StockCur.Received += StockCur.OnRecievedPrice;
|
|
StockCur.m_StockCur.Subscribe();
|
|
StockCur.m_Jpbid = new DSCBO1Lib.StockJpbid();
|
|
StockCur.m_Jpbid.SetInputValue(0, Item.m_strCode);
|
|
StockCur.m_Jpbid.Received += StockCur.OnReceivedCall;
|
|
StockCur.m_Jpbid.SubscribeLatest();
|
|
StockCur.m_iCurPrice = Item.m_iCurPrice;
|
|
StockCur.m_iMaxPrice = Item.m_iCurPrice;
|
|
m_aStockCur.Add(Item.m_strCode, StockCur);
|
|
}
|
|
}
|
|
|
|
public void SellItem(string strCode, int iPrice, int iCnt)
|
|
{
|
|
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, 0);
|
|
Td0311.SetInputValue(8, "03");
|
|
Td0311.BlockRequest2(0);
|
|
}
|
|
}
|
|
}
|