Dukascopy
 
 
Wiki JStore Search Login

JFOREX-3498 How to catch the closing position on SL?
 Post subject: JFOREX-3498 How to catch the closing position on SL? Post rating: 0   New post Posted: Tue 07 Feb, 2012, 17:02 

User rating: 3
Joined: Thu 28 Jul, 2011, 19:40
Posts: 72
Location: PolandPoland
Hi,
Maybe someone knows a sensible way how to catch the closing position on Stop Loss?

One way is in onMessage method do something like that:

 IOrder order = message.getOrder();
 if(order !=null){
      if(message.getType() == IMessage.Type.ORDER_CLOSE_OK){
             if(order.getClosePrice() <= order.getStopLossPrice()+2*instrumentThis.getPipValue() &&  order.getClosePrice() >= order.getStopLossPrice()-2*instrumentThis.getPipValue()){
                   //do something...
             }

       }       
}                     


But this is not elegant and sometimes unreliable...


 
 Post subject: Re: How to catch the closing position on SL? Post rating: 0   New post Posted: Wed 08 Feb, 2012, 11:59 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
There arrives a IMessage.Type.NOTIFICATION in onMessage which looks something like:
ORDER_FILLED-Order FILLED at 1.32694 USD (#129769226 STOP LOSS SELL 0.001 mil. EUR/USD @ LIMIT 1.32693 IF BID => 1.32693)  - Position #33177317
, however you can tell if it had been stop loss only by checking the string content (since there are multiple kind of messages of type NOTIFICATION). The message itself is subject to change, so it is not advised to permanently use string operations for detecting if the order was closed by stop loss. With JForex-API future releases there will be a designated IMessage.Type for messages on close by stop loss or take profit.


 
 Post subject: JFOREX-3498 IOrder take profit / stop loss notification Post rating: 0   New post Posted: Mon 23 Apr, 2012, 10:40 

User rating: 0
Joined: Fri 02 Mar, 2012, 14:06
Posts: 3
Hi,

When I enter a trade using the code below, how do I subscribe to be notified when that trade is closed by stop loss or take profit?

public void onTick(Instrument instrument, ITick tick) throws JFException {
       
        for (IOrder order : engine.getOrders(instrument)) if (order.getState() == IOrder.State.FILLED) {
            if (order.getProfitLossInUSD() > 20) { // Stop to BE
                if (order.getStopLossPrice() != order.getOpenPrice())
                    order.setStopLossPrice(order.getOpenPrice());
            } else if (order.isLong() && order.getProfitLossInUSD() > 30) {
                if (order.getStopLossPrice() != order.getOpenPrice() + 0.0010d)
                    order.setStopLossPrice(order.getOpenPrice() + 0.0010d);
            } else if (!order.isLong() && order.getProfitLossInUSD() > 30) {
                if (order.getStopLossPrice() != order.getOpenPrice() - 0.0010d)
                    order.setStopLossPrice(order.getOpenPrice() - 0.0010d);
            }
            if (order.getStopLossPrice() == 0) { // not set, but has been FILLED - need to set stop loss
                out.println("setting SL/TP");
                if (order.isLong()) {
                    order.setStopLossPrice(order.getOpenPrice() - 0.0030d);
                    order.setTakeProfitPrice(order.getOpenPrice() + 0.0050d);
                } else {
                    order.setStopLossPrice(order.getOpenPrice() + 0.0030d);
                    order.setTakeProfitPrice(order.getOpenPrice() - 0.0050d);
                }
            }
        }
...

        if (long30M == null || long30M.getState() == IOrder.State.CLOSED || long30M.getState() == IOrder.State.CANCELED) {
            if (indRSI30m > 56 &&
                indCCI30m >= 125 &&
                indST30m[0] < 75 &&
                bbWidth30m > 50 &&
                pDiff(askPrice, indEMA30m) >= 5d &&
                pDiff(askPrice, indSMA30m) >= 5d &&
                pDiff(askPrice, indBB30m[0]) >= 10d) {
                long30M = engine.submitOrder(getLabel(instrument), instrument, IEngine.OrderCommand.BUY, cTradeSize);
                out.println("long30m");
            }
        }
...


 
 Post subject: Re: JFOREX-3498 How to catch the closing position on SL? Post rating: 0   New post Posted: Tue 29 May, 2012, 02:51 

User rating: 0
Joined: Sun 27 May, 2012, 11:48
Posts: 13
Hello APIsupport,

when will there be support for having the functionality of checking if an order has triggerd its Take profit and stop loss?

thanks.


 
 Post subject: Re: JFOREX-3498 How to catch the closing position on SL? Post rating: 0   New post Posted: Wed 30 May, 2012, 13:15 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
API 2.6.70

IMessage.getReasons() returns a sub-set of predefined reasons, specific to successful types of message (ORDER_CHANGED_OK, ORDER_CLOSE_OK, ORDER_FILL_OK):

ORDER_CLOSED_BY_SL, // Order closed by Stop Loss trigger.
ORDER_CLOSED_BY_TP, // Order closed by Take Profit trigger.
ORDER_CLOSED_BY_MERGE, // Order get merged
ORDER_CHANGED_SL // Stop Loss trigger was changed.
ORDER_CHANGED_TP, // Take Profit trigger was changed.
ORDER_CHANGED_AMOUNT // Order amount was changed.
ORDER_CHANGED_PRICE, // Order price was changed.
ORDER_CHANGED_GTT // Execution timeout (Good Till Time) was changed.
ORDER_CHANGED_TYPE // Order Command was changed.
ORDER_FULLY_FILLED // Order is Fully Filled.

Wiki diagrams updated:
https://www.dukascopy.com/wiki/#Order_state


 

Jump to:  

  © 1998-2024 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