Ссылка на generi c type '<YourStruct>' требует аргументов в <...> - PullRequest
1 голос
/ 18 июня 2020

Я пытаюсь смоделировать API TDAmeritrade , используя кодируемые протоколы. Вы можете увидеть пример JSON в разделе «Сводка ответа». Все значения для Account кажутся нормальными, за исключением свойства positions.

Свойство инструмента структуры Position может быть одного из 5 типов: Option, CashEquivalent, MutualFund, FixedIncome и Equity. Я понял, что могу сделать свойство инструмента позиции кодируемым инструментом, создав общий тип c для позиции.

В свойстве Positions будет 0 или более позиций с Option в качестве свойства инструмента, 0 или более позиций с CashEquivalent в качестве свойства инструмента, 0 или более позиций с MutualFund в качестве свойства инструмента, 0 или больше позиций с фиксированным доходом в качестве свойства инструмента и / или 0 или более позиций с капиталом в качестве свойства инструмента.

Как я могу исправить эту ошибку и правильно представить это JSON Мне дано?

protocol Instrument: Codable {}

extension Option: Instrument {}
extension CashEquivalent: Instrument {}
extension MutualFund: Instrument {}
extension FixedIncome: Instrument {}
extension Equity: Instrument {}

struct Position<T: Instrument>: Codable {
    let shortQuantity, averagePrice, currentDayProfitLoss, currentDayProfitLossPercentage: Double
    let longQuantity, settledLongQuantity, settledShortQuantity, agedQuantity: Double
    let instrument: T
    let marketValue: Double
}

// MARK: - Account
struct Account: Codable {

    enum AccountType {
        case MARGIN, CASH
    }

    let type: AccountType
    let accountID: String
    let roundTrips: Int
    let isDayTrader, isClosingOnlyRestricted: Bool
    let positions: [Position]
    let orderStrategies: [OrderStrategy]
    let initialBalances: InitialBalances
    let currentBalances: Balances
    let projectedBalances: Balances

    enum CodingKeys: String, CodingKey {
        case type
        case accountID = "accountId"
        case roundTrips, isDayTrader, isClosingOnlyRestricted, positions, orderStrategies, initialBalances, currentBalances, projectedBalances
    }
}

Для читателей, которым нравится исчерпывающий код. Вот остальные предметы, которые у меня есть. Если я случайно пропущу один из этих намерений и целей, вы можете заменить его простым типом codable, например строкой: ie: typeAlias AssetType = String.

struct Option: Codable {
    let assetType: AssetType
    let cusip, symbol, optionDescription: String
    let type: OptionType
    let putCall: PutCall
    let underlyingSymbol: String
    let optionMultiplier: Int
    let optionDeliverables: [OptionDeliverable]

    enum CodingKeys: String, CodingKey {
        case assetType, cusip, symbol
        case optionDescription = "description"
        case type, putCall, underlyingSymbol, optionMultiplier, optionDeliverables
    }
}

struct CashEquivalent: Codable {
    let assetType: AssetType
    let cusip, symbol, cashEquivalentDescription: String
    let type: CashEquivalentType

    enum CodingKeys: String, CodingKey {
        case assetType, cusip, symbol
        case cashEquivalentDescription = "description"
        case type
    }
}

struct MutualFund: Codable {
    let assetType: AssetType
    let cusip, symbol, mutualFundDescription: String
    let type: MutualFundType

    enum CodingKeys: String, CodingKey {
        case assetType, cusip, symbol
        case mutualFundDescription = "description"
        case type
    }
}

struct FixedIncome: Codable {
    let assetType: AssetType
    let cusip, symbol, fixedIncomeDescription: String
    let maturityDate: String
    let variableRate, factor: Double

    enum CodingKeys: String, CodingKey {
        case assetType, cusip, symbol
        case fixedIncomeDescription = "description"
        case maturityDate, variableRate, factor
    }
}

struct Equity: InstrumentCodable {
    var assetType: AssetType
    var cusip: String
    var symbol: Symbol
    var description: String
}

Вот пример JSON ответа на мой последний вызов api учетной записи:

{
  "securitiesAccount": {
    "type": "CASH",
    "accountId": "111111111",
    "roundTrips": 0,
    "isDayTrader": false,
    "isClosingOnlyRestricted": false,
    "positions": [
      {
        "shortQuantity": 0,
        "averagePrice": 0.58,
        "currentDayProfitLoss": 1.8,
        "currentDayProfitLossPercentage": 0.34,
        "longQuantity": 9,
        "settledLongQuantity": 9,
        "settledShortQuantity": 0,
        "instrument": {
          "assetType": "EQUITY",
          "cusip": "26605Q106",
          "symbol": "DXF"
        },
        "marketValue": 7.02
      },
      {
        "shortQuantity": 0,
        "averagePrice": 0.47,
        "currentDayProfitLoss": -0.03,
        "currentDayProfitLossPercentage": -0.06,
        "longQuantity": 1,
        "settledLongQuantity": 1,
        "settledShortQuantity": 0,
        "instrument": {
          "assetType": "EQUITY",
          "cusip": "79971C201",
          "symbol": "SNMP"
        },
        "marketValue": 0.44
      }
    ],
    "initialBalances": {
      "accruedInterest": 0,
      "cashAvailableForTrading": 94.31,
      "cashAvailableForWithdrawal": 94.31,
      "cashBalance": 94.31,
      "bondValue": 0,
      "cashReceipts": 0,
      "liquidationValue": 100.57,
      "longOptionMarketValue": 0,
      "longStockValue": 6.26,
      "moneyMarketFund": 0,
      "mutualFundValue": 0,
      "shortOptionMarketValue": 0,
      "shortStockValue": 0,
      "isInCall": false,
      "unsettledCash": 0,
      "cashDebitCallValue": 0,
      "pendingDeposits": 0,
      "accountValue": 100.57
    },
    "currentBalances": {
      "accruedInterest": 0,
      "cashBalance": 94.31,
      "cashReceipts": 0,
      "longOptionMarketValue": 0,
      "liquidationValue": 101.77,
      "longMarketValue": 7.46,
      "moneyMarketFund": 0,
      "savings": 0,
      "shortMarketValue": 0,
      "pendingDeposits": 0,
      "cashAvailableForTrading": 94.31,
      "cashAvailableForWithdrawal": 94.31,
      "cashCall": 0,
      "longNonMarginableMarketValue": 94.31,
      "totalCash": 94.31,
      "shortOptionMarketValue": 0,
      "bondValue": 0,
      "cashDebitCallValue": 0,
      "unsettledCash": 0
    },
    "projectedBalances": {
      "cashAvailableForTrading": 94.31,
      "cashAvailableForWithdrawal": 94.31
    }
  }
}

Пример JSON, предоставленный TDAmeritrade API:

//Account:
{
  "securitiesAccount": "The type <securitiesAccount> has the following subclasses [MarginAccount, CashAccount] descriptions are listed below"
}

//The class <securitiesAccount> has the 
//following subclasses: 
//-MarginAccount
//-CashAccount
//JSON for each are listed below: 

//MarginAccount:
{
  "type": "'CASH' or 'MARGIN'",
  "accountId": "string",
  "roundTrips": 0,
  "isDayTrader": false,
  "isClosingOnlyRestricted": false,
  "positions": [
    {
      "shortQuantity": 0,
      "averagePrice": 0,
      "currentDayProfitLoss": 0,
      "currentDayProfitLossPercentage": 0,
      "longQuantity": 0,
      "settledLongQuantity": 0,
      "settledShortQuantity": 0,
      "agedQuantity": 0,
      "instrument": "The type <Instrument> has the following subclasses [Equity, FixedIncome, MutualFund, CashEquivalent, Option] descriptions are listed below\"",
      "marketValue": 0
    }
  ],
  "orderStrategies": [
    {
      "session": "'NORMAL' or 'AM' or 'PM' or 'SEAMLESS'",
      "duration": "'DAY' or 'GOOD_TILL_CANCEL' or 'FILL_OR_KILL'",
      "orderType": "'MARKET' or 'LIMIT' or 'STOP' or 'STOP_LIMIT' or 'TRAILING_STOP' or 'MARKET_ON_CLOSE' or 'EXERCISE' or 'TRAILING_STOP_LIMIT' or 'NET_DEBIT' or 'NET_CREDIT' or 'NET_ZERO'",
      "cancelTime": {
        "date": "string",
        "shortFormat": false
      },
      "complexOrderStrategyType": "'NONE' or 'COVERED' or 'VERTICAL' or 'BACK_RATIO' or 'CALENDAR' or 'DIAGONAL' or 'STRADDLE' or 'STRANGLE' or 'COLLAR_SYNTHETIC' or 'BUTTERFLY' or 'CONDOR' or 'IRON_CONDOR' or 'VERTICAL_ROLL' or 'COLLAR_WITH_STOCK' or 'DOUBLE_DIAGONAL' or 'UNBALANCED_BUTTERFLY' or 'UNBALANCED_CONDOR' or 'UNBALANCED_IRON_CONDOR' or 'UNBALANCED_VERTICAL_ROLL' or 'CUSTOM'",
      "quantity": 0,
      "filledQuantity": 0,
      "remainingQuantity": 0,
      "requestedDestination": "'INET' or 'ECN_ARCA' or 'CBOE' or 'AMEX' or 'PHLX' or 'ISE' or 'BOX' or 'NYSE' or 'NASDAQ' or 'BATS' or 'C2' or 'AUTO'",
      "destinationLinkName": "string",
      "releaseTime": "string",
      "stopPrice": 0,
      "stopPriceLinkBasis": "'MANUAL' or 'BASE' or 'TRIGGER' or 'LAST' or 'BID' or 'ASK' or 'ASK_BID' or 'MARK' or 'AVERAGE'",
      "stopPriceLinkType": "'VALUE' or 'PERCENT' or 'TICK'",
      "stopPriceOffset": 0,
      "stopType": "'STANDARD' or 'BID' or 'ASK' or 'LAST' or 'MARK'",
      "priceLinkBasis": "'MANUAL' or 'BASE' or 'TRIGGER' or 'LAST' or 'BID' or 'ASK' or 'ASK_BID' or 'MARK' or 'AVERAGE'",
      "priceLinkType": "'VALUE' or 'PERCENT' or 'TICK'",
      "price": 0,
      "taxLotMethod": "'FIFO' or 'LIFO' or 'HIGH_COST' or 'LOW_COST' or 'AVERAGE_COST' or 'SPECIFIC_LOT'",
      "orderLegCollection": [
        {
          "orderLegType": "'EQUITY' or 'OPTION' or 'INDEX' or 'MUTUAL_FUND' or 'CASH_EQUIVALENT' or 'FIXED_INCOME' or 'CURRENCY'",
          "legId": 0,
          "instrument": "\"The type <Instrument> has the following subclasses [Equity, FixedIncome, MutualFund, CashEquivalent, Option] descriptions are listed below\"",
          "instruction": "'BUY' or 'SELL' or 'BUY_TO_COVER' or 'SELL_SHORT' or 'BUY_TO_OPEN' or 'BUY_TO_CLOSE' or 'SELL_TO_OPEN' or 'SELL_TO_CLOSE' or 'EXCHANGE'",
          "positionEffect": "'OPENING' or 'CLOSING' or 'AUTOMATIC'",
          "quantity": 0,
          "quantityType": "'ALL_SHARES' or 'DOLLARS' or 'SHARES'"
        }
      ],
      "activationPrice": 0,
      "specialInstruction": "'ALL_OR_NONE' or 'DO_NOT_REDUCE' or 'ALL_OR_NONE_DO_NOT_REDUCE'",
      "orderStrategyType": "'SINGLE' or 'OCO' or 'TRIGGER'",
      "orderId": 0,
      "cancelable": false,
      "editable": false,
      "status": "'AWAITING_PARENT_ORDER' or 'AWAITING_CONDITION' or 'AWAITING_MANUAL_REVIEW' or 'ACCEPTED' or 'AWAITING_UR_OUT' or 'PENDING_ACTIVATION' or 'QUEUED' or 'WORKING' or 'REJECTED' or 'PENDING_CANCEL' or 'CANCELED' or 'PENDING_REPLACE' or 'REPLACED' or 'FILLED' or 'EXPIRED'",
      "enteredTime": "string",
      "closeTime": "string",
      "tag": "string",
      "accountId": 0,
      "orderActivityCollection": [
        "\"The type <OrderActivity> has the following subclasses [Execution] descriptions are listed below\""
      ],
      "replacingOrderCollection": [
        {}
      ],
      "childOrderStrategies": [
        {}
      ],
      "statusDescription": "string"
    }
  ],
  "initialBalances": {
    "accruedInterest": 0,
    "availableFundsNonMarginableTrade": 0,
    "bondValue": 0,
    "buyingPower": 0,
    "cashBalance": 0,
    "cashAvailableForTrading": 0,
    "cashReceipts": 0,
    "dayTradingBuyingPower": 0,
    "dayTradingBuyingPowerCall": 0,
    "dayTradingEquityCall": 0,
    "equity": 0,
    "equityPercentage": 0,
    "liquidationValue": 0,
    "longMarginValue": 0,
    "longOptionMarketValue": 0,
    "longStockValue": 0,
    "maintenanceCall": 0,
    "maintenanceRequirement": 0,
    "margin": 0,
    "marginEquity": 0,
    "moneyMarketFund": 0,
    "mutualFundValue": 0,
    "regTCall": 0,
    "shortMarginValue": 0,
    "shortOptionMarketValue": 0,
    "shortStockValue": 0,
    "totalCash": 0,
    "isInCall": false,
    "unsettledCash": 0,
    "pendingDeposits": 0,
    "marginBalance": 0,
    "shortBalance": 0,
    "accountValue": 0
  },
  "currentBalances": {
    "accruedInterest": 0,
    "cashBalance": 0,
    "cashReceipts": 0,
    "longOptionMarketValue": 0,
    "liquidationValue": 0,
    "longMarketValue": 0,
    "moneyMarketFund": 0,
    "savings": 0,
    "shortMarketValue": 0,
    "pendingDeposits": 0,
    "availableFunds": 0,
    "availableFundsNonMarginableTrade": 0,
    "buyingPower": 0,
    "buyingPowerNonMarginableTrade": 0,
    "dayTradingBuyingPower": 0,
    "dayTradingBuyingPowerCall": 0,
    "equity": 0,
    "equityPercentage": 0,
    "longMarginValue": 0,
    "maintenanceCall": 0,
    "maintenanceRequirement": 0,
    "marginBalance": 0,
    "regTCall": 0,
    "shortBalance": 0,
    "shortMarginValue": 0,
    "shortOptionMarketValue": 0,
    "sma": 0,
    "mutualFundValue": 0,
    "bondValue": 0,
    "isInCall": false,
    "stockBuyingPower": 0,
    "optionBuyingPower": 0
  },
  "projectedBalances": {
    "accruedInterest": 0,
    "cashBalance": 0,
    "cashReceipts": 0,
    "longOptionMarketValue": 0,
    "liquidationValue": 0,
    "longMarketValue": 0,
    "moneyMarketFund": 0,
    "savings": 0,
    "shortMarketValue": 0,
    "pendingDeposits": 0,
    "availableFunds": 0,
    "availableFundsNonMarginableTrade": 0,
    "buyingPower": 0,
    "buyingPowerNonMarginableTrade": 0,
    "dayTradingBuyingPower": 0,
    "dayTradingBuyingPowerCall": 0,
    "equity": 0,
    "equityPercentage": 0,
    "longMarginValue": 0,
    "maintenanceCall": 0,
    "maintenanceRequirement": 0,
    "marginBalance": 0,
    "regTCall": 0,
    "shortBalance": 0,
    "shortMarginValue": 0,
    "shortOptionMarketValue": 0,
    "sma": 0,
    "mutualFundValue": 0,
    "bondValue": 0,
    "isInCall": false,
    "stockBuyingPower": 0,
    "optionBuyingPower": 0
  }
}

//OR

//CashAccount:
{
  "type": "'CASH' or 'MARGIN'",
  "accountId": "string",
  "roundTrips": 0,
  "isDayTrader": false,
  "isClosingOnlyRestricted": false,
  "positions": [
    {
      "shortQuantity": 0,
      "averagePrice": 0,
      "currentDayProfitLoss": 0,
      "currentDayProfitLossPercentage": 0,
      "longQuantity": 0,
      "settledLongQuantity": 0,
      "settledShortQuantity": 0,
      "agedQuantity": 0,
      "instrument": "\"The type <Instrument> has the following subclasses [Equity, FixedIncome, MutualFund, CashEquivalent, Option] descriptions are listed below\"",
      "marketValue": 0
    }
  ],
  "orderStrategies": [
    {
      "session": "'NORMAL' or 'AM' or 'PM' or 'SEAMLESS'",
      "duration": "'DAY' or 'GOOD_TILL_CANCEL' or 'FILL_OR_KILL'",
      "orderType": "'MARKET' or 'LIMIT' or 'STOP' or 'STOP_LIMIT' or 'TRAILING_STOP' or 'MARKET_ON_CLOSE' or 'EXERCISE' or 'TRAILING_STOP_LIMIT' or 'NET_DEBIT' or 'NET_CREDIT' or 'NET_ZERO'",
      "cancelTime": {
        "date": "string",
        "shortFormat": false
      },
      "complexOrderStrategyType": "'NONE' or 'COVERED' or 'VERTICAL' or 'BACK_RATIO' or 'CALENDAR' or 'DIAGONAL' or 'STRADDLE' or 'STRANGLE' or 'COLLAR_SYNTHETIC' or 'BUTTERFLY' or 'CONDOR' or 'IRON_CONDOR' or 'VERTICAL_ROLL' or 'COLLAR_WITH_STOCK' or 'DOUBLE_DIAGONAL' or 'UNBALANCED_BUTTERFLY' or 'UNBALANCED_CONDOR' or 'UNBALANCED_IRON_CONDOR' or 'UNBALANCED_VERTICAL_ROLL' or 'CUSTOM'",
      "quantity": 0,
      "filledQuantity": 0,
      "remainingQuantity": 0,
      "requestedDestination": "'INET' or 'ECN_ARCA' or 'CBOE' or 'AMEX' or 'PHLX' or 'ISE' or 'BOX' or 'NYSE' or 'NASDAQ' or 'BATS' or 'C2' or 'AUTO'",
      "destinationLinkName": "string",
      "releaseTime": "string",
      "stopPrice": 0,
      "stopPriceLinkBasis": "'MANUAL' or 'BASE' or 'TRIGGER' or 'LAST' or 'BID' or 'ASK' or 'ASK_BID' or 'MARK' or 'AVERAGE'",
      "stopPriceLinkType": "'VALUE' or 'PERCENT' or 'TICK'",
      "stopPriceOffset": 0,
      "stopType": "'STANDARD' or 'BID' or 'ASK' or 'LAST' or 'MARK'",
      "priceLinkBasis": "'MANUAL' or 'BASE' or 'TRIGGER' or 'LAST' or 'BID' or 'ASK' or 'ASK_BID' or 'MARK' or 'AVERAGE'",
      "priceLinkType": "'VALUE' or 'PERCENT' or 'TICK'",
      "price": 0,
      "taxLotMethod": "'FIFO' or 'LIFO' or 'HIGH_COST' or 'LOW_COST' or 'AVERAGE_COST' or 'SPECIFIC_LOT'",
      "orderLegCollection": [
        {
          "orderLegType": "'EQUITY' or 'OPTION' or 'INDEX' or 'MUTUAL_FUND' or 'CASH_EQUIVALENT' or 'FIXED_INCOME' or 'CURRENCY'",
          "legId": 0,
          "instrument": "\"The type <Instrument> has the following subclasses [Equity, FixedIncome, MutualFund, CashEquivalent, Option] descriptions are listed below\"",
          "instruction": "'BUY' or 'SELL' or 'BUY_TO_COVER' or 'SELL_SHORT' or 'BUY_TO_OPEN' or 'BUY_TO_CLOSE' or 'SELL_TO_OPEN' or 'SELL_TO_CLOSE' or 'EXCHANGE'",
          "positionEffect": "'OPENING' or 'CLOSING' or 'AUTOMATIC'",
          "quantity": 0,
          "quantityType": "'ALL_SHARES' or 'DOLLARS' or 'SHARES'"
        }
      ],
      "activationPrice": 0,
      "specialInstruction": "'ALL_OR_NONE' or 'DO_NOT_REDUCE' or 'ALL_OR_NONE_DO_NOT_REDUCE'",
      "orderStrategyType": "'SINGLE' or 'OCO' or 'TRIGGER'",
      "orderId": 0,
      "cancelable": false,
      "editable": false,
      "status": "'AWAITING_PARENT_ORDER' or 'AWAITING_CONDITION' or 'AWAITING_MANUAL_REVIEW' or 'ACCEPTED' or 'AWAITING_UR_OUT' or 'PENDING_ACTIVATION' or 'QUEUED' or 'WORKING' or 'REJECTED' or 'PENDING_CANCEL' or 'CANCELED' or 'PENDING_REPLACE' or 'REPLACED' or 'FILLED' or 'EXPIRED'",
      "enteredTime": "string",
      "closeTime": "string",
      "tag": "string",
      "accountId": 0,
      "orderActivityCollection": [
        "\"The type <OrderActivity> has the following subclasses [Execution] descriptions are listed below\""
      ],
      "replacingOrderCollection": [
        {}
      ],
      "childOrderStrategies": [
        {}
      ],
      "statusDescription": "string"
    }
  ],
  "initialBalances": {
    "accruedInterest": 0,
    "cashAvailableForTrading": 0,
    "cashAvailableForWithdrawal": 0,
    "cashBalance": 0,
    "bondValue": 0,
    "cashReceipts": 0,
    "liquidationValue": 0,
    "longOptionMarketValue": 0,
    "longStockValue": 0,
    "moneyMarketFund": 0,
    "mutualFundValue": 0,
    "shortOptionMarketValue": 0,
    "shortStockValue": 0,
    "isInCall": false,
    "unsettledCash": 0,
    "cashDebitCallValue": 0,
    "pendingDeposits": 0,
    "accountValue": 0
  },
  "currentBalances": {
    "accruedInterest": 0,
    "cashBalance": 0,
    "cashReceipts": 0,
    "longOptionMarketValue": 0,
    "liquidationValue": 0,
    "longMarketValue": 0,
    "moneyMarketFund": 0,
    "savings": 0,
    "shortMarketValue": 0,
    "pendingDeposits": 0,
    "cashAvailableForTrading": 0,
    "cashAvailableForWithdrawal": 0,
    "cashCall": 0,
    "longNonMarginableMarketValue": 0,
    "totalCash": 0,
    "shortOptionMarketValue": 0,
    "mutualFundValue": 0,
    "bondValue": 0,
    "cashDebitCallValue": 0,
    "unsettledCash": 0
  },
  "projectedBalances": {
    "accruedInterest": 0,
    "cashBalance": 0,
    "cashReceipts": 0,
    "longOptionMarketValue": 0,
    "liquidationValue": 0,
    "longMarketValue": 0,
    "moneyMarketFund": 0,
    "savings": 0,
    "shortMarketValue": 0,
    "pendingDeposits": 0,
    "cashAvailableForTrading": 0,
    "cashAvailableForWithdrawal": 0,
    "cashCall": 0,
    "longNonMarginableMarketValue": 0,
    "totalCash": 0,
    "shortOptionMarketValue": 0,
    "mutualFundValue": 0,
    "bondValue": 0,
    "cashDebitCallValue": 0,
    "unsettledCash": 0
  }
}

//The class <Instrument> has the 
//following subclasses: 
//-Equity
//-FixedIncome
//-MutualFund
//-CashEquivalent
//-Option
//JSON for each are listed below: 

//Equity:
{
  "assetType": "'EQUITY' or 'OPTION' or 'INDEX' or 'MUTUAL_FUND' or 'CASH_EQUIVALENT' or 'FIXED_INCOME' or 'CURRENCY'",
  "cusip": "string",
  "symbol": "string",
  "description": "string"
}

//OR

//FixedIncome:
{
  "assetType": "'EQUITY' or 'OPTION' or 'INDEX' or 'MUTUAL_FUND' or 'CASH_EQUIVALENT' or 'FIXED_INCOME' or 'CURRENCY'",
  "cusip": "string",
  "symbol": "string",
  "description": "string",
  "maturityDate": "string",
  "variableRate": 0,
  "factor": 0
}

//OR

//MutualFund:
{
  "assetType": "'EQUITY' or 'OPTION' or 'INDEX' or 'MUTUAL_FUND' or 'CASH_EQUIVALENT' or 'FIXED_INCOME' or 'CURRENCY'",
  "cusip": "string",
  "symbol": "string",
  "description": "string",
  "type": "'NOT_APPLICABLE' or 'OPEN_END_NON_TAXABLE' or 'OPEN_END_TAXABLE' or 'NO_LOAD_NON_TAXABLE' or 'NO_LOAD_TAXABLE'"
}

//OR

//CashEquivalent:
{
  "assetType": "'EQUITY' or 'OPTION' or 'INDEX' or 'MUTUAL_FUND' or 'CASH_EQUIVALENT' or 'FIXED_INCOME' or 'CURRENCY'",
  "cusip": "string",
  "symbol": "string",
  "description": "string",
  "type": "'SAVINGS' or 'MONEY_MARKET_FUND'"
}

//OR

//Option:
{
  "assetType": "'EQUITY' or 'OPTION' or 'INDEX' or 'MUTUAL_FUND' or 'CASH_EQUIVALENT' or 'FIXED_INCOME' or 'CURRENCY'",
  "cusip": "string",
  "symbol": "string",
  "description": "string",
  "type": "'VANILLA' or 'BINARY' or 'BARRIER'",
  "putCall": "'PUT' or 'CALL'",
  "underlyingSymbol": "string",
  "optionMultiplier": 0,
  "optionDeliverables": [
    {
      "symbol": "string",
      "deliverableUnits": 0,
      "currencyType": "'USD' or 'CAD' or 'EUR' or 'JPY'",
      "assetType": "'EQUITY' or 'OPTION' or 'INDEX' or 'MUTUAL_FUND' or 'CASH_EQUIVALENT' or 'FIXED_INCOME' or 'CURRENCY'"
    }
  ]
}

//The class <OrderActivity> has the 
//following subclasses: 
//-Execution
//JSON for each are listed below: 

//Execution:
{
  "activityType": "'EXECUTION' or 'ORDER_ACTION'",
  "executionType": "'FILL'",
  "quantity": 0,
  "orderRemainingQuantity": 0,
  "executionLegs": [
    {
      "legId": 0,
      "quantity": 0,
      "mismarkedQuantity": 0,
      "price": 0,
      "time": "string"
    }
  ]
}

1 Ответ

1 голос
/ 18 июня 2020
// MARK: - Root
struct Root: Codable {
    let securitiesAccount: SecuritiesAccount
}

// MARK: - SecuritiesAccount
struct SecuritiesAccount: Codable {
    let type, accountId: String
    let roundTrips: Int
    let isDayTrader, isClosingOnlyRestricted: Bool
    let positions: [Position]
    let initialBalances: InitialBalances
    let currentBalances: [String: Double]
    let projectedBalances: ProjectedBalances
}

// MARK: - InitialBalances
struct InitialBalances: Codable {
    let accruedInterest: Int
    let cashAvailableForTrading, cashAvailableForWithdrawal, cashBalance: Double
    let bondValue, cashReceipts: Int
    let liquidationValue: Double
    let longOptionMarketValue: Int
    let longStockValue: Double
    let moneyMarketFund, mutualFundValue, shortOptionMarketValue, shortStockValue: Int
    let isInCall: Bool
    let unsettledCash, cashDebitCallValue, pendingDeposits: Int
    let accountValue: Double
}

// MARK: - Position
struct Position: Codable {
    let shortQuantity: Int
    let averagePrice, currentDayProfitLoss, currentDayProfitLossPercentage: Double
    let longQuantity, settledLongQuantity, settledShortQuantity: Int
    let instrument: Instrument
    let marketValue: Double
}

// MARK: - Instrument
struct Instrument: Codable {
    let assetType, cusip, symbol: String
}

// MARK: - ProjectedBalances
struct ProjectedBalances: Codable {
    let cashAvailableForTrading, cashAvailableForWithdrawal: Double
}

do {
    let root = try JSONDecoder().decode(Root.self, from: Data(json.utf8))
    print(root)
} catch {
    print(error)
}
...