Sample code for 30+ languages & platforms
DataFlex

ETrade Preview Order

See more ETrade Examples

The Preview Order API is used to submit an order request for preview before placing it.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoHttp
    Handle hoJsonToken
    String sSandboxUrl
    String sLiveUrl
    Handle hoXml
    Variant vResp
    Handle hoResp
    String sOrderType
    String sTotalOrderValue
    String sOrderTerm
    String sPriceType
    String sLimitPrice
    Integer iStopPrice
    String sMarketSession
    String sAllOrNone
    String sSymbol
    String sSecurityType
    String sSymbolDescription
    String sOrderAction
    String sQuantityType
    Integer iQuantity
    String sCancelQuantity
    String sReserveOrder
    String sReserveQuantity
    Integer i
    Integer iCount_i
    Integer iCode
    String sDescription
    String sV_type
    String sEgQual
    String sEstimatedCommission
    String sEstimatedTotalAmount
    Integer iNetPrice
    Integer iNetBid
    Integer iNetAsk
    Integer iGcd
    Integer iPreviewId
    String sPreviewTime
    String sDstFlag
    Integer iAccountId
    Integer iOptionLevelCd
    String sMarginLevelCd
    String sAhDisclosureFlag
    String sAoDisclosureFlag
    String sConditionalDisclosureFlag
    String sEhDisclosureFlag
    String sCurrentBp
    String sCurrentNetBp
    String sCurrentOor
    String sCurrentOrderImpact
    String sNetBp
    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}/orders/preview" To sSandboxUrl
    Move "https://api.etrade.com/v1/accounts/{$accountIdKey}/orders/preview" To sLiveUrl

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

    // Send a POST with the following XML body

    // Use this online tool to generate the code from sample XML: 
    // Generate Code to Create XML

    // <?xml version="1.0" encoding="UTF-8"?>
    // <PreviewOrderRequest>
    //    <orderType>EQ</orderType>
    //    <clientOrderId>sdfer333</clientOrderId>
    //    <Order>
    //       <allOrNone>false</allOrNone>
    //       <priceType>LIMIT</priceType>
    //       <orderTerm>GOOD_FOR_DAY</orderTerm>
    //       <marketSession>REGULAR</marketSession>
    //       <stopPrice />
    //       <limitPrice>188.51</limitPrice>
    //       <Instrument>
    //          <Product>
    //             <securityType>EQ</securityType>
    //             <symbol>FB</symbol>
    //          </Product>
    //          <orderAction>BUY</orderAction>
    //          <quantityType>QUANTITY</quantityType>
    //          <quantity>10</quantity>
    //       </Instrument>
    //    </Order>
    // </PreviewOrderRequest>

    Get Create (RefClass(cComChilkatXml)) To hoXml
    If (Not(IsComObjectCreated(hoXml))) Begin
        Send CreateComObject of hoXml
    End
    Set ComTag Of hoXml To "PreviewOrderRequest"
    Send ComUpdateChildContent To hoXml "orderType" "EQ"
    Send ComUpdateChildContent To hoXml "clientOrderId" "sdfer333"
    Send ComUpdateChildContent To hoXml "Order|allOrNone" "false"
    Send ComUpdateChildContent To hoXml "Order|priceType" "LIMIT"
    Send ComUpdateChildContent To hoXml "Order|orderTerm" "GOOD_FOR_DAY"
    Send ComUpdateChildContent To hoXml "Order|marketSession" "REGULAR"
    Send ComUpdateChildContent To hoXml "Order|stopPrice" ""
    Send ComUpdateChildContent To hoXml "Order|limitPrice" "188.51"
    Send ComUpdateChildContent To hoXml "Order|Instrument|Product|securityType" "EQ"
    Send ComUpdateChildContent To hoXml "Order|Instrument|Product|symbol" "FB"
    Send ComUpdateChildContent To hoXml "Order|Instrument|orderAction" "BUY"
    Send ComUpdateChildContent To hoXml "Order|Instrument|quantityType" "QUANTITY"
    Send ComUpdateChildContent To hoXml "Order|Instrument|quantity" "10"
    Set ComEmitCompact Of hoXml To True

    Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
    If (Not(IsComObjectCreated(hoResp))) Begin
        Send CreateComObject of hoResp
    End
    Get ComGetXml Of hoXml To sTemp1
    Get pvComObject of hoResp to vResp
    Get ComHttpStr Of hoHttp "POST" sSandboxUrl sTemp1 "utf-8" "application/xml" 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"?>
    // <PreviewOrderResponse>
    //    <orderType>EQ</orderType>
    //    <totalOrderValue>1892.05</totalOrderValue>
    //    <Order>
    //       <orderTerm>GOOD_FOR_DAY</orderTerm>
    //       <priceType>LIMIT</priceType>
    //       <limitPrice>188.51</limitPrice>
    //       <stopPrice>0</stopPrice>
    //       <marketSession>REGULAR</marketSession>
    //       <allOrNone>false</allOrNone>
    //       <Instrument>
    //          <Product>
    //             <symbol>FB</symbol>
    //             <securityType>EQ</securityType>
    //          </Product>
    //          <symbolDescription>FACEBOOK INC CL A</symbolDescription>
    //          <orderAction>BUY</orderAction>
    //          <quantityType>QUANTITY</quantityType>
    //          <quantity>10</quantity>
    //          <cancelQuantity>0.0</cancelQuantity>
    //          <reserveOrder>true</reserveOrder>
    //          <reserveQuantity>0.0</reserveQuantity>
    //       </Instrument>
    //       <messages>
    //          <Message>
    //             <code>1042</code>
    //             <description>200|You have an existing open order for this security on the same side of the market. If you did not intend to place a second order for this security, please modify your order now.</description>
    //             <type>WARNING</type>
    //          </Message>
    //          <Message>
    //             <code>3093</code>
    //             <description>Position Concentrated.</description>
    //             <type>WARNING</type>
    //          </Message>
    //       </messages>
    //       <egQual>EG_QUAL_NOT_A_MARKET_ORDER</egQual>
    //       <estimatedCommission>6.95</estimatedCommission>
    //       <estimatedTotalAmount>1892.05</estimatedTotalAmount>
    //       <netPrice>0</netPrice>
    //       <netBid>0</netBid>
    //       <netAsk>0</netAsk>
    //       <gcd>0</gcd>
    //       <ratio />
    //    </Order>
    //    <PreviewIds>
    //       <previewId>1020563279</previewId>
    //    </PreviewIds>
    //    <previewTime>1529018458516</previewTime>
    //    <dstFlag>true</dstFlag>
    //    <accountId>84246841</accountId>
    //    <optionLevelCd>4</optionLevelCd>
    //    <marginLevelCd>MARGIN_TRADING_ALLOWED</marginLevelCd>
    //    <Disclosure>
    //       <ahDisclosureFlag>false</ahDisclosureFlag>
    //       <aoDisclosureFlag>false</aoDisclosureFlag>
    //       <conditionalDisclosureFlag>true</conditionalDisclosureFlag>
    //       <ehDisclosureFlag>false</ehDisclosureFlag>
    //    </Disclosure>
    //    <cashBpDetails>
    //      <settled>
    //        <currentBp>5000.00</currentBp>
    //        <currentNetBp>5000.00</currentNetBp>
    //        <currentOor>0.00</currentOor>
    //        <currentOrderImpact>64.95</currentOrderImpact>
    //        <netBp>4935.05</netBp>
    //     </settled>
    //     <settledUnsettled>
    //       <currentBp>5000.00</currentBp>
    //       <currentNetBp>5000.00</currentNetBp>
    //       <currentOor>0.00</currentOor>
    //       <currentOrderImpact>64.95</currentOrderImpact>
    //       <netBp>4935.05</netBp>
    //       </settledUnsettled>
    //    </cashBpDetails>
    // </PreviewOrderResponse>

    Get ComBodyStr Of hoResp To sTemp1
    Get ComLoadXml Of hoXml sTemp1 To iSuccess
    Get ComGetXml Of hoXml To sTemp1
    Showln sTemp1

    Get ComGetChildContent Of hoXml "orderType" To sOrderType
    Get ComGetChildContent Of hoXml "totalOrderValue" To sTotalOrderValue
    Get ComGetChildContent Of hoXml "Order|orderTerm" To sOrderTerm
    Get ComGetChildContent Of hoXml "Order|priceType" To sPriceType
    Get ComGetChildContent Of hoXml "Order|limitPrice" To sLimitPrice
    Get ComGetChildIntValue Of hoXml "Order|stopPrice" To iStopPrice
    Get ComGetChildContent Of hoXml "Order|marketSession" To sMarketSession
    Get ComGetChildContent Of hoXml "Order|allOrNone" To sAllOrNone
    Get ComGetChildContent Of hoXml "Order|Instrument|Product|symbol" To sSymbol
    Get ComGetChildContent Of hoXml "Order|Instrument|Product|securityType" To sSecurityType
    Get ComGetChildContent Of hoXml "Order|Instrument|symbolDescription" To sSymbolDescription
    Get ComGetChildContent Of hoXml "Order|Instrument|orderAction" To sOrderAction
    Get ComGetChildContent Of hoXml "Order|Instrument|quantityType" To sQuantityType
    Get ComGetChildIntValue Of hoXml "Order|Instrument|quantity" To iQuantity
    Get ComGetChildContent Of hoXml "Order|Instrument|cancelQuantity" To sCancelQuantity
    Get ComGetChildContent Of hoXml "Order|Instrument|reserveOrder" To sReserveOrder
    Get ComGetChildContent Of hoXml "Order|Instrument|reserveQuantity" To sReserveQuantity
    Move 0 To i
    Get ComNumChildrenHavingTag Of hoXml "Order|messages|Message" To iCount_i
    While (i < iCount_i)
        Set ComI Of hoXml To i
        Get ComGetChildIntValue Of hoXml "Order|messages|Message[i]|code" To iCode
        Get ComGetChildContent Of hoXml "Order|messages|Message[i]|description" To sDescription
        Get ComGetChildContent Of hoXml "Order|messages|Message[i]|type" To sV_type
        Move (i + 1) To i
    Loop

    Get ComGetChildContent Of hoXml "Order|egQual" To sEgQual
    Get ComGetChildContent Of hoXml "Order|estimatedCommission" To sEstimatedCommission
    Get ComGetChildContent Of hoXml "Order|estimatedTotalAmount" To sEstimatedTotalAmount
    Get ComGetChildIntValue Of hoXml "Order|netPrice" To iNetPrice
    Get ComGetChildIntValue Of hoXml "Order|netBid" To iNetBid
    Get ComGetChildIntValue Of hoXml "Order|netAsk" To iNetAsk
    Get ComGetChildIntValue Of hoXml "Order|gcd" To iGcd
    Get ComGetChildIntValue Of hoXml "PreviewIds|previewId" To iPreviewId
    Get ComGetChildContent Of hoXml "previewTime" To sPreviewTime
    Get ComGetChildContent Of hoXml "dstFlag" To sDstFlag
    Get ComGetChildIntValue Of hoXml "accountId" To iAccountId
    Get ComGetChildIntValue Of hoXml "optionLevelCd" To iOptionLevelCd
    Get ComGetChildContent Of hoXml "marginLevelCd" To sMarginLevelCd
    Get ComGetChildContent Of hoXml "Disclosure|ahDisclosureFlag" To sAhDisclosureFlag
    Get ComGetChildContent Of hoXml "Disclosure|aoDisclosureFlag" To sAoDisclosureFlag
    Get ComGetChildContent Of hoXml "Disclosure|conditionalDisclosureFlag" To sConditionalDisclosureFlag
    Get ComGetChildContent Of hoXml "Disclosure|ehDisclosureFlag" To sEhDisclosureFlag
    Get ComGetChildContent Of hoXml "cashBpDetails|settled|currentBp" To sCurrentBp
    Get ComGetChildContent Of hoXml "cashBpDetails|settled|currentNetBp" To sCurrentNetBp
    Get ComGetChildContent Of hoXml "cashBpDetails|settled|currentOor" To sCurrentOor
    Get ComGetChildContent Of hoXml "cashBpDetails|settled|currentOrderImpact" To sCurrentOrderImpact
    Get ComGetChildContent Of hoXml "cashBpDetails|settled|netBp" To sNetBp
    Get ComGetChildContent Of hoXml "cashBpDetails|settledUnsettled|currentBp" To sCurrentBp
    Get ComGetChildContent Of hoXml "cashBpDetails|settledUnsettled|currentNetBp" To sCurrentNetBp
    Get ComGetChildContent Of hoXml "cashBpDetails|settledUnsettled|currentOor" To sCurrentOor
    Get ComGetChildContent Of hoXml "cashBpDetails|settledUnsettled|currentOrderImpact" To sCurrentOrderImpact
    Get ComGetChildContent Of hoXml "cashBpDetails|settledUnsettled|netBp" To sNetBp

    Showln "Success."


End_Procedure