Dukascopy
 
 
Wiki JStore Search Login

Attention! Read the forum rules carefully before posting a topic.

    Try to find an answer in Wiki before asking a question.
    Submit programming questions in this forum only.
    Off topics are strictly forbidden.

Any topics which do not satisfy these rules will be deleted.

Problems running code from Eclipse
 Post subject: Problems running code from Eclipse Post rating: 0   New post Posted: Wed 22 Jun, 2011, 03:23 

User rating: -
Hi, there.

I'm experiencing problems while running code from Eclipse. I tryed to adjust the sample sourcecode "TestMySQLAccess.java" to fit to Postgre. It runs properly in JForex platform, but when I tryed to make use of "Main.java" source code from Eclipse to call "TestPostgreAccess.java" it returned the following error:
log4j:WARN No appenders could be found for logger (bd.Main).
log4j:WARN Please initialize the log4j system properly.
Strategy "TestPostgreAccess" is started at 2011-06-22 02:12:11.357 GMT on the local computer with no parameters
Stopping "TestPostgreAccess" strategy at 2011-06-22 02:12:11.364 GMT on the local computer
java.lang.NullPointerException @ bd.TestPostgreAccess.onStop(TestPostgreAccess.java:50)
null: java.lang.NullPointerException
at bd.TestPostgreAccess.onStop(TestPostgreAccess.java:50)
at com.dukascopy.api.impl.execution.t.call(Unknown Source)
at com.dukascopy.api.impl.connect.an.onStop(Unknown Source)
at com.dukascopy.api.impl.connect.JForexTaskManager$a.call(Unknown Source)
at com.dukascopy.api.impl.execution.i.call(Unknown Source)
at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at com.dukascopy.api.impl.execution.e$a.f(Unknown Source)
at com.dukascopy.api.impl.execution.e$a.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

Please help me, I'm stucked three days trying to step over this problem.

Thanks.

Eduardo.


 
 Post subject: Re: Problems running code from Eclipse Post rating: 0   New post Posted: Wed 22 Jun, 2011, 15:54 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
If the onStop method is the same as for MySQL example then it looks as if dataSource was not initialized in first place. Could you please provide some more details on how are you trying to run your strategy (for instance, fragments of Main.java and your strategy)?


 
 Post subject: Re: Problems running code from Eclipse Post rating: 1   New post Posted: Thu 23 Jun, 2011, 02:38 

User rating: -
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. :cry:

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 {
       }
   }


 
 Post subject: Re: Problems running code from Eclipse Post rating: 0   New post Posted: Fri 24 Jun, 2011, 15:32 

User rating: -
Hi, here I am again.

Additional information: I tryed to debug the Strategy code and met the following when hit "F6" button on the TestPostgreAcces 38th line: s.class tab added to the debug window containing:
Class File Editor

Source not found
The jar file jForex-2.13.5.jar has no source attachment.
You can attach the source by clicking Attach Source below:

buttom: Attach Source

Thanks a lot.

Eduardo.


 
 Post subject: Re: Problems running code from Eclipse Post rating: 0   New post Posted: Mon 27 Jun, 2011, 14:03 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
eduardobasilio wrote:
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...
Did you succeed to manually insert a record in the database table? Did you test if you can read data from the database i.e. make a SELECT statement in some test class by using the same connectors?
eduardobasilio wrote:
Source not found
The jar file jForex-2.13.5.jar has no source attachment.
This just means that you can't debug JForex API source code because it does not get published.


 
 Post subject: Re: Problems running code from Eclipse Post rating: 0   New post Posted: Tue 28 Jun, 2011, 02:03 

User rating: -
I had created a new Strategy from the beginning that extracts bar history and its working well... It connects to DB and records the data.

Thanks.

Eduardo.


 

Jump to:  

  © 1998-2025 Dukascopy® Bank SA
On-line Currency forex trading with Swiss Forex Broker - ECN Forex Brokerage,
Managed Forex Accounts, introducing forex brokers, Currency Forex Data Feed and News
Currency Forex Trading Platform provided on-line by Dukascopy.com