package jforex.orders;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
import com.dukascopy.api.*;
@RequiresFullAccess
public class OrderHistoryAsync6MonthsAllInstr implements IStrategy {
private IConsole console;
private IHistory history;
private IContext context;
private Set<Instrument> instruments = new HashSet<Instrument>(Arrays.asList(Instrument.values()));
private Map<Instrument, List<IOrder>> histOrderMap = new HashMap<Instrument, List<IOrder>>();
private List<Instrument> loadingFinishedInstruments = new ArrayList<Instrument>();
@SuppressWarnings("serial")
public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS") {
{
setTimeZone(TimeZone.getTimeZone("GMT"));
}
};
@Override
public void onStart(IContext context) throws JFException {
console = context.getConsole();
history = context.getHistory();
this.context = context;
context.setSubscribedInstruments(instruments, true);
instruments = context.getSubscribedInstruments();
long to = System.currentTimeMillis();
long from = to - 6 * Period.MONTHLY.getInterval();
console.getOut().println(sdf.format(from) + " 〜 " + sdf.format(to) +
"期間のオーダーヒストリーをロード中。"
);
for (final Instrument instrument : instruments) {
history.readOrdersHistory(
instrument,
from,
to,
new LoadingOrdersListener() {
@Override
public void newOrder(Instrument instrument, IOrder orderData) {
List<IOrder> histOrderList = histOrderMap.get(instrument);
if (histOrderList == null) {
histOrderList = new ArrayList<IOrder>();
histOrderMap.put(instrument, histOrderList);
}
histOrderList.add(orderData);
}
},
new LoadingProgressListener() {
@Override
public void dataLoaded(long start, long end, long currentPosition, String information) {
}
@Override
public void loadingFinished(boolean allDataLoaded, long start, long end, long currentPosition) {
if (allDataLoaded) {
loadingFinishedInstruments.add(instrument);
console.getInfo().println(instrument + "のオーダーヒストリーのロード完了。" );
}
}
@Override
public boolean stopJob() {
return false;
}
}
);
}
}
public void onTick(Instrument instrument, ITick tick) throws JFException {
if (instrument != Instrument.EURUSD) {
return;
}
if (!loadingFinishedInstruments.containsAll(instruments)) {
console.getOut().println("未だロード完了していません。");
return;
}
console.getOut().println("全通貨ペアのロード完了しました。");
for (Map.Entry<Instrument, List<IOrder>> entry : histOrderMap.entrySet()) {
console.getOut().println("オーダーヒストリー情報: " + entry);
}
context.stop();
}
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {}
public void onMessage(IMessage message) throws JFException {}
public void onAccount(IAccount account) throws JFException {}
public void onStop() throws JFException {}
}