buy sell 관련 함수 정리 및 처리 -> 테스트는 필요

This commit is contained in:
kyh
2024-08-29 13:10:26 +09:00
parent c15a5f000b
commit 6bdf3ba130
2 changed files with 115 additions and 9 deletions

View File

@@ -98,7 +98,16 @@ namespace BitgetAPI
[Description("BitgetOrderCancel_FUTURE")]
BitgetCancelOrder_Future = 32,
[Description("BitgetPendingOrder_FUTURE")]
BitgetPendingOrder_Future = 33
BitgetPendingOrder_Future = 33,
[Description("BitgetChangeLeverage_FUTURE")]
BitgetChangeLeverage_Future = 34,
[Description("BitgetChangeMarginMode_FUTURE")]
BitgetChangeMarginMode_Future = 35,
[Description("BitgetChangePositionMode_FUTURE")]
BitgetChangePositionMode_Future = 36,
[Description("BitgetOrderDetail_FUTURE")]
BitgetOrderDetail_Future = 37,
#endregion
}
@@ -119,6 +128,14 @@ namespace BitgetAPI
sell =1
}
public enum FuturePositionMode
{
[Description("one_way_mode")]
one_way_mode = 0,
[Description("hedge_mode")]
hedge_mode = 1
}
public enum OrderType
{
[Description("limit")]
@@ -227,6 +244,11 @@ namespace BitgetAPI
public static string BitgetURL_BuySellOrder_Future = @"https://api.bitget.com/api/v2/mix/order/place-order";
public static string BitgetURL_CancelOrder_Future = @"https://api.bitget.com/api/v2/mix/order/cancel-order";
public static string BitgetURL_PendingOrders_Future = @"https://api.bitget.com/api/v2/mix/order/orders-pending?productType=usdt-futures";
public static string BitgetURL_ChangeLeverage_Future = @"https://api.bitget.com/api/v2/mix/account/set-leverage";
public static string BitgetURL_ChangeMarginMode_Future = @"https://api.bitget.com/api/v2/mix/account/set-margin-mode";
public static string BitgetURL_ChangePositionMode_Future = @"https://api.bitget.com/api/v2/mix/account/set-position-mode";
public static string BitgetURL_OrderDetail_Future = @"https://api.bitget.com/api/v2/mix/order/detail";
#endregion
}
}

View File

@@ -23,6 +23,7 @@ using static System.Windows.Forms.VisualStyles.VisualStyleElement.Tab;
using static DevExpress.XtraPrinting.Native.ExportOptionsPropertiesNames;
using System.Security.Cryptography;
using System.Globalization;
using Newtonsoft.Json;
namespace BitgetAPI.IndicatorConditionSetup
@@ -88,6 +89,18 @@ namespace BitgetAPI.IndicatorConditionSetup
GetFutureAccountList();
/**
* 사는 방법
* 1. ChangeFuturePositionMode 호출(한방향, 양방향 -> Long, Short)
* 2. FutureOrder 호출
* 1). Bitget_ChangeMarginMode_Future 를 통해Coin, ProductType 별 Margin Mode 변경
* 2). 변경된 Margin Coin 별 MarginMode에 Leverage를 Set 함
* 3). Buy 진행
* 3. GetPendingOrders를 통해 확인 가능
* 4. Sell 시 FutureOrder 호출
* 5. Order Detail를 통해 Client OID를 획득 후 처리
*/
// 추후 공통화 및 소스정리 필요
while (true)
{
@@ -890,7 +903,7 @@ namespace BitgetAPI.IndicatorConditionSetup
var reqStr = "/api/v2/mix/order/cancel-order";
var nvc = new NameValueCollection {
{"ACCESS-KEY", new Guid().ToString()},
{"ACCESS-KEY", YHKIM_AccessKey},
{"ACCESS-SIGN", GenerateSignString(timestamp, "POST", reqStr, "", null, YHKIM_SecreteKey)},
{"ACCESS-PASSPHRASE", YHKIM_PassphraseKey },
{"ACCESS-TIMESTAMP", timestamp}
@@ -901,18 +914,40 @@ namespace BitgetAPI.IndicatorConditionSetup
return (Dictionary<string, object>)WebConnection.SendWebServer(ExchangeWebReqType.BitgetCancelOrder_Future, BitgetURL_CancelOrder_Future, null, MCandleTimeType.M1, postStr, "", nvc);
}
public Dictionary<string, object> FutureOrder(string symbol, FutureMarginMode marginMode, double size, double price, FutureSide futureSide, OrderType orderType)
public Dictionary<string, object> ChangeFuturePositionMode(FuturePositionMode mode)
{
var timeSpan = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0));
var timestamp = (timeSpan.TotalSeconds * 1000).ToString();
var reqStr = "/api/v2/mix/account/set-position-mode";
var nvc = new NameValueCollection {
{"ACCESS-KEY", YHKIM_AccessKey},
{"ACCESS-SIGN", GenerateSignString(timestamp, "POST", reqStr, "", null, YHKIM_SecreteKey)},
{"ACCESS-PASSPHRASE", YHKIM_PassphraseKey },
{"ACCESS-TIMESTAMP", timestamp}
};
var postStr = string.Format("{\"productType\" : \"USDT-FUTURES\", \"posMode\" : \"{0}\"}", Utils.Convertor.GetEnumDescription((Enum)mode));
return (Dictionary<string, object>)WebConnection.SendWebServer(ExchangeWebReqType.BitgetChangePositionMode_Future, BitgetURL_ChangePositionMode_Future, null, MCandleTimeType.M1, postStr, "", nvc);
}
public Dictionary<string, object> FutureOrder(string symbol, FutureMarginMode marginMode, double size, double price, FutureSide futureSide, OrderType orderType, string leverage, string id = "")
{
var side = "";
var tradeSide = "";
if(futureSide == FutureSide.BuyShort)
if (futureSide == FutureSide.BuyShort)
{
id = new Guid().ToString();
side = "sell";
tradeSide = "open";
}
else if (futureSide == FutureSide.BuyLong)
{
id = new Guid().ToString();
side = "buy";
tradeSide = "Open";
}
@@ -937,16 +972,65 @@ namespace BitgetAPI.IndicatorConditionSetup
var reqStr = "/api/v2/mix/order/place-order";
var nvc = new NameValueCollection {
{"ACCESS-KEY", new Guid().ToString()},
{"ACCESS-KEY", YHKIM_AccessKey},
{"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}\", " +
if(futureSide == FutureSide.BuyShort|| futureSide == FutureSide.BuyLong)
{
//change margin mode 이후 leverage 처리 해야 함
var changeMarginModeStr = string.Format("{\"symbol\": \"{0}\", \"productType\": \"usdt-futures\", \"marginCoin\": \"USDT\", \"marginMode\": \"{1}\"}",
symbol, Utils.Convertor.GetEnumDescription((Enum)marginMode));
var nvcMargin = new NameValueCollection {
{"ACCESS-KEY", YHKIM_AccessKey},
{"ACCESS-SIGN", GenerateSignString(timestamp, "POST", "/api/v2/mix/account/set-margin-mode", "", null, YHKIM_SecreteKey)},
{"ACCESS-PASSPHRASE", YHKIM_PassphraseKey },
{"ACCESS-TIMESTAMP", timestamp}
};
var result = (Dictionary<string, object>)WebConnection.SendWebServer(ExchangeWebReqType.BitgetChangeMarginMode_Future, BitgetURL_ChangeMarginMode_Future, null, MCandleTimeType.M1, changeMarginModeStr, "", nvcMargin);
if (result["msg"]?.ToString() != "success")
{
return null;
}
var leverageStr = string.Format("{\"symbol\": \"{0}\", \"productType\": \"usdt-futures\", \"marginCoin\": \"USDT\", \"leverage\": \"{1}\", \"holdSide\": \"{3}\"}",
symbol, leverage, futureSide == FutureSide.BuyShort ? "short" : "long");
var nvcLeverage = new NameValueCollection {
{"ACCESS-KEY", YHKIM_AccessKey},
{"ACCESS-SIGN", GenerateSignString(timestamp, "POST", "/api/v2/mix/account/set-leverage", "", null, YHKIM_SecreteKey)},
{"ACCESS-PASSPHRASE", YHKIM_PassphraseKey },
{"ACCESS-TIMESTAMP", timestamp}
};
result = (Dictionary<string, object>)WebConnection.SendWebServer(ExchangeWebReqType.BitgetChangeLeverage_Future, BitgetURL_ChangeLeverage_Future, null, MCandleTimeType.M1, leverageStr, "", nvcLeverage);
if (result["msg"]?.ToString() != "success")
{
return null;
}
}
else
{
// sell은 ClientOID 가 필요
var orderDetailStr = "/api/v2/mix/order/detail";
var orderDetailnvc = new NameValueCollection {
{"ACCESS-KEY", YHKIM_AccessKey},
{"ACCESS-SIGN", GenerateSignString(timestamp, "GET", orderDetailStr, "symbol="+symbol.ToString()+"&orderId="+id.ToString()+"&productType=usdt-future", null, YHKIM_SecreteKey) },
{"ACCESS-PASSPHRASE", YHKIM_PassphraseKey },
{"ACCESS-TIMESTAMP",timestamp}
};
var orderDetail = (Dictionary<string, object>)WebConnection.SendWebServer(ExchangeWebReqType.BitgetOrderDetail_Future, BitgetURL_OrderDetail_Future, null, MCandleTimeType.M1, "", "", orderDetailnvc);
if (orderDetail["msg"]?.ToString() != "success") return null;
var data = JsonConvert.DeserializeObject<Dictionary<string, object>>(JsonConvert.SerializeObject(orderDetail["data"]));
id = data["clientOid"].ToString();
}
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());
symbol, Utils.Convertor.GetEnumDescription((Enum)marginMode), size.ToString(),
price.ToString(), side, tradeSide, Utils.Convertor.GetEnumDescription((Enum)orderType), id);
return (Dictionary<string, object>)WebConnection.SendWebServer(ExchangeWebReqType.BitgetOrder_Future, BitgetURL_BuySellOrder_Future, null, MCandleTimeType.M1, postStr, "", nvc);
}