public Configuration() { publicKey = ConfigurationManager.AppSettings["btcePublicKey"]; secretKey = ConfigurationManager.AppSettings["btceSecretKey"]; pairs = new List <BtcePairEnum>(); PairAggregatorIncrement = new Dictionary <BtcePairEnum, decimal>(); var pairsStrings = ConfigurationManager.AppSettings["Pairs"].Split(new[] { ',' }); foreach (var pairsString in pairsStrings) { pairs.Add(BtcePairHelper.FromString(pairsString)); } PairAggregatorIncrement.Add(BtcePairEnum.btc_usd, 1); PairAggregatorIncrement.Add(BtcePairEnum.ltc_usd, 0.1m); PairAggregatorIncrement.Add(BtcePairEnum.nmc_usd, 0.1m); PairAggregatorIncrement.Add(BtcePairEnum.ltc_btc, 0.0001m); }
public static bool HasCurrency(this BtcePair p, string currency) { return(BtcePairHelper.ToString(p).Contains(currency)); }
/// <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); } }
public static string Item2(this BtcePair p) { return(BtcePairHelper.ToString(p).Split(new char[] { '_' })[1]); }
/// <summary> /// Converts a CurrenyPair instance to a BtcePair /// </summary> /// <param name="pair"></param> /// <returns></returns> public static BtcePair ToBtcePair(this CurrencyPair pair) { return(BtcePairHelper.FromString(string.Format("{0}_{1}", pair.Item1, pair.Item2))); }