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.

Need help with pending orders
 Post subject: Need help with pending orders Post rating: 0   New post Posted: Wed 02 Feb, 2011, 16:12 
User avatar

User rating: 0
Joined: Tue 07 Dec, 2010, 10:51
Posts: 39
Hello everyone,

I'm having difficulty to send many pending orders at once, only one or two of the six pending orders are executed during back testing. Here is the function that I use:
    public void SendPendingOrder(Instrument instrument, IEngine engine, double TakeProfit, double StopLoss, double volumeParam,double price,IEngine.OrderCommand OrderType)  throws JFException
    {
       
       IOrder pendingOrder = engine.submitOrder(getLabel(instrument),instrument,OrderType,volumeParam,price,50,StopLoss,TakeProfit);
       pendingOrder.waitForUpdate(20000);
   
    }


Any additional code that I should add?

The problem must be the function because if I remove some of the orders that should be executed then the remaining are executed, so the calling of the function is not the problem.

Any help will be appreciated.

Thanks


 
 Post subject: Re: Need help with pending orders Post rating: 0   New post Posted: Wed 02 Feb, 2011, 16:29 

User rating: 3
Joined: Tue 17 May, 2011, 16:51
Posts: 38
Location: Sweden, Jonkoping
I think I've read here somewhere that the tester doesn't respect milliseconds in waitForUpdate (support will no doubt correct me if I'm wrong). A sample script showed it like below so that's what I've been doing. You could try it and see if anything improves.

import java.util.concurrent.TimeUnit;
---
pendingOrder.waitForUpdate(20, TimeUnit.SECONDS)


 
 Post subject: Re: Need help with pending orders Post rating: 0   New post Posted: Wed 02 Feb, 2011, 17:28 
User avatar

User rating: 0
Joined: Tue 07 Dec, 2010, 10:51
Posts: 39
Cashbone wrote:
I think I've read here somewhere that the tester doesn't respect milliseconds in waitForUpdate (support will no doubt correct me if I'm wrong). A sample script showed it like below so that's what I've been doing. You could try it and see if anything improves.

import java.util.concurrent.TimeUnit;
---
pendingOrder.waitForUpdate(20, TimeUnit.SECONDS)


Mmm... Interesting, thanks will see if this fix will resolve the problem.


 
 Post subject: Re: Need help with pending orders Post rating: 0   New post Posted: Wed 02 Feb, 2011, 17:33 
User avatar

User rating: 0
Joined: Tue 07 Dec, 2010, 10:51
Posts: 39
Cashbone the problem still persists, don't have a clue how to fix this. If I play around with the wait for update seconds, the orders that are executed varies between one and two of the six.


 
 Post subject: Re: Need help with pending orders Post rating: 0   New post Posted: Wed 02 Feb, 2011, 21:03 

User rating: 3
Joined: Tue 17 May, 2011, 16:51
Posts: 38
Location: Sweden, Jonkoping
Actually what I told you before was just half the story since it's been a while since I used this. :roll:

If this doesn't fix it I'm afraid I'm out of ideas (and know-how). Code waits until order-state moves from created to opened.

IOrder pendingOrder = engine.submitOrder(getLabel(instrument),instrument,OrderType,volumeParam,price,50,StopLoss,TakeProfit); 

while (order.getState() == IOrder.State.CREATED){
            pendingOrder.waitForUpdate(1, TimeUnit.SECONDS);
        }


 
 Post subject: Re: Need help with pending orders Post rating: 0   New post Posted: Wed 02 Feb, 2011, 22:28 
User avatar

User rating: 0
Joined: Tue 07 Dec, 2010, 10:51
Posts: 39
Cashbone wrote:
Actually what I told you before was just half the story since it's been a while since I used this. :roll:

If this doesn't fix it I'm afraid I'm out of ideas (and know-how). Code waits until order-state moves from created to opened.

IOrder pendingOrder = engine.submitOrder(getLabel(instrument),instrument,OrderType,volumeParam,price,50,StopLoss,TakeProfit); 

while (order.getState() == IOrder.State.CREATED){
            pendingOrder.waitForUpdate(1, TimeUnit.SECONDS);
        }




Still not working, but I really appreciate your help! Maybe I did something wrong in my code, here is the robot up to now, obviously not finished but up to this stage it should be able to stand on its own feet.

I'm backtesting how effective the weekly pivots are. At the start of a new week, 6 pending orders are placed at the 6 pivot levels, and the previous orders are deleted. As easy as that:
package PivotTrader;

import java.text.DecimalFormat;

import java.util.*;

import com.dukascopy.api.*;
import java.util.concurrent.TimeUnit;
public class PivotTrader implements IStrategy {
   private IEngine engine;
   private IConsole console;
   private IHistory history;
   private IContext context;
   private IIndicators indicators;
   private IUserInterface userInterface;
    private IAccount account;
   
   
    @Configurable("SLPips") public double SLPips=15;
    @Configurable("TPPips") public double TPPips=30;
   
   
    double R1TP;
    double R1SL;
   
    double R2TP;
    double R2SL;
   
    double R3TP;
    double R3SL;
   
   
    double S1TP;
    double S1SL;
   
    double S2TP;
    double S2SL;
   
    double S3TP;
    double S3SL;
   
   
    double WeekPivot;

    double WR1;
    double WS1;

    double WR2;
    double WS2;

    double WS3;
    double WR3;   

   

    //+------------------------------------------------------------------+
    //| onStart                                                          |
    //+------------------------------------------------------------------+
   
   public void onStart(IContext context) throws JFException {
        this.context = context; 
        engine = context.getEngine();
        indicators = context.getIndicators();
        history = context.getHistory();
        console = context.getConsole();
        indicators = context.getIndicators();
   }

   
    //+------------------------------------------------------------------+
    //| onAccount                                                        |
    //+------------------------------------------------------------------+
   public void onAccount(IAccount account) throws JFException
   {
      
      this.account = account;
   }

    //+------------------------------------------------------------------+
    //| onMessage                                                  |
    //+------------------------------------------------------------------+
   public void onMessage(IMessage message) throws JFException {
   }
   
    //+------------------------------------------------------------------+
    //| onStop                                                           |
    //+------------------------------------------------------------------+
   public void onStop() throws JFException {
   }
   
    //+------------------------------------------------------------------+
    //| onTick                                                           |
    //+------------------------------------------------------------------+
   public void onTick(Instrument instrument, ITick tick) throws JFException
   {
      

   }
   
   
    //+------------------------------------------------------------------+
    //| onBar                                                            |
    //+------------------------------------------------------------------+
   
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException
    {
       
       if (period == Period.WEEKLY)
       {
          double PipVal = instrument.getPipValue();
          double lot = 0.5;
          
          closeAll();
          CalcWeeklyPivots(instrument);
          
          R1TP = WR1 -TPPips*PipVal;
          R1SL = WR1 + SLPips*PipVal;
          
          R2TP = WR2 -TPPips*PipVal;
          R2SL = WR2 + SLPips*PipVal;
          
          R3TP = WR3 -TPPips*PipVal;
          R3SL = WR3 + SLPips*PipVal;
          
          
          S1TP = WS1 + TPPips*PipVal;
          S1SL = WS1 - SLPips*PipVal;
          
          S2TP = WS2 + TPPips*PipVal;
          S2SL = WS2 - SLPips*PipVal;
          
          S3TP = WS3 + TPPips*PipVal;
          S3SL = WS3 - SLPips*PipVal;
          
          SendPendingOrder(instrument, engine, R1TP , R1SL, lot,WR1,IEngine.OrderCommand.SELLLIMIT);
          SendPendingOrder(instrument, engine, R2TP , R2SL, lot,WR2,IEngine.OrderCommand.SELLLIMIT);
          SendPendingOrder(instrument, engine, R3TP , R3SL, lot,WR3,IEngine.OrderCommand.SELLLIMIT);

          
          SendPendingOrder(instrument, engine, S1TP , S1SL, lot,WS1,IEngine.OrderCommand.BUYLIMIT);
          SendPendingOrder(instrument, engine, S2TP , S2SL, lot,WS2,IEngine.OrderCommand.BUYLIMIT);
          SendPendingOrder(instrument, engine, S3TP , S3SL, lot,WS3,IEngine.OrderCommand.BUYLIMIT);


       }


    }
   
   
    //+------------------------------------------------------------------+
    //| Calculate Weekly Pivots                                  |
    //+------------------------------------------------------------------+
   
    public void CalcWeeklyPivots(Instrument instrument) throws JFException
    {
       
       IBar bar = history.getBar(instrument, Period.WEEKLY,OfferSide.ASK, 1);
       
        double WeekHigh  = bar.getHigh();
        double WeekLow   = bar.getLow();
        double WeekClose = bar.getClose();

        WeekPivot = ((WeekHigh + WeekLow + WeekClose)/3);

        WR1 = roundFiveDecimals((2*WeekPivot)-WeekLow);
        WS1 = roundFiveDecimals((2*WeekPivot)-WeekHigh);

        WR2 = roundFiveDecimals(WeekPivot+(WR1-WS1));
        WS2 = roundFiveDecimals(WeekPivot-(WR1-WS1));

        WS3 = roundFiveDecimals((WeekLow - (2*(WeekHigh-WeekPivot))));
        WR3 = roundFiveDecimals((WeekHigh + (2*(WeekPivot-WeekLow))));   
             
        return;

    }
   
   

    //+------------------------------------------------------------------+
    //| Sell Function                                                    |
    //+------------------------------------------------------------------+
   
    public void SendPendingOrder(Instrument instrument, IEngine engine, double TakeProfit, double StopLoss, double volumeParam,double price,IEngine.OrderCommand OrderType)  throws JFException
    {
       
       IOrder pendingOrder = engine.submitOrder(getLabel(instrument),instrument,OrderType,volumeParam,price,50,StopLoss,TakeProfit);
       
       while (pendingOrder.getState() == IOrder.State.CREATED){
            pendingOrder.waitForUpdate(1, TimeUnit.SECONDS);
        }
   
    }   
   

    //+------------------------------------------------------------------+
    //| Count Open Positions Function                                    |
    //+------------------------------------------------------------------+
    protected int positionsTotal(Instrument instrument) throws JFException
    {
        int counter = 0;
        for (IOrder order : engine.getOrders(instrument)) {
            if (order.getState() == IOrder.State.FILLED) {
                counter++;
            }
        }
        return counter;
    }
   
   
    //+------------------------------------------------------------------+
    //| Close All Filled Orders                                          |
    //+------------------------------------------------------------------+
    public void closeAll() throws JFException
    {
        for (IOrder order : engine.getOrders()) {

                order.close();
                order.waitForUpdate(100);

        }
        return;
    }
   
    //+------------------------------------------------------------------+
    //| Getlabel Function                                                |
    //+------------------------------------------------------------------+
    protected String getLabel(Instrument instrument)
    {
        String label = instrument.name();
       
        long time = new java.util.Date().getTime();

        label = label.substring(0, 2) + label.substring(3, 5);
        label = label + time;
        label = label.toLowerCase();
        return label;
    }
   
   
    //+------------------------------------------------------------------+
    //| roundFiveDecimals Function                                               |
    //+------------------------------------------------------------------+
    double roundFiveDecimals(double d)
    {
       DecimalFormat twoDForm = new DecimalFormat("#.#####");
       return Double.valueOf(twoDForm.format(d));
    }
   
   
}



 
 Post subject: Re: Need help with pending orders Post rating: 0   New post Posted: Wed 02 Feb, 2011, 23:59 

User rating: 3
Joined: Tue 17 May, 2011, 16:51
Posts: 38
Location: Sweden, Jonkoping
Good news...

com.dukascopy.api.JFException: Cannot create order with label that already exists @ PivotTrader.PivotTrader.SendPendingOrder(PivotTrader.java:189)

The problem is with the label generator. Just to check I added a random number to the label and it seems to work then. Guess it must be trying to submit a second order the same millisecond which makes that label a duplicate.

import java.util.Random;

---

    //+------------------------------------------------------------------+
    //| Getlabel Function                                                |
    //+------------------------------------------------------------------+
    protected String getLabel(Instrument instrument)
    {
        String label = instrument.name();
       
        long time = new java.util.Date().getTime();
       
        Random generator = new Random();
        int r = generator.nextInt(99999);

        label = label.substring(0, 2) + label.substring(3, 5);
        label = label + time + r;
        label = label.toLowerCase();
        return label;
    }


 
 Post subject: Re: Need help with pending orders Post rating: 0   New post Posted: Thu 03 Feb, 2011, 07:09 
User avatar

User rating: 0
Joined: Tue 07 Dec, 2010, 10:51
Posts: 39
Cashbone wrote:
Good news...

com.dukascopy.api.JFException: Cannot create order with label that already exists @ PivotTrader.PivotTrader.SendPendingOrder(PivotTrader.java:189)

The problem is with the label generator. Just to check I added a random number to the label and it seems to work then. Guess it must be trying to submit a second order the same millisecond which makes that label a duplicate.

import java.util.Random;

---

    //+------------------------------------------------------------------+
    //| Getlabel Function                                                |
    //+------------------------------------------------------------------+
    protected String getLabel(Instrument instrument)
    {
        String label = instrument.name();
       
        long time = new java.util.Date().getTime();
       
        Random generator = new Random();
        int r = generator.nextInt(99999);

        label = label.substring(0, 2) + label.substring(3, 5);
        label = label + time + r;
        label = label.toLowerCase();
        return label;
    }




Ah awesome thanks! Not sure why I didn't look at that function, stupid me. Will change the function and see if it works this time.

Question, where can I see the generated errors? That would help a lot!


 
 Post subject: Re: Need help with pending orders Post rating: 0   New post Posted: Thu 03 Feb, 2011, 07:18 
User avatar

User rating: 0
Joined: Tue 07 Dec, 2010, 10:51
Posts: 39
You are a genius, it actually worked! Thanks for your help. :mrgreen:

I'm starting an automated forex private forum you are welcome if you want to join:

https://www.forexconsilio.com/


 
 Post subject: Re: Need help with pending orders Post rating: 0   New post Posted: Thu 03 Feb, 2011, 16:31 

User rating: 3
Joined: Tue 17 May, 2011, 16:51
Posts: 38
Location: Sweden, Jonkoping
Saidar wrote:
Question, where can I see the generated errors? That would help a lot!


Tick the messages box in jforex tester or look at the report details ;)

But if you're using Eclipse or such I don't have a clue.


 
 Post subject: Re: Need help with pending orders Post rating: 0   New post Posted: Fri 04 Feb, 2011, 07:13 
User avatar

User rating: 0
Joined: Tue 07 Dec, 2010, 10:51
Posts: 39
Cashbone wrote:
Saidar wrote:
Question, where can I see the generated errors? That would help a lot!


Tick the messages box in jforex tester or look at the report details ;)

But if you're using Eclipse or such I don't have a clue.


Yes I'm using Eclipse... Thanks for the info and thanks again for the help!


 

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