Exemple #1
0
        /// <summary>Compare the equality of stocks</summary>
        public Stock Update(Stock stock)
        {
            Time     time    = new Time();
            TimeSpan _time   = time.ReturnTime();
            string   hour    = _time.Hours.ToString();
            string   minutes = _time.Minutes.ToString();

            // Default states 2, -1, 0
            if (stock.CurrentPrice == this.CurrentPrice)
            {
                stock.TimeStamp      = this.TimeStamp;
                stock.ChangeArray[0] = 0; // No Change
            }
            else
            {
                // Bearish signal 2 if stock drops below 30%
                stock.ChangeArray[0] = (stock.CurrentPrice < this.CurrentPrice - (this.CurrentPrice * 0.3))
                ? -2 : (stock.CurrentPrice < this.CurrentPrice) ? -1 : 2;
                // Update timeStamp
                if (_time.Minutes <= 9)
                {
                    minutes = "0" + minutes;
                }

                stock.TimeStamp = hour + ":" + minutes;
            } // Add other variables later

            return(stock);
        }
Exemple #2
0
        // Get stocks from the cache
        public void initialiseManager(int start, int end)
        {
            int  s        = new Random().Next(-99, 99);
            bool positive = s >= 0;
            bool negative = s < 0;

            Time     time        = new Time();
            TimeSpan _time       = time.ReturnTime();
            string   hour        = _time.Hours.ToString();
            string   minutes     = _time.Minutes.ToString();
            string   time_format = hour + ":" + minutes;

            try
            {
                copy(start, end);
                int pointer = start;

                while (pointer <= end)
                {
                    stock = new Stock(pointer.ToString(), Manager.ManagerCode.Value[pointer],
                                      Manager.ManagerName.Value[pointer], time_format,
                                      s, s, s, s, changeArray, 5, 6, 7, 8, 86);

                    cache.Add(stock);
                    pointer++;
                }

                /*      copy(start, end);
                 *     data = client.GetRealTimePrices(stockArray);
                 *     Stock stock;
                 *
                 * foreach (RealTimePrice data_ in data)
                 * {
                 *     stock = new Stock(ManagerCode.Value[pointer],
                 *     data_.Open, data_.Change, data_.ChangeP, data_.Volume, changeArray, data_.High, data_.Low,
                 *     (positive && !negative) ? 1 : -1, data_.Close, data_.PreviousClose);
                 *
                 *     cache.Add(stock);
                 * } */
            }
            catch (Exception ex)
            {
                if (ex is StackOverflowException || ex is KeyNotFoundException || ex is ArgumentNullException || ex is NullReferenceException || ex is ArgumentException ||
                    ex is IndexOutOfRangeException ||
                    ex is Newtonsoft.Json.JsonSerializationException ||
                    ex is MissingMemberException ||
                    ex is OverflowException || ex is System.Threading.Tasks.TaskCanceledException)
                {
                    Console.WriteLine("exception " + ex); // Redirect also if timeout
                }
            }
        }
Exemple #3
0
        // LOG ALL ERRORS INTO GLOBAL ERROR HANDLER
        protected override async Task ExecuteAsync(CancellationToken cancellationToken)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                cacheCancellationToken = new CancellationToken();
                // Log
                // Http Client // Do a manual call if this fails
            }

            CancellationToken newToken;

            while (!cancellationToken.IsCancellationRequested)
            {
                // Put on a seperate thread
                // Read Database
                if (_init_called == false)
                {
                    _            = initialise_cache();
                    _init_called = !_init_called;
                }

                Time time = new Time();

                // Time Check
                if (time.ReturnTime().Hours >= 17 && time.ReturnTime().Hours <= 8)
                {
                    if (_cacheFull)
                    {
                        if (_sessionEnded == false) // && getState sessionEnded=true
                        {
                            // Save last data to database
                            for (int key = 0; key < MAX; key++)
                            {
                                String JSON = Cache.Get(key).Serialize();
                                _ = saveEODdata(JSON);
                            }

                            // Stop the stream
                            _ = serviceWorker.StopAsync(cancellationToken);

                            newToken          = new CancellationToken();
                            cancellationToken = newToken;

                            Console.WriteLine("STOP STREAM  ");

                            _sessionEnded = true;
                            _init_work    = false;
                        }
                    }
                }

                if (_sessionEnded)
                {
                    _sessionEnded = false;
                }

                if (_init_work == false)
                {
                    // Start the Service Worker
                    await _stockHandler.Clients.All.lockStream(serviceWorker.API_REQUESTS, false);

                    _ = serviceWorker.StartAsync(cancellationToken);
                    Console.WriteLine("START STREAM");
                    _init_work = !_init_work;
                }

                if (_cacheFull)
                {
                    // Data is kept up to date

                    /*   for (int key = 0; key < MAX; key++)
                     * {
                     *     String JSON = Cache.Get(key).Serialize();
                     *     _ = saveEODdata(JSON);
                     * }*/
                    for (int key = 0; key < MAX; key++)
                    {
                        String JSON = Cache.Get(key).Serialize();
                        await _stockHandler.Clients.All.requestData(key, JSON);
                    }
                }
                //  }

                await Task.Delay(1000);
            }
        }