239 lines
9.8 KiB
C#
239 lines
9.8 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Collections.Generic;
|
|
using Million.Data;
|
|
using static Million.CommonProperty;
|
|
using System.Threading;
|
|
using System.Windows.Forms;
|
|
|
|
namespace Million.DbManagement
|
|
{
|
|
public class DbServiceFunctions
|
|
{
|
|
/// <summary>
|
|
/// DB Market 테이블 생성
|
|
/// </summary>
|
|
/// <param name="market"></param>
|
|
/// <returns></returns>
|
|
public static string CreateMarketTableQuery(string tableName)
|
|
{
|
|
var resultstring = "";
|
|
|
|
if (!string.IsNullOrWhiteSpace(tableName))
|
|
{
|
|
resultstring = @"if not exists (select * from sysobjects where name='" + tableName + @"' and xtype='U')
|
|
create table " + "\"" + tableName + "\"" + @"(
|
|
ExchangePlace nvarchar(50) not null,
|
|
KorName nvarchar(50),
|
|
CodeFullName nvarchar(50) not null,
|
|
CodeName nvarchar(50) not null,
|
|
Payment nvarchar(50) not null,
|
|
IsValid bit not null
|
|
) ";
|
|
}
|
|
return resultstring;
|
|
}
|
|
|
|
/// <summary>
|
|
/// DB 거래소 내 Market 테이블 생성
|
|
/// </summary>
|
|
/// <param name="market"></param>
|
|
/// <returns></returns>
|
|
public static string CreateMarketTable_ToDB(string tableName)
|
|
{
|
|
var resultstring = "";
|
|
|
|
if (!string.IsNullOrWhiteSpace(tableName))
|
|
{
|
|
resultstring = @"if not exists (select * from sysobjects where name='" + tableName + @"' and xtype='U')
|
|
create table " + "\"" + tableName + "\"" + @"(
|
|
DateTime datetime not null,
|
|
StartValue float not null,
|
|
EndValue float not null,
|
|
HighValue float not null,
|
|
LowValue float not null,
|
|
MoneyVolume float not null,
|
|
CandleTimeType int not null
|
|
); ";
|
|
}
|
|
return resultstring;
|
|
}
|
|
|
|
/// <summary>
|
|
/// DB로부터 코인(Market) 정보 가져오기
|
|
/// </summary>
|
|
/// <param name="tableName"></param>
|
|
/// <returns></returns>
|
|
public static void GetMarketTable_FromDB(string TableName)
|
|
{
|
|
var strDbCon = $"{CommonProperty.DbConnectionStr}Market";
|
|
|
|
// Query문 만들기
|
|
var sqlQuery = string.Format("select * from dbo.{0} where IsValid = 1;", TableName);
|
|
|
|
selectedMarketOfPlaceList = DBConnection.QueryGetExcuter_Market(strDbCon, sqlQuery);
|
|
}
|
|
|
|
/// <summary>
|
|
/// DB에서 각 Market 별 가장 최신, 오래된 데이터 한개씩 가져오기
|
|
/// </summary>
|
|
/// <param name="marketNameList"></param>
|
|
/// <param name="databaseName"></param>
|
|
/// <returns></returns>
|
|
public static List<CandleDataList> GetOldNewDataList_FromDB(List<MarketData> marketNameList, string databaseName)
|
|
{
|
|
var returnData = new List<CandleDataList>();
|
|
var strDbCon = CommonProperty.DbConnectionStr + databaseName;
|
|
|
|
//최신 데이터
|
|
foreach (var item in marketNameList)
|
|
{
|
|
var data = new CandleDataList();
|
|
data.ConGoodsData = item;
|
|
|
|
var sqlQuery = string.Format("SELECT TOP(1) * FROM [{0}] ORDER BY [DateTime] DESC", item.CodeFullName);
|
|
var recentlyDataList = DBConnection.QueryGetExcuter_Candle(strDbCon, sqlQuery);
|
|
|
|
if (recentlyDataList.Count > 0)
|
|
{
|
|
recentlyDataList[0].CandleType = CandleType.New;
|
|
data.CandleDataLists_Im = recentlyDataList;
|
|
}
|
|
else
|
|
{
|
|
data.CandleDataLists_Im = new List<CandleData>();
|
|
}
|
|
returnData.Add(data);
|
|
}
|
|
|
|
//오래된 데이터
|
|
foreach (var item in returnData)
|
|
{
|
|
if (item.CandleDataLists_Im.Count == 0)
|
|
continue;
|
|
|
|
// 1개라도 있을 경우
|
|
var sqlQuery = string.Format("SELECT TOP(1) * FROM [{0}] ORDER BY [DateTime] ASC", item.ConGoodsData.CodeFullName);
|
|
|
|
var recentlyDataList = DBConnection.QueryGetExcuter_Candle(strDbCon, sqlQuery);
|
|
if (recentlyDataList.Count > 0)
|
|
{
|
|
recentlyDataList[0].CandleType = CandleType.Old;
|
|
item.CandleDataLists_Im.AddRange(recentlyDataList);
|
|
}
|
|
}
|
|
|
|
return returnData;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 최신값, 가장오래된 값을 기준으로 Data 호출 및 처리
|
|
/// </summary>
|
|
/// <param name="place"></param>
|
|
/// <param name="recentlyDbDataList"></param>
|
|
public static void SetCandleData(string placeName, ExchangeWebReqType place, List<CandleDataList> recentlyDbDataList)
|
|
{
|
|
var dateTime = DateTime.Now;
|
|
|
|
//데이터 쌓는 부분은 재귀로 처리할 예정
|
|
/**
|
|
* 단계 조정
|
|
* 아무것도 없을 경우 0
|
|
* 있을 경우1
|
|
* 완료 2
|
|
*/
|
|
|
|
var insertQuery = "";
|
|
|
|
foreach (var item in recentlyDbDataList)
|
|
{
|
|
var sw = new System.Diagnostics.Stopwatch();
|
|
sw.Start();
|
|
var tableName = item.ConGoodsData.CodeFullName;
|
|
int step = 0;
|
|
var recentlyFlag = true;
|
|
switch (place)
|
|
{
|
|
case ExchangeWebReqType.UpbitCandleStick:
|
|
|
|
//최신 부터
|
|
var recentlyDateTime = new DateTime(1970, 1, 1);
|
|
var oldDateTime = new DateTime(1970, 1, 1);
|
|
CandleData recentlyData = null;
|
|
CandleData oldData = null;
|
|
|
|
if (item.CandleDataLists_Im.Count > 0)
|
|
{
|
|
recentlyData = item.CandleDataLists_Im.Find(t => t.CandleType == CandleType.New);
|
|
recentlyDateTime = recentlyData.DateTime;
|
|
oldData = item.CandleDataLists_Im.Find(t => t.CandleType == CandleType.Old);
|
|
oldDateTime = oldData.DateTime;
|
|
|
|
step = 1;
|
|
}
|
|
|
|
while (true)
|
|
{
|
|
var webDataList = UpbitApi.GetCandleStics(item.ConGoodsData.CodeFullName, CandleTimeType.M1, dateTime);
|
|
|
|
var insertDataList = new List<CandleData>();
|
|
|
|
if (step == 0)
|
|
{
|
|
insertDataList = webDataList;
|
|
}
|
|
else if (step == 1)
|
|
{
|
|
insertDataList.AddRange(webDataList.Where(t => t.DateTime > recentlyDateTime).ToList());
|
|
insertDataList.AddRange(webDataList.Where(t => t.DateTime < oldDateTime).ToList());
|
|
}
|
|
|
|
insertDataList = insertDataList.OrderBy(t => t.DateTime).ToList();
|
|
|
|
//데이터 insert
|
|
foreach (var data in insertDataList)
|
|
{
|
|
insertQuery += string.Format("INSERT INTO [dbo].[{0}]([DateTime],[StartValue], [EndValue], [HighValue], [LowValue], [MoneyVolume], [CandleTimeType]) VALUES ('{1}',{2},{3},{4},{5},{6},{7})",
|
|
tableName, data.DateTime.ToString("yyyy-MM-dd HH:mm:ss"), data.StartValue, data.EndValue, data.HighValue, data.LowValue, data.MoneyVolume, data.CandleTimeType);
|
|
}
|
|
|
|
if (insertDataList.Count == 0)
|
|
{
|
|
if (recentlyFlag)
|
|
{
|
|
recentlyFlag = false;
|
|
dateTime = oldDateTime.ToUniversalTime();
|
|
}
|
|
else
|
|
{
|
|
step = 2;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dateTime = insertDataList[0].DateTime.ToUniversalTime();
|
|
}
|
|
|
|
if (step == 2)
|
|
break;
|
|
//초당 8회 이상안됨 (0.125초)
|
|
if (!string.IsNullOrWhiteSpace(insertQuery))
|
|
DBConnection.QueryUpdateExcuter(DbConnectionStr + placeName, insertQuery);
|
|
Thread.Sleep(130);
|
|
insertQuery = "";
|
|
}
|
|
|
|
break;
|
|
case ExchangeWebReqType.BithumbCandleTicker:
|
|
break;
|
|
|
|
}
|
|
|
|
sw.Stop();
|
|
Console.WriteLine("Completed Set Candle Data of Table : " + tableName + ", Times : " + sw.Elapsed.ToString());
|
|
}
|
|
MessageBox.Show("Completed Set Candle Data of Place : " + placeName);
|
|
}
|
|
}
|
|
}
|