1

我写了一个简单的方法来发送 FIX 订单。

我有一个小问题,存储的第一个 ClOrdID 是 1,接下来是 11,然后是 21,依此类推。我希望有第一个元素 0 或 1,然后是 2,3 等...

我的代码有什么问题?

private static void sendSELLOrderSingle(String account,
        String symbol, String securityID, String securityExchange,
        double price, double orderQty) throws SessionNotFound,
        JDOMException, IOException {

    quickfix.fix42.NewOrderSingle order = new quickfix.fix42.NewOrderSingle();

    SessionID sessionId = (SessionID) initiator.getSessions().get(0);



    order.set(new TransactTime(new Date(0)));
    order.set(new HandlInst('1'));

    order.set(new Account(account));
    order.set(new Symbol(symbol));
    order.set(new SecurityID(securityID));
    order.set(new SecurityExchange(securityExchange));
    order.set(new Price(price));
    order.set(new OrdType(OrdType.LIMIT));
    order.set(new Side(Side.SELL));
    order.set(new OrderQty(orderQty));



    // POST TRADE ACTIVITY

    org.jdom2.Document doc = new org.jdom2.Document();

    FileInputStream fis = new FileInputStream("XML/historical orders.xml");
    // create a sax builder to parse the document
    SAXBuilder sb = new SAXBuilder();
    // parse the xml content provided by the file input stream and create a
    // Document object
    doc = sb.build(fis);
    // get the root element of the document

    org.jdom2.Element root = doc.getRootElement();
    fis.close();

    org.jdom2.Element clOrdID1 = new org.jdom2.Element("ClOrdID");
    clOrdID1.setText((Integer.toString(root.getChildren().size())) + 1);
    root.addContent(clOrdID1);

    order.set(new ClOrdID((Integer.toString(root.getChildren().size())) + 1));

    org.jdom2.Element account1 = new org.jdom2.Element("Account");
    account1.setText(account);
    clOrdID1.addContent(account1);

    org.jdom2.Element symbol1 = new org.jdom2.Element("Symbol");
    symbol1.setText(symbol);
    clOrdID1.addContent(symbol1);

    org.jdom2.Element securityID1 = new org.jdom2.Element("SecurityID");
    securityID1.setText(securityID);
    clOrdID1.addContent(securityID1);

    org.jdom2.Element securityExchange1 = new org.jdom2.Element(
            "SecurityExchange");
    securityExchange1.setText(securityExchange);
    clOrdID1.addContent(securityExchange1);

    org.jdom2.Element price1 = new org.jdom2.Element("Price");
    price1.setText(Double.toString(price));
    clOrdID1.addContent(price1);

    org.jdom2.Element ordType = new org.jdom2.Element("Order_Type");
    ordType.setText("Limit");
    clOrdID1.addContent(ordType);

    org.jdom2.Element side = new org.jdom2.Element("Side");
    side.setText("SELL");
    clOrdID1.addContent(side);

    org.jdom2.Element ordQnty = new org.jdom2.Element("Order_Quantity");
    ordQnty.setText(Double.toString(orderQty));
    clOrdID1.addContent(ordQnty);

    try {
        // get object to see output of prepared document
        XMLOutputter xmlOutput = new XMLOutputter();

        // passsed System.out to see document content on console
        xmlOutput.output(doc, System.out);

        // passed fileWriter to write content in specified file

        xmlOutput.output(doc, new FileWriter("XML/historical orders.xml"));

    } catch (IOException io) {
        System.out.println(io.getMessage());
    }


    Session.sendToTarget(order, sessionId);
}

我的 xml 文件: xml 文件

4

1 回答 1

1
clOrdID1.setText((Integer.toString(root.getChildren().size())) + 1);

上面的代码会得到元素个数的大小(比如1),然后转成字符串,现在是"1",然后就可以了"1" + 1,也就是字符串拼接,这样就得到了"11"

您想要做的是将 +1 移动到 toString() 方法内,并使其添加而不是字符串连接:

clOrdID1.setText((Integer.toString(1 + root.getChildren().size())));

排序!

于 2014-03-29T16:02:41.880 回答