首页 > 解决方案 > 如何限制列表的大小并使用新值循环 FIFO?

问题描述

有没有什么好方法可以做到这一点?我在网上搜索时没有找到任何适用的解决方案。使用下面的当前代码实现,我可以使用返回值的前“n”,并在满足计数时跳出循环。我正在尝试做的是填充列表直到达到长度(用户输入),然后保持最大长度,同时循环出最旧的值,最新值按照 FIFO 顺序代替旧值。

    private static decimal SMA(int length)
    {
        List<decimal> priceHistory = new List<decimal>();

        var client = new RestClient("https://api.tdameritrade.com/v1/marketdata/" + inputSymbol + "/pricehistory?periodType=month&period=1&frequencyType=daily&frequency=1&needExtendedHoursData=false");
        client.Timeout = -1;
        var request = new RestRequest(Method.GET);
        request.AddHeader("Authorization", "Bearer " + ReadAccessToken());
        request.AddParameter("text/plain", "", ParameterType.RequestBody);
        IRestResponse response = client.Execute(request);

        dynamic history = JsonConvert.DeserializeObject<JObject>(response.Content);

        int count = 0;

        foreach (var child in history["candles"])
        {
            count += 1;

            while (count <= length)
            {
                var close = (decimal)child["close"];
                priceHistory.Add(close);
                break;
            }
        }

        decimal simpleMovingAverage = priceHistory.Average();
        return simpleMovingAverage; 

    }

因此,对于下面长度为 == 5 的示例数据,一旦在循环结束时达到第十个值,第六个值将一直递归循环到 list[0] 中,第十个值将在 list[ 4]:

{[
  {
    "open": 177.27,
    "high": 178.84,
    "low": 177.205,
    "close": 178.7,
    "volume": 1081644,
    "datetime": 1613109600000
  },
  {
    "open": 179.0,
    "high": 179.69,
    "low": 176.3242,
    "close": 176.63,
    "volume": 1834033,
    "datetime": 1613455200000
  },
  {
    "open": 176.05,
    "high": 177.3,
    "low": 175.14,
    "close": 176.65,
    "volume": 1578242,
    "datetime": 1613541600000
  },
  {
    "open": 177.25,
    "high": 179.67,
    "low": 176.15,
    "close": 179.37,
    "volume": 2088619,
    "datetime": 1613628000000
  },
  {
    "open": 179.57,
    "high": 180.77,
    "low": 176.2,
    "close": 176.54,
    "volume": 2986839,
    "datetime": 1613714400000
  },
  {
    "open": 176.46,
    "high": 177.09,
    "low": 175.01,
    "close": 176.12,
    "volume": 2271502,
    "datetime": 1613973600000
  },
  {
    "open": 177.54,
    "high": 177.54,
    "low": 174.71,
    "close": 176.17,
    "volume": 2352428,
    "datetime": 1614060000000
  },
  {
    "open": 176.0,
    "high": 177.98,
    "low": 173.76,
    "close": 177.63,
    "volume": 2742552,
    "datetime": 1614146400000
  },
  {
    "open": 177.52,
    "high": 180.75,
    "low": 177.43,
    "close": 178.76,
    "volume": 2634298,
    "datetime": 1614232800000
  },
  {
    "open": 179.44,
    "high": 179.45,
    "low": 174.605,
    "close": 175.06,
    "volume": 3363387,
    "datetime": 1614319200000
  }
]}

标签: c#json

解决方案


我在继续研究时遇到了排队。这实际上完全符合要求。在这里发帖以防对其他人有帮助。

            Queue priceQueue = new Queue();

            int count = 5;

            foreach (var child in history["candles"])
            {
                var close = (decimal)child["close"];

                priceQueue.Enqueue(close);

                if (priceQueue.Count > count)
                {
                    priceQueue.Dequeue();
                }
            }

推荐阅读