Sample code for 30+ languages & platforms
DataFlex

ETrade View Portfolio

See more ETrade Examples

Gets portfolio information for the selected brokerage account.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoHttp
    Handle hoJsonToken
    String sSandboxUrl
    String sLiveUrl
    Variant vResp
    Handle hoResp
    Handle hoXml
    Integer iAccountId
    Integer iPositionId
    Integer iExpiryDay
    Integer iExpiryMonth
    Integer iExpiryYear
    String sSecurityType
    Integer iStrikePrice
    String sSymbol
    String sSymbolDescription
    String sDateAcquired
    String sPricePaid
    String sCommissions
    String sOtherFees
    Integer iQuantity
    String sPositionIndicator
    String sPositionType
    String sDaysGain
    String sDaysGainPct
    String sMarketValue
    String sTotalCost
    String sTotalGain
    String sTotalGainPct
    String sPctOfPortfolio
    String sCostPerShare
    Integer iTodayCommissions
    Integer iTodayFees
    Integer iTodayPricePaid
    Integer iTodayQuantity
    String sChange
    String sChangePct
    String sLastTrade
    Integer iLastTradeTime
    Integer iVolume
    String sLotsDetails
    String sQuoteDetails
    Integer iTotalPages
    Integer i
    Integer iCount_i
    String sTemp1
    Integer iTemp1

    Move False To iSuccess

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

    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End

    Set ComOAuth1 Of hoHttp To True
    Set ComOAuthVerifier Of hoHttp To ""
    Set ComOAuthConsumerKey Of hoHttp To "ETRADE_CONSUMER_KEY"
    Set ComOAuthConsumerSecret Of hoHttp To "ETRADE_CONSUMER_SECRET"

    // Load the access token previously obtained via the OAuth1 Authorization
    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonToken
    If (Not(IsComObjectCreated(hoJsonToken))) Begin
        Send CreateComObject of hoJsonToken
    End
    Get ComLoadFile Of hoJsonToken "qa_data/tokens/etrade.json" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Failed to load OAuth1 token"
        Procedure_Return
    End

    Get ComStringOf Of hoJsonToken "oauth_token" To sTemp1
    Set ComOAuthToken Of hoHttp To sTemp1
    Get ComStringOf Of hoJsonToken "oauth_token_secret" To sTemp1
    Set ComOAuthTokenSecret Of hoHttp To sTemp1

    Move "https://apisb.etrade.com/v1/accounts/{$accountIdKey}/portfolio" To sSandboxUrl
    Move "https://api.etrade.com/v1/accounts/{$accountIdKey}/portfolio" To sLiveUrl

    Get ComSetUrlVar Of hoHttp "accountIdKey" "6_Dpy0rmuQ9cu9IbTfvF2A" To iSuccess

    Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
    If (Not(IsComObjectCreated(hoResp))) Begin
        Send CreateComObject of hoResp
    End
    Get pvComObject of hoResp to vResp
    Get ComHttpNoBody Of hoHttp "GET" sSandboxUrl vResp To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Make sure a successful response was received.
    Get ComStatusCode Of hoResp To iTemp1
    If (iTemp1 > 200) Begin
        Get ComStatusLine Of hoResp To sTemp1
        Showln sTemp1
        Get ComHeader Of hoResp To sTemp1
        Showln sTemp1
        Get ComBodyStr Of hoResp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Sample XML response:

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

    // <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    // <PortfolioResponse>
    //     <AccountPortfolio>
    //         <accountId>83359700</accountId>
    //         <Position>
    //             <positionId>27005131</positionId>
    //             <Product>
    //                 <expiryDay>0</expiryDay>
    //                 <expiryMonth>0</expiryMonth>
    //                 <expiryYear>0</expiryYear>
    //                 <securityType>EQ</securityType>
    //                 <strikePrice>0</strikePrice>
    //                 <symbol>BR</symbol>
    //             </Product>
    //             <symbolDescription>BR</symbolDescription>
    //             <dateAcquired>-57600000</dateAcquired>
    //             <pricePaid>0</pricePaid>
    //             <commissions>0</commissions>
    //             <otherFees>0</otherFees>
    //             <quantity>10</quantity>
    //             <positionIndicator>TYPE2</positionIndicator>
    //             <positionType>LONG</positionType>
    //             <daysGain>-2.7999</daysGain>
    //             <daysGainPct>-1.3346</daysGainPct>
    //             <marketValue>207</marketValue>
    //             <totalCost>0</totalCost>
    //             <totalGain>207</totalGain>
    //             <totalGainPct>0</totalGainPct>
    //             <pctOfPortfolio>0.0018</pctOfPortfolio>
    //             <costPerShare>0</costPerShare>
    //             <todayCommissions>0</todayCommissions>
    //             <todayFees>0</todayFees>
    //             <todayPricePaid>0</todayPricePaid>
    //             <todayQuantity>0</todayQuantity>
    //             <Quick>
    //                 <change>-0.28</change>
    //                 <changePct>-1.3346</changePct>
    //                 <lastTrade>20.70</lastTrade>
    //                 <lastTradeTime>1343160240</lastTradeTime>
    //                 <volume>431591</volume>
    //             </Quick>
    //             <lotsDetails>https://apisb.etrade.com/v1/accounts/E5Nd4LJBsEi_UyHm4Vio9g/portfolio/27005131</lotsDetails>
    //             <quoteDetails>https://apisb.etrade.com/v1/market/quote/BR</quoteDetails>
    //         </Position>
    //         <Position>
    //             <positionId>4709131</positionId>
    //             <Product>
    //                 <expiryDay>0</expiryDay>
    //                 <expiryMonth>0</expiryMonth>
    //                 <expiryYear>0</expiryYear>
    //                 <securityType>EQ</securityType>
    //                 <strikePrice>0</strikePrice>
    //                 <symbol>GLD</symbol>
    //             </Product>
    //             <symbolDescription>GLD</symbolDescription>
    //             <dateAcquired>1335250800000</dateAcquired>
    //             <pricePaid>1</pricePaid>
    //             <commissions>5</commissions>
    //             <otherFees>0</otherFees>
    //             <quantity>2</quantity>
    //             <positionIndicator>TYPE1</positionIndicator>
    //             <positionType>LONG</positionType>
    //             <daysGain>0.9799</daysGain>
    //             <daysGainPct>0.3201</daysGainPct>
    //             <marketValue>307.04</marketValue>
    //             <totalCost>7</totalCost>
    //             <totalGain>300.04</totalGain>
    //             <totalGainPct>4286.2857</totalGainPct>
    //             <pctOfPortfolio>0.0028</pctOfPortfolio>
    //             <costPerShare>3.5</costPerShare>
    //             <todayCommissions>0</todayCommissions>
    //             <todayFees>0</todayFees>
    //             <todayPricePaid>0</todayPricePaid>
    //             <todayQuantity>0</todayQuantity>
    //             <Quick>
    //                 <change>0.49</change>
    //                 <changePct>0.3201</changePct>
    //                 <lastTrade>153.52</lastTrade>
    //                 <lastTradeTime>1343160000</lastTradeTime>
    //                 <volume>6510878</volume>
    //             </Quick>
    //             <lotsDetails>https://apisb.etrade.com/v1/accounts/E5Nd4LJBsEi_UyHm4Vio9g/portfolio/4709131</lotsDetails>
    //             <quoteDetails>https://apisb.etrade.com/v1/market/quote/GLD</quoteDetails>
    //         </Position>
    //         <Position>
    //             <positionId>4729131</positionId>
    //             <Product>
    //                 <expiryDay>0</expiryDay>
    //                 <expiryMonth>0</expiryMonth>
    //                 <expiryYear>0</expiryYear>
    //                 <securityType>EQ</securityType>
    //                 <strikePrice>0</strikePrice>
    //                 <symbol>MSFT</symbol>
    //             </Product>
    //             <symbolDescription>MSFT</symbolDescription>
    //             <dateAcquired>1335250800000</dateAcquired>
    //             <pricePaid>1</pricePaid>
    //             <commissions>5</commissions>
    //             <otherFees>0</otherFees>
    //             <quantity>1</quantity>
    //             <positionIndicator>TYPE2</positionIndicator>
    //             <positionType>LONG</positionType>
    //             <daysGain>-0.13</daysGain>
    //             <daysGainPct>-0.4439</daysGainPct>
    //             <marketValue>29.1499</marketValue>
    //             <totalCost>6</totalCost>
    //             <totalGain>23.1499</totalGain>
    //             <totalGainPct>385.8333</totalGainPct>
    //             <pctOfPortfolio>0.0002</pctOfPortfolio>
    //             <costPerShare>6</costPerShare>
    //             <todayCommissions>0</todayCommissions>
    //             <todayFees>0</todayFees>
    //             <todayPricePaid>0</todayPricePaid>
    //             <todayQuantity>0</todayQuantity>
    //             <Quick>
    //                 <change>-0.13</change>
    //                 <changePct>-0.4439</changePct>
    //                 <lastTrade>29.15</lastTrade>
    //                 <lastTradeTime>1343160000</lastTradeTime>
    //                 <volume>47711254</volume>
    //             </Quick>
    //             <lotsDetails>https://apisb.etrade.com/v1/accounts/E5Nd4LJBsEi_UyHm4Vio9g/portfolio/4729131</lotsDetails>
    //             <quoteDetails>https://apisb.etrade.com/v1/market/quote/MSFT</quoteDetails>
    //         </Position>
    //         <Position>
    //             <positionId>23971131</positionId>
    //             <Product>
    //                 <expiryDay>0</expiryDay>
    //                 <expiryMonth>0</expiryMonth>
    //                 <expiryYear>0</expiryYear>
    //                 <securityType>EQ</securityType>
    //                 <strikePrice>0</strikePrice>
    //                 <symbol>MSFT</symbol>
    //             </Product>
    //             <symbolDescription>MSFT</symbolDescription>
    //             <dateAcquired>-57600000</dateAcquired>
    //             <pricePaid>0</pricePaid>
    //             <commissions>0</commissions>
    //             <otherFees>0</otherFees>
    //             <quantity>-10</quantity>
    //             <positionIndicator>TYPE5</positionIndicator>
    //             <positionType>SHORT</positionType>
    //             <daysGain>1.30</daysGain>
    //             <daysGainPct>-0.4439</daysGainPct>
    //             <marketValue>-291.5</marketValue>
    //             <totalCost>0</totalCost>
    //             <totalGain>-291.5</totalGain>
    //             <totalGainPct>0</totalGainPct>
    //             <pctOfPortfolio>-0.0026</pctOfPortfolio>
    //             <costPerShare>0</costPerShare>
    //             <todayCommissions>0</todayCommissions>
    //             <todayFees>0</todayFees>
    //             <todayPricePaid>0</todayPricePaid>
    //             <todayQuantity>0</todayQuantity>
    //             <Quick>
    //                 <change>-0.13</change>
    //                 <changePct>-0.4439</changePct>
    //                 <lastTrade>29.15</lastTrade>
    //                 <lastTradeTime>1343160000</lastTradeTime>
    //                 <volume>47711254</volume>
    //             </Quick>
    //             <lotsDetails>https://apisb.etrade.com/v1/accounts/E5Nd4LJBsEi_UyHm4Vio9g/portfolio/23971131</lotsDetails>
    //             <quoteDetails>https://apisb.etrade.com/v1/market/quote/MSFT</quoteDetails>
    //         </Position>
    //         <Position>
    //             <positionId>4725131</positionId>
    //             <Product>
    //                 <expiryDay>0</expiryDay>
    //                 <expiryMonth>0</expiryMonth>
    //                 <expiryYear>0</expiryYear>
    //                 <securityType>EQ</securityType>
    //                 <strikePrice>0</strikePrice>
    //                 <symbol>RBL</symbol>
    //             </Product>
    //             <symbolDescription>RBL</symbolDescription>
    //             <dateAcquired>1335250800000</dateAcquired>
    //             <pricePaid>1</pricePaid>
    //             <commissions>5</commissions>
    //             <otherFees>0</otherFees>
    //             <quantity>2</quantity>
    //             <positionIndicator>TYPE2</positionIndicator>
    //             <positionType>LONG</positionType>
    //             <daysGain>-0.52</daysGain>
    //             <daysGainPct>-1.0252</daysGainPct>
    //             <marketValue>50.20</marketValue>
    //             <totalCost>7</totalCost>
    //             <totalGain>43.20</totalGain>
    //             <totalGainPct>617.1428</totalGainPct>
    //             <pctOfPortfolio>0.0004</pctOfPortfolio>
    //             <costPerShare>3.5</costPerShare>
    //             <todayCommissions>0</todayCommissions>
    //             <todayFees>0</todayFees>
    //             <todayPricePaid>0</todayPricePaid>
    //             <todayQuantity>0</todayQuantity>
    //             <Quick>
    //                 <change>-0.26</change>
    //                 <changePct>-1.0252</changePct>
    //                 <lastTrade>25.10</lastTrade>
    //                 <lastTradeTime>1343159760</lastTradeTime>
    //                 <volume>11827</volume>
    //             </Quick>
    //             <lotsDetails>https://apisb.etrade.com/v1/accounts/E5Nd4LJBsEi_UyHm4Vio9g/portfolio/4725131</lotsDetails>
    //             <quoteDetails>https://apisb.etrade.com/v1/market/quote/RBL</quoteDetails>
    //         </Position>
    //         <Position>
    //             <positionId>20841131</positionId>
    //             <Product>
    //                 <expiryDay>0</expiryDay>
    //                 <expiryMonth>0</expiryMonth>
    //                 <expiryYear>0</expiryYear>
    //                 <securityType>EQ</securityType>
    //                 <strikePrice>0</strikePrice>
    //                 <symbol>RPI</symbol>
    //             </Product>
    //             <symbolDescription>RPI</symbolDescription>
    //             <dateAcquired>1335250800000</dateAcquired>
    //             <pricePaid>7.5599</pricePaid>
    //             <commissions>5</commissions>
    //             <otherFees>45</otherFees>
    //             <quantity>2</quantity>
    //             <positionIndicator>TYPE2</positionIndicator>
    //             <positionType>LONG</positionType>
    //             <daysGain>0</daysGain>
    //             <daysGainPct>0</daysGainPct>
    //             <marketValue>2.8599</marketValue>
    //             <totalCost>65.12</totalCost>
    //             <totalGain>-62.2599</totalGain>
    //             <totalGainPct>-95.6081</totalGainPct>
    //             <pctOfPortfolio>0.00</pctOfPortfolio>
    //             <costPerShare>32.56</costPerShare>
    //             <todayCommissions>0</todayCommissions>
    //             <todayFees>0</todayFees>
    //             <todayPricePaid>0</todayPricePaid>
    //             <todayQuantity>0</todayQuantity>
    //             <Quick>
    //                 <change>0</change>
    //                 <changePct>0</changePct>
    //                 <lastTrade>1.43</lastTrade>
    //                 <lastTradeTime>1343150700</lastTradeTime>
    //                 <volume>1803</volume>
    //             </Quick>
    //             <lotsDetails>https://apisb.etrade.com/v1/accounts/E5Nd4LJBsEi_UyHm4Vio9g/portfolio/20841131</lotsDetails>
    //             <quoteDetails>https://apisb.etrade.com/v1/market/quote/RPI</quoteDetails>
    //         </Position>
    //         <Position>
    //             <positionId>4732131</positionId>
    //             <Product>
    //                 <expiryDay>0</expiryDay>
    //                 <expiryMonth>0</expiryMonth>
    //                 <expiryYear>0</expiryYear>
    //                 <securityType>EQ</securityType>
    //                 <strikePrice>0</strikePrice>
    //                 <symbol>RXD</symbol>
    //             </Product>
    //             <symbolDescription>RXD</symbolDescription>
    //             <dateAcquired>1335250800000</dateAcquired>
    //             <pricePaid>1</pricePaid>
    //             <commissions>2.99</commissions>
    //             <otherFees>0.0099</otherFees>
    //             <quantity>-3</quantity>
    //             <positionIndicator>TYPE5</positionIndicator>
    //             <positionType>SHORT</positionType>
    //             <daysGain>-1.296</daysGain>
    //             <daysGainPct>2.6799</daysGainPct>
    //             <marketValue>-49.6559</marketValue>
    //             <totalCost>0</totalCost>
    //             <totalGain>-49.6559</totalGain>
    //             <totalGainPct>-827.60</totalGainPct>
    //             <pctOfPortfolio>-0.0004</pctOfPortfolio>
    //             <costPerShare>0</costPerShare>
    //             <todayCommissions>0</todayCommissions>
    //             <todayFees>0</todayFees>
    //             <todayPricePaid>0</todayPricePaid>
    //             <todayQuantity>0</todayQuantity>
    //             <Quick>
    //                 <change>0.432</change>
    //                 <changePct>2.6799</changePct>
    //                 <lastTrade>16.552</lastTrade>
    //                 <lastTradeTime>1343157480</lastTradeTime>
    //                 <volume>2200</volume>
    //             </Quick>
    //             <lotsDetails>https://apisb.etrade.com/v1/accounts/E5Nd4LJBsEi_UyHm4Vio9g/portfolio/4732131</lotsDetails>
    //             <quoteDetails>https://apisb.etrade.com/v1/market/quote/RXD</quoteDetails>
    //         </Position>
    //         <totalPages>1</totalPages>
    //     </AccountPortfolio>
    // </PortfolioResponse>
    // 

    Get Create (RefClass(cComChilkatXml)) To hoXml
    If (Not(IsComObjectCreated(hoXml))) Begin
        Send CreateComObject of hoXml
    End
    Get ComBodyStr Of hoResp To sTemp1
    Get ComLoadXml Of hoXml sTemp1 To iSuccess
    Get ComGetXml Of hoXml To sTemp1
    Showln sTemp1

    Get ComGetChildIntValue Of hoXml "AccountPortfolio|accountId" To iAccountId
    Move 0 To i
    Get ComNumChildrenHavingTag Of hoXml "AccountPortfolio|Position" To iCount_i
    While (i < iCount_i)
        Set ComI Of hoXml To i
        Get ComGetChildIntValue Of hoXml "AccountPortfolio|Position[i]|positionId" To iPositionId
        Get ComGetChildIntValue Of hoXml "AccountPortfolio|Position[i]|Product|expiryDay" To iExpiryDay
        Get ComGetChildIntValue Of hoXml "AccountPortfolio|Position[i]|Product|expiryMonth" To iExpiryMonth
        Get ComGetChildIntValue Of hoXml "AccountPortfolio|Position[i]|Product|expiryYear" To iExpiryYear
        Get ComGetChildContent Of hoXml "AccountPortfolio|Position[i]|Product|securityType" To sSecurityType
        Get ComGetChildIntValue Of hoXml "AccountPortfolio|Position[i]|Product|strikePrice" To iStrikePrice
        Get ComGetChildContent Of hoXml "AccountPortfolio|Position[i]|Product|symbol" To sSymbol
        Get ComGetChildContent Of hoXml "AccountPortfolio|Position[i]|symbolDescription" To sSymbolDescription
        Get ComGetChildContent Of hoXml "AccountPortfolio|Position[i]|dateAcquired" To sDateAcquired
        Get ComGetChildContent Of hoXml "AccountPortfolio|Position[i]|pricePaid" To sPricePaid
        Get ComGetChildContent Of hoXml "AccountPortfolio|Position[i]|commissions" To sCommissions
        Get ComGetChildContent Of hoXml "AccountPortfolio|Position[i]|otherFees" To sOtherFees
        Get ComGetChildIntValue Of hoXml "AccountPortfolio|Position[i]|quantity" To iQuantity
        Get ComGetChildContent Of hoXml "AccountPortfolio|Position[i]|positionIndicator" To sPositionIndicator
        Get ComGetChildContent Of hoXml "AccountPortfolio|Position[i]|positionType" To sPositionType
        Get ComGetChildContent Of hoXml "AccountPortfolio|Position[i]|daysGain" To sDaysGain
        Get ComGetChildContent Of hoXml "AccountPortfolio|Position[i]|daysGainPct" To sDaysGainPct
        Get ComGetChildContent Of hoXml "AccountPortfolio|Position[i]|marketValue" To sMarketValue
        Get ComGetChildContent Of hoXml "AccountPortfolio|Position[i]|totalCost" To sTotalCost
        Get ComGetChildContent Of hoXml "AccountPortfolio|Position[i]|totalGain" To sTotalGain
        Get ComGetChildContent Of hoXml "AccountPortfolio|Position[i]|totalGainPct" To sTotalGainPct
        Get ComGetChildContent Of hoXml "AccountPortfolio|Position[i]|pctOfPortfolio" To sPctOfPortfolio
        Get ComGetChildContent Of hoXml "AccountPortfolio|Position[i]|costPerShare" To sCostPerShare
        Get ComGetChildIntValue Of hoXml "AccountPortfolio|Position[i]|todayCommissions" To iTodayCommissions
        Get ComGetChildIntValue Of hoXml "AccountPortfolio|Position[i]|todayFees" To iTodayFees
        Get ComGetChildIntValue Of hoXml "AccountPortfolio|Position[i]|todayPricePaid" To iTodayPricePaid
        Get ComGetChildIntValue Of hoXml "AccountPortfolio|Position[i]|todayQuantity" To iTodayQuantity
        Get ComGetChildContent Of hoXml "AccountPortfolio|Position[i]|Quick|change" To sChange
        Get ComGetChildContent Of hoXml "AccountPortfolio|Position[i]|Quick|changePct" To sChangePct
        Get ComGetChildContent Of hoXml "AccountPortfolio|Position[i]|Quick|lastTrade" To sLastTrade
        Get ComGetChildIntValue Of hoXml "AccountPortfolio|Position[i]|Quick|lastTradeTime" To iLastTradeTime
        Get ComGetChildIntValue Of hoXml "AccountPortfolio|Position[i]|Quick|volume" To iVolume
        Get ComGetChildContent Of hoXml "AccountPortfolio|Position[i]|lotsDetails" To sLotsDetails
        Get ComGetChildContent Of hoXml "AccountPortfolio|Position[i]|quoteDetails" To sQuoteDetails
        Move (i + 1) To i
    Loop

    Get ComGetChildIntValue Of hoXml "AccountPortfolio|totalPages" To iTotalPages

    Showln "Success."


End_Procedure