public Task <CoinTrend> GetTrend(string symbol)
        {
            var tcs = new TaskCompletionSource <CoinTrend>();

            if (_trend == null)
            {
                _trend = new CoinTrend
                {
                    Symbol       = symbol,
                    CurrentValue = OriginalValue,
                    Trend        = OriginalTrend
                };
            }
            else
            {
                _trend.Symbol        = symbol;
                _trend.CurrentValue += 1000;
                _trend.Trend         =
                    _trend.Trend == 0 ? 1
                    : _trend.Trend == 1 ? -1
                    : 0;
            }

            tcs.SetResult(_trend);
            return(tcs.Task);
        }
        public static async Task <HttpResponseMessage> Run(
            [HttpTrigger(
                 AuthorizationLevel.Function,
                 "get",
                 Route = "get/symbol/{symbol}")]
            HttpRequestMessage req,
            string symbol,
            TraceWriter log)
        {
            log.Info($"CoinTrendGetter executed at: {DateTime.Now} for {symbol}");

            // Create account, client and table
            var account     = CloudStorageAccount.Parse(CoinValueSaver.ConnectionString);
            var tableClient = account.CreateCloudTableClient();
            var table       = tableClient.GetTableReference(CoinValueSaver.TableName);
            await table.CreateIfNotExistsAsync();

            // Get the last 10 coin
            var coinsQuery = table.CreateQuery <CoinEntity>()
                             .Where(c => c.Symbol == symbol)
                             .ToList();
            var count = coinsQuery.Count;

            var trend = new CoinTrend();

            if (count == 0)
            {
                log.Info("No coins found");
            }
            else
            {
                var selectedCount = (count < StandardCount) ? count : StandardCount;
                var coinValues    = coinsQuery
                                    .Skip(count - selectedCount)
                                    .Select(c => c.PriceUsd)
                                    .ToArray();

                // Prepare the data for analysis
                var currentIndex = 1;
                var indexes      = coinValues.Select(c => (double)currentIndex++).ToArray();

                // Calculate the linear regression
                double rSquared, yIntercept, slope;
                Stats.CalculateLinearRegression(indexes, coinValues, 0, selectedCount, out rSquared, out yIntercept, out slope);

                // Prepare the result
                trend.Symbol       = symbol;
                trend.CurrentValue = coinValues.Last();
                trend.Trend        = (slope > 0.05) ? 1 // Positive trend
                    : (slope < -0.05) ? -1              // Negative trend
                    : 0;                                // Flat trend
            }

            return(req.CreateResponse(HttpStatusCode.OK, trend, "application/json"));
        }
Ejemplo n.º 3
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = "symbol/{symbol}")] HttpRequest req,
            [Table(CoinValueSaver.TableName, Connection = "AzureWebJobsStorage")] CloudTable table,
            [FromRoute] string symbol,
            ILogger log)
        {
            log.LogInformation("CoinTrendGetterSymbol executed at: {time} for {symbol}", DateTime.Now, symbol);

            var coinsQuery   = new TableQuery <CoinEntity>();
            var coinsSegment = await table.ExecuteQuerySegmentedAsync(coinsQuery, null);

            var coinsResult = coinsSegment.Results.Where(c => c.Symbol == symbol).ToList();

            var trend = new CoinTrend();

            var count = coinsResult.Count;

            if (count == 0)
            {
                // This is a user-generated error
                log.LogInformation("No coins for {symbol} found", symbol);
                return(new StatusCodeResult(StatusCodes.Status404NotFound));
            }
            else
            {
                var selectedCount = (count < StandardCount) ? count : StandardCount;
                var coinValues    = coinsResult
                                    .Skip(count - selectedCount)
                                    .Select(c => c.PriceUsd)
                                    .ToArray();

                // Prepare the data for analysis
                var currentIndex = 1;
                var indexes      = coinValues.Select(c => (double)currentIndex++).ToArray();

                // Calculate the linear regression
                double rSquared, yIntercept, slope;
                Stats.CalculateLinearRegression(indexes, coinValues, 0, selectedCount, out rSquared, out yIntercept, out slope);

                // Prepare the result
                trend.Symbol       = symbol;
                trend.CurrentValue = coinValues.Last();
                trend.Trend        = (slope > 0.05) ? 1 // Positive trend
                            : (slope < -0.05) ? -1      // Negative trend
                            : 0;                        // Flat trend
            }

            return(new OkObjectResult(trend));
        }
Ejemplo n.º 4
0
 public CoinTrendViewModel(CoinTrend model)
 {
     Model = model;
 }