///We remove identical lines public static List <IntradayTransfer> GetDataFromDb() { List <IntradayTransfer> intradayList = new List <IntradayTransfer>(); double previousPrice = -999; using (var db = new ApplicationDbContext()) { //3 - We add each item to our final list (26 first doesn;t contain RSI neither MACD calulation) foreach (var item in db.Intraday) { if (item.P == previousPrice) { continue; } IntradayTransfer newIntraday = new IntradayTransfer() { Price = item.P, }; intradayList.Add(newIntraday); previousPrice = item.P; } } //Calculate change from next day to current day intradayList.Where((p, index) => CalculateFuture(p, index, intradayList)).ToList(); //Add RSI calculation to the list TradeIndicator.CalculateRsiList(14, ref intradayList); TradeIndicator.CalculateMacdList(ref intradayList); return(intradayList.Skip(26).ToList()); }
public List <QuotationTransfer> GetChartData(string symbol, string interval) { List <QuotationTransfer> quotation = new List <QuotationTransfer>(); string apiUrl = string.Format("https://api.binance.com/api/v1/klines?symbol={0}&interval={1}&limit=1000", symbol, interval); //Get data from Binance API List <List <double> > coinQuotation = HttpHelper.GetApiData <List <List <double> > >(new Uri(apiUrl)); foreach (var item in coinQuotation) { QuotationTransfer newQuotation = new QuotationTransfer() { OpenTime = item[0], Open = item[1], High = item[2], Low = item[3], Close = item[4], Volume = item[5], CloseTime = item[6], QuoteAssetVolume = item[7], NumberOfTrades = item[8], BuyBaseAssetVolume = item[9], BuyQuoteAssetVolume = item[10], Ignore = item[11], }; quotation.Add(newQuotation); } //Add Indicators to the list TradeIndicator.CalculateIndicator(ref quotation); return(quotation); }
private void Awake() { if (instance != null) { Destroy(gameObject); } else { instance = this; } slider = transform.GetChild(0).GetComponent <Slider>(); qtyText = slider.transform.Find("QtyText").GetComponent <Text>(); }
/// <summary> /// Constructor without time stamp parameter. /// /// Time stamp is set to the current time. /// </summary> /// <param name="quantity">Quantity sold (assumed grearer than 0)/param> /// <param name="indicator">Buy/Sell</param> /// <param name="price">Price sold (assumed grearer than 0)</param> public Trade(int quantity, TradeIndicator indicator, double price) { // Set TimeStamp to the current time TimeStamp = DateTime.Now; // Check Quantity is strictly greater than 0 Helpers.CheckQuantityStrictlyPositive(quantity); // Check Price is strictly greater than 0 Helpers.CheckQuantityStrictlyPositive(price); Quantity = quantity; Indicator = indicator; Price = price; }
/// <summary> /// Constructor. /// </summary> /// <param name="quantity">Quantity sold (assumed grearer than 0)/param> /// <param name="indicator">Buy/Sell</param> /// <param name="price">Price sold (assumed grearer than 0)</param> public Trade(int quantity, TradeIndicator indicator, double price, DateTime timeStamp) { // Set TimeStamp to the current time TimeStamp = timeStamp; // Check Quantity is non-negative Helpers.CheckQuantityPositive(quantity); // Check Price is non-negative Helpers.CheckQuantityPositive(price); Quantity = quantity; Indicator = indicator; Price = price; }
public List <PredictionTransfer> GetPrediction([FromBody] System.Text.Json.JsonElement data) { //0 - Deserialize the JSON Object List <ModelInput> intradayList = JsonConvert.DeserializeObject <List <ModelInput> >(data.ToString()); List <PredictionTransfer> predictionList = new List <PredictionTransfer>(); if (intradayList.Count == 0) { return(predictionList); } //1 - Add RSI and MACD TradeIndicator.CalculateIndicator(ref intradayList); //2 - List models available var rootFolder = Environment.CurrentDirectory + "/AI/"; var modelPathList = Directory.GetFiles(rootFolder, "*", SearchOption.AllDirectories); if (modelPathList.Length == 0) { return(predictionList); } //3 - Iterate throw model and fire prediction foreach (var modelPath in modelPathList) { PredictionTransfer prediction = new PredictionTransfer(); var fromIndex = Path.GetFileName(modelPath).IndexOf("-") + 1; var toIndex = Path.GetFileName(modelPath).Length - fromIndex - 4; prediction.ModelName = Path.GetFileName(modelPath).Substring(fromIndex, toIndex); prediction.Future = CalculatePrediction(intradayList.Last(), modelPath).Future; prediction.Rsi = intradayList.Last().Rsi; prediction.Macd = intradayList.Last().Macd; prediction.MacdHist = intradayList.Last().MacdHist; prediction.MacdSign = intradayList.Last().MacdSign; predictionList.Add(prediction); } return(predictionList); }
// This is a generic method for testing buy/sell. private void Trade_Test(Action <ITradeService, IStock, Decimal, Int32, DateTime> action, TradeIndicator indicator, Decimal expectedPrice, Int32 expectedQuantity) { Moq.Mock <ITradeRepository> mockTradeRepo = new Moq.Mock <ITradeRepository>(); Moq.Mock <IStock> mockStock = new Moq.Mock <IStock>(); mockStock.Setup(mock => mock.Symbol).Returns("ABC"); ITrade actualTrade = null; IStock expectedStock = mockStock.Object; DateTime expectedTimestamp = DateTime.UtcNow; // Setup our mock dependency to listen for what we expect to happen and capture the trade created // so we can verify it. mockTradeRepo .Setup(mock => mock.Save(Moq.It.IsAny <ITrade>())) .Callback <ITrade>(trade => { actualTrade = trade; // Copy to unit test scope. }) .Verifiable(); // Run the action. ITradeService tradeService = new TradeService(mockTradeRepo.Object); action(tradeService, expectedStock, expectedPrice, expectedQuantity, expectedTimestamp); // Verify mockTradeRepo.Verify(x => x.Save(Moq.It.IsAny <ITrade>()), Moq.Times.Once); Assert.IsNotNull(actualTrade); Assert.AreEqual(expectedPrice, actualTrade.Price); Assert.AreEqual(expectedQuantity, actualTrade.Quantity); Assert.AreEqual(expectedTimestamp, actualTrade.TimeStamp); Assert.AreEqual(indicator, actualTrade.Indicator); Assert.ReferenceEquals(expectedStock, actualTrade.StockInformation); }
// This is a generic method for testing a range of buy/sell trades. private void Trade_Suite_Test(Action <ITradeService, IStock, Decimal, Int32, DateTime> action, TradeIndicator indicator) { Trade_Test(action, indicator, 100M, 10); // NORMAL Trade_Test(action, indicator, Decimal.MaxValue, 10); // MAX PRICE Trade_Test(action, indicator, 0.01M, 10); // MIN PRICE Trade_Test(action, indicator, Decimal.MaxValue, Int32.MaxValue); // MAX QUANTITY Trade_Test(action, indicator, 0.01M, Int32.MaxValue); // MIN QUANTITY try { Trade_Test(action, indicator, -1M, 10); } catch (Exception ex) { Assert.AreEqual("Failure recording trade", ex.Message); Assert.AreEqual("Price not above zero on trade", ex.InnerException.Message); } try { Trade_Test(action, indicator, 1M, 0); } catch (Exception ex) { Assert.AreEqual("Failure recording trade", ex.Message); Assert.AreEqual("Quantity not above zero on trade", ex.InnerException.Message); } }