Sample code for 30+ languages & platforms
PowerBuilder

ETrade Preview Order

See more ETrade Examples

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

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Http
oleobject loo_JsonToken
string ls_SandboxUrl
string ls_LiveUrl
oleobject loo_Xml
oleobject loo_Resp
string ls_OrderType
string ls_TotalOrderValue
string ls_OrderTerm
string ls_PriceType
string ls_LimitPrice
integer li_StopPrice
string ls_MarketSession
string ls_AllOrNone
string ls_Symbol
string ls_SecurityType
string ls_SymbolDescription
string ls_OrderAction
string ls_QuantityType
integer li_Quantity
string ls_CancelQuantity
string ls_ReserveOrder
string ls_ReserveQuantity
integer i
integer li_Count_i
integer li_Code
string ls_Description
string ls_V_type
string ls_EgQual
string ls_EstimatedCommission
string ls_EstimatedTotalAmount
integer li_NetPrice
integer li_NetBid
integer li_NetAsk
integer li_Gcd
integer li_PreviewId
string ls_PreviewTime
string ls_DstFlag
integer li_AccountId
integer li_OptionLevelCd
string ls_MarginLevelCd
string ls_AhDisclosureFlag
string ls_AoDisclosureFlag
string ls_ConditionalDisclosureFlag
string ls_EhDisclosureFlag
string ls_CurrentBp
string ls_CurrentNetBp
string ls_CurrentOor
string ls_CurrentOrderImpact
string ls_NetBp

li_Success = 0

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

loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if

loo_Http.OAuth1 = 1
loo_Http.OAuthVerifier = ""
loo_Http.OAuthConsumerKey = "ETRADE_CONSUMER_KEY"
loo_Http.OAuthConsumerSecret = "ETRADE_CONSUMER_SECRET"

// Load the access token previously obtained via the OAuth1 Authorization
loo_JsonToken = create oleobject
li_rc = loo_JsonToken.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_JsonToken.LoadFile("qa_data/tokens/etrade.json")
if li_Success <> 1 then
    Write-Debug "Failed to load OAuth1 token"
    destroy loo_Http
    destroy loo_JsonToken
    return
end if

loo_Http.OAuthToken = loo_JsonToken.StringOf("oauth_token")
loo_Http.OAuthTokenSecret = loo_JsonToken.StringOf("oauth_token_secret")

ls_SandboxUrl = "https://apisb.etrade.com/v1/accounts/{$accountIdKey}/orders/preview"
ls_LiveUrl = "https://api.etrade.com/v1/accounts/{$accountIdKey}/orders/preview"

loo_Http.SetUrlVar("accountIdKey","6_Dpy0rmuQ9cu9IbTfvF2A")

// 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>

loo_Xml = create oleobject
li_rc = loo_Xml.ConnectToNewObject("Chilkat.Xml")

loo_Xml.Tag = "PreviewOrderRequest"
loo_Xml.UpdateChildContent("orderType","EQ")
loo_Xml.UpdateChildContent("clientOrderId","sdfer333")
loo_Xml.UpdateChildContent("Order|allOrNone","false")
loo_Xml.UpdateChildContent("Order|priceType","LIMIT")
loo_Xml.UpdateChildContent("Order|orderTerm","GOOD_FOR_DAY")
loo_Xml.UpdateChildContent("Order|marketSession","REGULAR")
loo_Xml.UpdateChildContent("Order|stopPrice","")
loo_Xml.UpdateChildContent("Order|limitPrice","188.51")
loo_Xml.UpdateChildContent("Order|Instrument|Product|securityType","EQ")
loo_Xml.UpdateChildContent("Order|Instrument|Product|symbol","FB")
loo_Xml.UpdateChildContent("Order|Instrument|orderAction","BUY")
loo_Xml.UpdateChildContent("Order|Instrument|quantityType","QUANTITY")
loo_Xml.UpdateChildContent("Order|Instrument|quantity","10")
loo_Xml.EmitCompact = 1

loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")

li_Success = loo_Http.HttpStr("POST",ls_SandboxUrl,loo_Xml.GetXml(),"utf-8","application/xml",loo_Resp)
if li_Success = 0 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_JsonToken
    destroy loo_Xml
    destroy loo_Resp
    return
end if

// Make sure a successful response was received.
if loo_Resp.StatusCode > 200 then
    Write-Debug loo_Resp.StatusLine
    Write-Debug loo_Resp.Header
    Write-Debug loo_Resp.BodyStr
    destroy loo_Http
    destroy loo_JsonToken
    destroy loo_Xml
    destroy loo_Resp
    return
end if

// 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>

loo_Xml.LoadXml(loo_Resp.BodyStr)
Write-Debug loo_Xml.GetXml()

ls_OrderType = loo_Xml.GetChildContent("orderType")
ls_TotalOrderValue = loo_Xml.GetChildContent("totalOrderValue")
ls_OrderTerm = loo_Xml.GetChildContent("Order|orderTerm")
ls_PriceType = loo_Xml.GetChildContent("Order|priceType")
ls_LimitPrice = loo_Xml.GetChildContent("Order|limitPrice")
li_StopPrice = loo_Xml.GetChildIntValue("Order|stopPrice")
ls_MarketSession = loo_Xml.GetChildContent("Order|marketSession")
ls_AllOrNone = loo_Xml.GetChildContent("Order|allOrNone")
ls_Symbol = loo_Xml.GetChildContent("Order|Instrument|Product|symbol")
ls_SecurityType = loo_Xml.GetChildContent("Order|Instrument|Product|securityType")
ls_SymbolDescription = loo_Xml.GetChildContent("Order|Instrument|symbolDescription")
ls_OrderAction = loo_Xml.GetChildContent("Order|Instrument|orderAction")
ls_QuantityType = loo_Xml.GetChildContent("Order|Instrument|quantityType")
li_Quantity = loo_Xml.GetChildIntValue("Order|Instrument|quantity")
ls_CancelQuantity = loo_Xml.GetChildContent("Order|Instrument|cancelQuantity")
ls_ReserveOrder = loo_Xml.GetChildContent("Order|Instrument|reserveOrder")
ls_ReserveQuantity = loo_Xml.GetChildContent("Order|Instrument|reserveQuantity")
i = 0
li_Count_i = loo_Xml.NumChildrenHavingTag("Order|messages|Message")
do while i < li_Count_i
    loo_Xml.I = i
    li_Code = loo_Xml.GetChildIntValue("Order|messages|Message[i]|code")
    ls_Description = loo_Xml.GetChildContent("Order|messages|Message[i]|description")
    ls_V_type = loo_Xml.GetChildContent("Order|messages|Message[i]|type")
    i = i + 1
loop
ls_EgQual = loo_Xml.GetChildContent("Order|egQual")
ls_EstimatedCommission = loo_Xml.GetChildContent("Order|estimatedCommission")
ls_EstimatedTotalAmount = loo_Xml.GetChildContent("Order|estimatedTotalAmount")
li_NetPrice = loo_Xml.GetChildIntValue("Order|netPrice")
li_NetBid = loo_Xml.GetChildIntValue("Order|netBid")
li_NetAsk = loo_Xml.GetChildIntValue("Order|netAsk")
li_Gcd = loo_Xml.GetChildIntValue("Order|gcd")
li_PreviewId = loo_Xml.GetChildIntValue("PreviewIds|previewId")
ls_PreviewTime = loo_Xml.GetChildContent("previewTime")
ls_DstFlag = loo_Xml.GetChildContent("dstFlag")
li_AccountId = loo_Xml.GetChildIntValue("accountId")
li_OptionLevelCd = loo_Xml.GetChildIntValue("optionLevelCd")
ls_MarginLevelCd = loo_Xml.GetChildContent("marginLevelCd")
ls_AhDisclosureFlag = loo_Xml.GetChildContent("Disclosure|ahDisclosureFlag")
ls_AoDisclosureFlag = loo_Xml.GetChildContent("Disclosure|aoDisclosureFlag")
ls_ConditionalDisclosureFlag = loo_Xml.GetChildContent("Disclosure|conditionalDisclosureFlag")
ls_EhDisclosureFlag = loo_Xml.GetChildContent("Disclosure|ehDisclosureFlag")
ls_CurrentBp = loo_Xml.GetChildContent("cashBpDetails|settled|currentBp")
ls_CurrentNetBp = loo_Xml.GetChildContent("cashBpDetails|settled|currentNetBp")
ls_CurrentOor = loo_Xml.GetChildContent("cashBpDetails|settled|currentOor")
ls_CurrentOrderImpact = loo_Xml.GetChildContent("cashBpDetails|settled|currentOrderImpact")
ls_NetBp = loo_Xml.GetChildContent("cashBpDetails|settled|netBp")
ls_CurrentBp = loo_Xml.GetChildContent("cashBpDetails|settledUnsettled|currentBp")
ls_CurrentNetBp = loo_Xml.GetChildContent("cashBpDetails|settledUnsettled|currentNetBp")
ls_CurrentOor = loo_Xml.GetChildContent("cashBpDetails|settledUnsettled|currentOor")
ls_CurrentOrderImpact = loo_Xml.GetChildContent("cashBpDetails|settledUnsettled|currentOrderImpact")
ls_NetBp = loo_Xml.GetChildContent("cashBpDetails|settledUnsettled|netBp")

Write-Debug "Success."


destroy loo_Http
destroy loo_JsonToken
destroy loo_Xml
destroy loo_Resp