public static Ticker GetTicker(BtcePair pair) { return Ticker.ReadFromJObject( JObject.Parse( Query(string.Format("{1}api/2/{0}/ticker", BtcePairHelper.ToString(pair), ExchangeHost)))[ "ticker"] as JObject); }
public decimal RoundAmount(BtcePair pair, decimal amount, bool refreshCache = false) { var currencyPairInfo = GetPairInfo(pair, refreshCache); var amountFixed = Math.Max(amount, currencyPairInfo.MinAmount); amountFixed = Math.Round(amountFixed, currencyPairInfo.MinAmount.GetPrecision()); return amountFixed; }
public Order(BtcePair pair, TradeType type, decimal amount, decimal rate, UInt32 timestamp, int status) { Pair = pair; Amount = amount; Rate = rate; TimestampCreated = timestamp; Status = status; }
public decimal RoundPrice(BtcePair pair, decimal price, bool refreshCache = false) { var currencyPairInfo = GetPairInfo(pair, refreshCache); var priceFixed = Math.Min(Math.Max(price, currencyPairInfo.MinPrice), currencyPairInfo.MaxPrice); priceFixed = Math.Round(priceFixed, currencyPairInfo.MinPrice.GetPrecision()); return priceFixed; }
public static Depth GetDepth(BtcePair pair) { string resStr; string queryStr = string.Format("https://btc-e.com/api/2/{0}/depth", BtcePairHelper.ToString(pair)); resStr = Query(queryStr); var res = JObject.Parse(resStr); return Depth.ReadFromJObject(res); }
public static Ticker GetTicker(BtcePair pair) { string resStr; string queryStr = string.Format("https://btc-e.com/api/2/{0}/ticker", BtcePairHelper.ToString(pair)); resStr = Query(queryStr); var res = JObject.Parse(resStr); return Ticker.ReadFromJObject(res["ticker"] as JObject); }
private BtcePairInfo GetPairInfo(BtcePair pair, bool refreshCache) { if (apiInfoCache == null || refreshCache) { apiInfoCache = GetApiInfo(); } var currencyPairInfo = apiInfoCache.Pairs[pair]; return currencyPairInfo; }
public static decimal GetFee(BtcePair pair) { string resStr; string queryStr = string.Format("https://btc-e.com/api/2/{0}/fee", BtcePairHelper.ToString(pair)); resStr = Query(queryStr); var res = JObject.Parse(resStr); return res.Value<decimal>("trade"); }
public Trade(BtcePair pair, TradeType type, decimal amount, decimal rate, int orderId, bool isYourOrder, UInt32 timestamp) { Pair = pair; Type = type; Amount = amount; Rate = rate; OrderId = orderId; IsYourOrder = isYourOrder; Timestamp = timestamp; }
public void Test002_AddCurrency_Currency_Capital_Letter() { var control = new Control(); var currencies = control.AddCurrencies(); var btcepair = new BtcePair(); foreach (string currency in currencies) { var a = (BtcePair)Enum.Parse(typeof(BtcePair), currency); } }
public void Test001_AddCurrency_No_Problems() { var control = new Control(); var currencies = control.AddCurrencies(); var btcepair = new BtcePair(); foreach (string currency in currencies) { var a = (BtcePair) Enum.Parse(typeof(BtcePair), currency.ToLower()); } }
public TradeHistory GetTradeHistory( int? from = null, int? count = null, int? fromId = null, int? endId = null, bool? orderAsc = null, DateTime? since = null, DateTime? end = null, BtcePair? pair = null) { return _apiv2.GetTradeHistory(from, count, fromId, endId, orderAsc, since, end, pair); }
public static Tuple<int, int> GetPrecision(BtcePair btcPair) { Tuple<BtcePair, int, int> pairPrecision = precisions.SingleOrDefault(x => x.Item1 == btcPair); if (pairPrecision == default(Tuple<BtcePair, int, int>)) { return Tuple.Create(4, 8); // ex. 12.3456 USD for 0.12345678 BTC: } else { return Tuple.Create(pairPrecision.Item2, pairPrecision.Item3); } }
public static List<TradeInfo> GetTrades(BtcePair pair) { string resStr; string queryStr = string.Format("https://btc-e.com/api/2/{0}/trades", BtcePairHelper.ToString(pair)); resStr = Query(queryStr); var res = JArray.Parse(resStr); var ret = new List<TradeInfo>(); foreach (var item in res) { ret.Add(TradeInfo.ReadFromJObject(item as JObject)); } return ret; }
public static decimal GetFee(bool buy, decimal price, BtcePair pair) { var request = WebRequest.Create("https://btc-e.com/ajax/order"); request.Proxy = WebRequest.DefaultWebProxy; request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials; request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; var reqStream = request.GetRequestStream(); string args = String.Format("calculate={0}&btc_count=1&btc_price={1}&pair={2}", buy ? "buy" : "sell", price.ToString().Replace(',', '.'), (int)pair); var data = Encoding.ASCII.GetBytes(args); reqStream.Write(data, 0, data.Length); reqStream.Close(); var response = new StreamReader(request.GetResponse().GetResponseStream()).ReadToEnd(); return JObject.Parse(response).Value<decimal>("fee"); }
public static Depth GetDepth(BtcePair pair) { return (Depth.ReadFromJObject( JObject.Parse(Query(string.Format("{1}api/2/{0}/depth", BtcePairHelper.ToString(pair), ExchangeHost))))); }
/// <summary> /// Returns the fee for the supplied currnecy pair /// </summary> /// <param name="pair">Currency pair to obtain fee for </param> /// <returns>Fee amount for trading the supplied currency pair</returns> public static decimal GetFee(BtcePair pair) { return JObject.Parse(Query(string.Format("{1}api/2/{0}/fee", BtcePairHelper.ToString(pair), ExchangeHost))).Value<decimal>("trade"); }
public TradeInfo[] GetTrades(BtcePair pair) { return BtceApiV3.GetTrades(new BtcePair[] { pair }).FirstOrDefault().Value; }
/// <summary> /// Recursive method to build an arbitrage tree /// </summary> /// <param name="tickerDic"></param> /// <param name="pair"></param> /// <param name="parentNode"></param> /// <param name="amount"></param> /// <param name="isSell"></param> /// <param name="targetCurrency"></param> private void BuildArbitrageTree(Dictionary<BtcePair, Ticker> tickerDic, BtcePair pair, ArbitrageAction parentNode, decimal amount, bool isSell, string targetCurrency) { var ticker = tickerDic[pair]; decimal rate = isSell ? ticker.Sell : ticker.Buy; //rate = ticker.Last; var ac = new MyAction { Currency1 = isSell ? pair.Item1() : pair.Item2(), Currency2 = isSell ? pair.Item2() : pair.Item1(), UnitsCurrency1 = amount, UnitsCurrency2 = isSell ? amount * rate - (amount * rate * _fee / 100) : amount / rate - (amount / rate) * _fee / 100, Pair = pair, Rate = rate, IsBuyOrder = !isSell }; var newNode = parentNode.AddChild(ac); var currencyNow = isSell ? pair.Item2() : pair.Item1(); if (currencyNow == targetCurrency) { newNode.Data.IsFinalAction = true; return; //Done for that branch of the tree } //Check next possible conversions foreach (var p in _allowedPairs) { if (p == pair || p.Item1() != currencyNow && p.Item2() != currencyNow) { continue;//Current, or unrelated pair } var parent = parentNode; bool found = false; if (p.Item1() != targetCurrency && p.Item2() != targetCurrency) { //If related pair, but not toing to target currency, check if we've already been through one of the currencies while (parent != null) { if (p == parent.Data.Pair || p.Item1() == parent.Data.Currency1 || p.Item1() == parent.Data.Currency2 || p.Item2() == parent.Data.Currency1 || p.Item2() == parent.Data.Currency2) { found = true; break; } parent = parent.Parent; } if (found) { continue;//We've already used one of the currencies in that pair } } BuildArbitrageTree(tickerDic, p, newNode, ac.UnitsCurrency2, p.Item1() == currencyNow, targetCurrency); } }
public static List <TradeInfo> GetTrades(BtcePair pair) { string queryStr = string.Format("https://btc-e.com/api/2/{0}/trades", BtcePairHelper.ToString(pair)); return(JArray.Parse(WebApi.Query(queryStr)).OfType <JObject>().Select(TradeInfo.ReadFromJObject).ToList()); }
public Task <Ticker> GetTickerAsync(BtcePair pair) { return(Task.Run(() => BtceApi.GetTicker(pair))); }
public OrderList GetOrderList( int? from = null, int? count = null, int? fromId = null, int? endId = null, bool? orderAsc = null, DateTime? since = null, DateTime? end = null, BtcePair? pair = null, bool? active = null ) { var args = new Dictionary<string, string>() { { "method", "OrderList" } }; if (from != null) args.Add("from", from.Value.ToString()); if (count != null) args.Add("count", count.Value.ToString()); if (fromId != null) args.Add("from_id", fromId.Value.ToString()); if (endId != null) args.Add("end_id", endId.Value.ToString()); if (orderAsc != null) args.Add("order", orderAsc.Value ? "ASC" : "DESC"); if (since != null) args.Add("since", UnixTime.GetFromDateTime(since.Value).ToString()); if (end != null) args.Add("end", UnixTime.GetFromDateTime(end.Value).ToString()); if (pair != null) args.Add("pair", BtcePairHelper.ToString(pair.Value)); if (active != null) args.Add("active", active.Value ? "1" : "0"); var resultStr = Query(args); var result = JObject.Parse(resultStr); if (result.Value<int>("success") == 0) { throw new Exception(result.Value<string>("error")); } return OrderList.ReadFromJObject(result["return"] as JObject); }
public static string ToString(BtcePair v) { if (v == BtcePair.BtcUsd) return "btc_usd"; if (v == BtcePair.LtcBtc) return "ltc_btc"; if (v == BtcePair.LtcUsd) return "ltc_usd"; if (v == BtcePair.NmcBtc) return "nmc_btc"; if (v == BtcePair.BtcRur) return "btc_rur"; if (v == BtcePair.UsdRur) return "usd_rur"; throw new NotSupportedException(); }
public static List<TradeInfo> GetTrades(BtcePair pair) { string queryStr = string.Format("https://btc-e.com/api/2/{0}/trades", BtcePairHelper.ToString(pair)); return JArray.Parse(Query(queryStr)).OfType<JObject>().Select(TradeInfo.ReadFromJObject).ToList(); }
public static Ticker GetTicker(BtcePair pair) { string queryStr = string.Format("https://btc-e.com/api/2/{0}/ticker", BtcePairHelper.ToString(pair)); return(Ticker.ReadFromJObject(JObject.Parse(WebApi.Query(queryStr))["ticker"] as JObject)); }
public static TradeInfo[] GetTrades(BtcePair pair) { return JArray.Parse(Query(string.Format("{1}api/2/{0}/trades", BtcePairHelper.ToString(pair), ExchangeHost))).OfType<JObject>().Select(TradeInfo.ReadFromJObject).ToArray(); }
/// <summary> /// Starts the arbitrage /// </summary> /// <param name="originalAmount"></param> /// <param name="targetCurrency"></param> /// <param name="frequencyInSec"></param> /// <param name="profitThreshold"></param> /// <param name="realTrading"></param> /// <param name="allowedPairs"></param> public void Start(decimal originalAmount, string targetCurrency, int frequencyInSec, decimal profitThreshold, bool realTrading, BtcePair[] allowedPairs) { realTrading = false; //!!!DO not use real trading _allowedPairs = allowedPairs; _pairsAsString = _allowedPairs.Select(p => BtcePairHelper.ToString(p)).ToArray(); _mustStop = false; OnReportProgress("Starting Arbitrage - Monitoring opportunities..."); while (!_mustStop) { Dictionary<BtcePair, Ticker> tickers; try { tickers = BtceApiV3.GetTicker(_allowedPairs); } catch (Exception ex) { Logger.Log(ex); OnReportProgress("Error: " + ex.ToString()); System.Threading.Thread.Sleep(1000 * frequencyInSec); continue; } var pairs = _allowedPairs.Where(p => p.HasCurrency(targetCurrency)); var ac = new MyAction { UnitsCurrency1 = 0, UnitsCurrency2 = originalAmount, Pair = BtcePair.Unknown }; NTree<MyAction> tree = new NTree<MyAction>(ac); foreach (var p in pairs) { BuildArbitrageTree(tickers, p, tree, originalAmount, p.Item1() == targetCurrency, targetCurrency); } var leaves = new List<NTree<MyAction>>(); tree.Traverse(n => { if (n.Data.IsFinalAction) { leaves.Add(n); } }); decimal maxProfit = 0; List<NTree<MyAction>> bestChain = null; int bestIndex = 0; for (var lIndex = 0; lIndex < leaves.Count; lIndex++) { // System.Diagnostics.Debug.WriteLine("Option " + (lIndex + 1)); var l = leaves[lIndex]; var t = l.GetTree(); for (var nIndex = 1; nIndex < t.Count; nIndex++) { var c = t[nIndex].Data; //System.Diagnostics.Debug.WriteLine(string.Format("Converting {0:0.00###} {1:0.00###} to {2:0.00###} {3:0.00###}", c.UnitsCurrency1, c.Currency1, c.UnitsCurrency2, c.Currency2)); } decimal profit = l.Data.UnitsCurrency2 - originalAmount; // System.Diagnostics.Debug.WriteLine("Profit " + profit.ToString("0.00###")); if (profit > maxProfit) { maxProfit = l.Data.UnitsCurrency2 - originalAmount; bestChain = t; bestIndex = lIndex; } } if (bestChain != null) { //System.Diagnostics.Debug.WriteLine("Best Option: " + (bestIndex + 1)); OnReportProgress("Max profit: " + maxProfit.ToString("0.00###")); for (var nIndex = 1; nIndex < bestChain.Count; nIndex++) { var c = bestChain[nIndex].Data; OnReportProgress(c.Description); } _currentChain = bestChain; var percentage = maxProfit / originalAmount * 100; OnReportProgress(string.Format("Percentage {0:0.00}", percentage)); if (percentage > profitThreshold) { FollowChain(bestChain, realTrading); } } else { System.Diagnostics.Debug.WriteLine("No profit possible"); } System.Diagnostics.Debug.WriteLine("======================================================================="); System.Threading.Thread.Sleep(1000 * frequencyInSec); } }
/// <summary> /// Returns the fee for the supplied currnecy pair /// </summary> /// <param name="pair">Currency pair to obtain fee for </param> /// <returns>Fee amount for trading the supplied currency pair</returns> public static decimal GetFee(BtcePair pair) { return (JObject.Parse(Query(string.Format("{1}api/2/{0}/fee", BtcePairHelper.ToString(pair), ExchangeHost))) .Value <decimal>("trade")); }
public Depth GetDepth(BtcePair pair) { return GetDepth(new[] { pair }).FirstOrDefault().Value; }
public static string ToString(BtcePair v) { return Enum.GetName(typeof(BtcePair), v).ToLowerInvariant(); }
public decimal GetFee(BtcePair pair, bool refreshCache = false) { var currencyPairInfo = GetPairInfo(pair, refreshCache); return currencyPairInfo.Fee; }
public TradeAnswer Trade(BtcePair pair, TradeType type, decimal rate, decimal amount) { var args = new Dictionary<string, string>() { { "method", "Trade" }, { "pair", BtcePairHelper.ToString(pair) }, { "type", TradeTypeHelper.ToString(type) }, { "rate", DecimalToString(rate) }, { "amount", DecimalToString(amount) } }; var resultStr = Query(args); var result = JObject.Parse(resultStr); if (result.Value<int>("success") == 0) { throw new Exception(result.Value<string>("error")); } return TradeAnswer.ReadFromJObject(result["return"] as JObject); }
public decimal GetFee(BtcePair pair) { return GetFee(pair, false); }
public static decimal GetFee(BtcePair pair) { string queryStr = string.Format("https://btc-e.com/api/2/{0}/fee", BtcePairHelper.ToString(pair)); return(JObject.Parse(WebApi.Query(queryStr)).Value <decimal>("trade")); }
public Ticker GetTicker(BtcePair pair) { return GetTicker(new[] { pair }).FirstOrDefault().Value; }
public static Depth GetDepth(BtcePair pair) { string queryStr = string.Format("https://btc-e.com/api/2/{0}/depth", BtcePairHelper.ToString(pair)); return(Depth.ReadFromJObject(JObject.Parse(WebApi.Query(queryStr)))); }
public static string ToString(BtcePair v) { if (v == BtcePair.BtcUsd) return "btc_usd"; if (v == BtcePair.LtcBtc) return "ltc_btc"; if (v == BtcePair.LtcUsd) return "ltc_usd"; if (v == BtcePair.NmcBtc) return "nmc_btc"; if (v == BtcePair.BtcRur) return "btc_rur"; if (v == BtcePair.UsdRur) return "usd_rur"; if (v == BtcePair.EurUsd) return "eur_usd"; if (v == BtcePair.NvcBtc) return "nvc_btc"; if (v == BtcePair.TrcBtc) return "trc_btc"; if (v == BtcePair.PpcBtc) return "ppc_btc"; if (v == BtcePair.FtcBtc) return "ftc_btc"; if (v == BtcePair.CncBtc) return "cnc_btc"; throw new NotSupportedException(); }