Files
upper-limit-crawler/ULTrader.cs
2016-08-09 14:42:50 +09:00

193 lines
4.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace upper_limit_crawler
{
public class ULTrader
{
CPFORETRADELib.CpForeTdUtil m_Util = null;
DSCBO1Lib.CpConclusion m_Conclusion = null;
string[] m_astrAccounts = null;
object lockBuy = new object();
object lockSell = new object();
bool m_bHasConclusion = false;
public ULTrader()
{
}
public void Init()
{
m_Util = new CPFORETRADELib.CpForeTdUtil();
m_Conclusion = new DSCBO1Lib.CpConclusion();
m_Util.TradeInit();
m_astrAccounts = m_Util.AccountNumber;
StartConclusion();
}
public string GetAccount()
{
return m_astrAccounts[0];
}
void StartConclusion()
{
m_Conclusion.Received += Conclusion_Received;
m_Conclusion.Subscribe();
}
private void Conclusion_Received()
{
string strAccount = m_Conclusion.GetHeaderValue(1);
string strCodeName = m_Conclusion.GetHeaderValue(2);
long iCount = m_Conclusion.GetHeaderValue(3);
long iPrice = m_Conclusion.GetHeaderValue(4);
long iTradeNo = m_Conclusion.GetHeaderValue(5);
long iTradeNoOrg = m_Conclusion.GetHeaderValue(6);
string strCode = m_Conclusion.GetHeaderValue(9);
bool bBuy = (m_Conclusion.GetHeaderValue(12) == "2");
string strResult = m_Conclusion.GetHeaderValue(14);
string strTradeType = m_Conclusion.GetHeaderValue(16);
string strLog = "";
if (strTradeType == "1") // 정상주문
{ strLog += "[정상주문]"; }
else if (strTradeType == "2") // 정정주문
{ strLog += "[정정주문]"; }
else if (strTradeType == "3") // 취소주문
{ strLog += "[취소주문]"; }
if (bBuy == true)
strLog += "(매수) ";
else
strLog += "(매도) ";
strLog += string.Format("[{0}:{1}] {2:###,###,##0}원 {3:###,###,##0}주 (총{4:###,###,##0}원) ", strCodeName, strCode, iPrice, iCount, iPrice * iCount);
if (strResult == "1") // 체결
{ strLog += "체결"; }
else if (strResult == "2") // 확인
{ strLog += "확인"; }
else if (strResult == "3") // 거부
{ strLog += "거부"; }
else if (strResult == "4") // 접수
{ strLog += "접수"; }
ULUtil.Trace(strLog);
if (strResult == "1")
m_bHasConclusion = true;
}
public bool HasConclusion()
{
return m_bHasConclusion;
}
public void ResetConclusion()
{
m_bHasConclusion = false;
}
//0 - (string)주문종류코드 주문구분
// 1 매도
// 2 매수
//1 - (string)계좌번호
//2 - (string)상품관리구분코드 계좌구분
//3 - (string)종목코드
//4 - (long)주문수량
//5 - (long)주문단가
//6 - (string)매매구분
//7 - (string)주문조건구분코드 매매조건
//0 없음
//1 IOC
//2 FOK
//8 - (string)주문호가구분코드
// 1 보통[default]
// 2 임의
// 3 시장가
// 5 조건부지정가
// 6 희망대량
// 9 자사주
// 12 최유리지정가
// 13 최우선지정가
// 10 스톡옵션자사주
// 23 임의시장가
// 25 임의조건부지정가
// 51 장중대량
// 52 장중바스켓
// 61 개시전종가
// 62 개시전종가대량
// 63 개시전시간외바스켓
// 67 개시전금전신탁자사주
// 69 개시전대량자기
// 71 신고대량(전장시가)
// 72 시간외대량
// 73 신고대량(종가)
// 77 금전신탁종가대량
// 11 금전신탁자사주
// 80 시간외바스켓
// 79 시간외대량자기
public void Buy(string strCode, int iUnitPrice, int iBidAmount)
{
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");
ULUtil.BlockRequest(Td0311Bid);
}
public void Sell(string strCode, int iUnitPrice, int iCnt)
{
while (ULUtil.GetLimitRemainCountRQ() <= 0) ;
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");
ULUtil.BlockRequest(Td0311Ask);
}
public void SellCurPrice(string strCode, int iCnt)
{
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");
ULUtil.BlockRequest(Td0311Ask);
}
}
}