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.

Code to Submit Order
 Post subject: Code to Submit Order Post rating: 0   New post Posted: Wed 09 Mar, 2011, 14:08 

User rating: 0
Joined: Wed 09 Mar, 2011, 13:48
Posts: 6
Hi,

Can anyone tell me the Code to submit a conditional order with entry at current market price and Stoploss at ask<=[stop]


String labelname = getLabel();
order = engine.submitOrder(labelname, instrument, OrderCommand.BUY, amount, entry, 0, 0, 0);
IOrder order = engine.getOrder(labelname);
order.setStopLossPrice(stopLoss, OfferSide.ASK);

showing error Order state IMMUTABLE
Please tell me how to solve this error.

Thanks


 
 Post subject: Re: Code to Submit Order Post rating: 0   New post Posted: Thu 10 Mar, 2011, 08:50 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Hi,
You can modify the order in Opened/Filled state, for more details please take a look at the following JForex Wiki page: https://www.dukascopy.com/wiki/index.php ... Loss_price


 
 Post subject: Re: Code to Submit Order Post rating: 0   New post Posted: Thu 10 Mar, 2011, 10:16 

User rating: 0
Joined: Wed 09 Mar, 2011, 13:48
Posts: 6
But i want to submit a order with offer side ASK for stoploss field..........how can i do it.....


 
 Post subject: Re: Code to Submit Order Post rating: 0   New post Posted: Thu 10 Mar, 2011, 11:15 

User rating: 0
Joined: Wed 09 Mar, 2011, 13:48
Posts: 6
Hi,

I have tried this (Wiki page: https://www.dukascopy.com/wiki/index.php) not getting any solutions.

can anyone solve this...

For Side BUY
If i submit a order with following code
order = engine.submitOrder(getLabel(), instrument, OrderCommand.BUY, amount, 0, slippage, stopLoss, 0);
by default it is taking the stoploss offer side "BID" But i want to take Offer side as "ASK"


For Side SELL
If i submit a order with following code
order = engine.submitOrder(getLabel(), instrument, OrderCommand.SELL, amount, 0, slippage, stopLoss, 0);
by default it is taking the stoploss offer side "ask" But i want to take Offer side as "BId"

How can i do it......

Thanks


 
 Post subject: Re: Code to Submit Order Post rating: 0   New post Posted: Mon 21 Mar, 2011, 14:47 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Hi,
You can only choose the side of order stop loss by modifying the existing order. When you submit orders, the default side will be applied for stop loss.
So basically what you need to do is create an order, wait until the order is in the 'opened' or 'filled' state and then add the desired stop loss.


 
 Post subject: Re: Code to Submit Order Post rating: 0   New post Posted: Tue 28 Jun, 2011, 10:05 

User rating: -
Support wrote:
Hi,
You can only choose the side of order stop loss by modifying the existing order. When you submit orders, the default side will be applied for stop loss.
So basically what you need to do is create an order, wait until the order is in the 'opened' or 'filled' state and then add the desired stop loss.


Hello,

Please advise what is wrong in following code:
public double amount = 0.1;
  public double takeProfit = 0.02;
  public double stopLoss = 0.1;
private int opportunityNo = 0;
-------------
opportunityNo++;
crossedOrder.usdjpy = engine.submitOrder("SHORT"+opportunityNo, Instrument.USDJPY, IEngine.OrderCommand.SELL, amount, 0, 0,
    history.getLastTick(this.instrument).getAsk() + stopLoss,
    history.getLastTick(this.instrument).getAsk() - takeProfit);


When I'm testing strategy in historical tester stop loss or take profit sometimes are correct (same as I set in strategy) sometimes 10 times bigger. Is it possible to get a force order close with set take profit or stop loss without any slippage??

Thanks for help in advance


 
 Post subject: Re: Code to Submit Order Post rating: 0   New post Posted: Tue 28 Jun, 2011, 12:51 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
aero wrote:
Please advise what is wrong in following code:
public double amount = 0.1;
  public double takeProfit = 0.02;
  public double stopLoss = 0.1;
private int opportunityNo = 0;
-------------
opportunityNo++;
crossedOrder.usdjpy = engine.submitOrder("SHORT"+opportunityNo, Instrument.USDJPY, IEngine.OrderCommand.SELL, amount, 0, 0,
    history.getLastTick(this.instrument).getAsk() + stopLoss,
    history.getLastTick(this.instrument).getAsk() - takeProfit);
When I'm testing strategy in historical tester stop loss or take profit sometimes are correct (same as I set in strategy) sometimes 10 times bigger.
The code looks fine. Could you please elaborate - when are SL or TP 10 times bigger? At which moment and how do you check that?
aero wrote:
Is it possible to get a force order close with set take profit or stop loss without any slippage??
If you mean IOrder.close() then it executes regardless of slippage amount, see:
https://www.dukascopy.com/wiki/index.php?title=Close_Order


 
 Post subject: Re: Code to Submit Order Post rating: 0   New post Posted: Wed 29 Jun, 2011, 09:29 

User rating: -
Hello

Quote:
The code looks fine. Could you please elaborate - when are SL or TP 10 times bigger? At which moment and how do you check that?


I wrote the following code to check difference between position open and close price. When I set i.e. TP 5pips and SL 5 during testing strategy sometimes positions are closed when 5 pips are reached and sometimes with 20-30 pips slippage.

double diff = order.getClosePrice() - order.getOpenPrice();
            console.getOut().println(order.getLabel()+": "+order.getInstrument().toString()+" open: "+order.getOpenPrice()+", close: "+order.getClosePrice()+ ", diff: "+numFormatter.format(diff));


Quote:
If you mean IOrder.close() then it executes regardless of slippage amount, see:


Would you be so kind to give me code example of IOrder.close() where I can set SL and TP regardless of slippage?
Kind regards


 
 Post subject: Re: Code to Submit Order Post rating: 0   New post Posted: Wed 29 Jun, 2011, 13:31 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
aero wrote:
I wrote the following code to check difference between position open and close price. When I set i.e. TP 5pips and SL 5 during testing strategy sometimes positions are closed when 5 pips are reached and sometimes with 20-30 pips slippage.

double diff = order.getClosePrice() - order.getOpenPrice();
            console.getOut().println(order.getLabel()+": "+order.getInstrument().toString()+" open: "+order.getOpenPrice()+", close: "+order.getClosePrice()+ ", diff: "+numFormatter.format(diff));
It depends on the market situation, see Execution Process section of Stop Loss and Take Profit orders in JForex wiki.
aero wrote:
Would you be so kind to give me code example of IOrder.close() where I can set SL and TP regardless of slippage?
Kind regards
Consider the following strategy:
package jforex.strategies.oneorder;

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

public class OneOrderNoSlippage2 implements IStrategy {

    private IConsole console;
    private IEngine engine;
    private IHistory history;
   
    private IOrder order;
    @Override
    public void onStart(IContext context) throws JFException {
        engine = context.getEngine();
        console = context.getConsole();
        history = context.getHistory();
        console.getOut().println("Start");
       
        int slippage = 0;
        int price = 0; //since this is market order
        order = engine.submitOrder("OneOrder", Instrument.EURUSD, OrderCommand.BUY, 0.01, price, slippage,
                history.getLastTick(Instrument.EURUSD).getBid() - 0.005, //SL
                history.getLastTick(Instrument.EURUSD).getAsk() + 0.005);//TP
    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {}
   
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
       
        if(instrument != Instrument.EURUSD || period != Period.TEN_SECS || !engine.getOrders().contains(order))
            return;
       
        try{
            order.close();
        }catch(JFException e){
            printErr(e);
        }
    }

    public void onMessage(IMessage message) throws JFException {
        print(message.getType() + " " +message.getContent());
    }
    public void onAccount(IAccount account) throws JFException {}
    public void onStop() throws JFException {}
    private void print(Object o){        console.getOut().println(o);    }
    private void printErr(Object o){        console.getErr().println(o);    }

}
If you meant a bit different case then please specify it by modifying the strategy.


 

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