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.

Close all Opened orfer on current pairs.
 Post subject: Close all Opened orfer on current pairs. Post rating: 0   New post Posted: Mon 14 Sep, 2009, 05:58 

User rating: -
Greetings,

I'm new at JForex and would like to ask help from support and seniors,..
I'm trying to built my trading strategies here on JForex...
but it seems I can't found enough samples of coding..
If someone can create me a module / function to close all opened order on current pairs, that would be wonderfull.
Can someone help me??
Thank you very much!


 
 Post subject: Re: Close all Opened orfer on current pairs. Post rating: 0   New post Posted: Mon 14 Sep, 2009, 14:06 
User avatar

User rating: 3
Joined: Wed 18 May, 2011, 16:25
Posts: 331
Location: SwitzerlandSwitzerland
Hi iadip,

there are several ways to close orders. If you have to close them outside of onTick(..), you have to call the closing in it's won thread.

Here's an example:
/**
* closes all created/opened/filled orders af an instrument.
* the closings are processed in their own threads, so this function may be used outside of onTick.
* if an error occurs during a close, the procedure continues with the next order
* @param _iInstrument
* @return number of orders closed
*/
private int nCloseOrders(Instrument _iInstrument)
{
   int nClosedOrders = 0;
   for (final IOrder order : engine.getOrders(_iInstrument))
   {
      if (order.getState() == IOrder.State.CREATED ||
            order.getState() == IOrder.State.OPENED ||
            order.getState() == IOrder.State.FILLED)
      {
//execute the close statement in it's own thread...
         Future<Integer> nFutureCloseStatus = context.executeTask
         (
               new Callable<Integer>()
               {
                  public Integer call()
                  {
                     int nStatus = 0;
                     try
                     {
                        order.close();
                        return 1;
                     }
                     catch (JFException e)
                     {
                        console.getOut().println"OnStop.CloseAllOpenPositions: Error occurred during closing order " + order.getId() + " : " + e.getMessage());
                        nStatus = -1;
                     }
                     return nStatus;
                  }
               }
         );

         nClosedOrders++;
         int nCloseStatus = nFutureCloseStatus.get();    //wait for the result of the close...
         if ((int)nCloseStatus == 1)
            console.getOut().println(">>> CloseAllPositions: order" + order.getLabel() + " successfully closed.");
         else
            console.getOut().println(">>> CloseAllPositions: order" + order.getLabel() + " could not get closed successfully.");

      }
   }
   
   return nClosedOrders;
}


Best, RR.


 

Jump to:  

cron
  © 1998-2024 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