Ищем последнюю «последнюю» цену. Кроме того, я хотел бы получить среднее значение за последние 4 периода времени. Используется JSONLint для проверки правильности строки JSON. URL пропущен из-за проблем с конфиденциальностью.
Желаемый результат должен иметь:
I.e: Indicator_mean_Intra = последняя цена печати (0,74056 + 4 «последние» цены означают (0,74056, 0,740165,0,738415,0,845215) / 2
import json
from pprint import pprint
with open('URL') as g:
USDCAN = json.load(g)
pprint(USDCAN )
## With USDCAN , you can now also find values like so:
USDCAN ["requestedSymbol"][0]["id"]
USDCAN ["requestedSymbol"]["data"]["intervals"] <--- what about factoring for time? ["last"]
Вывод с удаленными средними данными для краткости:
[{
"requestedSymbol": "USD/CAD",
"success": true,
"data": {
"symbol": "USD/CAD",
"name": "USD/CAD",
"baseCurrency": "USD",
"intervals": [{
"start": "2018-05-13T16:00:00+00:00",
"end": "2018-05-13T19:59:59.999+00:00",
"open": 1.27959,
"high": 1.27959,
"low": 1.27804,
"last": 1.278225
}, {
"start": "2018-05-16T04:00:00+00:00",
"end": "2018-05-16T07:59:59.999+00:00",
"open": 1.285645,
"high": 1.28625,
"low": 1.285645,
"last": 1.28625
}]
}
}, {
"requestedSymbol": "USD/EUR",
"success": true,
"data": {
"symbol": "USD/EUR",
"name": "USD/EUR",
"baseCurrency": "USD",
"intervals": [{
"start": "2018-05-13T16:00:00+00:00",
"end": "2018-05-13T19:59:59.999+00:00",
"open": 0.83738,
"high": 0.837485,
"low": 0.83689,
"last": 0.836995
}, {
"start": "2018-05-13T20:00:00+00:00",
"end": "2018-05-13T23:59:59.999+00:00",
"open": 0.837,
"high": 0.837555,
"low": 0.8364,
"last": 0.836635
}, {
"start": "2018-05-14T00:00:00+00:00",
"end": "2018-05-14T03:59:59.999+00:00",
"open": 0.836645,
"high": 0.8368,
"low": 0.8355,
"last": 0.835805
}, {
"start": "2018-05-14T04:00:00+00:00",
"end": "2018-05-14T07:59:59.999+00:00",
"open": 0.835795,
"high": 0.836725,
"low": 0.8344115,
"last": 0.8348295
}, {
"start": "2018-05-14T08:00:00+00:00",
"end": "2018-05-14T11:59:59.999+00:00",
"open": 0.834785,
"high": 0.835505,
"low": 0.8339,
"last": 0.8343415
}, {
"start": "2018-05-14T12:00:00+00:00",
"end": "2018-05-14T15:59:59.999+00:00",
"open": 0.834405,
"high": 0.8360155,
"low": 0.8335,
"last": 0.835045
}, {
"start": "2018-05-14T16:00:00+00:00",
"end": "2018-05-14T19:59:59.999+00:00",
"open": 0.835025,
"high": 0.8379,
"low": 0.8348,
"last": 0.837655
}, {
"start": "2018-05-14T20:00:00+00:00",
"end": "2018-05-14T23:59:59.999+00:00",
"open": 0.837645,
"high": 0.8388,
"low": 0.837485,
"last": 0.8382
}, {
"start": "2018-05-15T00:00:00+00:00",
"end": "2018-05-15T03:59:59.999+00:00",
"open": 0.838125,
"high": 0.8384,
"low": 0.8376,
"last": 0.837995
}, {
"start": "2018-05-15T04:00:00+00:00",
"end": "2018-05-15T07:59:59.999+00:00",
"open": 0.838005,
"high": 0.839605,
"low": 0.837556,
"last": 0.8386
}, {
"start": "2018-05-15T08:00:00+00:00",
"end": "2018-05-15T11:59:59.999+00:00",
"open": 0.838715,
"high": 0.842015,
"low": 0.8381875,
"last": 0.841845
}, {
"start": "2018-05-15T12:00:00+00:00",
"end": "2018-05-15T15:59:59.999+00:00",
"open": 0.841865,
"high": 0.846,
"low": 0.841015,
"last": 0.842895
}, {
"start": "2018-05-15T16:00:00+00:00",
"end": "2018-05-15T19:59:59.999+00:00",
"open": 0.842885,
"high": 0.8449,
"low": 0.841999,
"last": 0.844395
}, {
"start": "2018-05-15T20:00:00+00:00",
"end": "2018-05-15T23:59:59.999+00:00",
"open": 0.844415,
"high": 0.846225,
"low": 0.844085,
"last": 0.84598
}, {
"start": "2018-05-16T00:00:00+00:00",
"end": "2018-05-16T03:59:59.999+00:00",
"open": 0.84598,
"high": 0.84633,
"low": 0.84497,
"last": 0.845065
}, {
"start": "2018-05-16T04:00:00+00:00",
"end": "2018-05-16T07:59:59.999+00:00",
"open": 0.84509,
"high": 0.8462745,
"low": 0.844844,
"last": 0.845215
}]
}
}, {
"requestedSymbol": "USD/GBP",
"success": true,
"data": {
"symbol": "USD/GBP",
"name": "USD/GBP",
"baseCurrency": "USD",
"intervals": [{
"start": "2018-05-13T08:00:00+00:00",
"end": "2018-05-13T11:59:59.999+00:00",
"open": 0.738415,
"high": 0.738415,
"low": 0.738415,
"last": 0.738415
}, {
"start": "2018-05-15T12:00:00+00:00",
"end": "2018-05-15T15:59:59.999+00:00",
"open": 0.739895,
"high": 0.7435,
"low": 0.7393,
"last": 0.740165
}, {
"start": "2018-05-16T04:00:00+00:00",
"end": "2018-05-16T07:59:59.999+00:00",
"open": 0.740605,
"high": 0.741,
"low": 0.7402,
"last": 0.74056
}]
}
}]
Для краткости я удалил некоторые средние значения, чтобы не сделать скрипт слишком длинным. Спасибо за ваше время.