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.

why engine doesn't submit order?
 Post subject: why engine doesn't submit order? Post rating: 0   New post Posted: Tue 12 Jun, 2018, 06:55 

User rating: 3
Joined: Sat 09 Jun, 2018, 03:18
Posts: 24
Location: China,
If an order exists , even if there is a situation that satisfies my strategy later, another order cannot be opened.

For example,
IHistory history = context.getHistory();
double firstHistoryBarClosePrice = history.getBar(myInstrument, myPeriod, OfferSide.BID, 1).getClose();
double secondHistoryBarClosePrice = history.getBar(myInstrument, myPeriod, OfferSide.BID, 2).getClose();

if(firstHistoryBarClosePrice <secondHistoryBarClosePrice)
      engine.submitOrder(Label, selectedInstrument, IEngine.OrderCommand.BUY,tradeAmount, 0, slippage,  stoploss, takeprofit, 0, "");


if 2018.6.11 22:00,an order was submitted,and it's state is opened. Then at 23:00,a situation also can satisfy the strategy,but the engine won't submitt order.

There is only one order can exist.

Please help


 
 Post subject: Re: why engine doesn't submit order? Post rating: 0   New post Posted: Tue 12 Jun, 2018, 13:16 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
It's impossible to create two orders with same label. Each order label must be unique.
If problem will not disappear, please provide full console log and i will try help you.


 
 Post subject: Re: why engine doesn't submit order? Post rating: 0   New post Posted: Tue 12 Jun, 2018, 15:14 

User rating: 3
Joined: Sat 09 Jun, 2018, 03:18
Posts: 24
Location: China,
API Support wrote:
It's impossible to create two orders with same label. Each order label must be unique.
If problem will not disappear, please provide full console log and i will try help you.


That's the full code,only one order can exist,another order can't open.Please help.


package examples;

import java.util.*;
import java.text.*;

import com.dukascopy.api.*;

public class example implements IStrategy {
    private IEngine engine;
    private IConsole console;
    private IHistory history;
    private IContext context;
    private IIndicators indicators;
    private IUserInterface userInterface;
 
 

    @Configurable("selectedInstrument:")
    public Instrument selectedInstrument = Instrument.EURUSD;
    @Configurable("tradeAmount:")
    public double tradeAmount = 0.0020;
    @Configurable("slippage:")
    public int slippage = 1;
    @Configurable("stopLossPips:")
    public int stopLossPips = 50;
    @Configurable("takeProfitPips:")
    public int takeProfitPips = 200;

    private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd_HHmmss");

    public void onStart(IContext context) throws JFException {
        this.engine = context.getEngine();
        this.console = context.getConsole();
        this.history = context.getHistory();
        this.context = context;
        this.indicators = context.getIndicators();
        this.userInterface = context.getUserInterface();

        context.setSubscribedInstruments(Collections.singleton(selectedInstrument), true);
    }

    public void onAccount(IAccount account) throws JFException {
    }

    public void onMessage(IMessage message) throws JFException {
    }

    public void onStop() throws JFException {
    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {
       
       
        if (!instrument.equals(selectedInstrument)) {
            return;
        }

        if (!engine.getOrders(selectedInstrument).isEmpty()) {
            return;
        }
                                                   
       
    }

    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException
    {
       
        if (!instrument.equals(selectedInstrument)) {
            return;
        }

        if (!engine.getOrders(selectedInstrument).isEmpty()) {
            return;
        }
       
         if (!period.equals(Period.TEN_MINS)){
         return;
         }
         
         long time = bidBar.getTime();
         
        double firstHistoryBarClosePrice = history.getBar(instrument, period.TEN_MINS, OfferSide.BID, 1).getClose();
        double secondHistoryBarClosePrice = history.getBar(instrument, period.TEN_MINS, OfferSide.BID, 2).getClose();
       
       
     
       
        if(firstHistoryBarClosePrice<secondHistoryBarClosePrice)
        {
           
           
            ITick lastTick = history.getLastTick(instrument);
            double sl = lastTick.getAsk() - instrument.getPipValue() * stopLossPips;
            double tp = lastTick.getAsk() + instrument.getPipValue() * takeProfitPips;

            engine.submitOrder(getLabel(time), selectedInstrument, IEngine.OrderCommand.BUY,tradeAmount, 0, slippage,  sl, tp, 0, "");     
             
         
           
        }
       
       
       
    }

    private String getLabel(long time) {
        return "IVF" + DATE_FORMAT.format(time) + generateRandom(10000) + generateRandom(10000);
    }

    private String generateRandom(int n) {
        int randomNumber = (int) (Math.random() * n);
        String answer = "" + randomNumber;
        if (answer.length() > 3) {
            answer = answer.substring(0, 4);
        }
        return answer;
    }
   

   
}


 
The Best Answer  Post subject: Re: why engine doesn't submit order? Post rating: 0   New post Posted: Tue 12 Jun, 2018, 17:26 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Quote:
        if (!engine.getOrders(selectedInstrument).isEmpty()) {
            return;
        }


After sending your first order -
!engine.getOrders(selectedInstrument).isEmpty()
will always be false. Accordingly, here each time will perform return from onBar method. So, your strategy limits itself to only one order.


 
 Post subject: Re: why engine doesn't submit order? Post rating: 0   New post Posted: Wed 13 Jun, 2018, 01:05 

User rating: 3
Joined: Sat 09 Jun, 2018, 03:18
Posts: 24
Location: China,
API Support wrote:
Quote:
        if (!engine.getOrders(selectedInstrument).isEmpty()) {
            return;
        }


After sending your first order -
!engine.getOrders(selectedInstrument).isEmpty()
will always be false. Accordingly, here each time will perform return from onBar method. So, your strategy limits itself to only one order.



Thanks a lot!


 

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