Esempio n. 1
0
        public async Task <CryptsyOrderResult> CreateOrder(long marketId, CryptsyOrderType orderType, decimal quantity, decimal price, CancellationToken token = default(CancellationToken))
        {
            return(await RetryHelper.DoAsync(async() =>
            {
                if (orderType == CryptsyOrderType.Na)
                {
                    return new CryptsyOrderResult {
                        Success = false, OrderId = -1, Message = "orderType must be BUY or SELL."
                    }
                }
                ;

                CryptsyResponse response = await CryptsyPrivateQuery(new Dictionary <string, string> {
                    { "method", "createorder" },
                    { "marketid", Convert.ToString(marketId) },
                    { "ordertype", orderType == CryptsyOrderType.Buy ? "Buy" : "Sell" },
                    { "quantity", Convert.ToString(quantity, CultureInfo.InvariantCulture) },
                    { "price", Convert.ToString(price, CultureInfo.InvariantCulture) }
                }, token, RequestCategory.SubmitOrder);

                return response.Success ?
                new CryptsyOrderResult {
                    Success = true, OrderId = response.OrderId, Message = response.Info
                } :
                new CryptsyOrderResult {
                    Success = false, OrderId = -1, Message = response.Error
                };
            }, TimeSpan.FromMilliseconds(Constant.DefaultRetryInterval)));
        }
Esempio n. 2
0
        public async Task <ConcurrentBag <CryptsyMarketInfo> > GetOpenMarkets(bool basicInfoOnly = false, CancellationToken token = default(CancellationToken))
        {
            return(await RetryHelper.DoAsync(async() =>
            {
                CryptsyResponse answer = await CryptsyPublicQuery(new Dictionary <string, string> {
                    { "method", "marketdatav2" }
                }, false, token);

                return answer.Success ? CryptsyMarketInfo.ReadMultipleFromJObject(answer.Data as JObject, basicInfoOnly) : null;
            }, TimeSpan.FromMilliseconds(Constant.DefaultRetryInterval)));
        }
Esempio n. 3
0
        //Gets withdrawals & deposits
        public async Task <List <CryptsyTransaction> > GetTransactions(CancellationToken token = default(CancellationToken))
        {
            return(await RetryHelper.DoAsync(async() =>
            {
                CryptsyResponse response = await CryptsyPrivateQuery(new Dictionary <string, string> {
                    { "method", "mytransactions" }
                }, token);

                return response.Success ? CryptsyTransaction.ReadMultipleFromJArray(response.Data as JArray) : null;
            }, TimeSpan.FromMilliseconds(Constant.DefaultRetryInterval)));
        }
Esempio n. 4
0
        public async Task <CryptsyAccountBalance> GetBalance(CancellationToken token = default(CancellationToken))
        {
            return(await RetryHelper.DoAsync(async() =>
            {
                CryptsyResponse response = await CryptsyPrivateQuery(new Dictionary <string, string> {
                    { "method", "getinfo" }
                }, token, RequestCategory.AccountHoldings);

                return response.Success ? CryptsyAccountBalance.ReadFromJObject(response.Data as JObject) : null;
            }, TimeSpan.FromMilliseconds(Constant.CryptsyRetryInterval)));
        }
Esempio n. 5
0
        //Returns: Dictionary<MarketID, OrderBook>
        public async Task <Dictionary <Int64, CryptsyOrderBook> > GetAllOrderBooks(CancellationToken token = default(CancellationToken))
        {
            return(await RetryHelper.DoAsync(async() =>
            {
                CryptsyResponse response = await CryptsyPublicQuery(new Dictionary <string, string> {
                    { "method", "orderdata" }
                }, false, token);

                //Response is an array of markets, with only the requested market
                return response.Success ? CryptsyOrderBook.ReadMultipleFromJObject(response.Data as JObject) : null;
            }, TimeSpan.FromMilliseconds(Constant.DefaultRetryInterval)));
        }
Esempio n. 6
0
        public async Task <Exchange.Cryptsy.CryptsyOrderBook> GetOrderBook(long marketId, CancellationToken token = default(CancellationToken))
        {
            return(await RetryHelper.DoAsync(async() =>
            {
                CryptsyResponse response = await CryptsyPublicQuery(new Dictionary <string, string> {
                    { "method", "singleorderdata" }, { "marketid", Convert.ToString(marketId) }
                }, false, token);

                //Response is an array of markets, with only the requested market
                return response.Success ? CryptsyOrderBook.ReadMultipleFromJObject(response.Data as JObject)[marketId] : null;
            }, TimeSpan.FromMilliseconds(Constant.DefaultRetryInterval)));
        }
Esempio n. 7
0
        public async Task <List <CryptsyTrade> > GetAllMyTrades(CancellationToken token = default(CancellationToken))
        {
            return(await RetryHelper.DoAsync(async() =>
            {
                CryptsyResponse response = await CryptsyPrivateQuery(new Dictionary <string, string> {
                    { "method", "allmytrades" }
                }, token);

                //Response is an array of markets, with only the requested market
                return response.Success ? CryptsyTrade.ReadMultipleFromJArray(response.Data as JArray) : null;
            }, TimeSpan.FromMilliseconds(Constant.DefaultRetryInterval)));
        }
Esempio n. 8
0
        public async Task <string> GenerateNewAddress(string currencyCode, CancellationToken token = default(CancellationToken))
        {
            return(await RetryHelper.DoAsync(async() =>
            {
                CryptsyResponse response = await CryptsyPrivateQuery(
                    new Dictionary <string, string> {
                    { "method", "generatenewaddress" }, { "currencycode", currencyCode }
                },
                    token);

                return response.Success && response.Data != null ? response.Data.Value <string>("address") : null;
            }, TimeSpan.FromMilliseconds(Constant.DefaultRetryInterval)));
        }
Esempio n. 9
0
        public async Task <List <CryptsyOrder> > GetMyOrders(long marketId, CancellationToken token = default(CancellationToken))
        {
            return(await RetryHelper.DoAsync(async() =>
            {
                CryptsyResponse response = await CryptsyPrivateQuery(
                    new Dictionary <string, string> {
                    { "method", "myorders" }, { "marketid", Convert.ToString(marketId) }
                },
                    token, RequestCategory.OpenOrders);

                //Response is an array of markets, with only the requested market
                return response.Success ? CryptsyOrder.ReadMultipleFromJArray(response.Data as JArray, marketId) : null;
            }, TimeSpan.FromMilliseconds(Constant.DefaultRetryInterval)));
        }
Esempio n. 10
0
        public static CryptsyResponse ReadFromJObject(JObject o)
        {
            var r = new CryptsyResponse()
            {
                Success = o.Value <int>("success") == 1,
                Error   = o.Value <string>("error")
            };

            r.Data    = o.Value <JToken>("return");
            r.OrderId = o.Value <Int64?>("orderid") ?? -1;
            r.Info    = o.Value <string>("moreinfo") ?? r.Error;


            return(r);
        }
Esempio n. 11
0
        public async Task <CryptsyMarketInfo> GetMarketInfo(long marketId, CancellationToken token = default(CancellationToken))
        {
            return(await RetryHelper.DoAsync(async() =>
            {
                var args = new Dictionary <string, string>
                {
                    { "marketid", Convert.ToString(marketId) },
                    { "method", "singlemarketdata" }
                };

                CryptsyResponse answer = await CryptsyPublicQuery(args, false, token);

                return answer.Success ? CryptsyMarketInfo.ReadMultipleFromJObject(answer.Data as JObject).SingleOrDefault() : null;
            }, TimeSpan.FromMilliseconds(Constant.DefaultRetryInterval)));
        }
Esempio n. 12
0
        private CryptsyResponse GetCryptsyResponse(ref string data)
        {
            var     jsonData = new string(data.ToCharArray());
            JObject result   = JObject.Parse(data);

            data = null;
            CryptsyResponse cryptsyResponse = CryptsyResponse.ReadFromJObject(result);

            if (!string.IsNullOrEmpty(cryptsyResponse.Error))
            {
                //_log.Error($"Cryptsy Response Error : {cryptsyResponse.Error} , Data(json) : {jsonData}");
                LastMessage = cryptsyResponse.Error;
            }

            return(cryptsyResponse);
        }
Esempio n. 13
0
        public async Task <CryptsyOrderStatusInfo> GetCryptsyOrderStatus(Order order, CancellationToken token = default(CancellationToken))
        {
            return(await RetryHelper.DoAsync(async() =>
            {
                CryptsyResponse response = await CryptsyPrivateQuery(new Dictionary <string, string>
                {
                    { "method", "getorderstatus" },
                    { "orderid", Convert.ToString(order.Id) }
                }, token, RequestCategory.OrderStatus);

                var orderStatus = response.Data["orderinfo"].ToObject <CryptsyOrderStatusInfo>();
                orderStatus.TradeInfo = response.Data["tradeinfo"].ToObject <List <CryptsyTrade> >();

                return orderStatus;
            }, TimeSpan.FromMilliseconds(Constant.DefaultRetryInterval)));
        }
Esempio n. 14
0
        public async Task <CryptsyOrderResult> CancelOrder(long orderId, CancellationToken token = default(CancellationToken))
        {
            return(await RetryHelper.DoAsync(async() =>
            {
                CryptsyResponse response = await CryptsyPrivateQuery(
                    new Dictionary <string, string>
                {
                    { "method", "cancelorder" },
                    { "orderid", Convert.ToString(orderId) }
                }, token, RequestCategory.CancelOrder);

                return new CryptsyOrderResult {
                    Success = response.Success, OrderId = orderId, Message = response.Info ?? Convert.ToString(response.Data)
                };
            }, TimeSpan.FromMilliseconds(Constant.DefaultRetryInterval)));
        }
Esempio n. 15
0
        //Set basicInfoOnly=true to skip recent trades & top 20 buy and sell orders
        public ConcurrentBag <CryptsyMarketInfo> GetOpenMarketsPeriodically(CryptsyMarket[] markets, bool basicInfoOnly = false)
        {
            var resultList = new ConcurrentBag <CryptsyMarketInfo>();

            var options = new ParallelOptions {
                MaxDegreeOfParallelism = Environment.ProcessorCount * 50
            };
            int marketCounts = markets.Length;

            var sw = new Stopwatch();

            sw.Start();

            Parallel.For(1, marketCounts,
                         options,
                         () => new Tuple <CryptsyResponse>(new CryptsyResponse()),
                         (i, pls, state) =>
            {
                try
                {
                    CryptsyResponse answer = CryptsyPublicQuery(new Dictionary <string, string> {
                        { "method", "singlemarketdata" }, { "marketid", Convert.ToString(markets[i - 1].MarketId) }
                    }, i % 2 == 0).Result;
                    state = new Tuple <CryptsyResponse>(answer);
                }
                catch
                {
                    state = new Tuple <CryptsyResponse>(null);
                }
                return(state);
            },
                         state =>
            {
                if (state.Item1 != null && state.Item1.Success && state.Item1.Data is JObject)
                {
                    var data = CryptsyMarketInfo.ReadMultipleFromJObject((JObject)state.Item1.Data, basicInfoOnly);
                    data.ToList().ForEach(resultList.Add);
                }
            });
            sw.Stop();

            return(resultList);
        }
Esempio n. 16
0
        public async Task <decimal> CalculateFee(CryptsyOrderType orderType, decimal quantity, decimal price, CancellationToken token = default(CancellationToken))
        {
            var culture = CultureHelper.GetEnglishCulture();

            return(await RetryHelper.DoAsync(async() =>
            {
                if (orderType == CryptsyOrderType.Na)
                {
                    return -1;
                }

                CryptsyResponse response = await CryptsyPrivateQuery(
                    new Dictionary <string, string>
                {
                    { "method", "calculatefees" },
                    { "ordertype", orderType == CryptsyOrderType.Buy ? "Buy" : "Sell" },
                    { "quantity", Convert.ToString(quantity, culture.NumberFormat) },
                    { "price", Convert.ToString(price, culture.NumberFormat) }
                }, token);

                return response.Success && response.Data != null ? response.Data.Value <decimal>("fee") : -1;
            }, TimeSpan.FromMilliseconds(Constant.DefaultRetryInterval)));
        }
Esempio n. 17
0
        //Returns null if unsuccessful, otherwise list of info-messages
        public async Task <List <string> > CancelAllOrders(CancellationToken token = default(CancellationToken))
        {
            return(await RetryHelper.DoAsync(async() =>
            {
                CryptsyResponse response = await CryptsyPrivateQuery(
                    new Dictionary <string, string>
                {
                    { "method", "cancelallorders" }
                }, token, RequestCategory.CancelAllOrder);

                if (response.Success)
                {
                    var r = new List <string>();
                    if (response.Data != null) //Any orders canceled?
                    {
                        r.AddRange(response.Data.Select(Convert.ToString));
                    }

                    return r;
                }

                return null;
            }, TimeSpan.FromMilliseconds(Constant.DefaultRetryInterval)));
        }