public void Run() { using PaprikaApiClient client = new PaprikaApiClient(); using zcryptoContext ctx = new zcryptoContext(); List <Buy> activeBuys = DataConsistentController.GetActiveBuys(); foreach (Buy b in activeBuys) { API_Market_Price price = client.GetMarketPrice(b.CoinId); if (price is not null) { if (ctx.ReportedExchangeRates.Where(x => x.BuyId == b.Id).Count() > 1000) { ctx.ReportedExchangeRates.RemoveRange(ctx.ReportedExchangeRates.Where(x => x.BuyId == b.Id)); ctx.SaveChanges(); } ctx.ReportedExchangeRates.Add(new ReportedExchangeRate() { BuyId = b.Id, PlnExchange = price.quotes.PLN.price, ApiUpdateTime = price.last_updated, ExchangeDiff = (price.quotes.PLN.price * (double)b.Count) - (double)(b.ExchangeRatePln * b.Count) }); ctx.SaveChanges(); } } }
public static void UpsertCoin(API_Coin coin) { using (zcryptoContext ctx = new zcryptoContext()) { Coin foundCoin = ctx.Coins.Where(x => x.Id.Equals(coin.id)).FirstOrDefault(); if (foundCoin is null) { ctx.Coins.Add(new Coin() { Id = coin.id, IsActive = coin.is_active, IsNew = coin.is_new, Name = coin.name, Rank = coin.rank, Symbol = coin.symbol, Type = coin.type }); } else { foundCoin.IsActive = coin.is_active; foundCoin.IsNew = coin.is_new; foundCoin.Name = coin.name; foundCoin.Rank = coin.rank; foundCoin.Symbol = coin.symbol; foundCoin.Type = coin.type; } ctx.SaveChanges(); } }
public void Run() { using zcryptoContext ctx = new zcryptoContext(); using BinanceClient client = new BinanceClient(); List <LowPriceSymbolScan> symbolsToScan = ctx.LowPriceSymbolScans.ToList(); foreach (LowPriceSymbolScan symbol in symbolsToScan) { List <IBinanceKline> klines = client.Spot.Market.GetKlines(symbol.Symbol, KlineInterval.FiveMinutes, null, null, symbol.LastKlines).Data.ToList(); decimal goingDown = CheckItsGoingDown(klines, symbol.PercentThreshold, symbol.Symbol); if (goingDown > 0) { List <LowPriceSymbolScanAlert> alerts = ctx.LowPriceSymbolScanAlerts.ToList(); if (alerts.Count(x => x.LowPriceSymbolScanId == symbol.Id && x.ActiveDuration >= DateTime.Now) > 0) { foreach (var alert in ctx.LowPriceSymbolScanAlerts.Where(x => x.LowPriceSymbolScanId == symbol.Id && x.ActiveDuration >= DateTime.Now)) { alert.ActiveDuration = DateTime.Now.AddMinutes(15); } } else { ctx.LowPriceSymbolScanAlerts.Add(new LowPriceSymbolScanAlert() { ActiveDuration = DateTime.Now.AddMinutes(15), LowPriceSymbolScanId = symbol.Id, PercentLow = goingDown }); } ctx.SaveChanges(); } } }
public IActionResult AddBuy([FromBody] Buy buy, [FromHeader] string securityCode) { if (securityCode == null || !securityCode.Equals("secret")) { return(Unauthorized()); } try { using (zcryptoContext ctx = new zcryptoContext()) { ctx.Buys.Add(buy); ctx.SaveChanges(); } return(Ok()); } catch (Exception e) { return(BadRequest(e.Message)); } }
public API_Market_Price GetMarketPrice(string coinId) { using zcryptoContext dbCtx = new zcryptoContext(); if (DataConsistentController.IsCoinBlacklisted(coinId)) { return(null); } System.Diagnostics.Debug.WriteLine(coinId); IRestResponse response = restClient.Get(new RestRequest($"v1/coins/{coinId}/markets?quotes=PLN,EUR", Method.GET)); try { ValidateResponse(response); }catch (Exception) { if (response.StatusCode == System.Net.HttpStatusCode.NotFound) { dbCtx.CoinBlacklistIntegrations.Add(new CoinBlacklistIntegration() { CoinId = coinId, Reason = "Not found by markets endpoint" }); dbCtx.SaveChanges(); return(null); } else { throw; } } string jsonCoinList = response.Content; API_Market_Price foundMarketPrice = JsonConvert.DeserializeObject <List <API_Market_Price> >(jsonCoinList) .Where(x => x.exchange_id.Equals(DEFAULT_EXCHANGE_ID)).FirstOrDefault(); return(foundMarketPrice); }