Sample code for 30+ languages & platforms
PowerBuilder

ETrade v1 Preview Order

See more HTTP Misc Examples

Gets the order details for a selected brokerage account based on the search criteria provided.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Http
oleobject loo_Json
oleobject loo_Xml
string ls_XmlContent
string ls_EndpointUrl
oleobject loo_Resp
integer li_StatusCode
string ls_TagPath
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_MarginBuyingPower

li_Success = 0

// This example 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 3-Legged Authorization examples Step1 and Step2.
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

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

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

// See the ETrade v1 API documentation HERE.

// Sample XML Request
// 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")

ls_XmlContent = loo_Xml.GetXml()
ls_EndpointUrl = "https://apisb.etrade.com/v1/accounts/{accountIdKey}/orders/preview"

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

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

// A 200 status code indicates success.
li_StatusCode = loo_Resp.StatusCode
Write-Debug "statusCode = " + string(li_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...

loo_Xml.LoadXml(loo_Resp.BodyStr)

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_MarginBuyingPower = loo_Xml.GetChildContent("marginBuyingPower")

// Sample XML Response

// <?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>
//    <marginBuyingPower>86758.05</marginBuyingPower>
// </PreviewOrderResponse>


destroy loo_Http
destroy loo_Json
destroy loo_Xml
destroy loo_Resp