'Python Calculate sum of x object attributes in a list
I have a problem. I am trying to calculate the Moving Average over my list of candlesticks. To do so, I had something like this:
def calculate(ma, candlesticks):
maData = []
i = 0
while i < len(candlesticks) - ma + 1:
# Calculate the average of current window
movingAverage = round(np.sum(candlesticks[i:i+ma]) / ma, 2)
# Store the moving average
maData.append(movingAverage)
# Shift window to right by one position
i += 1
return maData
The problem I have is that the list of candlesticks contains objects with attributes. I need to calculate the moving average over the closes, so I thought I use:
movingAverage = round(np.sum(candlesticks[i:i+ma].close) / ma, 2)
But python says:
AttributeError: 'list' object has no attribute 'close'
The first round of this loop gives the following result when I print candlesticks[i:i+ma]:
[{'dateTimeChanged': '2022-04-01T21:40:59.000Z', 'openTime': 1577836800000, 'symbol': 'LTCUSDT', 'interval': '1d', 'open': 41.29, 'high': 42.08, 'low': 41.16, 'close': 41.16, 'volume': 110044.87445}, {'dateTimeChanged': '2022-04-01T21:40:59.000Z', 'openTime': 1577923200000, 'symbol': 'LTCUSDT', 'interval': '1d', 'open': 41.58, 'high': 41.74, 'low': 39.17, 'close': 39.17, 'volume': 194486.2157}, {'dateTimeChanged': '2022-04-01T21:40:59.000Z', 'openTime': 1578009600000, 'symbol': 'LTCUSDT', 'interval': '1d', 'open': 39.44, 'high': 42.39, 'low': 38.77, 'close': 38.77, 'volume': 426771.00676}, {'dateTimeChanged': '2022-04-01T21:40:59.000Z', 'openTime': 1578096000000, 'symbol': 'LTCUSDT', 'interval': '1d', 'open': 42.23, 'high': 42.9, 'low': 41.77, 'close': 41.77, 'volume': 248814.62818}, {'dateTimeChanged': '2022-04-01T21:40:59.000Z', 'openTime': 1578182400000, 'symbol': 'LTCUSDT', 'interval': '1d', 'open': 42.79, 'high': 44.61, 'low': 42.6, 'close': 42.6, 'volume': 291964.84958}, {'dateTimeChanged': '2022-04-01T21:40:59.000Z', 'openTime': 1578268800000, 'symbol': 'LTCUSDT', 'interval': '1d', 'open': 43.29, 'high': 46.08, 'low': 43.16, 'close': 43.16, 'volume': 384930.9909}, {'dateTimeChanged': '2022-04-01T21:40:59.000Z', 'openTime': 1578355200000, 'symbol': 'LTCUSDT', 'interval': '1d', 'open': 45.8, 'high': 47.03, 'low': 44.48, 'close': 44.48, 'volume': 493847.10532}, {'dateTimeChanged': '2022-04-01T21:40:59.000Z', 'openTime': 1578441600000, 'symbol': 'LTCUSDT', 'interval': '1d', 'open': 46.3, 'high': 48.54, 'low': 44, 'close': 44, 'volume': 628050.68438}, {'dateTimeChanged': '2022-04-01T21:40:59.000Z', 'openTime': 1578528000000, 'symbol': 'LTCUSDT', 'interval': '1d', 'open': 45.35, 'high': 45.79, 'low': 43.9, 'close': 43.9, 'volume': 296221.65692}, {'dateTimeChanged': '2022-04-01T21:40:59.000Z', 'openTime': 1578614400000, 'symbol': 'LTCUSDT', 'interval': '1d', 'open': 44.75, 'high': 49.72, 'low': 43.71, 'close': 43.71, 'volume': 729803.12291}]
How can I use the attribute close of the candlestick list to calculate the moving avearge?
Solution 1:[1]
You cannot just access .close on the list, you need to access it on the individual elements. Instead of
candlesticks[i:i+ma].close
do
[candle.close for candle in candlesticks[i:i+ma]]
Solution 2:[2]
The problem is that candlesticks[i:i+ma] is an array of ma elements, that is why you can't access the close property. Instead, what you could do is
s = sum(candlesticks[i + j].close for j in range(ma))
movingAverage = round(s / ma, 2)
The nice thing about this is that you pass an iterator to the sum function, so you don't create an extra list to perform the addition.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | luk2302 |
| Solution 2 | ALM_AndroidDev |
