0

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

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 发送了请求,但尚未收到回复。如果我从他们那里得到解决方案,我会回来并在这里发布。同时,非常感谢任何帮助。

4

1 回答 1

0

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

于 2016-03-30T15:54:33.927 回答