private void populateCalcValues(DataLayer.TrnAltCoinTraderValue.AltCoinTraderDisplayRow returnRow, DataLayer.TrnAltCoinTraderValue.AltCoinTraderSecurityRow securityRow, decimal rateValue, decimal altCoinRate, decimal krakenRate, decimal feeWithRate) { //go and get all the data for this security item BusLayer.Currency currencyHelper = new Currency(); DataLayer.TrnAltCoinTraderValue.TrnAltCoinTraderValueDataTable allData = currencyHelper.GetAllAltCoinTraderRowsForSecurityKey(securityRow.SecurityKey, (int)BusLayer.Currency.PrmSellBuy.Buy); //create a dictionary for each of the required return types Dictionary <string, ReturnObject> returnValues = new Dictionary <string, ReturnObject>(); ReturnObject emptyObject = new ReturnObject(); emptyObject.UnitSellPrice = 0; emptyObject.OrderVolume = 0; emptyObject.Margin = 0; emptyObject.OrderCost = 0; //add the initial set returnValues.Add("Less25k", emptyObject); emptyObject = new ReturnObject(); emptyObject.UnitSellPrice = 0; emptyObject.OrderVolume = 0; emptyObject.Margin = 0; emptyObject.OrderCost = 0; returnValues.Add("Btw25k50k", emptyObject); emptyObject = new ReturnObject(); emptyObject.UnitSellPrice = 0; emptyObject.OrderVolume = 0; emptyObject.Margin = 0; emptyObject.OrderCost = 0; returnValues.Add("Btw50k100k", emptyObject); emptyObject = new ReturnObject(); emptyObject.UnitSellPrice = 0; emptyObject.OrderVolume = 0; emptyObject.Margin = 0; emptyObject.OrderCost = 0; returnValues.Add("Btw100k150k", emptyObject); emptyObject = new ReturnObject(); emptyObject.UnitSellPrice = 0; emptyObject.OrderVolume = 0; emptyObject.Margin = 0; emptyObject.OrderCost = 0; returnValues.Add("Btw150k200k", emptyObject); emptyObject = new ReturnObject(); emptyObject.UnitSellPrice = 0; emptyObject.OrderVolume = 0; emptyObject.Margin = 0; emptyObject.OrderCost = 0; returnValues.Add("Grt200k", emptyObject); decimal currentTotalValue = 0; decimal currentTotalVolume = 0; //loop through each of the rows and add the values to the dictionarys foreach (DataLayer.TrnAltCoinTraderValue.TrnAltCoinTraderValueRow currentScrapeRow in allData) { //assign the current total value of this item currentTotalValue += currentScrapeRow.TotalValue; currentTotalVolume += currentScrapeRow.CoinValue; if (currentTotalValue > 300000) { break; } if (currentTotalValue < 25000) { returnValues["Less25k"].OrderVolume = currentTotalVolume; returnValues["Less25k"].OrderCost = currentTotalValue; } if (currentTotalValue > 25000 && currentTotalValue < 50000) { //just add to the existing dict returnValues["Btw25k50k"].OrderVolume = currentTotalVolume; returnValues["Btw25k50k"].OrderCost = currentTotalValue; } if (currentTotalValue > 50000 && currentTotalValue < 100000) { //just add to the existing dict returnValues["Btw50k100k"].OrderVolume = currentTotalVolume; returnValues["Btw50k100k"].OrderCost = currentTotalValue; } if (currentTotalValue > 100000 && currentTotalValue < 150000) { //just add to the existing dict returnValues["Btw100k150k"].OrderVolume = currentTotalVolume; returnValues["Btw100k150k"].OrderCost = currentTotalValue; } if (currentTotalValue > 150000 && currentTotalValue < 200000) { //just add to the existing dict returnValues["Btw150k200k"].OrderVolume = currentTotalVolume; returnValues["Btw150k200k"].OrderCost = currentTotalValue; } if (currentTotalValue > 200000) { //just add to the existing dict returnValues["Grt200k"].OrderVolume = currentTotalVolume; returnValues["Grt200k"].OrderCost = currentTotalValue; } } foreach (KeyValuePair <string, ReturnObject> currObj in returnValues) { //loop through each of the rows that we know have data for //calculate the avg price, which is the total value / total volume if (currObj.Value.OrderCost > 0) { //kraken calc decimal krakenZARBuyPrice = decimal.Parse(returnRow.AskPrice) * rateValue; //kraken rate fee for ZAR decimal krakenWithdrawlFeeZAR = krakenZARBuyPrice * feeWithRate; //kraken trade fee for ZAR decimal krakenTradeFeeZAR = krakenZARBuyPrice * krakenRate; decimal krakenBuyPricePlusFees = krakenZARBuyPrice + krakenWithdrawlFeeZAR + krakenTradeFeeZAR; returnValues[currObj.Key].UnitSellPrice = returnValues[currObj.Key].OrderCost / returnValues[currObj.Key].OrderVolume; //alt coin calc decimal altCoinSellPrice = returnValues[currObj.Key].UnitSellPrice; //altcoin trade fee for ZAR decimal altCoinTradeFeeZAR = altCoinSellPrice * altCoinRate; decimal altCoinSellPriceLessFees = altCoinSellPrice - altCoinTradeFeeZAR; decimal marginZAR = altCoinSellPriceLessFees - krakenBuyPricePlusFees; decimal marginPercentage = marginZAR / krakenZARBuyPrice; returnValues[currObj.Key].Margin = (marginPercentage) * 100; } } //update the current row with the values try { returnRow.Less25k = Math.Round(returnValues["Less25k"].UnitSellPrice, 2).ToString().Replace(",", "."); returnRow.MrgLess25k = Math.Round(returnValues["Less25k"].Margin, 2).ToString().Replace(",", "."); returnRow.OrderVolumeLess25k = Math.Round(returnValues["Less25k"].OrderVolume, 6).ToString().Replace(",", "."); returnRow.OrderCostLess25k = Math.Round(returnValues["Less25k"].OrderCost, 2).ToString().Replace(",", "."); } catch (Exception ex) { } try { returnRow.Btw25k50k = Math.Round(returnValues["Btw25k50k"].UnitSellPrice, 2).ToString().Replace(",", "."); returnRow.MrgBtw25k50k = Math.Round(returnValues["Btw25k50k"].Margin, 2).ToString().Replace(",", "."); returnRow.OrderVolumeBtw25k50k = Math.Round(returnValues["Btw25k50k"].OrderVolume, 6).ToString().Replace(",", "."); returnRow.OrderCostBtw25k50k = Math.Round(returnValues["Btw25k50k"].OrderCost, 2).ToString().Replace(",", "."); } catch (Exception ex) { } try { returnRow.Btw50k100k = Math.Round(returnValues["Btw50k100k"].UnitSellPrice, 2).ToString().Replace(",", "."); returnRow.MrgBtw50k100k = Math.Round(returnValues["Btw50k100k"].Margin, 2).ToString().Replace(",", "."); returnRow.OrderVolumeBtw50k100k = Math.Round(returnValues["Btw50k100k"].OrderVolume, 6).ToString().Replace(",", "."); returnRow.OrderCostBtw50k100k = Math.Round(returnValues["Btw50k100k"].OrderCost, 2).ToString().Replace(",", "."); } catch (Exception ex) { } try { returnRow.Btw100k150k = Math.Round(returnValues["Btw100k150k"].UnitSellPrice, 2).ToString().Replace(",", "."); returnRow.MrgBtw100k150k = Math.Round(returnValues["Btw100k150k"].Margin, 2).ToString().Replace(",", "."); returnRow.OrderVolumeBtw100k150k = Math.Round(returnValues["Btw100k150k"].OrderVolume, 6).ToString().Replace(",", "."); returnRow.OrderCostBtw100k150k = Math.Round(returnValues["Btw100k150k"].OrderCost, 2).ToString().Replace(",", "."); } catch (Exception ex) { } try { returnRow.Btw150k200k = Math.Round(returnValues["Btw150k200k"].UnitSellPrice, 2).ToString().Replace(",", "."); returnRow.MrgBtw150k200k = Math.Round(returnValues["Btw150k200k"].Margin, 2).ToString().Replace(",", "."); returnRow.OrderVolumeBtw150k200k = Math.Round(returnValues["Btw150k200k"].OrderVolume, 6).ToString().Replace(",", "."); returnRow.OrderCostBtw150k200k = Math.Round(returnValues["Btw150k200k"].OrderCost, 2).ToString().Replace(",", "."); } catch (Exception ex) { } try { returnRow.Grt200k = Math.Round(returnValues["Grt200k"].UnitSellPrice, 2).ToString().Replace(",", "."); returnRow.MrgGrt200k = Math.Round(returnValues["Grt200k"].Margin, 2).ToString().Replace(",", "."); returnRow.OrderVolumeGrt200k = Math.Round(returnValues["Grt200k"].OrderVolume, 6).ToString().Replace(",", "."); returnRow.OrderCostGrt200k = Math.Round(returnValues["Grt200k"].OrderCost, 2).ToString().Replace(",", "."); } catch (Exception ex) { } }
private void GetData() { //the name of the current page we are attempting to get data from string currentCurrencyListItem = string.Empty; try { WebClient webClient = new WebClient(); webClient.Headers.Add("user-agent", "Only a test!"); string websiteUrl = "https://www.altcointrader.co.za/|https://www.altcointrader.co.za/xrp|https://www.altcointrader.co.za/eth|https://www.altcointrader.co.za/dash|https://www.altcointrader.co.za/ltc|https://www.altcointrader.co.za/zec"; string[] websiteUrls = websiteUrl.Split('|'); foreach (string url in websiteUrls) { Guid securityKey = Guid.NewGuid(); new WebClient(); webClient.Headers.Add("user-agent", "Only a test!"); string js5on = webClient.DownloadString(url); currentCurrencyListItem = url.Replace("https://www.altcointrader.co.za/", "").ToUpper(); if (currentCurrencyListItem == string.Empty) { currentCurrencyListItem = "BTC"; } BusLayer.Currency currencyHelper = new BusLayer.Currency(); DataLayer.MstCurrencyList.MstCurrencyListRow currencyListRow = currencyHelper.GetCurrentListRowForCode(currentCurrencyListItem); // This expression looks for a sub-string in the form of // "<p>...<a...>...ItemText...</a> and returns the item text. //string expression = @"<p>[^<]*<a[^>]*>(?<item>[^<]*)</a>"; //string ex = @"<div class='trade-orders orange-border'></div>"; string className = "trade-table"; string globalPattern = String.Format("<table[^>]*?class=([\"'])[^>]*{0}[^>]*\\1[^>]*>(.*?)</table>", className); // This executes the regular expression and returns all // matches found. MatchCollection matches = Regex.Matches( js5on, globalPattern, RegexOptions.Singleline | RegexOptions.Multiline | RegexOptions.IgnoreCase ); //go through the first 2 tables only int count = 0; while (count < 2) { string currclassName = string.Empty; if (count == 0) { currclassName = "orderUdSell"; } else { currclassName = "orderUdBuy"; } string currglobalPattern = String.Format("<tr[^>]*?class = ([\"'])[^>]*{0}[^>]*\\1[^>]*>(.*?)</tr>", currclassName); string currentTable = matches[count].ToString(); MatchCollection currMatches = Regex.Matches( currentTable, currglobalPattern, RegexOptions.Singleline | RegexOptions.Multiline | RegexOptions.IgnoreCase ); foreach (var obj in currMatches) { string currentItem = obj.ToString(); int prmSellBuyId = 0; //replace all the bad characters if (count == 0) { prmSellBuyId = 1; currentItem = currentItem.Replace("\t", "").Replace("\r\n", "").Replace("<tr class = 'orderUdSell'>", "").Replace("</tr>", ""); } else { prmSellBuyId = 2; currentItem = currentItem.Replace("\t", "").Replace("\r\n", "").Replace("<tr class = 'orderUdBuy'>", "").Replace("</tr>", ""); } //[1] - Price //[3] - Coin Amount //[5] - Total string[] spltRes = currentItem.Split('<'); string priceValue = string.Empty; string coinValue = string.Empty; string totalValue = string.Empty; if (count == 0) { priceValue = spltRes[1].Replace("td class = 'orderUdSPr'>", ""); coinValue = spltRes[3].Replace("td class = 'orderUdSAm'>", ""); totalValue = spltRes[5].Replace("td>", ""); } else { priceValue = spltRes[1].Replace("td class = 'orderUdBPr'>", ""); coinValue = spltRes[3].Replace("td class = 'orderUdBAm'>", ""); totalValue = spltRes[5].Replace("td>", ""); } System.IO.File.WriteAllText(@"c:\temp\values.txt", "Coin: " + coinValue + " Converted Coin:" + decimal.Parse(coinValue, new CultureInfo("en-US"))); //create the new row DataLayer.TrnAltCoinTraderValue.TrnAltCoinTraderValueDataTable newTbl = new DataLayer.TrnAltCoinTraderValue.TrnAltCoinTraderValueDataTable(); DataLayer.TrnAltCoinTraderValue.TrnAltCoinTraderValueRow newRow = newTbl.NewTrnAltCoinTraderValueRow(); newRow.TrnAltCoinTraderValueGuid = Guid.NewGuid(); newRow.SecurityKey = securityKey; newRow.MstCurrencyListGuid = currencyListRow.MstCurrencyListGuid; newRow.CoinValue = decimal.Parse(coinValue, new CultureInfo("en-US")); newRow.PriceValue = decimal.Parse(priceValue, new CultureInfo("en-US")); newRow.TotalValue = decimal.Parse(totalValue, new CultureInfo("en-US")); newRow.CreateDate = DateTime.Now; newRow.PrmCurrencyId = 3;// currencyRow.PrmCurrencyId; newRow.PrmCurrencySourceId = 1; newRow.PrmSellBuyId = prmSellBuyId; newTbl.AddTrnAltCoinTraderValueRow(newRow); currencyHelper.UpdateCurrencyValue(newRow); } count++; } } } catch (Exception ex) { //throw an exception with the code of the page that we are laoding throw new Exception("Unable to get page data for : " + currentCurrencyListItem, ex); } }