Files
upper-limit-crawler/Form1.cs

317 lines
11 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Collections;
namespace upper_limit_crawler
{
//struct ITEM
//{
// string m_strCode;
// DateTime m_Time;
// int m_iCurPrice;
// int m_iAskCount;
// int m_iBidCount;
// int m_iStartPrice;
// int m_iTradingVolume;
// float m_fRatePerStart;
//}
public partial class Form1 : Form
{
CPSYSDIBLib.CpSvrNew7043 m_7043 = new CPSYSDIBLib.CpSvrNew7043();
DSCBO1Lib.StockMst2 m_StockMst2 = new DSCBO1Lib.StockMst2();
ArrayList m_TraceList = new ArrayList();
MySqlConnection m_DBCon;
Timer timerWatch = new Timer();
Timer timerTrace = new Timer();
public Form1()
{
InitializeComponent();
string strConn = "Server=mjjo53.us.to;Database=upperlimit;Uid=mjjo;Pwd=whaudwls;charset=utf8;";
m_DBCon = new MySqlConnection(strConn);
m_DBCon.Open();
InitWatch();
timerWatch.Interval = 10 * 1000;
timerWatch.Tick += timerWatch_Tick;
//timerWatch.Start();
timerTrace.Interval = 10 * 1000;
timerTrace.Tick += timerTrace_Tick;
//timerTrace.Start();
}
private void InitWatch()
{
m_7043.SetInputValue(0, '1');
m_7043.SetInputValue(1, '2');
m_7043.SetInputValue(2, '1');
m_7043.SetInputValue(3, 41);
m_7043.SetInputValue(4, '1');
m_7043.SetInputValue(5, '0');
m_7043.SetInputValue(6, '0');
m_7043.SetInputValue(7, 0);
m_7043.SetInputValue(8, 30);
}
private void AddTrace(string strCode)
{
if (m_TraceList.Contains(strCode) == true)
return;
m_TraceList.Add(strCode);
}
private bool IsOnTime()
{
DateTime CurTime = DateTime.Now;
if(CurTime.DayOfWeek == DayOfWeek.Sunday || CurTime.DayOfWeek == DayOfWeek.Saturday)
return false;
if(CurTime.Hour < 8 || CurTime.Hour > 15)
return false;
if(CurTime.Hour == 8 && CurTime.Minute < 50)
return false;
if(CurTime.Hour == 15 && CurTime.Minute > 15)
return false;
return true;
}
private void timerWatch_Tick(object sender, EventArgs e)
{
if (IsOnTime() == false)
return;
int iResult = m_7043.BlockRequest2(1);
if (iResult != 0)
{
MessageBox.Show("요청 에러");
return;
}
//string strMsg = string.Format("종목/현재가/대비플래그/대비/대비율/거래량/시가/시가대비/시가대비율/연속일");
//Console.WriteLine(strMsg);
//textBox1.Clear();
//textBox1.AppendText(strMsg + "\n");
bool bContinue = true;
while (bContinue)
{
int iCnt = m_7043.GetHeaderValue(0);
for (int i = 0; i < iCnt; i++)
{
object Code = m_7043.GetDataValue(0, i);
object Name = m_7043.GetDataValue(1, i);
object CurPrice = m_7043.GetDataValue(2, i);
object flag = m_7043.GetDataValue(3, i);
object Comp = m_7043.GetDataValue(4, i);
object CompRate = m_7043.GetDataValue(5, i);
object DealAmount = m_7043.GetDataValue(6, i);
object StartPrice = m_7043.GetDataValue(7, i);
object StartPriceComp = m_7043.GetDataValue(8, i);
object StartPriceCompRate = m_7043.GetDataValue(9, i);
object SerialDay = m_7043.GetDataValue(10, i);
if ((float)StartPriceCompRate >= 9.0f && (float)StartPriceCompRate <= 10.0f)
AddTrace((string)Code);
//cmd.CommandText = string.Format("insert into trace(code, name, time, curprice, askcount, bidcount, startprice, tradingvolume, rateperstart) values('{0}', '{1}', '{2}', now, {3}, {4}, {5}, {6}, {7})",
// Code, Name, CurPrice, );
//cmd.ExecuteNonQuery();
//strMsg = string.Format("[{0}:{1}]/{2}/{3}/{4}/{5}/{6}/{7}/{8}/{9}/{10}",
// Name.GetType(), Code.GetType(),
// CurPrice.GetType(), flag.GetType(), Comp.GetType(), CompRate.GetType(),
// DealAmount.GetType(), StartPrice.GetType(), StartPriceComp.GetType(), StartPriceCompRate.GetType(),
// SerialDay.GetType());
//Console.WriteLine(strMsg);
//textBox1.AppendText(strMsg + "\n");
}
bContinue = (m_7043.Continue == 1);
if (bContinue == true)
iResult = m_7043.BlockRequest2(1);
}
}
private void timerTrace_Tick(object sender, EventArgs e)
{
//0 - (string) 종목 코드
//1 - (string) 종목명
//2 - (long) 시간(HHMM)
//3 - (long) 현재가
//4 - (long) 전일대비
//5 - (char) 상태구분
// 코드 내용
// '1' 상한
// '2' 상승
// '3' 보합
// '4' 하한
// '5' 하락
// '6' 기세상한
// '7' 기세상승
// '8' 기세하한
// '9' 기세하락
//6 - (long) 시가
//7 - (long) 고가
//8 - (long) 저가
//9 - (long) 매도호가
//10 - (long) 매수호가
//11 - (unsigned long) 거래량 [주의] 단위 1주
//12 - (long) 거래대금 [주의] 단위 천원
//13 - (long) 총매도잔량
//14 - (long) 총매수잔량
//15 - (long) 매도잔량
//16 - (long) 매수잔량
//17 - (unsigned long) 상장주식수
//18 - (long) 외국인보유비율(%)
//19 - (long) 전일종가
//20 - (unsigned long) 전일거래량
//21 - (long) 체결강도
//22 - (unsigned long) 순간체결량
//23 - (char) 체결가비교 Flag
// 코드 내용
// 'O' 매도
// 'B' 매수
//24 - (char) 호가비교 Flag
// 코드 내용
// 'O' 매도
// 'B' 매수
//25- (char) 동시호가구분
// 코드 내용
// '1' 동시호가
// '2' 장중
//26 - (long) 예상체결가
//27 - (long) 예상체결가 전일대비
//28 - (long) 예상체결가 상태구분
// 코드 내용
// '1' 상한
// '2' 상승
// '3' 보합
// '4' 하한
// '5' 하락
// '6' 기세상한
// '7' 기세상승
// '8' 기세하한
// '9' 기세하락
//29- (unsigned long) 예상체결가 거래량
if(IsOnTime() == false)
return;
if (m_TraceList.Count <= 0)
return;
string strCodes = "";
for (int i = 0; i < m_TraceList.Count; i++)
{
if (i > 0)
strCodes += ",";
strCodes += m_TraceList[i];
}
m_StockMst2.SetInputValue(0, strCodes);
int iResult = 0;
iResult = m_StockMst2.BlockRequest2(1);
bool bContinue = true;
MySqlTransaction trans = m_DBCon.BeginTransaction();
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = m_DBCon;
cmd.Transaction = trans;
DateTime CurTime = DateTime.Now;
while(bContinue == true)
{
int iCnt = m_StockMst2.GetHeaderValue(0);
for (int i = 0; i < iCnt; i++)
{
object Code = m_StockMst2.GetDataValue(0, i);
object Name = m_StockMst2.GetDataValue(1, i);
object Time = m_StockMst2.GetDataValue(2, i);
object CurPrice = m_StockMst2.GetDataValue(3, i);
object StartPrice = m_StockMst2.GetDataValue(6, i);
object AskPrice = m_StockMst2.GetDataValue(9, i);
object BidPrice = m_StockMst2.GetDataValue(10, i);
object TradingVolume = m_StockMst2.GetDataValue(11, i);
object AskCount = m_StockMst2.GetDataValue(15, i);
object BidCount = m_StockMst2.GetDataValue(16, i);
float fRatePerStart = ((int)CurPrice - (int)StartPrice)*100 / (float)(int)StartPrice;
string time = CurTime.ToString("yyyy-MM-dd hh:mm:ss");
time = time.Substring(0, 10) + " " + (int)Time / 100 + ":" + (int)Time % 100 + ":00";
// insert to db
string query = string.Format("insert into trace(code, name, time, startprice, curprice, rateperstart, askprice, askcount, bidprice, bidcount, tradingvolume) values('{0}', '{1}', '{2}', {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10})",
Code, Name, time, StartPrice, CurPrice, fRatePerStart, AskPrice, AskCount, BidPrice, BidCount, TradingVolume);
Console.WriteLine(query);
cmd.CommandText = query;
cmd.ExecuteNonQuery();
// extract if loss
// extract if trailing loss
}
bContinue = (m_StockMst2.Continue == 1);
if (bContinue == true)
iResult = m_StockMst2.BlockRequest2(1);
}
trans.Commit();
}
private void btSearch_Click(object sender, EventArgs e)
{
}
private void cbSearch_CheckedChanged(object sender, EventArgs e)
{
if (cbSearch.Checked == true)
{
timerWatch.Start();
timerTrace.Start();
}
else
{
timerWatch.Stop();
timerTrace.Stop();
}
}
}
}