如何在python中检索Oanda Rest API“AccountDetail类”的嵌套字典数据

2024-06-26 08:27:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用OANDARESTv20API,并使用Oandas包装器和名为“AccountDetail”的类检索(我的演示帐户的)AccountDetail。已检索此数据,如以下代码所示:-

api = oandapyV20.API(access_token=key)
r = accounts.AccountDetails(acc_id)
p = api.request(r)
print(p)

WARE r和p只是一个变量,accounts是模块,AccountDetails是该accounts模块的类。api使用oandapyV20模块的另一个变量,该模块具有api类。这是我连接rest api的基本开始,也是我如何管理以下数据的开始:-

{
    "lastTransactionID": "894",
    "account": {
        "pendingOrderCount": 0,
        "pl": "-109.1811",
        "guaranteedExecutionFees": "0.0000",
        "lastTransactionID": "894",
        "commission": "0.0000",
        "orders": [],
        "marginCloseoutNAV": "99837.2255",
        "marginCallMarginUsed": "110.1160",
        "positionValue": "11011.6000",
        "balance": "99860.2279",
        "resettablePLTime": "0",
        "dividendAdjustment": "0",
        "positions": [
            {
                "pl": "-1.4039",
                "dividendAdjustment": "0.0000",
                "guaranteedExecutionFees": "0.0000",
                "commission": "0.0000",
                "short": {
                    "pl": "0.0000",
                    "dividendAdjustment": "0.0000",
                    "guaranteedExecutionFees": "0.0000",
                    "units": "0",
                    "unrealizedPL": "0.0000",
                    "resettablePL": "0.0000",
                    "financing": "0.0000"
                },
                "long": {
                    "pl": "-1.4039",
                    "dividendAdjustment": "0.0000",
                    "guaranteedExecutionFees": "0.0000",
                    "units": "0",
                    "unrealizedPL": "0.0000",
                    "resettablePL": "-1.4039",
                    "financing": "0.0000"
                },
                "marginUsed": "0.0000",
                "instrument": "EUR_USD",
                "unrealizedPL": "0.0000",
                "resettablePL": "-1.4039",
                "financing": "0.0000"
            },
            {
                "pl": "-55.4953",
                "dividendAdjustment": "0.0000",
                "guaranteedExecutionFees": "0.0000",
                "commission": "0.0000",
                "short": {
                    "tradeIDs": [
                        "893"
                    ],
                    "pl": "-102.6948",
                    "dividendAdjustment": "0.0000",
                    "averagePrice": "118.473",
                    "units": "-10000",
                    "guaranteedExecutionFees": "0.0000",
                    "unrealizedPL": "-23.8631",
                    "resettablePL": "-102.6948",
                    "financing": "-0.7154"
                },
                "long": {
                    "pl": "47.1995",
                    "dividendAdjustment": "0.0000",
                    "guaranteedExecutionFees": "0.0000",
                    "units": "0",
                    "unrealizedPL": "0.0000",
                    "resettablePL": "47.1995",
                    "financing": "0.0000"
                },
                "marginUsed": "110.1160",
                "instrument": "EUR_JPY",
                "unrealizedPL": "-23.8631",
                "resettablePL": "-55.4953",
                "financing": "-0.7154"
            },
            {
                "pl": "-0.0060",
                "dividendAdjustment": "0.0000",
                "guaranteedExecutionFees": "0.0000",
                "commission": "0.0000",
                "short": {
                    "pl": "0.0000",
                    "dividendAdjustment": "0.0000",
                    "guaranteedExecutionFees": "0.0000",
                    "units": "0",
                    "unrealizedPL": "0.0000",
                    "resettablePL": "0.0000",
                    "financing": "0.0000"
                },
                "long": {
                    "pl": "-0.0060",
                    "dividendAdjustment": "0.0000",
                    "guaranteedExecutionFees": "0.0000",
                    "units": "0",
                    "unrealizedPL": "0.0000",
                    "resettablePL": "-0.0060",
                    "financing": "0.0000"
                },
                "marginUsed": "0.0000",
                "instrument": "USD_CAD",
                "unrealizedPL": "0.0000",
                "resettablePL": "-0.0060",
                "financing": "0.0000"
            },
            {
                "pl": "10.1980",
                "dividendAdjustment": "0.0000",
                "guaranteedExecutionFees": "0.0000",
                "commission": "0.0000",
                "short": {
                    "pl": "0.0000",
                    "dividendAdjustment": "0.0000",
                    "guaranteedExecutionFees": "0.0000",
                    "units": "0",
                    "unrealizedPL": "0.0000",
                    "resettablePL": "0.0000",
                    "financing": "0.0000"
                },
                "long": {
                    "pl": "10.1980",
                    "dividendAdjustment": "0.0000",
                    "guaranteedExecutionFees": "0.0000",
                    "units": "0",
                    "unrealizedPL": "0.0000",
                    "resettablePL": "10.1980",
                    "financing": "-0.0178"
                },
                "marginUsed": "0.0000",
                "instrument": "USD_CHF",
                "unrealizedPL": "0.0000",
                "resettablePL": "10.1980",
                "financing": "-0.0178"
            },
            {
                "pl": "-172.7607",
                "dividendAdjustment": "0.0000",
                "guaranteedExecutionFees": "0.0000",
                "commission": "0.0000",
                "short": {
                    "pl": "-172.7607",
                    "dividendAdjustment": "0.0000",
                    "guaranteedExecutionFees": "0.0000",
                    "units": "0",
                    "unrealizedPL": "0.0000",
                    "resettablePL": "-172.7607",
                    "financing": "-13.1231"
                },
                "long": {
                    "pl": "0.0000",
                    "dividendAdjustment": "0.0000",
                    "guaranteedExecutionFees": "0.0000",
                    "units": "0",
                    "unrealizedPL": "0.0000",
                    "resettablePL": "0.0000",
                    "financing": "0.0000"
                },
                "marginUsed": "0.0000",
                "instrument": "USD_JPY",
                "unrealizedPL": "0.0000",
                "resettablePL": "-172.7607",
                "financing": "-13.1231"
            },
            {
                "pl": "206.7998",
                "dividendAdjustment": "0.0000",
                "guaranteedExecutionFees": "0.0000",
                "commission": "0.0000",
                "short": {
                    "pl": "226.8000",
                    "dividendAdjustment": "0.0000",
                    "guaranteedExecutionFees": "0.0000",
                    "units": "0",
                    "unrealizedPL": "0.0000",
                    "resettablePL": "226.8000",
                    "financing": "-6.1463"
                },
                "long": {
                    "pl": "-20.0002",
                    "dividendAdjustment": "0.0000",
                    "guaranteedExecutionFees": "0.0000",
                    "units": "0",
                    "unrealizedPL": "0.0000",
                    "resettablePL": "-20.0002",
                    "financing": "0.0000"
                },
                "marginUsed": "0.0000",
                "instrument": "GBP_USD",
                "unrealizedPL": "0.0000",
                "resettablePL": "206.7998",
                "financing": "-6.1463"
            },
            {
                "pl": "-131.5783",
                "dividendAdjustment": "0.0000",
                "guaranteedExecutionFees": "0.0000",
                "commission": "0.0000",
                "short": {
                    "pl": "0.0000",
                    "dividendAdjustment": "0.0000",
                    "guaranteedExecutionFees": "0.0000",
                    "units": "0",
                    "unrealizedPL": "0.0000",
                    "resettablePL": "0.0000",
                    "financing": "0.0000"
                },
                "long": {
                    "pl": "-131.5783",
                    "dividendAdjustment": "0.0000",
                    "guaranteedExecutionFees": "0.0000",
                    "units": "0",
                    "unrealizedPL": "0.0000",
                    "resettablePL": "-131.5783",
                    "financing": "-4.1511"
                },
                "marginUsed": "0.0000",
                "instrument": "GBP_JPY",
                "unrealizedPL": "0.0000",
                "resettablePL": "-131.5783",
                "financing": "-4.1511"
            },
            {
                "pl": "-112.8966",
                "dividendAdjustment": "0.0000",
                "guaranteedExecutionFees": "0.0000",
                "commission": "0.0000",
                "short": {
                    "pl": "-112.9000",
                    "dividendAdjustment": "0.0000",
                    "guaranteedExecutionFees": "0.0000",
                    "units": "0",
                    "unrealizedPL": "0.0000",
                    "resettablePL": "-112.9000",
                    "financing": "-3.5352"
                },
                "long": {
                    "pl": "0.0034",
                    "dividendAdjustment": "0.0000",
                    "guaranteedExecutionFees": "0.0000",
                    "units": "0",
                    "unrealizedPL": "0.0000",
                    "resettablePL": "0.0034",
                    "financing": "0.0000"
                },
                "marginUsed": "0.0000",
                "instrument": "AUD_USD",
                "unrealizedPL": "0.0000",
                "resettablePL": "-112.8966",
                "financing": "-3.5352"
            },
            {
                "pl": "170.9776",
                "dividendAdjustment": "0.0000",
                "guaranteedExecutionFees": "0.0000",
                "commission": "0.0000",
                "short": {
                    "pl": "0.0000",
                    "dividendAdjustment": "0.0000",
                    "guaranteedExecutionFees": "0.0000",
                    "units": "0",
                    "unrealizedPL": "0.0000",
                    "resettablePL": "0.0000",
                    "financing": "0.0000"
                },
                "long": {
                    "pl": "170.9776",
                    "dividendAdjustment": "0.0000",
                    "guaranteedExecutionFees": "0.0000",
                    "units": "0",
                    "unrealizedPL": "0.0000",
                    "resettablePL": "170.9776",
                    "financing": "-2.8191"
                },
                "marginUsed": "0.0000",
                "instrument": "AUD_JPY",
                "unrealizedPL": "0.0000",
                "resettablePL": "170.9776",
                "financing": "-2.8191"
            },
            {
                "pl": "13.7535",
                "dividendAdjustment": "0.0000",
                "guaranteedExecutionFees": "0.0000",
                "commission": "0.0000",
                "short": {
                    "pl": "0.0000",
                    "dividendAdjustment": "0.0000",
                    "guaranteedExecutionFees": "0.0000",
                    "units": "0",
                    "unrealizedPL": "0.0000",
                    "resettablePL": "0.0000",
                    "financing": "0.0000"
                },
                "long": {
                    "pl": "13.7535",
                    "dividendAdjustment": "0.0000",
                    "guaranteedExecutionFees": "0.0000",
                    "units": "0",
                    "unrealizedPL": "0.0000",
                    "resettablePL": "13.7535",
                    "financing": "-0.0830"
                },
                "marginUsed": "0.0000",
                "instrument": "NZD_USD",
                "unrealizedPL": "0.0000",
                "resettablePL": "13.7535",
                "financing": "-0.0830"
            },
            {
                "pl": "-15.3982",
                "dividendAdjustment": "0.0000",
                "guaranteedExecutionFees": "0.0000",
                "commission": "0.0000",
                "short": {
                    "pl": "-15.3982",
                    "dividendAdjustment": "0.0000",
                    "guaranteedExecutionFees": "0.0000",
                    "units": "0",
                    "unrealizedPL": "0.0000",
                    "resettablePL": "-15.3982",
                    "financing": "0.0000"
                },
                "long": {
                    "pl": "0.0000",
                    "dividendAdjustment": "0.0000",
                    "guaranteedExecutionFees": "0.0000",
                    "units": "0",
                    "unrealizedPL": "0.0000",
                    "resettablePL": "0.0000",
                    "financing": "0.0000"
                },
                "marginUsed": "0.0000",
                "instrument": "NZD_CHF",
                "unrealizedPL": "0.0000",
                "resettablePL": "-15.3982",
                "financing": "0.0000"
            },
            {
                "pl": "-21.3710",
                "dividendAdjustment": "0.0000",
                "guaranteedExecutionFees": "0.0000",
                "commission": "0.0000",
                "short": {
                    "pl": "0.0000",
                    "dividendAdjustment": "0.0000",
                    "guaranteedExecutionFees": "0.0000",
                    "units": "0",
                    "unrealizedPL": "0.0000",
                    "resettablePL": "0.0000",
                    "financing": "0.0000"
                },
                "long": {
                    "pl": "-21.3710",
                    "dividendAdjustment": "0.0000",
                    "guaranteedExecutionFees": "0.0000",
                    "units": "0",
                    "unrealizedPL": "0.0000",
                    "resettablePL": "-21.3710",
                    "financing": "0.0000"
                },
                "marginUsed": "0.0000",
                "instrument": "XAU_USD",
                "unrealizedPL": "0.0000",
                "resettablePL": "-21.3710",
                "financing": "0.0000"
            }
        ],
        "marginRate": "0.01",
        "createdTime": "2020-05-02T07:33:48.590220868Z",
        "marginCloseoutMarginUsed": "110.1160",
        "trades": [
            {
                "initialMarginRequired": "109.9940",
                "openTime": "2020-05-27T19:46:08.402723077Z",
                "currentUnits": "-10000",
                "marginUsed": "110.1160",
                "realizedPL": "0.0000",
                "id": "893",
                "state": "OPEN",
                "dividendAdjustment": "0.0000",
                "price": "118.473",
                "instrument": "EUR_JPY",
                "initialUnits": "-10000",
                "unrealizedPL": "-23.8631",
                "financing": "-0.7154"
            }
        ],
        "marginUsed": "110.1160",
        "financing": "-30.5910",
        "marginCallPercent": "0.00110",
        "NAV": "99836.3648",
        "alias": "Primary",
        "openTradeCount": 1,
        "marginCloseoutPositionValue": "11011.6000",
        "marginAvailable": "99726.3658",
        "marginCloseoutPercent": "0.00055",
        "hedgingEnabled": false,
        "marginCloseoutUnrealizedPL": "-23.0024",
        "openPositionCount": 1,
        "createdByUserID": 14583473,
        "currency": "USD",
        "withdrawalLimit": "99726.3658",
        "unrealizedPL": "-23.8631",
        "resettablePL": "-109.1811",
        "guaranteedStopLossOrderMode": "DISABLED",
        "id": "xx-xx-xx-xx-xx"
    }
}

现在主要的问题是如何从交易中检索initialUnits值并将其存储在变量中,以便进一步将其用于其他条件语句

 "trades": [
            {
                "initialMarginRequired": "109.9940",
                "openTime": "2020-05-27T19:46:08.402723077Z",
                "currentUnits": "-10000",
                "marginUsed": "110.1160",
                "realizedPL": "0.0000",
                "id": "893",
                "state": "OPEN",
                "dividendAdjustment": "0.0000",
                "price": "118.473",
                "instrument": "EUR_JPY",
                "initialUnits": "-10000",
                "unrealizedPL": "-23.8631",
                "financing": "-0.7154"
            }
        ]

Tags: apilongshortusdplunitsinstrumentcommission