package jforex.orders;
import com.dukascopy.api.*;
import static com.dukascopy.api.IOrder.State.*;
import com.dukascopy.api.IEngine.OrderCommand;
public class CloseOrderVarious implements IStrategy {
private IConsole console;
private IEngine engine;
private IHistory history;
private Instrument instrument = Instrument.EURUSD;
@Override
public void onStart(IContext context) throws JFException {
console = context.getConsole();
engine = context.getEngine();
history = context.getHistory();
context.setSubscribedInstruments(java.util.Collections.singleton(instrument), true);
IOrder order = engine.submitOrder("order", instrument, OrderCommand.BUY, 0.01);
order.waitForUpdate(2000, FILLED);
if (order.getState() != FILLED) {
console.getOut().println("オーダー状態がFILLED以外なので、クローズオーダーは出来ません。現在のオーダー状態:" + order.getState());
context.stop();
}
order.close(0.003);
order.waitForUpdate(2000);
double lastBid = history.getLastTick(instrument).getBid();
order.close(0.003, lastBid - 100 * instrument.getPipValue());
order.waitForUpdate(2000);
order.close(0.003, lastBid - 100 * instrument.getPipValue(), 5);
order.waitForUpdate(2000);
order.close();
order.waitForUpdate(2000, CLOSED);
if (order.getState() != CLOSED) {
console.getErr().println("ポジションが全てクローズされていないので、同じラベルでの新規オーダーは出来ません。現在のオーダー状態:"
+ order.getState());
context.stop();
}
double price = history.getLastTick(instrument).getAsk() + 5 * instrument.getPipValue();
order = engine.submitOrder("order", instrument, OrderCommand.BUYSTOP, 0.01, price);
order.waitForUpdate(2000, OPENED);
if (order.getState() != OPENED) {
console.getErr().println("オーダー状態がOPENED以外の時は、オーダーキャンセル出来ません。現在のオーダー状態:"
+ order.getState());
context.stop();
}
order.close();
order.waitForUpdate(2000, CANCELED);
price = history.getLastTick(instrument).getAsk();
for(int i = -5; i< 6; i++){
String label = String.format("order_%sAsk%s", Math.abs(i),i < 0 ? "below" : "above");
order = engine.submitOrder(label, instrument, OrderCommand.BUYSTOP, 0.01, price + i*instrument.getPipValue());
order.waitForUpdate(2000, OPENED, FILLED);
}
for(IOrder o: engine.getOrders()){
console.getNotif().println("アクティブオーダー: " + engine.getOrders());
if (o.getState() == OPENED || o.getState() == FILLED) {
o.close();
o.waitForUpdate(2000, CLOSED, CANCELED);
}
}
context.stop();
}
@Override
public void onMessage(IMessage message) throws JFException {
IOrder order = message.getOrder();
if (order == null || message.getType() == IMessage.Type.NOTIFICATION) {
return;
}
console.getOut().println(message);
if (message.getType() == IMessage.Type.ORDER_CLOSE_OK) {
console.getInfo().println(order + " " +
(order.getState() == CANCELED ? "キャンセルしました" :
order.getState() == FILLED ? "部分クローズしました" :
"全クローズしました"));
}
}
@Override
public void onTick(Instrument instrument, ITick tick) throws JFException {}
@Override
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {}
@Override
public void onAccount(IAccount account) throws JFException {}
@Override
public void onStop() throws JFException {}
}