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.

After order merge, history.getOrders() returns empty
 Post subject: After order merge, history.getOrders() returns empty Post rating: 0   New post Posted: Mon 20 Jun, 2011, 07:22 

User rating: -
Hi,

After merging two orders, history.getOrders() returns empty list. Here is the complete sequence of steps I followed.

1. Place order1 with stop loss SL.
2. After a few ticks, place order2 (with the same order command and same amount) with the same SL. history.getOrders.size() returns 2. Both are filled.
3. Now change the SL for both orders to 0.
4. Merge the orders. I receive ORDER_CLOSE_OK for both original orders.
5. Now checking the history.getOrders() returns empty list i.e. history.getOrders().size() return 0.

Is it a bug or I'm doing something wrong.

Thanks,
GBPTrader.


 
 Post subject: Re: After order merge, history.getOrders() returns empty Post rating: 0   New post Posted: Mon 20 Jun, 2011, 12:44 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
The IEngine.getOrders() method does not return closed orders, see:
https://www.dukascopy.com/client/javadoc//com/dukascopy/api/IEngine.html#getOrders()


 
 Post subject: Re: After order merge, history.getOrders() returns empty Post rating: 0   New post Posted: Mon 20 Jun, 2011, 16:15 

User rating: -
Shouldn't it report the new 'merged' order which is in the 'FILLED' state?


 
 Post subject: Re: After order merge, history.getOrders() returns empty Post rating: 0   New post Posted: Wed 22 Jun, 2011, 09:55 

User rating: -
Hi Support,

I'm eagerly looking forward to your answer on this one.

After the two orders are merged, they get closed and a new merged order is created in 'FILLED' state. Shouldn't history.getOrders() return the newly merged order?

Thanks,
GBPTrader


 
 Post subject: Re: After order merge, history.getOrders() returns empty Post rating: 0   New post Posted: Wed 22 Jun, 2011, 10:16 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
It is not clear if you try to merge filled or already closed orders and what are the amounts and directions of the orders. Could you please provide a code example for the case that you have described?


 
 Post subject: Re: After order merge, history.getOrders() returns empty Post rating: 0   New post Posted: Wed 22 Jun, 2011, 11:09 

User rating: -
Sorry for mentioning history.getOrders(). I meant engine.getOrders() instead.

I'm trying to merge FILLED orders. Please see code example below. I have not tested it since I already have a test strategy running in my DEMO account and I didn't want to affect it by running this example. But it compiles and explains what I'm trying to do.

package jforex;

import java.util.*;

import com.dukascopy.api.*;

public class Example_Merge implements IStrategy {
   private IEngine engine;
   private IConsole console;
   private IHistory history;
   private IContext context;
   private IIndicators indicators;
   private IUserInterface userInterface;
   
   public void onStart(IContext context) throws JFException {
      this.engine = context.getEngine();
      this.console = context.getConsole();
      this.history = context.getHistory();
      this.context = context;
      this.indicators = context.getIndicators();
      this.userInterface = context.getUserInterface();
   }

   public void onAccount(IAccount account) throws JFException {
   }

   public void onMessage(IMessage message) throws JFException {
            switch (message.getType()) {
                case ORDER_FILL_OK:
                    List<IOrder> fills = engine.getOrders();
                    if (fills.size() > 1) {
                        //initiate the merge process
                        engine.mergeOrders("MERGE" + engine.getOrders().size(), fills.toArray(new IOrder[fills.size()]));
                    }
                    break;
                case ORDERS_MERGE_OK:
                    console.getOut().println("NUMBER OF FILLED ORDERS AFTER MERGE = " + engine.getOrders().size());
                    break;
            }
   }

   public void onStop() throws JFException {
   }

   public void onTick(Instrument instrument, ITick tick) throws JFException {
            if (engine.getOrders().size() < 2) {
         engine.submitOrder("LABEL" + engine.getOrders().size(), Instrument.GBPUSD, IEngine.OrderCommand.BUY, 0.01
                                 , 0.0, 0.0, 0.0 , 0.0);
            }
   }
   
        public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
        }
}


 
 Post subject: Re: After order merge, history.getOrders() returns empty Post rating: 0   New post Posted: Wed 22 Jun, 2011, 12:59 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
The execution of an example you provided failed with an exception:
com.dukascopy.api.JFException: Cannot merge orders in state other than FILLED @ jforex.Example_Merge.onMessage(Example_Merge.java:33)
As well note that
engine.submitOrder("LABEL" + engine.getOrders().size(), Instrument.GBPUSD, IEngine.OrderCommand.BUY, 0.01, 0.0, 0.0, 0.0 , 0.0);
equals
engine.submitOrder("LABEL" + engine.getOrders().size(), Instrument.GBPUSD, IEngine.OrderCommand.BUY, 0.01);
Please check if the following startegy clarifies the merging principle (it uses the same type of orders as your strategy does):
package jforex.strategies.oneorder;

import java.text.SimpleDateFormat;
import java.util.TimeZone;

import com.dukascopy.api.*;
import com.dukascopy.api.IEngine.OrderCommand;

public class OrderMerge2 implements IStrategy {

   private IConsole console;
   private IEngine engine;
   
   private Instrument instrument = Instrument.EURUSD;
   private Period period = Period.TEN_SECS;
   
   private IOrder order1, order2, mergedOrder;
   private SimpleDateFormat sdf;
   
   @Override
   public void onStart(IContext context) throws JFException {
      engine = context.getEngine();
      console = context.getConsole();
      console.getOut().println("Start");
      
      order1 = engine.submitOrder("order1", instrument, OrderCommand.BUY, 0.01);
      order2 = engine.submitOrder("order2", instrument, OrderCommand.BUY, 0.01);
      
      sdf = new SimpleDateFormat("HH:mm:ss"){{setTimeZone(TimeZone.getTimeZone("GMT"));}};
   }

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

   @Override
   public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
      if(instrument != this.instrument || period != this.period)
         return;
      
      if(order1.getState() == IOrder.State.FILLED && order2.getState() == IOrder.State.FILLED){
         mergedOrder = engine.mergeOrders("mergedOrder"+1, order1, order2);
      }
      
      print(sdf.format(bidBar.getTime()) + " Order count: " + engine.getOrders().size());
   }

   @Override
   public void onMessage(IMessage message) throws JFException {
      //if the message is related to an order then print its info
      if( message.getOrder() != null)
         print(sdf.format(message.getCreationTime()) + " " + message.getOrder().getLabel() + ": "  + message.getType()
               + " "  + message.getContent() + "; order count:" + engine.getOrders().size());
      
   }

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

   @Override
   public void onStop() throws JFException {
      //close all orders
      for(IOrder o : engine.getOrders())
         o.close();

   }
   
   private void print(Object o){
      console.getOut().println(o);
   }

}


 
 Post subject: Re: After order merge, history.getOrders() returns empty Post rating: 0   New post Posted: Wed 22 Jun, 2011, 19:30 

User rating: -
Hi,

Sorry about the exception. I had not tested the code but I think you have got it right in your example what I was tying to convey.

I created another DEMO account and tested the sample code that you provided and it seems to work as expected.

I'll have another look at my program. Probably it's a bug on my part. If not, I'll get back to you with my investigations.

Thanks for your help. I appreciate it.

Sincerely,
GBPTrader


 
 Post subject: Re: After order merge, history.getOrders() returns empty Post rating: 0   New post Posted: Thu 23 Jun, 2011, 10:04 

User rating: -
Hi Support,

I think I have found the problem. Took me a while to figure it out. Please test and let me know if it's a bug and if there's a workaround for it.

Please run the example below. I've slightly modified the example provided by you by adding a stop loss price and then setting it to 0 before doing the merge. The interesting part is that it happens only if I try to do the merge in onMessage checking for ORDER_CHANGED_OK message and using the historical tester. I do not find it to be a problem if actually running it in DEMO environment. So, please make sure to run it in the 'Historical Tester' mode.

package jforex;
 
import java.text.SimpleDateFormat;
import java.util.TimeZone;
 
import com.dukascopy.api.*;
import com.dukascopy.api.IEngine.OrderCommand;
 
public class Example_Merge implements IStrategy {
 
   private IConsole console;
   private IEngine engine;
   
   private Instrument instrument = Instrument.EURUSD;
   private Period period = Period.TEN_SECS;
   
   private IOrder order1, order2, mergedOrder;
   private SimpleDateFormat sdf;
   
   @Override
   public void onStart(IContext context) throws JFException {
      engine = context.getEngine();
      console = context.getConsole();
      console.getOut().println("Start");
       
      order1 = engine.submitOrder("order1", instrument, OrderCommand.BUY, 0.01, 0.0, 0.0, 1.4200, 0.0);
      order2 = engine.submitOrder("order2", instrument, OrderCommand.BUY, 0.01, 0.0, 0.0, 1.4200, 0.0);
       
      sdf = new SimpleDateFormat("HH:mm:ss"){{setTimeZone(TimeZone.getTimeZone("GMT"));}};
   }
 
   @Override
   public void onTick(Instrument instrument, ITick tick) throws JFException {}
 
   @Override
   public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
      if(instrument != this.instrument || period != this.period)
         return;
      if (order1.getState() == IOrder.State.FILLED) {
         order1.setStopLossPrice(0);
      }
      if (order2.getState() == IOrder.State.FILLED) {
         order2.setStopLossPrice(0);
      }
       
      print(sdf.format(bidBar.getTime()) + " Order count: " + engine.getOrders().size());
   }
 
   @Override
   public void onMessage(IMessage message) throws JFException {
      if (message.getType() == IMessage.Type.ORDER_CHANGED_OK) {
         if (order1.getState() == IOrder.State.FILLED && order2.getState() == IOrder.State.FILLED
             && order1.getStopLossPrice() == 0.0 && order2.getStopLossPrice() == 0.0){
            mergedOrder = engine.mergeOrders("mergedOrder"+1, order1, order2);
         }
      }
      //if the message is related to an order then print its info
      if( message.getOrder() != null)
         print(sdf.format(message.getCreationTime()) + " " + message.getOrder().getLabel() + ": "  + message.getType()
               + " "  + message.getContent() + "; order count:" + engine.getOrders().size());
       
   }
 
   @Override
   public void onAccount(IAccount account) throws JFException {}
 
   @Override
   public void onStop() throws JFException {
      //close all orders
      for(IOrder o : engine.getOrders())
         o.close();
 
   }
   
   private void print(Object o){
      console.getOut().println(o);
   }
 
}


Thanks,
GBPTrader.


 
 Post subject: Re: After order merge, history.getOrders() returns empty Post rating: 0   New post Posted: Thu 23 Jun, 2011, 15:27 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
GBPTrader wrote:
Please test and let me know if it's a bug and if there's a workaround for it.
Consider merging and submitting orders in other methods than onMessage (e.g. onTick, onBar).


 

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