Dukascopy
 
 
Wiki JStore Search Login

Attention! Read the forum rules carefully before posting a topic.

    Try to find an answer in Wiki before asking a question.
    Submit programming questions in this forum only.
    Off topics are strictly forbidden.

Any topics which do not satisfy these rules will be deleted.

Orders closing @MKT even when price is specified?
 Post subject: Orders closing @MKT even when price is specified? Post rating: 0   New post Posted: Thu 03 Oct, 2013, 19:05 

User rating: 1
Joined: Thu 22 Sep, 2011, 14:38
Posts: 7
Location: Ireland,
Hi,

I'm seeing messages I didn't expect when closing an order within an automated JForex strategy...

The code is thus:

order.close(order.getAmount(), closingOfferPrice, this.closingTradeSlippage);


As implied by the code, the closing price is specified. However, the subsequent JForex messages I get back are:

17:48:23 Order ACCEPTED: #215862548 BUY 0.01 mil. EUR/USD @ MKT MAX SLIPPAGE 0.00005 - Position #55199712
17:48:23 Closing order BUY 10000 EUR/USD @ MKT is sent at 2013-10-03 17:48:23.456 GMT by the strategy "MHTManage2": from the local computer

The amount and slippage are correct, but do these messages indicate the requested price has been ignored? I suspect it does, because I've seen trades with a result that should not have been possible at the requested price+slippage?

Thanks for any input, Mark

PS As run on my permanent demo account, up to date as of 3rd October 2013


 
 Post subject: Re: Orders closing @MKT even when price is specified? Post rating: 0   New post Posted: Fri 04 Oct, 2013, 11:40 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
See the price parameter description:
https://www.dukascopy.com/client/javadoc/com/dukascopy/api/IOrder.html#close(double,%20double,%20double)
For further requests please post a full example strategy which replicates the case. Describe what is the expected behaviour and what the strategy does instead - what you find being wrong.


 
 Post subject: Re: Orders closing @MKT even when price is specified? Post rating: 0   New post Posted: Mon 07 Oct, 2013, 13:49 

User rating: 1
Joined: Thu 22 Sep, 2011, 14:38
Posts: 7
Location: Ireland,
Thanks for the reply.

I did read the API description, and I included the relevant line of code.

To be clear about the problem: I have (correctly, I am sure) specified a required close price and a slippage.

However, this is the message back from JForex: 17:48:23 Order ACCEPTED: #215862548 BUY 0.01 mil. EUR/USD @ MKT MAX SLIPPAGE 0.00005 - Position #55199712

It is saying that I placed an order "@ MKT" with 0.5 pips slippage. This is not true. I did not ask for an order at market, I asked for an order at a specific price. The API specifies that if there is not enough liquidity at the specified price the order will be rejected, so I would expect either an order accepted at the price I specified, or a rejection - not an order at market.

I hope that is clear?

Regards, Mark


 
 Post subject: Re: Orders closing @MKT even when price is specified? Post rating: 0   New post Posted: Wed 09 Oct, 2013, 08:57 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
API Support wrote:
For further requests please post a full example strategy which replicates the case.
You still did not provide us with a strategy you use so we can't derive definite conclusions about the case that you describe. However, we created a small example strategy such that you can see how the closing by certain price does work with different slippages and price distances:
package jforex.orders;

import java.io.PrintStream;
import java.util.concurrent.TimeUnit;

import com.dukascopy.api.*;

import static com.dukascopy.api.IOrder.State.*;
import com.dukascopy.api.IEngine.OrderCommand;

/**
 * The strategy creates an order and then partially closes it
   in multiple iterations by using different slippages and close price distances
 */
public class CloseOrderByPrice implements IStrategy {

    private IConsole console;
    private IEngine engine;
    private IHistory history;
    private Instrument instrument = Instrument.EURUSD;
    IOrder order;

    @Override
    public void onStart(IContext context) throws JFException {
        console = context.getConsole();
        engine = context.getEngine();
        history = context.getHistory();

        order = engine.submitOrder("order", instrument, OrderCommand.BUY, 0.1);
        order.waitForUpdate(2000, FILLED);

        if (order.getState() != FILLED) {
            console.getOut().println("Can't close order - order not filled: " + order.getState());
            context.stop();
        }
        // conditional partial close by price and slippage
        double[] pips = {100, 10, 1};
        double[] slippages = {0, 10, 100};
       
        for(double pip : pips){
            for (double slippage: slippages){
                close(pip, slippage);
            }
        }

        context.stop();

    }
   
    @SuppressWarnings("resource")
    private void close(double pips, double slippage) throws JFException{
        double lastBid = history.getLastTick(instrument).getBid();
        double closePrice = lastBid + pips * instrument.getPipValue();
        order.close(0.003, closePrice, slippage);
        IMessage message = order.waitForUpdate(2, TimeUnit.SECONDS);       
        PrintStream ps = message.getType() == IMessage.Type.ORDER_CLOSE_OK
                ? console.getOut()
                : console.getWarn();
        ps.format("pips=%s slippage=%s last bid=%.5f close price=%.5f \n\t%s", pips, slippage, lastBid, closePrice, message).println();
    }

    @Override
    public void onMessage(IMessage message) throws JFException {}

    @Override
    public void onTick(Instrument instrument, ITick tick) throws JFException {}

    @Override
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {}

    @Override
    public void onAccount(IAccount account) throws JFException {}

    @Override
    public void onStop() throws JFException {}
}



Attachments:
CloseOrderByPrice.java [2.46 KiB]
Downloaded 246 times
DISCLAIMER: Dukascopy Bank SA's waiver of responsability - Documents, data or information available on this webpage may be posted by third parties without Dukascopy Bank SA being obliged to make any control on their content. Anyone accessing this webpage and downloading or otherwise making use of any document, data or information found on this webpage shall do it on his/her own risks without any recourse against Dukascopy Bank SA in relation thereto or for any consequences arising to him/her or any third party from the use and/or reliance on any document, data or information found on this webpage.
 
 Post subject: Re: Orders closing @MKT even when price is specified? Post rating: 0   New post Posted: Thu 10 Oct, 2013, 08:16 

User rating: 1
Joined: Thu 22 Sep, 2011, 14:38
Posts: 7
Location: Ireland,
Hi again,

Thanks for the code, which also seems to demonstrate my point precisely.

I ran your example exactly as it is, and these are the resulting messages from JForex (in reverse time order). Note that every single sell order is sent and accepted "@ MKT" - even though a specific close price was requested? The strategy debug messages are also below.

I'm sure I'm missing something obvious, but this is very confusing...

Regards, Mark

07:03:38 Strategy "CloseOrderByPrice" is stopped at 2013-10-10 07:03:38.453 GMT on the local computer with no parameters. Reason: Stopped by Engine
07:03:38 Stopping "CloseOrderByPrice" strategy at 2013-10-10 07:03:38.438 GMT on the local computer
07:03:38 Order FILLED at 1.35044 USD (#216478804 SELL 0.003 mil. EUR/USD @ MKT MAX SLIPPAGE 0.01)  - Position #55347629
07:03:38 Order ACCEPTED: #216478804 SELL 0.003 mil. EUR/USD @ MKT MAX SLIPPAGE 0.01 - Position #55347629
07:03:37 Closing order SELL 88000 EUR/USD @ MKT is sent at 2013-10-10 07:03:38.281 GMT by the strategy "CloseOrderByPrice": from the local computer
07:03:38 Order FILLED at 1.35044 USD (#216478802 SELL 0.003 mil. EUR/USD @ MKT MAX SLIPPAGE 0.001)  - Position #55347629
07:03:38 Order ACCEPTED: #216478802 SELL 0.003 mil. EUR/USD @ MKT MAX SLIPPAGE 0.001 - Position #55347629
07:03:38 Order 216478801 rejected
07:03:37 Closing order SELL 91000 EUR/USD @ MKT is sent at 2013-10-10 07:03:38.149 GMT by the strategy "CloseOrderByPrice": from the local computer
07:03:38 Order ACCEPTED: #216478801 SELL 0.003 mil. EUR/USD @ MKT MAX SLIPPAGE 0.0 - Position #55347629
07:03:37 Closing order SELL 91000 EUR/USD @ MKT is sent at 2013-10-10 07:03:37.950 GMT by the strategy "CloseOrderByPrice": from the local computer
07:03:37 Order FILLED at 1.35044 USD (#216478799 SELL 0.003 mil. EUR/USD @ MKT MAX SLIPPAGE 0.01)  - Position #55347629
07:03:37 Order ACCEPTED: #216478799 SELL 0.003 mil. EUR/USD @ MKT MAX SLIPPAGE 0.01 - Position #55347629
07:03:37 Closing order SELL 94000 EUR/USD @ MKT is sent at 2013-10-10 07:03:37.421 GMT by the strategy "CloseOrderByPrice": from the local computer
07:03:37 Order FILLED at 1.35044 USD (#216478797 SELL 0.003 mil. EUR/USD @ MKT MAX SLIPPAGE 0.001)  - Position #55347629
07:03:37 Order ACCEPTED: #216478797 SELL 0.003 mil. EUR/USD @ MKT MAX SLIPPAGE 0.001 - Position #55347629
07:03:36 Closing order SELL 97000 EUR/USD @ MKT is sent at 2013-10-10 07:03:37.270 GMT by the strategy "CloseOrderByPrice": from the local computer
07:03:37 Order 216478796 rejected
07:03:37 Order ACCEPTED: #216478796 SELL 0.003 mil. EUR/USD @ MKT MAX SLIPPAGE 0.0 - Position #55347629
07:03:36 Closing order SELL 97000 EUR/USD @ MKT is sent at 2013-10-10 07:03:37.059 GMT by the strategy "CloseOrderByPrice": from the local computer
07:03:36 Order FILLED at 1.35044 USD (#216478794 SELL 0.003 mil. EUR/USD @ MKT MAX SLIPPAGE 0.01)  - Position #55347629
07:03:36 Order ACCEPTED: #216478794 SELL 0.003 mil. EUR/USD @ MKT MAX SLIPPAGE 0.01 - Position #55347629
07:03:36 Closing order SELL 100000 EUR/USD @ MKT is sent at 2013-10-10 07:03:36.350 GMT by the strategy "CloseOrderByPrice": from the local computer
07:03:36 Order 216478793 rejected
07:03:36 Order ACCEPTED: #216478793 SELL 0.003 mil. EUR/USD @ MKT MAX SLIPPAGE 0.001 - Position #55347629
07:03:35 Closing order SELL 100000 EUR/USD @ MKT is sent at 2013-10-10 07:03:36.230 GMT by the strategy "CloseOrderByPrice": from the local computer
07:03:36 Order 216478792 rejected
07:03:36 Order ACCEPTED: #216478792 SELL 0.003 mil. EUR/USD @ MKT MAX SLIPPAGE 0.0 - Position #55347629
07:03:35 Closing order SELL 100000 EUR/USD @ MKT is sent at 2013-10-10 07:03:36.012 GMT by the strategy "CloseOrderByPrice": from the local computer
07:03:34 Order FILLED at 1.35047 USD (#216478790 BUY 0.1 mil. EUR/USD @ MKT MAX SLIPPAGE 0.0005)  - Position #55347629
07:03:34 Order ACCEPTED: #216478790 BUY 0.1 mil. EUR/USD @ MKT MAX SLIPPAGE 0.0005 - Position #55347629
07:03:33 Order BUY 100000 EUR/USD @ MKT  is sent at 2013-10-10 07:03:34.180 GMT by the strategy "CloseOrderByPrice": from the local computer
07:03:32 Strategy "CloseOrderByPrice" Strategy ID: 6C826A372C1E328E3B829B80E1B36ED5 is started at 2013-10-10 07:03:32.810 GMT on the local computer with no parameters
07:03:28 Starting "CloseOrderByPrice" strategy at 2013-10-10 07:03:28.959 GMT on the local computer


07:03:38 Message Type: ORDER_CLOSE_OK; Text: ; Related Order: [order]-FILLED / EUR/USD / 1.35047 / 0.085 / 0.085
07:03:38 pips=1.0 slippage=100.0 last bid=1.35044 close price=1.35054
07:03:37 Message Type: ORDER_CLOSE_OK; Text: ; Related Order: [order]-FILLED / EUR/USD / 1.35047 / 0.088 / 0.088
07:03:37 pips=1.0 slippage=10.0 last bid=1.35044 close price=1.35054
07:03:37 Message Type: ORDER_CLOSE_REJECTED; Text: REJECTED_COUNTERPARTY-Order 216478801 rejected; Related Order: [order]-FILLED / EUR/USD / 1.35047 / 0.091 / 0.091
07:03:37 pips=1.0 slippage=0.0 last bid=1.35044 close price=1.35054
07:03:37 Message Type: ORDER_CLOSE_OK; Text: ; Related Order: [order]-FILLED / EUR/USD / 1.35047 / 0.091 / 0.091
07:03:37 pips=10.0 slippage=100.0 last bid=1.35044 close price=1.35144
07:03:37 Message Type: ORDER_CLOSE_OK; Text: ; Related Order: [order]-FILLED / EUR/USD / 1.35047 / 0.094 / 0.094
07:03:37 pips=10.0 slippage=10.0 last bid=1.35044 close price=1.35144
07:03:36 Message Type: ORDER_CLOSE_REJECTED; Text: REJECTED_COUNTERPARTY-Order 216478796 rejected; Related Order: [order]-FILLED / EUR/USD / 1.35047 / 0.097 / 0.097
07:03:36 pips=10.0 slippage=0.0 last bid=1.35044 close price=1.35144
07:03:36 Message Type: ORDER_CLOSE_OK; Text: ; Related Order: [order]-FILLED / EUR/USD / 1.35047 / 0.097 / 0.097
07:03:36 pips=100.0 slippage=100.0 last bid=1.35044 close price=1.36044
07:03:36 Message Type: ORDER_CLOSE_REJECTED; Text: REJECTED_COUNTERPARTY-Order 216478793 rejected; Related Order: [order]-FILLED / EUR/USD / 1.35047 / 0.1 / 0.1
07:03:36 pips=100.0 slippage=10.0 last bid=1.35044 close price=1.36044
07:03:35 Message Type: ORDER_CLOSE_REJECTED; Text: REJECTED_COUNTERPARTY-Order 216478792 rejected; Related Order: [order]-FILLED / EUR/USD / 1.35047 / 0.1 / 0.1
07:03:35 pips=100.0 slippage=0.0 last bid=1.35044 close price=1.36044


 
 Post subject: Re: Orders closing @MKT even when price is specified? Post rating: 1   New post Posted: Thu 10 Oct, 2013, 10:12 
User avatar

User rating: 70
Joined: Sat 22 Sep, 2012, 17:43
Posts: 118
Location: Brazil, Fortaleza, Ceará
emmt33,

The example provided by support and tested by yourself demonstrates that the API on your machine is honouring the requested close price and slippage very well.

With respect to @MKT, the example provided by support does issue an initial market order (an order with no specified price and default slippage).
You haven't mentioned pending / limit orders so you will be working with market orders and slippages even if you specify a price.

If you provide a strategy or strategy sample demonstrating your ordering, closing and relevant output including details of what you consider to be unexpected behaviour, more can be done to help you.

Because of your emphasis on having specified a price in your close order and expecting a rejection when liquidity isn't there, we would want to see your use of 0 as a slippage parameter - not an omission resulting in the default or a miscalculated parameter.

Perhaps what you really want is for the ORDER ACCEPTED message to also specify the price it is targeting so you can see your requested price reflected.

The example provided by support effectively lets you see ORDER_CLOSE_OK and ORDER_CLOSE_REJECTED messages for several requested prices and slippages yet you claim their sample proves your point. How?


 
 Post subject: Re: Orders closing @MKT even when price is specified? Post rating: 0   New post Posted: Thu 10 Oct, 2013, 10:40 

User rating: 1
Joined: Thu 22 Sep, 2011, 14:38
Posts: 7
Location: Ireland,
Hi CriticalSection,

thanks for joining our discussion.

Quote:
Perhaps what you really want is for the ORDER ACCEPTED message to also specify the price it is targeting so you can see your requested price reflected.


*Exactly*

Having specifically requested a closing price, the messages JForex are giving me indicate that my order was accepted at market, with my specified slippage. As far as I know "at market", at the time the order is accepted, could be some distance from the price I requested - How do I know the order has been correctly processed, since the message would be exactly the same if I hadn't specified a price at all?

For example: I request a closing price with a slippage of 5 pips. The price slips by 4 pips before the order is accepted. The message tells me the order has been accepted at market with my specified slippage of 5 pips. So is that order going to slip by a potential 9 pips from my original price?

Apologies if this is obvious to you, I just want to clarify what is happening.

Regards, Mark


 
 Post subject: Re: Orders closing @MKT even when price is specified? Post rating: 0   New post Posted: Thu 10 Oct, 2013, 11:08 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
emmt33 wrote:
Having specifically requested a closing price, the messages JForex are giving me indicate that my order was accepted at market, with my specified slippage. As far as I know "at market", at the time the order is accepted, could be some distance from the price I requested - How do I know the order has been correctly processed, since the message would be exactly the same if I hadn't specified a price at all?
For changes in the platform Messages tab contents please post your request in the Trading Platforms section as it is not part of the JForex-API.


 
 Post subject: Re: Orders closing @MKT even when price is specified? Post rating: 1   New post Posted: Thu 10 Oct, 2013, 11:20 

User rating: 1
Joined: Thu 22 Sep, 2011, 14:38
Posts: 7
Location: Ireland,
Quote:
For changes in the platform Messages tab contents please post your request in the Trading Platforms section as it is not part of the JForex-API.


Thanks,

I couldn't tell if this was an implementation error (i.e. API) or an incorrect message. You've kindly confirmed that you believe the implementation to be correct, so I'll take my comments from "Automated Trading/Knowledge Base" (JForex-API?) to a more relevant section ;)

Regards, Mark


 

Jump to:  

cron
  © 1998-2025 Dukascopy® Bank SA
On-line Currency forex trading with Swiss Forex Broker - ECN Forex Brokerage,
Managed Forex Accounts, introducing forex brokers, Currency Forex Data Feed and News
Currency Forex Trading Platform provided on-line by Dukascopy.com