package singlejartest;
import com.dukascopy.api.*;
import com.dukascopy.api.system.ISystemListener;
import com.dukascopy.api.system.ITesterClient;
import com.dukascopy.api.system.TesterFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.TimeZone;
import java.util.concurrent.Future;
@RequiresFullAccess
public class TesterMainCustomReport {
private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
private static String jnlpUrl = "https://www.dukascopy.com/client/demo/jclient/jforex.jnlp";
private static String userName = "username";
private static String password = "password";
public static void insertStringInFile(File inFile, int lineno, String lineToBeInserted) throws Exception {
File outFile = new File("$$$$$$$$.tmp");
FileInputStream fis = new FileInputStream(inFile);
BufferedReader in = new BufferedReader(new InputStreamReader(fis));
FileOutputStream fos = new FileOutputStream(outFile);
PrintWriter out = new PrintWriter(fos);
String thisLine = "";
int i = 1;
while ((thisLine = in.readLine()) != null) {
if (i == lineno)
out.println(lineToBeInserted);
out.println(thisLine);
i++;
}
out.flush();
out.close();
in.close();
inFile.delete();
outFile.renameTo(inFile);
}
public static void main(String[] args) throws Exception {
final ITesterClient client = TesterFactory.getDefaultInstance();
client.setSystemListener(new ISystemListener() {
@Override
public void onStart(long processId) {
LOGGER.info("ストラテジー起動: " + processId);
}
@Override
public void onStop(long processId) {
LOGGER.info("ストラテジー停止: " + processId);
File reportFile = new File("C:\\temp\\report2.html");
try {
client.createReport(processId, reportFile);
insertStringInFile(reportFile, 90, "__________INSERTED LINE__________1.2345");
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
}
if (client.getStartedStrategies().size() == 0) {
System.exit(0);
}
}
@Override
public void onConnect() {
LOGGER.info("接続完了");
}
@Override
public void onDisconnect() {
}
});
LOGGER.info("接続中…");
client.connect(jnlpUrl, userName, password);
int i = 10;
while (i > 0 && !client.isConnected()) {
Thread.sleep(1000);
i--;
}
if (!client.isConnected()) {
LOGGER.error("デューカスコピーのサーバーに接続出来ませんでした");
System.exit(1);
}
String dateFromStr = "05/25/2011 00:00:00";
String dateToStr = "05/26/2011 00:00:00";
final SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
Date dateFrom = dateFormat.parse(dateFromStr);
Date dateTo = dateFormat.parse(dateToStr);
client.setDataInterval(Period.THIRTY_MINS, OfferSide.BID, ITesterClient.InterpolationMethod.CLOSE_TICK, dateFrom.getTime(),
dateTo.getTime());
Set instruments = new HashSet();
instruments.add(Instrument.EURUSD);
LOGGER.info("通貨ペア登録…");
client.setSubscribedInstruments(instruments);
client.setInitialDeposit(Instrument.EURUSD.getSecondaryCurrency(), 50000);
LOGGER.info("データのダウンロード中");
Future future = client.downloadData(null);
future.get();
LOGGER.info("ストラテジー開始");
client.startStrategy(new MA_Play(), new LoadingProgressListener() {
@Override
public void dataLoaded(long startTime, long endTime, long currentTime, String information) {
LOGGER.info(information);
}
@Override
public void loadingFinished(boolean allDataLoaded, long startTime, long endTime, long currentTime) {
}
@Override
public boolean stopJob() {
return false;
}
});
}
}