package jforex;
import java.util.*;
import com.dukascopy.api.*;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.ExecutionException;
import java.lang.InterruptedException;
public class HistoryTester implements IStrategy {
private IEngine engine;
private IConsole console;
private IHistory history;
private IContext context;
private IIndicators indicators;
private IUserInterface userInterface;
private Future<Object> future;
private boolean isOrdered = false;
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 ( ! isOrdered )
{
if (instrument == Instrument.EURUSD)
{
String label = String.format("order%d", System.currentTimeMillis());
MarketOrder marketBuy = new MarketOrder("BUY" + label, instrument, IEngine.OrderCommand.BUYSTOP, 1000, tick.getAsk() + 5 * instrument.getPipValue(), 1000.0);
console.getOut().println("> a");
try {
future = context.executeTask(marketBuy);
console.getOut().println("> b");
IOrder myOrder = (IOrder)future.get();
console.getOut().println("> c");
} catch (InterruptedException e)
{
console.getErr().println("InterruptedException");
console.getErr().println(e.getMessage());
} catch ( ExecutionException e)
{
console.getErr().println("ExecutionException");
console.getErr().println(e.getMessage());
}
isOrdered = true;
}
}
}
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
}
private class MarketOrder implements Callable<Object>{
private String label;
private Instrument instrument;
private IEngine.OrderCommand orderCommand;
private double amount;
private double price;
private double slippage;
private double stopLossPrice;
private double takeProfitPrice;
public MarketOrder( String label_,
Instrument instrument_,
IEngine.OrderCommand orderCommand_,
double amount_,
double price_,
double slippage_)
{
this.label = label_;
this.instrument = instrument_;
this.orderCommand = orderCommand_;
this.amount = amount_;
this.price = price_;
this.slippage = slippage_;
this.stopLossPrice = 0.0;
this.takeProfitPrice = 0.0;
console.getOut().println("MarketOrder() " + this.label);
}
public IOrder call() throws Exception {
console.getOut().println("MarketOrder.call()");
try {
String label = String.format("order%d", System.currentTimeMillis());
return engine.submitOrder( this.label,
this.instrument,
this.orderCommand,
this.amount/1000000.0,
this.price,
this.slippage,
this.stopLossPrice,
this.takeProfitPrice);
} catch (Exception e) {
console.getErr().println("exception");
console.getErr().println(e.getMessage());
return null;
}
}
}
}