Sample code for 30+ languages & platforms
Unicode C

ETrade v1 View Portfolio

See more HTTP Misc Examples

Get portfolio information for a selected brokerage account.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkHttpW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkXmlW.h>

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

    success = FALSE;

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

    http = CkHttpW_Create();

    CkHttpW_putOAuth1(http,TRUE);
    CkHttpW_putOAuthVerifier(http,L"");
    CkHttpW_putOAuthConsumerKey(http,L"ETRADE_CONSUMER_KEY");
    CkHttpW_putOAuthConsumerSecret(http,L"ETRADE_CONSUMER_SECRET");

    // Load the access token previously obtained via the OAuth1 3-Legged Authorization examples Step1 and Step2.
    json = CkJsonObjectW_Create();
    success = CkJsonObjectW_LoadFile(json,L"qa_data/tokens/etrade.json");
    if (success != TRUE) {
        wprintf(L"Failed to load OAuth1 token\n");
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(json);
        return;
    }

    CkHttpW_putOAuthToken(http,CkJsonObjectW_stringOf(json,L"oauth_token"));
    CkHttpW_putOAuthTokenSecret(http,CkJsonObjectW_stringOf(json,L"oauth_token_secret"));

    // See the ETrade v1 API documentation HERE.

    CkHttpW_SetUrlVar(http,L"accountIdKey",L"vsnhtF7d9jXxBy6HyaAC4vQ");
    respStr = CkHttpW_quickGetStr(http,L"https://apisb.etrade.com/v1/accounts/{$accountIdKey}/portfolio");
    if (CkHttpW_getLastMethodSuccess(http) != TRUE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(json);
        return;
    }

    // A 200 status code indicates success.
    statusCode = CkHttpW_getLastStatus(http);
    wprintf(L"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 = CkXmlW_Create();
    CkXmlW_LoadXml(xml,respStr);

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

    totalPages = CkXmlW_GetChildIntValue(xml,L"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>


    CkHttpW_Dispose(http);
    CkJsonObjectW_Dispose(json);
    CkXmlW_Dispose(xml);

    }