package jforex.orders.merge;
import java.util.concurrent.TimeUnit;
import com.dukascopy.api.IAccount;
import com.dukascopy.api.IBar;
import com.dukascopy.api.IConsole;
import com.dukascopy.api.IContext;
import com.dukascopy.api.IEngine;
import com.dukascopy.api.IEngine.OrderCommand;
import com.dukascopy.api.IHistory;
import com.dukascopy.api.IMessage;
import com.dukascopy.api.IOrder;
import com.dukascopy.api.IStrategy;
import com.dukascopy.api.ITick;
import com.dukascopy.api.Instrument;
import com.dukascopy.api.JFException;
import com.dukascopy.api.Period;
import com.dukascopy.api.RequiresFullAccess;
import com.dukascopy.api.util.DateUtils;
@RequiresFullAccess
public class MergeRemoveSlTp implements IStrategy {
private IConsole console;
private IEngine engine;
private IHistory history;
private int mergeCount;
@Override
public void onStart(IContext context) throws JFException {
engine = context.getEngine();
console = context.getConsole();
history = context.getHistory();
context.setSubscribedInstruments(java.util.Collections.singleton(Instrument.EURUSD), true);
console.getOut().println("Start");
double price = history.getLastTick(Instrument.EURUSD).getBid();
IOrder order1 = engine.submitOrder("order1", Instrument.EURUSD, OrderCommand.BUY , 0.001, 0, 20, price - 0.0010, price + 0.0010);
IOrder order2 = engine.submitOrder("order2", Instrument.EURUSD, OrderCommand.SELL, 0.002, 0, 20, 0 , 0 );
IOrder order3 = engine.submitOrder("order3", Instrument.EURUSD, OrderCommand.BUY , 0.002, 0, 20, price - 0.0010, 0 );
order1.waitForUpdate(2000, IOrder.State.FILLED);
order2.waitForUpdate(2000, IOrder.State.FILLED);
order3.waitForUpdate(2000, IOrder.State.FILLED);
removeSlTp(order1, order2, order3);
IOrder mergedOrder = engine.mergeOrders("mergedOrder" + ++mergeCount, order1, order2, order3);
IMessage message = mergedOrder.waitForUpdate(2, TimeUnit.SECONDS);
print("ポジションマージされました: " + message.getType() + " - " + message);
if(mergedOrder.getState() == IOrder.State.FILLED){
double slPrice = mergedOrder.isLong() ? price - 0.0010 : price + 0.0010;
mergedOrder.setStopLossPrice(slPrice);
}
}
private void removeSlTp(IOrder... orders) throws JFException{
for(IOrder o: orders){
if(Double.compare(o.getStopLossPrice(),0) != 0){
o.setStopLossPrice(0);
print(o.getLabel() + " のストップロスを削除しました。");
o.waitForUpdate(2000);
}
if(Double.compare(o.getTakeProfitPrice(),0) != 0){
o.setTakeProfitPrice(0);
print(o.getLabel() + " のリミットを削除しました。");
o.waitForUpdate(2000);
}
}
}
@Override
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {}
@Override
public void onMessage(IMessage message) throws JFException {
if (message.getOrder() != null)
print("<html><font color=\"gray\">" + DateUtils.format(message.getCreationTime()) + " - "+ message+"</font>");
}
private void print(Object o) {
console.getOut().println(o);
}
@Override
public void onAccount(IAccount account) throws JFException { }
@Override
public void onStop() throws JFException {
for (IOrder o : engine.getOrders())
o.close();
}
@Override
public void onTick(Instrument instrument, ITick tick) throws JFException {}
}