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.

Not able to close position
 Post subject: Not able to close position Post rating: 0   Post Posted: Wed 18 Aug, 2010, 16:38 

User rating: 0
Joined: Mon 07 Dec, 2009, 20:14
Posts: 14
My strategy seems to have opened a position (in the demo environment) which can't be closed. Although I submitted an order of the minimal amount of 1000 it got filled by the strange amount of 1.44 GBP/AUD.
While trying to close the position only 1.00 got closed leaving 0.44 open. I can't close the position manually either.

The trade log looks like this:
...

18.08.2010 14:39:09 Order #46197543, Position #9686994 BUY 0.44 GBP/AUD REJECTED
18.08.2010 14:39:09 Order #46197541, Position #9686994 BUY 0.44 GBP/AUD REJECTED
18.08.2010 14:39:09 Order #46197539, Position #9686994 BUY 0.44 GBP/AUD REJECTED
18.08.2010 14:39:09 Order #46197536, Position #9686994 BUY 0.44 GBP/AUD REJECTED
18.08.2010 14:39:08 Order #46197534, Position #9686994 BUY 0.44 GBP/AUD REJECTED
18.08.2010 14:39:08 Order #46197530, Position #9686994 BUY 0.44 GBP/AUD REJECTED
18.08.2010 14:39:08 Order #46197527, Position #9686994 BUY 0.44 GBP/AUD REJECTED
18.08.2010 14:39:08 Order #46197525, Position #9686994 BUY 0.44 GBP/AUD REJECTED
18.08.2010 14:39:07 Order #46197523, Position #9686994 BUY 1.00 GBP/AUD Average 1.73495 FILLED
14:39:07.952 Trade #38464902 1.00 1.73495
18.08.2010 14:39:07 Order #46197522, Position #9686994 SELL 1.44 GBP/AUD Average 1.73555 FILLED
14:39:07.339 Trade #1-38464898 1.44 1.73555


 
 Post subject: Not able to close position Post rating: 0   Post Posted: Thu 19 Aug, 2010, 09:25 

User rating: 0
Joined: Mon 07 Dec, 2009, 20:14
Posts: 14
Ran into the same problem on a different demo environment

The onMessage response to the order.close() looks like this:
order orderId:[Order_GBPAUD1]-FILLED / GBP/AUD / 1.73555 / 1.6E-7 / 1.6E-7 onMessage type:ORDER_CLOSE_REJECTED
conetent: message: REJECTED_COUNTERPARTY-Order 46378706 was REJECTED: Rejected by counterparty

Trade log:
.....
18.08.2010 14:39:09 Order #46197538, Position #9686993 BUY 0.16 GBP/AUD REJECTED
18.08.2010 14:39:09 Order #46197537, Position #9686993 BUY 0.16 GBP/AUD REJECTED
18.08.2010 14:39:09 Order #46197535, Position #9686993 BUY 0.16 GBP/AUD REJECTED
18.08.2010 14:39:08 Order #46197533, Position #9686993 BUY 0.16 GBP/AUD REJECTED
18.08.2010 14:39:08 Order #46197531, Position #9686993 BUY 0.16 GBP/AUD REJECTED
18.08.2010 14:39:08 Order #46197529, Position #9686993 BUY 0.16 GBP/AUD REJECTED
18.08.2010 14:39:08 Order #46197528, Position #9686993 BUY 0.16 GBP/AUD REJECTED
18.08.2010 14:39:08 Order #46197526, Position #9686993 BUY 0.16 GBP/AUD REJECTED
18.08.2010 14:39:08 Order #46197524, Position #9686993 BUY 2.00 GBP/AUD Average 1.73495 FILLED
14:39:08.012 Trade #38464903 2.00 1.73495


 
 Post subject: Re: Not able to close position Post rating: 0   Post Posted: Thu 19 Aug, 2010, 11:13 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Hi,
Could you please provide the code you used to open the order mentioned?


 
 Post subject: Re: Not able to close position Post rating: 0   Post Posted: Thu 19 Aug, 2010, 19:08 

User rating: 0
Joined: Mon 07 Dec, 2009, 20:14
Posts: 14
Unfortunately I can't fully disclose the code.

A very simplified version looks like this (the original is over 1000 lines).

I deleted all irrelevant lines and kept the maintaining code untouched as much as possible. I's important to note however that the original code can have 3 OrderProcess threads running concurrently. I log all output (including error output) to a logfile in which I didn't find any exceptions or errors.

Regarding submitorder()/trading:
In the original version the instrument and orderCommand can change. The slippage and amount(defaultAmount) are 'static' once the strategy starts as in this version (exact same variables).

package com.forex.strategies;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.SimpleTimeZone;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

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

public class StrategyBughunter implements IStrategy{
   private double defaultAmount = 0.001;
    private double defaultSlippage = 0;
       
    private IContext context;
    private IEngine engine;
    private IConsole console;
   
    private Instrument mainInstrument = Instrument.GBPCHF;
   
    private OrderProcess orderProcess = new OrderProcess();
    {
      orderProcess.start();
    }

    @Override
    public void onStart(IContext context) throws JFException {
       this.context = context;
        engine = context.getEngine();
        this.console = context.getConsole();
    }

    @Override
    public void onStop() throws JFException {
       for (IOrder order : engine.getOrders()) {
         order.close();
      }
    }
   
    @Override
    public void onTick(Instrument instrument, ITick tick) throws JFException {
       /*
        * Do some smart thing and start trading once you think you can
        */
       if(Math.random() > 0.99){
          orderProcess.sendEntrySignal(OrderCommand.BUY, tick);
       }
    }

   public class OrderProcess extends Thread{
      private Object entryWaitObject = new Object();
      
      private ITick entrySignal;
      private OrderCommand orderCommand;
      private volatile IOrder order;
      private double price;

      private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");

      public OrderProcess(){
         dateFormat.setCalendar(Calendar.getInstance(new SimpleTimeZone(0, "GMT")));
      }

      public void sendEntrySignal(OrderCommand orderCommand, ITick tick){
         synchronized(entryWaitObject){
            if(entrySignal == null){
               this.orderCommand = orderCommand;
               entrySignal = tick;
               entryWaitObject.notify();
            }
         }
      }
      
      public void run(){
         while (true) {
            entrySignal = null;

            order = null;

            /* --------------------------------
             * Wait for Entry Signal
             -------------------------------- */
            synchronized(entryWaitObject) {
               do{
                        try{
                           entryWaitObject.wait();
                        }catch(InterruptedException ignored){}
                    }while(entrySignal == null);
            }
            
            /* --------------------------------
             * About to Submit Order
             -------------------------------- */
            ITick mainTick = entrySignal;
            price = (orderCommand == OrderCommand.BUY || orderCommand == OrderCommand.PLACE_BID ? mainTick.getAsk() : mainTick.getBid());
            
            /* --------------------------------
             * Submit Order
             -------------------------------- */
            Future<IOrder> future = context.executeTask(
                new Callable<IOrder>() {
                   public IOrder call() {
                      try {
                         Instrument mainCP = mainInstrument;
                         int num = engine.getOrders(mainCP).size() + 1;
                         return engine.submitOrder("Order_" + mainCP.getPrimaryCurrency() + mainCP.getSecondaryCurrency() + num, mainCP, orderCommand, defaultAmount, price, defaultSlippage);
                      } catch (JFException e) {
                        e.printStackTrace(console.getOut());
                        return null;
                     }
                   }
                });
            
            try {
               order = future.get();
               
               /*
                * do some other smart things, close position and take profit
                */
               
            } catch (InterruptedException e) {
               e.printStackTrace();
            } catch (ExecutionException e) {
               e.printStackTrace();
            }
         }
      }
   }

    @Override
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) {
    }

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

   @Override
   public void onMessage(IMessage message) throws JFException {
   }
}


 
 Post subject: Re: Not able to close position Post rating: 0   Post Posted: Wed 15 Jun, 2011, 11:41 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
You get ORDER_CLOSE_REJECTED if the order is in filling process, but if the filling process didn't succeed on the current counterparty it's not sent to another, it's canceled. You will get ORDER_CLOSED_OK after it's CANCELED though


 

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