Esempio n. 1
0
        public ByBitOrderResult PlaceNewConditionalOrder(
            Dictionary <string, object> order_query)
        {
            string         signature     = this.CreateSignature(this.api_sec_, order_query.ToQueryString().Substring(1).Replace("True", "true").Replace("False", "false"));
            ByBit_Response byBitResponse = this.Post <ByBit_Response>("/v2/private/stop-order/create", (Dictionary <string, object>)null, (object)new ByBitConditionalOrderRequest()
            {
                APIKey      = (string)order_query["api_key"],
                BasePrice   = (string)order_query["base_price"],
                OrderType   = (string)order_query["order_type"],
                Price       = (string)order_query["price"],
                Quantity    = (string)order_query["qty"],
                Side        = (string)order_query["side"],
                StopPrice   = (string)order_query["stop_px"],
                Symbol      = (string)order_query["symbol"],
                TimeInForce = (string)order_query["time_in_force"],
                TimeStamp   = (string)order_query["timestamp"],
                Sign        = signature
            });

            if (byBitResponse == null)
            {
                return((ByBitOrderResult)null);
            }
            if (byBitResponse.Result != null)
            {
                return(JsonConvert.DeserializeObject <ByBitOrderResult>(byBitResponse.Result.ToString()));
            }
            this.LastMessage = byBitResponse.RetMsg;
            this.LastError   = ErrorType.UnknownError;
            return((ByBitOrderResult)null);
        }
Esempio n. 2
0
        public ByBit_Position[] GetPositions(string symbol)
        {
            ByBit_Response byBitResponse = this.Get <ByBit_Response>("/v2/private/position/list", new Dictionary <string, object>()
            {
                {
                    "api_key",
                    (object)this.api_key_
                },
                {
                    nameof(symbol),
                    (object)symbol
                },
                {
                    "timestamp",
                    (object)DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString()
                }
            });

            if (byBitResponse != null && byBitResponse.Result != null)
            {
                ByBit_Position byBitPosition = JsonConvert.DeserializeObject <ByBit_Position>(byBitResponse.Result.ToString());
                if (byBitPosition != null)
                {
                    List <ByBit_Position> byBitPositionList = new List <ByBit_Position>();
                    if (double.Parse(byBitPosition.Size) > 0.0)
                    {
                        byBitPositionList.Add(byBitPosition);
                    }
                    return(byBitPositionList.ToArray());
                }
            }
            return((ByBit_Position[])null);
        }
Esempio n. 3
0
        private void RequestTicker(string symbol)
        {
            if (string.IsNullOrEmpty(symbol))
            {
                return;
            }
            string str = this.RequestHttp("https://api.bybit.com/v2/public/orderBook/L2?symbol=" + symbol, "GET");

            if (str.Substring(0, 14) == "RESPONSE_ERROR")
            {
                this.atb_manager_.AppendLog("ByBit : " + str);
            }
            else
            {
                try
                {
                    ByBit_Response byBitResponse = JsonConvert.DeserializeObject <ByBit_Response>(str);
                    if (byBitResponse == null || byBitResponse.RetCode != 0)
                    {
                        return;
                    }
                    double ask = 0.0;
                    List <ByBit_OrderBookData> bitOrderBookDataList = JsonConvert.DeserializeObject <List <ByBit_OrderBookData> >(byBitResponse.Result.ToString());
                    if (bitOrderBookDataList == null || bitOrderBookDataList.Count <= 10)
                    {
                        return;
                    }
                    double price = (double)bitOrderBookDataList[0].Price;
                    for (int index = 1; index < bitOrderBookDataList.Count; ++index)
                    {
                        if (bitOrderBookDataList[index].Side == "Sell")
                        {
                            ask = (double)bitOrderBookDataList[index].Price;
                            break;
                        }
                    }
                    this.atb_manager_.UpdateRate(nameof(ByBit), new DateTime(1970, 1, 1).AddSeconds(double.Parse(byBitResponse.TimeNow)).ToLocalTime(), price, ask);
                }
                catch (Exception ex)
                {
                }
            }
        }
Esempio n. 4
0
        public ByBit_Order[] GetConditionalOrders(string symbol)
        {
            Dictionary <string, object> query = new Dictionary <string, object>()
            {
                {
                    "api_key",
                    (object)this.api_key_
                },
                {
                    "stop_order_status",
                    (object)"Untriggered"
                },
                {
                    nameof(symbol),
                    (object)symbol
                },
                {
                    "timestamp",
                    (object)DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString()
                }
            };

            try
            {
                ByBit_Response byBitResponse = this.Get <ByBit_Response>("/v2/private/stop-order/list", query);
                if (byBitResponse != null)
                {
                    ByBit_OrderResult byBitOrderResult = JsonConvert.DeserializeObject <ByBit_OrderResult>(byBitResponse.Result.ToString());
                    if (byBitOrderResult != null)
                    {
                        return(byBitOrderResult.Data.ToArray());
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return((ByBit_Order[])null);
        }
Esempio n. 5
0
        public string GetAPIInfo()
        {
            ByBit_Response byBitResponse = this.Get <ByBit_Response>("/v2/private/account/api-key", new Dictionary <string, object>()
            {
                {
                    "api_key",
                    (object)this.api_key_
                },
                {
                    "timestamp",
                    (object)DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString()
                }
            });

            if (byBitResponse != null && byBitResponse.Result != null)
            {
                List <ByBit_APIInfo> byBitApiInfoList = JsonConvert.DeserializeObject <List <ByBit_APIInfo> >(byBitResponse.Result.ToString());
                if (byBitApiInfoList != null)
                {
                    return(byBitApiInfoList[0].UserID.ToString());
                }
            }
            return("");
        }
Esempio n. 6
0
        public ByBitMarginResult GetMargin()
        {
            ByBit_Response byBitResponse = this.Get <ByBit_Response>("/v2/private/wallet/balance", new Dictionary <string, object>()
            {
                {
                    "api_key",
                    (object)this.api_key_
                },
                {
                    "timestamp",
                    (object)DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString()
                }
            });

            if (byBitResponse != null && byBitResponse.Result != null)
            {
                ByBitMarginResult byBitMarginResult = JsonConvert.DeserializeObject <ByBitMarginResult>(byBitResponse.Result.ToString());
                if (byBitMarginResult != null)
                {
                    return(byBitMarginResult);
                }
            }
            return((ByBitMarginResult)null);
        }
Esempio n. 7
0
        public override List <Rate> GetOHLCData(string time_frame, int watches)
        {
            string str1 = "1";
            int    num1 = 60;

            if (time_frame == "M1")
            {
                str1 = "1";
                num1 = 1;
            }
            else if (time_frame == "M5")
            {
                str1 = "5";
                num1 = 5;
            }
            else if (time_frame == "M15")
            {
                str1 = "15";
                num1 = 15;
            }
            else if (time_frame == "H1")
            {
                str1 = "60";
                num1 = 60;
            }
            else if (time_frame == "H4")
            {
                str1 = "240";
                num1 = 240;
            }
            else if (time_frame == "D1")
            {
                str1 = "D1";
                num1 = 1440;
            }
            long   num2 = DateTimeOffset.UtcNow.ToUnixTime() - (long)(watches * num1 * 60);
            string str2 = this.RequestHttp("https://api.bybit.com/v2/public/mark-price-kline?symbol=" + Constants.TradeSymbol + "&interval=" + str1 + "&from=" + num2.ToString(), "GET");

            if (str2.Substring(0, 14) == "RESPONSE_ERROR")
            {
                this.atb_manager_.AppendLog("ByBit : " + str2);
                return((List <Rate>)null);
            }
            try
            {
                ByBit_Response byBitResponse = JsonConvert.DeserializeObject <ByBit_Response>(str2);
                DateTime       dateTime      = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
                if (byBitResponse != null)
                {
                    if (byBitResponse.RetCode == 0)
                    {
                        List <ByBit_OHLCData> byBitOhlcDataList = JsonConvert.DeserializeObject <List <ByBit_OHLCData> >(byBitResponse.Result.ToString());
                        if (byBitOhlcDataList == null)
                        {
                            return((List <Rate>)null);
                        }
                        List <Rate> rateList = new List <Rate>();
                        foreach (ByBit_OHLCData byBitOhlcData in byBitOhlcDataList)
                        {
                            rateList.Add(new Rate(dateTime.AddSeconds((double)byBitOhlcData.StartAt).ToLocalTime(), byBitOhlcData.Open, byBitOhlcData.Open, byBitOhlcData.High, byBitOhlcData.High, byBitOhlcData.Low, byBitOhlcData.Low, byBitOhlcData.Close, byBitOhlcData.Close));
                        }
                        return(rateList);
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return((List <Rate>)null);
        }