Write ticks in CSV
Every generated tick will be written in a format "Average Price(Bid + (Bid + Ask)/2)" to a file with a timestamp based on a local PC time. The file name is being generated as Dukascopy_serverexpert + ForexPair +Year_Month_Day.csv.
package jforex;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.dukascopy.api.*;
@RequiresFullAccess
public class WriteTickToFile implements IStrategy {
private IEngine engine;
private IConsole console;
private String FILE_NAME ;
private SimpleDateFormat sdf;
private SimpleDateFormat sdf2;
@Configurable("Instrument")
public Instrument selectedInstrument = Instrument.EURUSD;
@Configurable("Directory")
public String path = "C:\\temp\\";
FileWriter fstream;
BufferedWriter out;
public void onStart(IContext context) throws JFException {
Date date = new Date();
this.engine = context.getEngine();
this.console = context.getConsole();
sdf = new SimpleDateFormat("yyyy.MM.dd");
sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
FILE_NAME = path + "WriteTickToFile_" + selectedInstrument.toString().replace("/", "") + "_" + sdf.format(date) +".csv";
try {
fstream = new FileWriter(FILE_NAME, true);
out = new BufferedWriter(fstream);
} catch (IOException e) {
writeMessage("Can not initialize output stream; Stopping the strategy");
context.stop();
}
}
public void writeMessage(String message) {
console.getOut().println(message);
}
public void onAccount(IAccount account) throws JFException {
}
public void onMessage(IMessage message) throws JFException {
}
public void onStop() throws JFException {
try {
if (out != null) {
out.flush();
out.close();
}
} catch (IOException e) {
writeMessage("Can not close output stream");
e.printStackTrace();
}
}
public void onTick(Instrument instrument, ITick tick) throws JFException {
if (instrument.equals(selectedInstrument)) {
try {
out.append( sdf2.format(tick.getTime()) + ";" + ((tick.getAsk() + tick.getBid())/2) + "\n");
} catch (IOException e) {
writeMessage("File write error " );
e.printStackTrace();
}
}
}
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
if (instrument.equals(selectedInstrument) && period.equals(Period.TEN_SECS)) {
try {
out.flush();
} catch (IOException e) {
writeMessage("Can not write message from the buffer to the file. Will retry in 1 minute.");
}
}
}
}
The information on this web site is provided only as general information, which may be incomplete or outdated. Click here for full disclaimer.