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.

Merge
 Post subject: Merge Post rating: 0   New post Posted: Fri 16 Jan, 2015, 11:06 
User avatar

User rating: 0
Joined: Tue 28 May, 2013, 16:00
Posts: 37
Location: Hungary, Győr
Hi

please take a look at the merge part of the java code. Everything is at block 196. SL TP values are set at 0, but when a merge is finished, the positions are closing. If I set a value after merge, then the SL TP doesn't run. :? :?
What is the solution?

 private void block_196(Integer flow ) {
        try {////////////
           
  IOrder[] orders = new IOrder[]{ _Buy,  _Buytwo ,  _BUYtree ,};
for(IOrder o: orders){
    if(Double.compare(o.getStopLossPrice(),0) != 0){
        o.setStopLossPrice(0);
        o.waitForUpdate(2000);                         
    }
    if(Double.compare(o.getTakeProfitPrice(),0) != 0){
        o.setTakeProfitPrice(0);
        o.waitForUpdate(2000);                         
    }
}

 IOrder[] orderss = new IOrder[]{_SELL,  _SellTwo,  _Selltree ,_SellFour ,};
for(IOrder o: orderss){
  if(Double.compare(o.getStopLossPrice(),0) != 0){
        o.setStopLossPrice(0);
        o.waitForUpdate(2000);                         
    }
    if(Double.compare(o.getTakeProfitPrice(),0) != 0){
 
        o.setTakeProfitPrice(0);
        o.waitForUpdate(2000);                         
    }
}
             
     IOrder buy1 = _SELL;
     IOrder buy2 = _Buy;
     IOrder buy3 = _SellTwo;
     IOrder buy4 = _Buytwo;
     IOrder buy5 = _Selltree;
     IOrder buy6 = _BUYtree;
     IOrder buy7 = _SellFour;
     IOrder buy8 = _BuyFour;
     buy1.waitForUpdate(2000, IOrder.State.FILLED);
     buy2.waitForUpdate(2000, IOrder.State.FILLED);
     buy3.waitForUpdate(2000, IOrder.State.FILLED);
     buy4.waitForUpdate(2000, IOrder.State.FILLED);
     buy5.waitForUpdate(2000, IOrder.State.FILLED);
     buy6.waitForUpdate(2000, IOrder.State.FILLED);
     buy7.waitForUpdate(2000, IOrder.State.FILLED);
     
     IOrder buyMerge = engine.mergeOrders("mergedBuyPosition",  buy2,  buy4,  buy6 );
       buyMerge.waitForUpdate(2000, IOrder.State.FILLED);
 
     {
         
     IOrder sellMerge = engine.mergeOrders("mergedSellPosition", buy1,  buy3,  buy5,  buy7);
     sellMerge.waitForUpdate(2000, IOrder.State.OPENED);{
         sellMerge.waitForUpdate(2000);
         

   
         
         
        if (+100<( buyMerge.getProfitLossInUSD()+ sellMerge.getProfitLossInUSD())){
         PositionsViewer_block_200 (flow);
         
          System.out.println(" okok...... " + sellMerge.getProfitLossInUSD()   );
          System.out.println(" okok...... " + buyMerge.getProfitLossInUSD()   );
         }   
     }
     }
               
       
                } catch(JFException e) { ////////
            e.printStackTrace();
        }
    }

 




Thank you


 
 Post subject: Re: Merge Post rating: 0   New post Posted: Fri 16 Jan, 2015, 14:18 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
If you mean that buy2, buy4, buy6 are closed after merge, then it's how it should be, that's how merge works: you get one new IOrder object (in your case buyMerge) that is sum of three merged IOrder objects (buy2, buy4, buy6). Objects buy2, buy4, buy6 will be in Closed state after successful merge and you are supposed to work with buyMerge after that (including applying stop loss/take profit).

Here is code example that opens three positions (two with stop losses), removes stop losses, then merges three positions into one and applies stop loss to newly merged position.
public void onStart(IContext context) throws JFException {
        this.context = context;
        this.console = context.getConsole();
        this.engine = context.getEngine();
        this.history = context.getHistory();
        double price = history.getLastTick(Instrument.EURUSD).getBid();
       
        IOrder order1 = engine.submitOrder("order1", Instrument.EURUSD, OrderCommand.BUY, 0.01, 0, 20, price - 0.0010, price + 0.0010);
        order1.waitForUpdate(IOrder.State.FILLED);

        IOrder order2 = engine.submitOrder("order2", Instrument.EURUSD, OrderCommand.SELL, 0.02, 0, 20, 0, 0);
        order2.waitForUpdate(IOrder.State.FILLED);
       
        IOrder order3 = engine.submitOrder("order3", Instrument.EURUSD, OrderCommand.BUY, 0.02, 0, 20, price - 0.0010, 0);
        order3.waitForUpdate(IOrder.State.FILLED);

        IOrder[] orders = new IOrder[]{order1, order2, order3};
        for (IOrder o: orders) {
            if (Double.compare(o.getStopLossPrice(),0) != 0) {
                o.setStopLossPrice(0);
                o.waitForUpdate(2000);
            }
            if (Double.compare(o.getTakeProfitPrice(),0) != 0) {
                o.setTakeProfitPrice(0);
                o.waitForUpdate(2000);
            }
        }

        IOrder mergedOrder = engine.mergeOrders("mergedOrder", order1, order2, order3);
        mergedOrder.waitForUpdate(IOrder.State.FILLED);

        double stopLoss = price - 0.0010;
        mergedOrder.setStopLossPrice(stopLoss);
        mergedOrder.waitForUpdate(2000);
}


 
 Post subject: Re: Merge Post rating: 0   New post Posted: Fri 16 Jan, 2015, 14:52 
User avatar

User rating: 0
Joined: Tue 28 May, 2013, 16:00
Posts: 37
Location: Hungary, Győr
after the event I want to merge the positions. How can I do this?
Because I see that it creates the merge az onStart. The merge is creatd here, and if I want to, then I work with this afterwards, and if not, then with something else? If I want to merge buy1 sel1 buy2 sell2 etc positions, but not all of them are created at the time, then it will merge those after they have been created, and also, will it add them to the positon as well?


 
 Post subject: Re: Merge Post rating: 0   New post Posted: Tue 20 Jan, 2015, 15:24 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
IStrategy interface has onMessage method, that receives different messages after some events occurred. See examples here: https://www.dukascopy.com/wiki/#Manage_Order_State

You can only merge positions in state IOrder.State.FILLED. See more info about merge here - https://www.dukascopy.com/wiki/#Merge_Positions.


 

Jump to:  

  © 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