Please, see
orderClosedInLastBar method in this strategy.
package jforex.strategies;
import java.util.*;
import com.dukascopy.api.*;
public class OneOrderInBarStrategy implements IStrategy {
private IEngine engine;
private IConsole console;
private IHistory history;
private IContext context;
private IIndicators indicators;
private IUserInterface userInterface;
@Configurable("Instrument")
public Instrument selectedInstrument = Instrument.EURUSD;
@Configurable("Period")
public Period selectedPeriod = Period.TEN_SECS; // bar duration
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();
}
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 != selectedInstrument)
return;
console.getOut().println(orderClosedInLastBar());
}
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
}
public boolean orderClosedInLastBar() throws JFException {
Calendar cal = Calendar.getInstance();
long end = cal.getTimeInMillis();
cal.add(Calendar.DATE, -1);
long start = cal.getTimeInMillis();
// get orders for the last day
// only orders from selected instrument are fetched
List<IOrder> orders = history.getOrdersHistory(selectedInstrument, start, end);
// fetches current bid bar from history
IBar lastBidBar = history.getBar(selectedInstrument, selectedPeriod, OfferSide.BID, 0);
for (IOrder order : orders) {
// check whether there is an order that has been closed during the
// current bid bar
if (lastBidBar.getTime() <= order.getCloseTime()) {
return true;
}
}
return false;
}
}