Hi, thanks for your reply.
I can't say that I fixed that error at all, but somehow the code is running. I put main.java into JForexClientLibrary project and I think that its log4j configuration made it run. Previously I was trying to build a new project and add the jar files to the project library. Every little jar was there, but the code was not running... somewhere there must had been a "hidden configuration" I could not see. So that I gave up trying to build a hole project from the begining.
But now main class is running, it calls my strategy properly, no errors are showing up in the console tab, but nothing has been recorded in db...
Here goes the class that holds main method (obviously I edited the user ID and password):
package singlejartest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.dukascopy.api.system.ClientFactory;
import com.dukascopy.api.system.IClient;
import com.dukascopy.api.system.ISystemListener;
/**
* This small program demonstrates how to initialize Dukascopy client and start a strategy
*/
public class RunTestPostgre {
private static final Logger LOGGER = LoggerFactory.getLogger(RunTestPostgre.class);
//url of the DEMO jnlp
private static String jnlpUrl = "https://www.dukascopy.com/client/demo/jclient/jforex.jnlp";
//user name
private static String userName = "DEMO3xxxxx";
//password
private static String password = "xxxxx";
public static void main(String[] args) throws Exception {
//get the instance of the IClient interface
final IClient client = ClientFactory.getDefaultInstance();
//set the listener that will receive system events
client.setSystemListener(new ISystemListener() {
private int lightReconnects = 3;
@Override
public void onStart(long processId) {
LOGGER.info("Strategy started: " + processId);
}
@Override
public void onStop(long processId) {
LOGGER.info("Strategy stopped: " + processId);
if (client.getStartedStrategies().size() == 0) {
System.exit(0);
}
}
@Override
public void onConnect() {
LOGGER.info("Connected");
lightReconnects = 3;
}
@Override
public void onDisconnect() {
LOGGER.warn("Disconnected");
if (lightReconnects > 0) {
client.reconnect();
--lightReconnects;
} else {
try {
//sleep for 10 seconds before attempting to reconnect
Thread.sleep(10000);
} catch (InterruptedException e) {
//ignore
}
try {
client.connect(jnlpUrl, userName, password);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
}
}
}
});
LOGGER.info("Connecting...");
//connect to the server using jnlp, user name and password
client.connect(jnlpUrl, userName, password);
//wait for it to connect
int i = 10; //wait max ten seconds
while (i > 0 && !client.isConnected()) {
Thread.sleep(1000);
i--;
}
if (!client.isConnected()) {
LOGGER.error("Failed to connect Dukascopy servers");
System.exit(1);
}
//subscribe to the instruments
/*Set<Instrument> instruments = new HashSet<Instrument>();
instruments.add(Instrument.EURUSD);
LOGGER.info("Subscribing instruments...");
client.setSubscribedInstruments(instruments);
//start the strategy
*
*/
LOGGER.info("Starting strategy");
client.startStrategy(new TestPostgreAccess());
//now it's running
}
}
And the strategy code is as follow (with password changed):
package singlejartest;
import com.dukascopy.api.*;
import java.util.*;
import java.sql.*;
import javax.sql.*;
import org.apache.commons.dbcp.*;
/**
* commons-pool can be downloaded here - https://commons.apache.org/pool/
* commons-dbcp can be downloaded here - https://commons.apache.org/dbcp/
* mysql java connector can be downloaded here - https://dev.mysql.com/downloads/connector/j/5.1.html
*
* @author Dmitry Shohov
*/
@RequiresFullAccess
@Library("F:/Pessoais/JForex/commons-pool-1.5.6/commons-pool-1.5.6.jar;F:/Pessoais/JForex/commons-dbcp-1.4/commons-dbcp-1.4.jar;F:/Pessoais/JForex/lib/libs/postgresql-8.4-702.jdbc4.jar")
public class TestPostgreAccess implements IStrategy{
private IConsole console;
private DataSource dataSource;
private Calendar gmtCalendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
public void onStart(IContext context) throws JFException {
console = context.getConsole();
try {
java.util.Properties properties = new java.util.Properties();
properties.put("driverClassName", "org.postgresql.Driver");
properties.put("url", "jdbc:postgresql://localhost/postgres");
properties.put("username", "postgres");
properties.put("password", "xxxxxxx");
properties.put("poolPreparedStatements", "true");
dataSource = BasicDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
console.getErr().println(e);
}
}
public void onAccount(IAccount account) throws JFException {
}
public void onMessage(IMessage message) throws JFException {
}
public void onStop() throws JFException {
try {
((BasicDataSource) dataSource).close();
} catch (SQLException e) {
console.getErr().println(e);
}
}
public void onTick(Instrument instrument, ITick tick) throws JFException {
try {
Connection connection = dataSource.getConnection();
try {
PreparedStatement statement = connection.prepareStatement("insert into ticks(instrument, tick_time, ask, bid, askVol, bidVol) values (?, ?, ?, ?, ?, ?)");
try {
statement.setString(1, instrument.toString());
statement.setTimestamp(2, new Timestamp(tick.getTime()), gmtCalendar);
statement.setDouble(3, tick.getAsk());
statement.setDouble(4, tick.getBid());
statement.setDouble(5, tick.getAskVolume());
statement.setDouble(6, tick.getBidVolume());
statement.execute();
} finally {
statement.close();
}
} finally {
connection.close();
}
} catch (SQLException e) {
console.getErr().println(e);
}
}
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
}
}