Files
BitGet_API/BitgetApi/MainForm.cs

264 lines
11 KiB
C#
Raw Normal View History

2024-05-22 16:56:51 +09:00
using BitgetAPI.Data;
2024-05-23 16:32:30 +09:00
using BitgetAPI.WebService;
2024-05-22 16:56:51 +09:00
using DevExpress.XtraEditors;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.WebSockets;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
2024-05-23 16:32:30 +09:00
using static BitgetAPI.CommonProperty;
using static DevExpress.Utils.Html.ViewInfo.DxHtmlOrderedListInfoCore;
2024-05-22 16:56:51 +09:00
2024-05-23 16:32:30 +09:00
namespace BitgetAPI
2024-05-22 16:56:51 +09:00
{
public partial class MainForm : Form
{
CommonProperty commonProperty = CommonProperty.Instance;
#region 폼 내 사용되는 스레드
// 트리 현황판 업데이트 스레드
Thread treeListMarketInfoListUpdateTimer = null;
// 현황판 웹소켓 스레드
Thread th_MarketStatus = null;
// 종목별 캔들 데이터 스레드
Thread th_MarketCandeData = null;
// 종목별 캔들 데이터 : 캔들 개수
const int candleCount = 200;
#endregion
2024-05-22 16:56:51 +09:00
public MainForm()
{
InitializeComponent();
init();
}
// 폼 초기화 작업
public void init()
{
2024-05-23 16:32:30 +09:00
commonProperty.mainform = this;
2024-05-22 16:56:51 +09:00
// 거래소 항목 추가 및 설정하기
comboBoxEdit_ExchangePlace.Properties.Items.AddRange(Enum.GetNames(typeof(CommonProperty.ExchangePlace)));
comboBoxEdit_ExchangePlace.SelectedIndex = (int)CommonProperty.ExchangePlace.Bitget;
commonProperty.selectedChangePlace = CommonProperty.ExchangePlace.Bitget;
2024-05-23 16:32:30 +09:00
treeList_MarketInfo.DataSource = commonProperty.webSocketCandles;
// 현황판 정렬
treeList_MarketInfo.BeginSort();
treeList_MarketInfo.Columns["quoteVolume"].SortOrder = SortOrder.Descending;
treeList_MarketInfo.EndSort();
// 현황판 웹소켓
th_MarketStatus = new Thread(() => ThreadEventAsync(BitgetWebSocketType.현황정보_전체종목, "ticker"));
th_MarketStatus.Start();
// 트리 현황판 업데이트
treeListMarketInfoListUpdateTimer = new Thread(new ThreadStart(TreeListMarketInfoListUpdate));
treeListMarketInfoListUpdateTimer.Start();
2024-05-22 16:56:51 +09:00
// API Key 가져오기 및 등록
// BITGET Raster 소켓 테스트
// 데이터 분석
// 캔들 가져오기
// 구름떼, 볼린저 생성
}
private async void ThreadEventAsync(BitgetWebSocketType bitgetWebSocketType, string channel)
2024-05-22 16:56:51 +09:00
{
// WebSocket 생성
var webConnection = new WebConnection();
webConnection.BitGetWebSocektStart(bitgetWebSocketType, channel);
2024-05-23 16:32:30 +09:00
}
2024-05-22 16:56:51 +09:00
private void TreeListMarketInfoListUpdate( )
2024-05-23 16:32:30 +09:00
{
while(true)
2024-05-22 16:56:51 +09:00
{
Thread.Sleep(300);
2024-05-22 16:56:51 +09:00
2024-05-23 16:32:30 +09:00
Invoke(new MethodInvoker(delegate ()
2024-05-22 16:56:51 +09:00
{
2024-05-23 16:32:30 +09:00
treeList_MarketInfo.RefreshDataSource();
treeList_MarketInfo.ClearSelection();
}));
2024-05-22 16:56:51 +09:00
}
}
// 종목별 캔들데이터 가져오기 (초기 설정)
private void getMarketCandleData_WebService(ExchangeWebReqType exchangeWebReqType)
{
// WebServicee 초기 캔들 데이터 가져오기 : 종목, 개수
foreach (var marketCandleDataList in commonProperty.candleDataLists)
{
var marketName = marketCandleDataList.ConGoodsData.MarketName;
// 캔들 데이터 가져오기
foreach (var interval in Enum.GetValues(typeof(CommonProperty.MCandleTimeType)))
{
// Description
var description = Utils.Convertor.GetEnumDescription((Enum)interval);
// 1M, 3M Names....
var intervalName = Enum.GetName(typeof(CommonProperty.MCandleTimeType), interval);
var url = "";
switch (commonProperty.selectedChangePlace)
{
case ExchangePlace.Upbit:
break;
case ExchangePlace.Bitget:
switch (commonProperty.selectedBitgetProductType)
{
case BitgetProductType.COIN:
exchangeWebReqType = ExchangeWebReqType.BitgetCandleStick_Coin;
var defaultDateTime = DateTime.Now;
var startDateTime = DateTime.FromFileTimeUtc(defaultDateTime.Ticks);
// count + 50 개 캔들 추가 가져오기
var endDateTime = DateTime.FromFileTimeUtc(defaultDateTime.AddSeconds((int)interval * -1).Ticks);
// 종목, 인터벌시간, 시작시간(UTC), 종료시간(UTC), 제한 개수 (default:100, Max:1000)
url = string.Format(BitgetURL_MarketCandle_Coin,
marketName, description, startDateTime.ToString(), endDateTime.ToString(), candleCount.ToString());
break;
case BitgetProductType.FUTURE:
exchangeWebReqType = ExchangeWebReqType.BitgetCandleStick_Future;
url = string.Format(CommonProperty.BitgetURL_MarketCandle_Future,
marketName, description, candleCount.ToString(), "USDT-FUTURES");
break;
}
break;
}
// 초당 20회 제한 !!!!
// Thread.Sleep(50);
var candleList = BitgetAPI.WebService.WebConnection.SendWebServer(exchangeWebReqType, url, marketCandleDataList, (CommonProperty.MCandleTimeType) interval);
}
break;
}
}
2024-05-22 16:56:51 +09:00
// 거래소 콤보박스 클릭 시 이벤트 처리
private void comboBox_ExchangePlace_SelectedIndexChanged(object sender, EventArgs e)
{
var combobox = (ComboBoxEdit)sender;
// 거래소에 따라서 코인 타입 생성하기
switch (combobox.SelectedIndex)
{
case (int)CommonProperty.ExchangePlace.Bitget:
// 코인 타입 설정하기
comboBoxEdit_ConinType.Properties.Items.AddRange(Enum.GetNames(typeof(CommonProperty.BitgetProductType)));
comboBoxEdit_ConinType.SelectedIndex = (int)CommonProperty.BitgetProductType.FUTURE;
commonProperty.selectedBitgetProductType = CommonProperty.BitgetProductType.FUTURE;
break;
}
}
// 코인 타입 콤보박스 클릭 시 이벤트 처리
private void comboBox_CoinType_SelectedIndexChanged(object sender, EventArgs e)
{
var combobox = (ComboBoxEdit)sender;
// 선택된 타입에 따라서 종목 정보 가져오기
ExchangeWebReqType exchangeWebReqType = ExchangeWebReqType.BitgetMarket_Future;
switch (combobox.SelectedIndex)
{
case (int)CommonProperty.BitgetProductType.COIN:
commonProperty.selectedBitgetProductType = CommonProperty.BitgetProductType.COIN;
exchangeWebReqType = ExchangeWebReqType.BitgetMarket_Spot;
break;
case (int)CommonProperty.BitgetProductType.FUTURE:
commonProperty.selectedBitgetProductType = CommonProperty.BitgetProductType.FUTURE;
exchangeWebReqType = ExchangeWebReqType.BitgetMarket_Future;
break;
case (int)CommonProperty.BitgetProductType.MARGIN_보류:
commonProperty.selectedBitgetProductType = CommonProperty.BitgetProductType.MARGIN_보류;
break;
}
// 마켓 정보 가져오기
var marketInfos = (List<MarketInfo>)BitgetAPI.WebService.WebConnection.SendWebServer(exchangeWebReqType);
labelControl_MarketCount.Text = marketInfos.Count().ToString();
treeList_MarketInfo.DataSource = marketInfos;
treeList_MarketInfo.RefreshDataSource();
List<MarketCandleDataList> candleDataLists = new List<MarketCandleDataList>();
commonProperty.candleDataLists = candleDataLists;
2024-05-23 16:32:30 +09:00
// 캔들 리스트 생성하기
foreach (var item in marketInfos)
{
MarketCandleDataList candleDataList = new MarketCandleDataList();
candleDataList.ConGoodsData = item;
foreach (var interval in Enum.GetValues(typeof(CommonProperty.MCandleTimeType)))
{
candleDataList.candleDataList.Add((MCandleTimeType)interval, new List<CandleData>());
}
candleDataLists.Add(candleDataList);
}
// 조건 설정 : 체크콤보박스 마켓 정보 추가하기
foreach (var item in commonProperty.candleDataLists)
checkedComboBoxEdit_MarketInfoList.Properties.Items.Add(item.ConGoodsData.MarketName);
2024-05-23 16:32:30 +09:00
commonProperty.webSocketCandles = new List<BitgetApi.Data.WebSocketTickerInfo>();
#region 캔들 웹소켓 : 종목 50, 캔들 1 개만 해도 뒤지려고 함 !! (일딴 패스)
/*
// 시간별 분봉 웹소켓
foreach (var interval in Enum.GetValues(typeof(CommonProperty.MCandleTimeType)))
{
var description = Utils.Convertor.GetEnumDescription((Enum)interval);
var th_CandleStatus = new Thread(() => ThreadEventAsync(BitgetWebSocketType.캔들정보, "candle" + description));
th_CandleStatus.Start();
break;
}*/
#endregion
2024-05-22 16:56:51 +09:00
// WebServicee 초기 캔들 데이터 가져오기 : 종목, 개수
// 종목별 캔들 데이터 가져오기 (WebService)
th_MarketCandeData = new Thread(() => getMarketCandleData_WebService(exchangeWebReqType));
th_MarketCandeData.Start();
2024-05-23 16:32:30 +09:00
}
2024-05-22 16:56:51 +09:00
// 폼 닫기 시 처리 이벤트
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
2024-05-23 16:32:30 +09:00
{
if (th_MarketStatus != null)
{
th_MarketStatus.Abort();
th_MarketStatus = null;
}
if (treeListMarketInfoListUpdateTimer != null)
{
treeListMarketInfoListUpdateTimer.Abort();
treeListMarketInfoListUpdateTimer = null;
}
2024-05-22 16:56:51 +09:00
}
}
}