ETrade Java API问题 - previewEquityOrder和previewOptionOrder抛出ETWSException

时间:2016-03-23 23:21:36

标签: java etrade-api

我正在使用ETrade Java API。我能够使用大部分功能,但我遇到了 previewEquityOrder previewOptionOrder 功能的问题。以下是我调用这些函数时得到的错误消息/异常:

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)

对于 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)

以下Java代码可以重现该问题。您可以使用以下命令在Mac上编译此代码。在Windows机器上,将“:”替换为“;”作为分隔符。

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

您可以使用以下命令从命令行运行已编译的类:

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>

您需要传递ETrade使用者密钥和使用者密钥作为命令行参数来运行它。

请注意,身份验证部分可以通过获取帐户列表进行验证。

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 "";
        }
    }
}

我在ETrade社区论坛上发布了这个,但该论坛不是非常活跃。我还向ETrade发出了请求,但尚未得到答复。如果我从他们那里得到解决方案,我会回来把它发布在这里。同时,我们非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

在调试上面的代码之后,我发现问题是我将ReserveOrder设置为TRUE但是我没有提供所需的ReserveOrderQuantity。我从ETrade开发人员平台指南的Java代码片段中获得了上述代码。这显然是他们文档中的错误。