0

I am working with the ETrade Java API. I was able to use most of the functions but I am having trouble with the previewEquityOrder and the previewOptionOrder functions. Here are the error messages/ exceptions I get when I call these functions:

URL : https://etwssandbox.etrade.com/order/sandbox/rest/previewequityorder
? Java exception occurred:
com.etrade.etws.sdk.common.ETWSException
at com.etrade.etws.sdk.common.ETWSUtil.constructException(ETWSUtil.java:9)
at com.etrade.etws.sdk.core.ConnectionUtils.invoke(ConnectionUtils.java:90)
at com.etrade.etws.sdk.core.ConnectionUtils.invoke(ConnectionUtils.java:32)
at com.etrade.etws.sdk.client.OrderClient.previewEquityOrder(OrderClient.java:145)

For the previewOptionOrder:

URL : https://etwssandbox.etrade.com/order/sandbox/rest/previewoptionorder
? Java exception occurred:
com.etrade.etws.sdk.common.ETWSException
at com.etrade.etws.sdk.common.ETWSUtil.constructException(ETWSUtil.java:9)
at com.etrade.etws.sdk.core.ConnectionUtils.invoke(ConnectionUtils.java:90)
at com.etrade.etws.sdk.core.ConnectionUtils.invoke(ConnectionUtils.java:32)
at com.etrade.etws.sdk.client.OrderClient.previewOptionOrder(OrderClient.java:167)

The following Java code can reproduce the problem. You can compile this code on a Mac using the following command. On windows machine, replace the " : " with " ; " as the separator.

javac -classpath "./commons-codec-1.3.jar:./commons-httpclient-3.1.jar:./commons-httpclient-contrib-ssl-3.1.jar:./commons-lang-2.4-javadoc.jar:./commons-lang-2.4-sources.jar:./commons-lang-2.4.jar:./commons-logging-api.jar:./commons-logging.jar:./etws-accounts-sdk-1.0.jar:./etws-common-connections-1.0.jar:./etws-market-sdk-1.0.jar:./etws-oauth-sdk-1.0.jar:./etws-order-sdk-1.0.jar:./log4j-1.2.15.jar:./xstream-1.3.1.jar:" test.java

You can run the compiled class from command line using the following command:

java -classpath "./commons-codec-1.3.jar:./commons-httpclient-3.1.jar:./commons-httpclient-contrib-ssl-3.1.jar:./commons-lang-2.4-javadoc.jar:./commons-lang-2.4-sources.jar:./commons-lang-2.4.jar:./commons-logging-api.jar:./commons-logging.jar:./etws-accounts-sdk-1.0.jar:./etws-common-connections-1.0.jar:./etws-market-sdk-1.0.jar:./etws-oauth-sdk-1.0.jar:./etws-order-sdk-1.0.jar:./log4j-1.2.15.jar:./xstream-1.3.1.jar:" test <consumer_key> <consumer_secret>

You will need to pass in the ETrade consumer key and consumer secret as command line arguments to run this.

Notice that the authentication part works which is verified by getting the accounts list.

import com.etrade.etws.account.Account;
import com.etrade.etws.account.AccountListResponse;
import com.etrade.etws.oauth.sdk.client.IOAuthClient;
import com.etrade.etws.oauth.sdk.client.OAuthClientImpl;
import com.etrade.etws.oauth.sdk.common.Token;
import com.etrade.etws.sdk.client.ClientRequest;
import com.etrade.etws.sdk.client.Environment;
import com.etrade.etws.sdk.common.ETWSException;
import com.etrade.etws.sdk.client.AccountsClient;
import com.etrade.*;

import com.etrade.etws.order.PreviewEquityOrder;
import com.etrade.etws.order.PreviewEquityOrderResponse;
import com.etrade.etws.order.EquityOrderRequest;
import com.etrade.etws.order.EquityOrderTerm;
import com.etrade.etws.order.EquityOrderAction;
import com.etrade.etws.order.MarketSession;
import com.etrade.etws.order.EquityPriceType;
import com.etrade.etws.order.EquityOrderRoutingDestination;
import com.etrade.etws.sdk.client.OrderClient;
import java.math.BigInteger;

import java.awt.Desktop;
import java.net.URI;
import java.*;
import java.io.IOException;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;

public class test
{
    public static void main(String[] args) throws IOException, ETWSException
    {
        //Variables

        if(args.length<2){
            System.out.println("Class test needs two input argument as follows:");
            System.out.println("test <consumer_key> <consumer_secret>");
            return;
        }

        String oauth_consumer_key           = args[0]; // Your consumer key
        String oauth_consumer_secret        = args[1]; // Your consumer secret
        String oauth_request_token          = null; // Request token
        String oauth_request_token_secret   = null; // Request token secret
        String oauth_verify_code            = null;
        String oauth_access_token           = null;
        String oauth_access_token_secret    = null;


        ClientRequest request       = new ClientRequest();
        System.out.println("HERE");
        IOAuthClient client         = OAuthClientImpl.getInstance(); // Instantiate IOAUthClient


        // Instantiate ClientRequest
        request.setEnv(Environment.SANDBOX); // Use sandbox environment
        request.setConsumerKey(oauth_consumer_key); //Set consumer key
        request.setConsumerSecret(oauth_consumer_secret);
        Token token = client.getRequestToken(request); // Get request-token object

        oauth_request_token         = token.getToken(); // Get token string
        oauth_request_token_secret  = token.getSecret(); // Get token secret

        request.setToken(oauth_request_token);
        request.setTokenSecret(oauth_request_token_secret);

        String authorizeURL = null;
        authorizeURL = client.getAuthorizeUrl(request);
        System.out.println(authorizeURL);
        System.out.println("Copy the URL into your browser. Get the verification code and type here");
        oauth_verify_code = get_verification_code();
        //oauth_verify_code = Verification(client,request);

        request.setVerifierCode(oauth_verify_code);
        token = client.getAccessToken(request);
        oauth_access_token = token.getToken();
        oauth_access_token_secret = token.getSecret();

        request.setToken(oauth_access_token);
        request.setTokenSecret(oauth_access_token_secret);



        // Get Account List
        AccountsClient account_client = new AccountsClient(request);
        AccountListResponse response = account_client.getAccountList();
        List<Account> alist = response.getResponse();
        Iterator<Account> al = alist.iterator();
        while (al.hasNext()) {
            Account a = al.next();
            System.out.println("===================");
            System.out.println("Account: " + a.getAccountId());
            System.out.println("===================");
        }


        // Preview Equity Order
        OrderClient order_client = new OrderClient(request);
        PreviewEquityOrder orderRequest = new PreviewEquityOrder(); 
        EquityOrderRequest eor = new EquityOrderRequest(); 
        eor.setAccountId("83405188"); // sample values
        eor.setSymbol("AAPL");
        eor.setAllOrNone("FALSE"); 
        eor.setClientOrderId("asdf1234"); 
        eor.setOrderTerm(EquityOrderTerm.GOOD_FOR_DAY); 
        eor.setOrderAction(EquityOrderAction.BUY); 
        eor.setMarketSession(MarketSession.REGULAR); 
        eor.setPriceType(EquityPriceType.MARKET); 
        eor.setQuantity(new BigInteger("100")); 
        eor.setRoutingDestination(EquityOrderRoutingDestination.AUTO.value()); 
        eor.setReserveOrder("TRUE"); 
        orderRequest.setEquityOrderRequest(eor); 
        PreviewEquityOrderResponse order_response = order_client.previewEquityOrder(orderRequest);

    }
    public static String get_verification_code() {

        try{
            BufferedReader br =
            new BufferedReader(new InputStreamReader(System.in));

            String input;

            input=br.readLine();
            return input;


        }catch(IOException io){
            io.printStackTrace();
            return "";
        }
    }
}

I posted this on the ETrade community forum but that forum is not very active. I also sent a request to ETrade and haven't gotten a reply yet. If I get a solution from them, I will come back and post it here. In the mean time any help is greatly appreciated.

1 Answers1

0

After debugging my above code, I figured out that the problem is that I was setting the ReserveOrder to TRUE but I wasn't providing the required ReserveOrderQuantity. I got the above code from the Java code snippet in the ETrade Developer Platform Guide. This is clearly a bug in their documentation.