package jforex.test;
import com.dukascopy.api.*;
import com.dukascopy.api.IEngine.OrderCommand;
import static com.dukascopy.api.IEngine.OrderCommand.*;
public class SwitchOrderCommand implements IStrategy {
private IEngine engine;
private IConsole console;
private IHistory history;
@Override
public void onStart(IContext context) throws JFException {
this.engine = context.getEngine();
this.console = context.getConsole();
this.history = context.getHistory();
context.setSubscribedInstruments(java.util.Collections.singleton(Instrument.EURUSD), true);
double price = history.getLastTick(Instrument.EURUSD).getBid();
OrderCommand[] orderCommands = OrderCommand.values();
for (int i = 0; i < orderCommands.length; i++){
try{
if (orderCommands[i] == BUY || orderCommands[i] == SELL){
engine.submitOrder("order"+i, Instrument.EURUSD, orderCommands[i], 0.001);
} else {
engine.submitOrder("order"+i, Instrument.EURUSD, orderCommands[i], 0.001, price);
}
} catch (JFException e) {
console.getErr().println(orderCommands[i] + " " + e);
}
}
for (IOrder order : engine.getOrders()) {
switch (order.getOrderCommand()) {
case BUY:
print(order.getLabel() + " は成行ロングオーダーです。");
break;
case SELL:
print(order.getLabel() + " は成行ショートオーダーです。");
break;
default:
print(order.getLabel() + " は " + (order.isLong() ? "ロングの" : "ショートの") + " " + order.getOrderCommand()
+ "条件付きオーダーです。");
}
}
for (IOrder order : engine.getOrders()) {
print(order.getLabel() + " " + order.getOrderCommand());
}
}
@Override
public void onStop() throws JFException {
for (IOrder o : engine.getOrders()){
o.close();
}
}
private void print(Object o){
console.getOut().println(o);
}
@Override
public void onTick(Instrument instrument, ITick tick) throws JFException {}
@Override
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
if(instrument == Instrument.EURUSD && period == Period.TEN_SECS){
for (IOrder order : engine.getOrders()) {
print(order.getLabel() + " " + order.getOrderCommand());
}
}
}
@Override
public void onMessage(IMessage message) throws JFException {}
@Override
public void onAccount(IAccount account) throws JFException {}
}