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.

Working few strategies on one instrument only with own orders
 Post subject: Working few strategies on one instrument only with own orders Post rating: 0   Post Posted: Thu 09 Feb, 2012, 09:16 
User avatar

User rating: 0
Joined: Thu 25 Aug, 2011, 14:04
Posts: 45
Location: Russian Federation,
Hello Support Team,

I have two strategies, which are working on one instrument. How to do that they works only with own orders (close and open orders). I try do this via ArrayList in onBar method, but it's not work right.
//......
private List<IOrder> ordersmystr1 = new ArrayList<IOrder>();

//............
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {

.....
{
                for (IOrder orderInMarket : engine.getOrders()) {
                    if (orderInMarket.isLong() && instrument.equals(selectedInstrument)) {
                        if (ordersmystr1.size() > 0){
                         for (IOrder order: ordersmystr1){
                            orderInMarket.close();
                            //order.close();
                            }   
                             ordersmystr1.clear();
                            }
                        print("Closing Long position");
                       
                    }
                }
         cmd = OrderCommand.SELL;
      }
      
      for (IOrder orderInMarket : engine.getOrders()) {
                    if (!orderInMarket.isLong() && instrument.equals(selectedInstrument)) {
                        if (ordersmystr1.size() > 0){
                         for (IOrder order: ordersmystr1){
                            orderInMarket.close();
                            //order.close();           
                            }   
                             ordersmystr1.clear();
                            }
                        print("Closing Short position");
                       
                    }
                }
            cmd = OrderCommand.BUY;
      }
       
      if(cmd != null){
         order = engine.submitOrder(getLabel(selectedInstrument), instrument, cmd, _lots);
            ordersmystr1.add(order);
      }
}

Could you provide sample with correct code?

Best regards,
vs_trade


 
 Post subject: Re: Working few strategies on one instrument only with own orders Post rating: 0   Post Posted: Thu 09 Feb, 2012, 09:40 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
You should not do the iteration:
 for (IOrder orderInMarket : engine.getOrders()) {
only iterate:
for (IOrder order: ordersmystr1){
otherwise you process every order multiple times.
Before closing the order you should check its state (see: https://www.dukascopy.com/wiki/#Order_state)
In case of any other problems, consider logging more values (e.g., messages in onMessage, logging IOrder.toString() on close operation, etc.) to see what execution branches get taken and why.


 
 Post subject: Re: Working few strategies on one instrument only with own orders Post rating: 0   Post Posted: Thu 09 Feb, 2012, 12:05 
User avatar

User rating: 0
Joined: Thu 25 Aug, 2011, 14:04
Posts: 45
Location: Russian Federation,
Hello Support Team,

After changes I have follow Exception in strategy:

where // 145 for (IOrder order: ordersmystr1) {
2012-02-09 10:53:18 Strategy tester: java.util.ConcurrentModificationException @ jforex.mystr.customInd.MyStr.onBar(MyStr.java:145)
2012-02-09 10:53:18 Closing Long position
where // 167 for (IOrder order: ordersmystr1) {
2012-02-09 10:53:17 Strategy tester: java.util.ConcurrentModificationException @ jforex.mystr.customInd.MyStr.onBar(MyStr.java:167)
2012-02-09 10:53:17 Closing Short position

After sometimes working, get next Exception:

where // 149 order.close();
11:01:20 Strategy tester: com.dukascopy.api.JFException: Cannot close order in CREATED, CLOSED or CANCELED state, current state - [CREATED] @ jforex.mystr.customInd.MyStr.onBar(MyStr.java:149)


Best regards,
vs_trade


 
 Post subject: Re: Working few strategies on one instrument only with own orders Post rating: 0   Post Posted: Thu 09 Feb, 2012, 12:13 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
vs_trade wrote:
Hello Support Team,

After changes I have follow Exception in strategy:

where // 145 for (IOrder order: ordersmystr1) {
2012-02-09 10:53:18 Strategy tester: java.util.ConcurrentModificationException @ jforex.mystr.customInd.MyStr.onBar(MyStr.java:145)
2012-02-09 10:53:18 Closing Long position
where // 167 for (IOrder order: ordersmystr1) {
2012-02-09 10:53:17 Strategy tester: java.util.ConcurrentModificationException @ jforex.mystr.customInd.MyStr.onBar(MyStr.java:167)
2012-02-09 10:53:17 Closing Short position
See:
https://docs.oracle.com/javase/1.4.2/doc ... ption.html
Just type in ConcurrentModificationException in search engine in order to find how to deal with it.
vs_trade wrote:
After sometimes working, get next Exception:

where // 149 order.close();
11:01:20 Strategy tester: com.dukascopy.api.JFException: Cannot close order in CREATED, CLOSED or CANCELED state, current state - [CREATED] @ jforex.mystr.customInd.MyStr.onBar(MyStr.java:149)
This is why we advised you in our previous post to check the order state, also see the link that we posted.


 

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