From 081c666c985333db45c3c6c50d82057709babdf7 Mon Sep 17 00:00:00 2001 From: kyh <1329522@naver.com> Date: Thu, 29 Aug 2024 11:02:17 +0900 Subject: [PATCH] test --- BitgetApi/BitgetApi.csproj | 1 - BitgetApi/CommonProperty.cs | 51 ++++++++- .../IndicatorProcess.cs | 103 ++++++++++++++++++ BitgetApi/WebService/WebConnection.cs | 29 ++++- 4 files changed, 173 insertions(+), 11 deletions(-) diff --git a/BitgetApi/BitgetApi.csproj b/BitgetApi/BitgetApi.csproj index 3cfffae..3f69e2e 100644 --- a/BitgetApi/BitgetApi.csproj +++ b/BitgetApi/BitgetApi.csproj @@ -94,7 +94,6 @@ MainForm.cs - ResXFileCodeGenerator Resources.Designer.cs diff --git a/BitgetApi/CommonProperty.cs b/BitgetApi/CommonProperty.cs index a68334b..c09d510 100644 --- a/BitgetApi/CommonProperty.cs +++ b/BitgetApi/CommonProperty.cs @@ -91,6 +91,10 @@ namespace BitgetAPI BitgetCandleStick_Coin = 22, [Description("BitgetMCandle_FUTURE")] BitgetCandleStick_Future = 23, + [Description("BitgetAccountList_FUTURE")] + BitgetAccountList_Future = 30, + [Description("BitgetOrder_FUTURE")] + BitgetOrder_Future = 31 #endregion } @@ -98,9 +102,47 @@ namespace BitgetAPI public enum BitgetWebSocketType { 현황정보_전체종목 = 0, - 캔들정보 = 1 + 캔들정보 = 1, + 보유List = 2, + 주문하기 = 3, } + public enum Side + { + [Description("buy")] + buy = 0, + [Description("sell")] + sell =1 + } + + public enum OrderType + { + [Description("limit")] + limit = 0, + [Description("market")] + market = 1, + } + + #region 선물거래 관련 + public enum FutureMarginMode + { + [Description("isolated")] + isolated = 0, + [Description("crossed")] + crossed = 1, + } + + + + public enum FutureSide + { + BuyShort = 0, + BuyLong = 1, + SellShort = 0, + SellLong = 1, + } + #endregion + // 시가/고가/종가/저가 public enum CandlePriceType { @@ -150,8 +192,7 @@ namespace BitgetAPI 캔들 = 1, 거래량 = 2 } - - + #region 업비트 URL public static string UpbitURL = "https://api.upbit.com/"; public static string UpbitURL_MarketInfo = "https://api.upbit.com/v1/market/all"; @@ -176,8 +217,10 @@ namespace BitgetAPI public static string BitgetURL_MarketInfo_FUTURE = "https://api.bitget.com/api/v2/mix/market/tickers?productType=USDT-FUTURES"; public static string BitgetURL_MarketCandle_Coin = "https://api.bitget.com/api/v2/spot/market/candles?symbol={0}&granularity={1}in&startTime={2}&endTime={3}&limit={4}"; - public static string BitgetURL_MarketCandle_Future = "https://api.bitget.com/api/v2/mix/market/candles?symbol={0}&granularity={1}&limit={2}&productType={3}"; + public static string BitgetURL_MarketCandle_Future = "https://api.bitget.com/api/v2/mix/market/candles?symbol={0}&granularity={1}&limit={2}&productType={3}"; + public static string BitgetURL_AccountList_Future = @"https://api.bitget.com/api/v2/mix/account/accounts?productType=USDT-FUTURES"; + public static string BitgetURL_BuySellOrder_Future = @"https://api.bitget.com/api/v2/mix/order/place-order?"; #endregion } diff --git a/BitgetApi/IndicatorConditionSetup/IndicatorProcess.cs b/BitgetApi/IndicatorConditionSetup/IndicatorProcess.cs index 8a0117c..47a9766 100644 --- a/BitgetApi/IndicatorConditionSetup/IndicatorProcess.cs +++ b/BitgetApi/IndicatorConditionSetup/IndicatorProcess.cs @@ -1,15 +1,29 @@ using BitgetApi.Data; using BitgetAPI.IndicatorConditionSetup; +using BitgetAPI.WebService; +using DevExpress.Utils.CodedUISupport; +using DevExpress.Utils.Filtering.Internal; using DevExpress.Utils.IoC; +using DevExpress.XtraPrinting.HtmlExport.Native; using DevExpress.XtraPrinting.Native; +using DevExpress.XtraSpellChecker; using System; using System.Collections.Generic; +using System.Collections.Specialized; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using static BitgetAPI.CommonProperty; +using System.Web; +using System.Windows.Media.Media3D; +using DevExpress.XtraPrinting; +using static System.Windows.Forms.VisualStyles.VisualStyleElement.Tab; +using static DevExpress.XtraPrinting.Native.ExportOptionsPropertiesNames; +using System.Security.Cryptography; +using System.Globalization; + namespace BitgetAPI.IndicatorConditionSetup { @@ -17,6 +31,11 @@ namespace BitgetAPI.IndicatorConditionSetup { CommonProperty commonProperty = CommonProperty.Instance; + //영호 Key + private string YHKIM_AccessKey = "bg_f93aa530288b6ec9d62095994c51972a"; + private string YHKIM_SecreteKey = "2fce18bcd12ca23a7920c48295a58da031443ea383af8114ed599c1b14ac678e"; + private string YHKIM_PassphraseKey = "dudgh0505"; + // 스레드 시작/종료 플래그 public bool Is_th_SignalInfo; public Dictionary> 전저점 = new Dictionary>(); @@ -67,6 +86,8 @@ namespace BitgetAPI.IndicatorConditionSetup signals_LD.Clear(); signals_SU.Clear(); + GetFutureAccountList(); + // 추후 공통화 및 소스정리 필요 while (true) { @@ -818,6 +839,88 @@ namespace BitgetAPI.IndicatorConditionSetup } } + public List> GetFutureAccountList() + { + var timeSpan = DateTime.Now; + + var timestamp = (((DateTimeOffset)timeSpan).ToUnixTimeSeconds() * 1000).ToString(); + + var reqStr = "/api/v2/mix/account/accounts"; + + var nvc = new NameValueCollection { + {"ACCESS-KEY", YHKIM_AccessKey}, + {"ACCESS-SIGN", GenerateSignString(timestamp, "GET", reqStr, "productType=USDT-FUTURES", null, YHKIM_SecreteKey) }, + {"ACCESS-PASSPHRASE", YHKIM_PassphraseKey }, + {"ACCESS-TIMESTAMP",timestamp} + }; + + return (List>)WebConnection.SendWebServer(ExchangeWebReqType.BitgetAccountList_Future, BitgetURL_AccountList_Future, null, MCandleTimeType.M1, "", "", nvc); + } + + + + public Dictionary FutureOrder(string symbol, FutureMarginMode marginMode, double size, double price, FutureSide futureSide, OrderType orderType) + { + var side = ""; + var tradeSide = ""; + + if(futureSide == FutureSide.BuyShort) + { + side = "sell"; + tradeSide = "open"; + } + else if (futureSide == FutureSide.BuyLong) + { + side = "buy"; + tradeSide = "Open"; + } + else if (futureSide == FutureSide.SellShort) + { + side = "sell"; + tradeSide = "close"; + } + else if (futureSide == FutureSide.SellShort) + { + side = "buy"; + tradeSide = "close"; + } + + if (orderType == OrderType.limit) + price = 0; + + var timeSpan = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)); + + var timestamp = (timeSpan.TotalSeconds * 1000).ToString(); + + var reqStr = "/api/v2/mix/order/place-order"; + + var nvc = new NameValueCollection { + {"ACCESS-KEY", new Guid().ToString()}, + {"ACCESS-SIGN", GenerateSignString(timestamp, "POST", reqStr, "", null, YHKIM_SecreteKey)}, + {"ACCESS-PASSPHRASE", YHKIM_PassphraseKey }, + {"ACCESS-TIMESTAMP", timestamp} + }; + + var postStr = string.Format("{\"symbol\": \"{0}\", \"productType\": \"usdt - futures\", \"marginMode\": \"{1}\", \"marginCoin\": \"USDT\", \"size\": \"{2}\", \"price\": \"{3}\", \"side\": \"{4}\", " + + "\"tradeSide\": \"{5}\", \"orderType\": \"{6}\", \"force\": \"gtc\", \"clientOid\": \"{8}\" }", + symbol, Utils.Convertor.GetEnumDescription((Enum)marginMode), size.ToString(), + price.ToString(), side, tradeSide, Utils.Convertor.GetEnumDescription((Enum)orderType), new Guid().ToString()); + + return (Dictionary)WebConnection.SendWebServer(ExchangeWebReqType.BitgetOrder_Future, BitgetURL_BuySellOrder_Future, null, MCandleTimeType.M1, postStr, "", nvc); + } + + public string GenerateSignString(string timestamp, string method, string requestPath, string queryString, string body, string secretKey) + { + method = method.ToUpper(); + body = body == null ? "" : body; + queryString = string.IsNullOrWhiteSpace(queryString) ? "" : "?" + queryString; + string preHash = timestamp + method + requestPath + queryString + body; + + var encSecretKey_byteData = System.Text.Encoding.UTF8.GetBytes(secretKey); + var encPreHash_byteData = System.Text.Encoding.UTF8.GetBytes(preHash); + + return Convert.ToBase64String(new HMACSHA256(encSecretKey_byteData).ComputeHash(encPreHash_byteData)); + } public void calIchimokuProcess_Add() { diff --git a/BitgetApi/WebService/WebConnection.cs b/BitgetApi/WebService/WebConnection.cs index fce8b34..7185968 100644 --- a/BitgetApi/WebService/WebConnection.cs +++ b/BitgetApi/WebService/WebConnection.cs @@ -23,6 +23,7 @@ using System.Runtime.InteropServices; using DevExpress.XtraEditors.Filtering; using DevExpress.XtraExport.Xls; using DevExpress.Utils.Filtering.Internal; +using System.Collections.Specialized; namespace BitgetAPI.WebService { @@ -40,7 +41,7 @@ namespace BitgetAPI.WebService public static object SendWebServer(CommonProperty.ExchangeWebReqType exchangeWebReqType, string url = "", MarketCandleDataList marketCandleDataList = null, CommonProperty.MCandleTimeType interval = MCandleTimeType.M1, - string postData = "", string subUri = "") + string postData = "", string subUri = "", NameValueCollection headers = null) { object returnJsonData = null; @@ -56,6 +57,8 @@ namespace BitgetAPI.WebService // 웹 전송 타입 지정 var webConType = CommonProperty.WebConnectionType.GET; + if(postData.Length > 0) webConType = CommonProperty.WebConnectionType.POST; + // 1. WebRequest 객체 형성 및 HttpWebRequest 로 형변환 // 2. 거래소에 따른 헤더 파일 처리(POST 일 경우만 지정함) @@ -82,13 +85,13 @@ namespace BitgetAPI.WebService break; case CommonProperty.ExchangeWebReqType.BitgetCandleStick_Future: wReq = (HttpWebRequest)WebRequest.Create(new Uri(CommonProperty.BitgetURL_MarketCandle_Future)); - break; - #endregion + break; + #endregion } } + else wReq = (HttpWebRequest)WebRequest.Create(new Uri(url)); - - + // GET, POST 구분 switch (webConType) { @@ -105,6 +108,12 @@ namespace BitgetAPI.WebService break; } + if (headers != null) + { + wReq.Headers.Add(headers); + wReq.ContentType = "application/json"; + } + using (var wRes = (HttpWebResponse)wReq.GetResponse()) { var respPostStream = wRes.GetResponseStream(); @@ -144,7 +153,6 @@ namespace BitgetAPI.WebService return commonProperty.marketInfoList; } - break; case CommonProperty.ExchangeWebReqType.BitgetCandleStick_Coin: { @@ -251,6 +259,15 @@ namespace BitgetAPI.WebService } } break; + case ExchangeWebReqType.BitgetAccountList_Future: + { + var datalist = JsonConvert.DeserializeObject>(resResult); + return JsonConvert.DeserializeObject>>(datalist["data"]?.ToString()); + } + case ExchangeWebReqType.BitgetOrder_Future: + { + return JsonConvert.DeserializeObject>(resResult); + } #endregion } }