public IScriptLastTrades GetLastTrades(IScriptMarket market)
        {
            LastTradesContainer trades = null;

            try
            {
                var response = Query(false, "/spot/v3/instruments/" + market.SecondaryCurrency.ToUpper() + "-" + market.PrimaryCurrency.ToUpper() + "/trades", new Dictionary <string, string>()
                {
                    { "limit", "100" }
                });

                if (response != null && response.Value <bool>("success"))
                {
                    trades        = new LastTradesContainer();
                    trades.Market = market;
                    trades.Trades = response.Value <JArray>("result")
                                    .Select(c => Trade.ParsePublicTrade(market, c as JObject))
                                    .Cast <IScriptOrder>()
                                    .OrderByDescending(c => c.Timestamp)
                                    .ToList();
                }
            }
            catch (Exception e)
            {
                OnError(e.Message);
            }

            return(trades);
        }
Beispiel #2
0
        public IScriptOrderbook GetOrderbook(IScriptMarket market)
        {
            IScriptOrderbook orderbook = null;

            Console.Out.WriteLine("GetOrderbook");

            try
            {
                Market instrument = _markets.Find(c => c.PrimaryCurrency == market.PrimaryCurrency && c.SecondaryCurrency == market.SecondaryCurrency);
                JArray response   = (JArray)Call("GetL2Snapshot", new ApexL2SnapshotRequest(1, instrument.InstrumentId, 50));

                if (response != null)
                {
                    orderbook = new Orderbook(response);
                }

                if (_level2Subscriptions.Contains(market.PrimaryCurrency + market.SecondaryCurrency) == false)
                {
                    Call("SubscribeLevel2", new ApexSubscribeLevel2(1, market.PrimaryCurrency + market.SecondaryCurrency, 100));
                    _level2Subscriptions.Add(market.PrimaryCurrency + market.SecondaryCurrency);
                }
            }
            catch (Exception e)
            {
                OnError(e.Message);
            }

            return(orderbook);
        }
Beispiel #3
0
        public IScriptOrderbook GetOrderbook(IScriptMarket market)
        {
            IScriptOrderbook orderbook = null;

            try
            {
                var response = Query(false, "/public/getorderbook?", new Dictionary <string, string>()
                {
                    { "market", market.SecondaryCurrency.ToUpper() + "-" + market.PrimaryCurrency.ToUpper() },
                    { "type", "both" },
                    { "depth", "50" }
                });

                if (response != null && response.Value <bool>("success"))
                {
                    orderbook = new Orderbook(response.Value <JObject>("result"));
                }
            }
            catch (Exception e)
            {
                OnError(e.Message);
            }

            return(orderbook);
        }
Beispiel #4
0
        public string PlaceOrder(IScriptMarket market, ScriptedOrderType direction, decimal price, decimal amount, bool isMarketOrder, string template = "", bool hiddenOrder = false)
        {
            var result = "";

            try
            {
                var parameters = new Dictionary <string, string>
                {
                    { "apikey", _publicKey },
                    { "market", market.SecondaryCurrency.ToUpper() + "-" + market.PrimaryCurrency.ToUpper() },
                    { "quantity", amount.ToString(CultureInfo.InvariantCulture) },
                    { "rate", price.ToString(CultureInfo.InvariantCulture) }
                };

                var apiCall = "buylimit";
                if (direction == ScriptedOrderType.Sell)
                {
                    apiCall = "selllimit";
                }

                var response = Query(true, "/market/" + apiCall + "?", parameters);

                if (response != null && response.Value <bool>("success"))
                {
                    result = response.Value <JObject>("result").Value <string>("uuid");
                }
            }
            catch (Exception e)
            {
                OnError(e.Message);
            }

            return(result);
        }
Beispiel #5
0
        public string PlaceOrder(IScriptMarket market, ScriptedOrderType direction, decimal price, decimal amount, bool isMarketOrder, string template = "", bool hiddenOrder = false)
        {
            var result = "";

            Console.Out.WriteLine("PlaceOrder1");

            try
            {
                Market instrument = _markets.Find(c => c.PrimaryCurrency == market.PrimaryCurrency && c.SecondaryCurrency == market.SecondaryCurrency);
                int    side       = (direction == ScriptedOrderType.Buy) ? 0 : 1;
                int    type       = (isMarketOrder) ? ApexOrderType.Market : ApexOrderType.Limit;

                Console.Out.WriteLine("instrument");
                Console.Out.WriteLine(instrument.InstrumentId.ToString());
                Console.Out.WriteLine("side");
                Console.Out.WriteLine(side.ToString());
                Console.Out.WriteLine("amount");
                Console.Out.WriteLine(amount.ToString());
                Console.Out.WriteLine("type");
                Console.Out.WriteLine(type.ToString());
                Console.Out.WriteLine("price");
                Console.Out.WriteLine(price.ToString());

                JObject response = (JObject)Call("SendOrder", new ApexSendOrder(instrument.InstrumentId, 1, _userInfo.AccountId, side, amount.ToString(), type, price.ToString()));

                result = response.Value <string>("OrderId");
            } catch (Exception e)
            {
                OnError(e.Message);
            }

            return(result);
        }
Beispiel #6
0
        public IScriptLastTrades GetLastTrades(IScriptMarket market)
        {
            LastTradesContainer trades = null;

            Console.Out.WriteLine("GetLastTrades");

            try
            {
                var response = Query(false, "/v1/trade-history?", new Dictionary <string, string>()
                {
                    { "symbol", market.PrimaryCurrency.ToUpper() + market.SecondaryCurrency.ToUpper() },
                    { "count", "100" }
                });

                if (response != null)
                {
                    trades        = new LastTradesContainer();
                    trades.Market = market;
                    trades.Trades = response.Value <JArray>("result")
                                    .Select(c => Trade.ParsePublicTrade(market, c as JObject))
                                    .Cast <IScriptOrder>()
                                    .OrderByDescending(c => c.Timestamp)
                                    .ToList();
                }
            }
            catch (Exception e)
            {
                OnError(e.Message);
            }

            return(trades);
        }
Beispiel #7
0
        private void GetOrderDetailsFromTrades(IScriptMarket market, string orderId, decimal amount, Order order, List <IScriptOrder> trades)
        {
            order.OrderId = orderId;
            order.Market  = market;

            if (order.Status == ScriptedOrderStatus.Unkown)
            {
                order.Status = trades.Sum(c => c.AmountFilled) >= amount
                    ? ScriptedOrderStatus.Completed
                    : ScriptedOrderStatus.Cancelled;
            }

            order.Price        = GetAveragePrice(trades.ToList());
            order.Amount       = amount;
            order.AmountFilled = trades.Sum(c => c.AmountFilled);
            order.AmountFilled = Math.Min(order.Amount, order.AmountFilled);

            order.FeeCost = trades.Sum(c => c.FeeCost);

            if (!trades.Any())
            {
                return;
            }

            order.Timestamp   = trades.First().Timestamp;
            order.FeeCurrency = trades[0].FeeCurrency;
            order.IsBuyOrder  = trades[0].IsBuyOrder;
        }
Beispiel #8
0
        public IScriptTick GetTicker(IScriptMarket market)
        {
            IScriptTick ticker = null;

            Console.Out.WriteLine("GetTicker");

            try
            {
                Market  instrument = _markets.Find(c => c.PrimaryCurrency == market.PrimaryCurrency && c.SecondaryCurrency == market.SecondaryCurrency);
                JObject response   = (JObject)Call("GetLevel1", new ApexL1SnapshotRequest(1, instrument.InstrumentId));

                ticker = new Ticker(response, market.PrimaryCurrency, market.SecondaryCurrency);

                if (_level2Subscriptions.Contains(market.PrimaryCurrency + market.SecondaryCurrency) == false)
                {
                    Call("SubscribeLevel1", new ApexSubscribeLevel1(1, instrument.InstrumentId));
                    _level1Subscriptions.Add(market.PrimaryCurrency + market.SecondaryCurrency);
                }
            }
            catch (Exception e)
            {
                OnError(e.Message);
            }

            return(ticker);
        }
Beispiel #9
0
        public ScriptedOrderStatus GetOrderStatus(string orderId, IScriptMarket scriptMarket, decimal price, decimal amount, bool isBuyOrder)
        {
            var status = ScriptedOrderStatus.Unkown;

            Console.Out.WriteLine("GetOrderStatus");

            return(status);
        }
Beispiel #10
0
        public IScriptOrder GetOrderDetails(string orderId, IScriptMarket market, decimal price, decimal amount, bool isBuyOrder)
        {
            // This might be called even when the order is in the open orders.
            // Make sure that the order is not open.

            Order order = null;

            Console.Out.WriteLine("GetOrderDetails");

            return(order);
        }
Beispiel #11
0
        public bool CancelOrder(IScriptMarket market, string orderId, bool isBuyOrder)
        {
            var result = false;

            Console.Out.WriteLine("CancelOrder");

            try
            {
                JObject response = (JObject)Call("CancelOrder", new ApexCancelOrder(1, _userInfo.AccountId, int.Parse(orderId)));

                result = response.Value <bool>("result");
            } catch (Exception e)
            {
                OnError(e.Message);
            }

            return(result);
        }
        public string PlaceOrder(IScriptMarket market, ScriptedOrderType direction, decimal price, decimal amount, bool isMarketOrder, string template = "", bool hiddenOrder = false)
        {
            var result = "";

            try
            {
                var apiCall = "buy";
                if (direction == ScriptedOrderType.Sell)
                {
                    apiCall = "sell";
                }

                var parameters = new Dictionary <string, string>
                {
                    //{"apikey", _publicKey},
                    { "instrument_id", market.SecondaryCurrency.ToUpper() + "-" + market.PrimaryCurrency.ToUpper() },
                    { "side", apiCall }
                    { "size", amount.ToString(CultureInfo.InvariantCulture) },
Beispiel #13
0
        public bool CancelOrder(IScriptMarket market, string orderId, bool isBuyOrder)
        {
            var result = false;

            try
            {
                var response = Query(true, "/market/cancel?", new Dictionary <string, string>()
                {
                    { "apikey", _publicKey },
                    { "uuid", orderId }
                });

                result = response != null && response.Value <bool>("success");
            }
            catch (Exception e)
            {
                OnError(e.Message);
            }

            return(result);
        }
Beispiel #14
0
        public IScriptOrder GetOrderDetails(string orderId, IScriptMarket market, decimal price, decimal amount, bool isBuyOrder)
        {
            // This might be called even when the order is in the open orders.
            // Make sure that the order is not open.

            Order order = null;

            try
            {
                var status = GetOrderStatus(orderId, market, price, amount, isBuyOrder);
                if (status != ScriptedOrderStatus.Completed && status != ScriptedOrderStatus.Cancelled)
                {
                    order        = new Order();
                    order.Status = status;
                    return(order);
                }

                Thread.Sleep(500);

                var history = GetTradeHistory();
                if (history == null)
                {
                    return(null);
                }

                var trades = history
                             .Where(c => c.OrderId == orderId)
                             .ToList();

                order = new Order();
                GetOrderDetailsFromTrades(market, orderId, amount, order, trades);
            }
            catch (Exception e)
            {
                OnError(e.Message);
            }

            return(order);
        }
        public IScriptTick GetTicker(IScriptMarket market)
        {
            IScriptTick ticker = null;

            try
            {
                var response = Query(false, "/spot/v3/instruments/", new Dictionary <string, string>()
                {
                    { "market", market.SecondaryCurrency.ToUpper() + "-" + market.PrimaryCurrency.ToUpper() },
                });

                if (response != null && response.Value <bool>("success"))
                {
                    ticker = new Ticker(response.Value <JObject>("result"), market.PrimaryCurrency, market.SecondaryCurrency);
                }
            }
            catch (Exception e)
            {
                OnError(e.Message);
            }

            return(ticker);
        }
Beispiel #16
0
        public ScriptedOrderStatus GetOrderStatus(string orderId, IScriptMarket scriptMarket, decimal price, decimal amount, bool isBuyOrder)
        {
            var status = ScriptedOrderStatus.Unkown;

            try
            {
                var response = Query(true, "/account/getorder?", new Dictionary <string, string>()
                {
                    { "apikey", _publicKey }, { "uuid", orderId }
                });

                if (response != null && response.Value <bool>("success"))
                {
                    status = Order.ParseSingle(response.Value <JObject>("result")).Status;
                }
            }
            catch (Exception e)
            {
                OnError(e.Message);
            }

            return(status);
        }
        private void AssertMarket(IScriptMarket market)
        {
            Assert.IsTrue(market.PrimaryCurrency != market.SecondaryCurrency, "Double currency detected");

            if (Api.PlatformType != ScriptedExchangeType.Spot)
            {
                Assert.IsTrue(market.Leverage.Count > 0, "Missing leverage levels");
            }

            if (!AllowZeroPriceDecimals)
            {
                Assert.IsTrue(market.PriceDecimals != 0.0M, $"Zero price decimals detected  {market.PrimaryCurrency} {market.SecondaryCurrency}");
            }

            if (!AllowZeroAmountDecimals)
            {
                Assert.IsTrue(market.AmountDecimals != 0.0M, $"Zero amount decimals detected {market.PrimaryCurrency} {market.SecondaryCurrency}");
            }

            if (!AllowZeroFee)
            {
                Assert.IsTrue(market.Fee > 0.0M, $"Missing fee percentage: {market}");
            }
        }
Beispiel #18
0
 public string PlaceOrder(IScriptMarket market, ScriptedLeverageOrderType direction, decimal price, decimal amount, decimal leverage, bool isMarketOrder, string template = "", bool isHiddenOrder = false)
 {
     return(null);
 }
Beispiel #19
0
 public decimal GetMaxPositionAmount(IScriptMarket pair, decimal tickClose, Dictionary <string, decimal> wallet, decimal leverage, ScriptedLeverageSide scriptedLeverageSide)
 {
     return(1);
 }
Beispiel #20
0
 public decimal GetContractValue(IScriptMarket pair, decimal price)
 {
     return(1);
 }