Sample code for 30+ languages & platforms
C

ETrade v1 View Portfolio

See more HTTP Misc Examples

Get portfolio information for a selected brokerage account.

Chilkat C Downloads

C
#include <C_CkHttp.h>
#include <C_CkJsonObject.h>
#include <C_CkXml.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttp http;
    HCkJsonObject json;
    const char *respStr;
    int statusCode;
    HCkXml xml;
    const char *tagPath;
    int accountId;
    int i;
    int count_i;
    const char *positionId;
    int expiryDay;
    int expiryMonth;
    int expiryYear;
    const char *securityType;
    int strikePrice;
    const char *symbol;
    const char *symbolDescription;
    int dateAcquired;
    int pricePaid;
    int commissions;
    int otherFees;
    int quantity;
    const char *positionIndicator;
    const char *positionType;
    const char *daysGain;
    const char *daysGainPct;
    const char *marketValue;
    int totalCost;
    const char *totalGain;
    int totalGainPct;
    const char *pctOfPortfolio;
    int costPerShare;
    int todayCommissions;
    int todayFees;
    int todayPricePaid;
    int todayQuantity;
    const char *adjPrevClose;
    const char *change;
    const char *changePct;
    const char *lastTrade;
    int lastTradeTime;
    const char *quoteStatus;
    int volume;
    const char *lotsDetails;
    const char *quoteDetails;
    int totalPages;

    success = FALSE;

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    http = CkHttp_Create();

    CkHttp_putOAuth1(http,TRUE);
    CkHttp_putOAuthVerifier(http,"");
    CkHttp_putOAuthConsumerKey(http,"ETRADE_CONSUMER_KEY");
    CkHttp_putOAuthConsumerSecret(http,"ETRADE_CONSUMER_SECRET");

    // Load the access token previously obtained via the OAuth1 3-Legged Authorization examples Step1 and Step2.
    json = CkJsonObject_Create();
    success = CkJsonObject_LoadFile(json,"qa_data/tokens/etrade.json");
    if (success != TRUE) {
        printf("Failed to load OAuth1 token\n");
        CkHttp_Dispose(http);
        CkJsonObject_Dispose(json);
        return;
    }

    CkHttp_putOAuthToken(http,CkJsonObject_stringOf(json,"oauth_token"));
    CkHttp_putOAuthTokenSecret(http,CkJsonObject_stringOf(json,"oauth_token_secret"));

    // See the ETrade v1 API documentation HERE.

    CkHttp_SetUrlVar(http,"accountIdKey","vsnhtF7d9jXxBy6HyaAC4vQ");
    respStr = CkHttp_quickGetStr(http,"https://apisb.etrade.com/v1/accounts/{$accountIdKey}/portfolio");
    if (CkHttp_getLastMethodSuccess(http) != TRUE) {
        printf("%s\n",CkHttp_lastErrorText(http));
        CkHttp_Dispose(http);
        CkJsonObject_Dispose(json);
        return;
    }

    // A 200 status code indicates success.
    statusCode = CkHttp_getLastStatus(http);
    printf("statusCode = %d\n",statusCode);

    // Use the following online tool to generate parsing code from sample XML: 
    // Generate Parsing Code from XML

    // A sample XML response is shown below...

    xml = CkXml_Create();
    CkXml_LoadXml(xml,respStr);

    accountId = CkXml_GetChildIntValue(xml,"AccountPortfolio|accountId");
    i = 0;
    count_i = CkXml_NumChildrenHavingTag(xml,"AccountPortfolio|Position");
    while (i < count_i) {
        CkXml_putI(xml,i);
        positionId = CkXml_getChildContent(xml,"AccountPortfolio|Position[i]|positionId");
        expiryDay = CkXml_GetChildIntValue(xml,"AccountPortfolio|Position[i]|Product|expiryDay");
        expiryMonth = CkXml_GetChildIntValue(xml,"AccountPortfolio|Position[i]|Product|expiryMonth");
        expiryYear = CkXml_GetChildIntValue(xml,"AccountPortfolio|Position[i]|Product|expiryYear");
        securityType = CkXml_getChildContent(xml,"AccountPortfolio|Position[i]|Product|securityType");
        strikePrice = CkXml_GetChildIntValue(xml,"AccountPortfolio|Position[i]|Product|strikePrice");
        symbol = CkXml_getChildContent(xml,"AccountPortfolio|Position[i]|Product|symbol");
        symbolDescription = CkXml_getChildContent(xml,"AccountPortfolio|Position[i]|symbolDescription");
        dateAcquired = CkXml_GetChildIntValue(xml,"AccountPortfolio|Position[i]|dateAcquired");
        pricePaid = CkXml_GetChildIntValue(xml,"AccountPortfolio|Position[i]|pricePaid");
        commissions = CkXml_GetChildIntValue(xml,"AccountPortfolio|Position[i]|commissions");
        otherFees = CkXml_GetChildIntValue(xml,"AccountPortfolio|Position[i]|otherFees");
        quantity = CkXml_GetChildIntValue(xml,"AccountPortfolio|Position[i]|quantity");
        positionIndicator = CkXml_getChildContent(xml,"AccountPortfolio|Position[i]|positionIndicator");
        positionType = CkXml_getChildContent(xml,"AccountPortfolio|Position[i]|positionType");
        daysGain = CkXml_getChildContent(xml,"AccountPortfolio|Position[i]|daysGain");
        daysGainPct = CkXml_getChildContent(xml,"AccountPortfolio|Position[i]|daysGainPct");
        marketValue = CkXml_getChildContent(xml,"AccountPortfolio|Position[i]|marketValue");
        totalCost = CkXml_GetChildIntValue(xml,"AccountPortfolio|Position[i]|totalCost");
        totalGain = CkXml_getChildContent(xml,"AccountPortfolio|Position[i]|totalGain");
        totalGainPct = CkXml_GetChildIntValue(xml,"AccountPortfolio|Position[i]|totalGainPct");
        pctOfPortfolio = CkXml_getChildContent(xml,"AccountPortfolio|Position[i]|pctOfPortfolio");
        costPerShare = CkXml_GetChildIntValue(xml,"AccountPortfolio|Position[i]|costPerShare");
        todayCommissions = CkXml_GetChildIntValue(xml,"AccountPortfolio|Position[i]|todayCommissions");
        todayFees = CkXml_GetChildIntValue(xml,"AccountPortfolio|Position[i]|todayFees");
        todayPricePaid = CkXml_GetChildIntValue(xml,"AccountPortfolio|Position[i]|todayPricePaid");
        todayQuantity = CkXml_GetChildIntValue(xml,"AccountPortfolio|Position[i]|todayQuantity");
        adjPrevClose = CkXml_getChildContent(xml,"AccountPortfolio|Position[i]|adjPrevClose");
        change = CkXml_getChildContent(xml,"AccountPortfolio|Position[i]|Quick|change");
        changePct = CkXml_getChildContent(xml,"AccountPortfolio|Position[i]|Quick|changePct");
        lastTrade = CkXml_getChildContent(xml,"AccountPortfolio|Position[i]|Quick|lastTrade");
        lastTradeTime = CkXml_GetChildIntValue(xml,"AccountPortfolio|Position[i]|Quick|lastTradeTime");
        quoteStatus = CkXml_getChildContent(xml,"AccountPortfolio|Position[i]|Quick|quoteStatus");
        volume = CkXml_GetChildIntValue(xml,"AccountPortfolio|Position[i]|Quick|volume");
        lotsDetails = CkXml_getChildContent(xml,"AccountPortfolio|Position[i]|lotsDetails");
        quoteDetails = CkXml_getChildContent(xml,"AccountPortfolio|Position[i]|quoteDetails");
        i = i + 1;
    }

    totalPages = CkXml_GetChildIntValue(xml,"AccountPortfolio|totalPages");

    // <?xml version="1.0" encoding="UTF-8"?>
    // <PortfolioResponse>
    //    <AccountPortfolio>
    //       <accountId>83554788</accountId>
    //       <Position>
    //          <positionId>10087531</positionId>
    //          <Product>
    //             <expiryDay>0</expiryDay>
    //             <expiryMonth>0</expiryMonth>
    //             <expiryYear>0</expiryYear>
    //             <securityType>EQ</securityType>
    //             <strikePrice>0</strikePrice>
    //             <symbol>A</symbol>
    //          </Product>
    //          <symbolDescription>A</symbolDescription>
    //          <dateAcquired>-68400000</dateAcquired>
    //          <pricePaid>0</pricePaid>
    //          <commissions>0</commissions>
    //          <otherFees>0</otherFees>
    //          <quantity>-120</quantity>
    //          <positionIndicator>TYPE2</positionIndicator>
    //          <positionType>SHORT</positionType>
    //          <daysGain>190.80</daysGain>
    //          <daysGainPct>2.4472</daysGainPct>
    //          <marketValue>-7605.60</marketValue>
    //          <totalCost>0</totalCost>
    //          <totalGain>-7605.60</totalGain>
    //          <totalGainPct>0</totalGainPct>
    //          <pctOfPortfolio>-0.0008</pctOfPortfolio>
    //          <costPerShare>0</costPerShare>
    //          <todayCommissions>0</todayCommissions>
    //          <todayFees>0</todayFees>
    //          <todayPricePaid>0</todayPricePaid>
    //          <todayQuantity>0</todayQuantity>
    //          <adjPrevClose>64.970000</adjPrevClose>
    //          <Quick>
    //             <change>-1.59</change>
    //             <changePct>-2.4472</changePct>
    //             <lastTrade>63.38</lastTrade>
    //             <lastTradeTime>1529429280</lastTradeTime>
    //             <quoteStatus>DELAYED</quoteStatus>
    //             <volume>2431617</volume>
    //          </Quick>
    //          <lotsDetails>https://api.etrade.com/v1/accounts/JDIozUumZpHdgbIjMnAAHQ/portfolio/10087531</lotsDetails>
    //          <quoteDetails>https://api.etrade.com/v1/market/quote/A</quoteDetails>
    //       </Position>
    //       <Position>
    //          <positionId>140357348131</positionId>
    //          <Product>
    //             <expiryDay>0</expiryDay>
    //             <expiryMonth>0</expiryMonth>
    //             <expiryYear>0</expiryYear>
    //             <securityType>EQ</securityType>
    //             <strikePrice>0</strikePrice>
    //             <symbol>TWTR</symbol>
    //          </Product>
    //          <symbolDescription>TWTR</symbolDescription>
    //          <dateAcquired>-68400000</dateAcquired>
    //          <pricePaid>0</pricePaid>
    //          <commissions>0</commissions>
    //          <otherFees>0</otherFees>
    //          <quantity>3</quantity>
    //          <positionIndicator>TYPE2</positionIndicator>
    //          <positionType>LONG</positionType>
    //          <daysGain>-3.915</daysGain>
    //          <daysGainPct>-2.8369</daysGainPct>
    //          <marketValue>134.085</marketValue>
    //          <totalCost>0</totalCost>
    //          <totalGain>134.085</totalGain>
    //          <totalGainPct>0</totalGainPct>
    //          <pctOfPortfolio>0.0235</pctOfPortfolio>
    //          <costPerShare>0</costPerShare>
    //          <todayCommissions>0</todayCommissions>
    //          <todayFees>0</todayFees>
    //          <todayPricePaid>0</todayPricePaid>
    //          <todayQuantity>0</todayQuantity>
    //          <adjPrevClose>46.000000</adjPrevClose>
    //          <Quick>
    //             <change>-1.305</change>
    //             <changePct>-2.8369</changePct>
    //             <lastTrade>44.695</lastTrade>
    //             <lastTradeTime>1529429280</lastTradeTime>
    //             <quoteStatus>DELAYED</quoteStatus>
    //             <volume>26582141</volume>
    //          </Quick>
    //          <lotsDetails>https://api.etrade.com/v1/accounts/yIFaUoJ81qyAhgxLWRQ42g/portfolio/140357348131</lotsDetails>
    //          <quoteDetails>https://api.etrade.com/v1/market/quote/TWTR</quoteDetails>
    //       </Position>
    //       <totalPages>1</totalPages>
    //    </AccountPortfolio>
    // </PortfolioResponse>


    CkHttp_Dispose(http);
    CkJsonObject_Dispose(json);
    CkXml_Dispose(xml);

    }