문제

I've written a simple method to send FIX orders.

I have a little problem, the first ClOrdID stored is 1, the next 11, and then 21 and so on so forth. I was looking to have the first element 0 or 1, and then 2,3 etc...

What's wrong with my code?

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

My xml file: The xml file

도움이 되었습니까?

해결책

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

Te above code will get the size of the number of elements (e.g. 1), and then convert it to a string, now it is "1", and then it will do "1" + 1, which is string concatenation, so you will get "11".

What you want to do is move the +1 to be inside the toString() method, and make it addition instead of String-concatenation:

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

Sorted!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top