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.

Multi intervals test by using Eclipse
 Post subject: Multi intervals test by using Eclipse Post rating: 0   Post Posted: Sun 04 Sep, 2011, 13:05 

User rating: 0
Joined: Sun 04 Sep, 2011, 12:12
Posts: 2
Location: JapanJapan
Hi, support.

I try to test my strategy in some intervals at one time by using Eclipse.
If I test two intervals, I'll get two result reports.
I modified TesterMain class code in JForexClientLibrary, and created a new class to input interval information.

package singlejartest;

import com.dukascopy.api.Instrument;
import com.dukascopy.api.LoadingProgressListener;
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.File;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.Future;

/**
 * This small program demonstrates how to initialize Dukascopy tester and start a strategy
 */
public class TesterMain {
    private static final Logger LOGGER = LoggerFactory.getLogger(Main.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 = "username";
    //password
    private static String password = "password";
   
    /* my settings */
    // cache directory
    private static String cacheDir = "D:\\JForex\\.cache\\";
    // report file
    private static String reportRoot = "D:\\Test\\P1_Lv1_Single\\";

    public static void main(String[] args) throws Exception {
       ArrayList<TestInterval> intervalList = new ArrayList<TestInterval>();
      intervalList.add(new TestInterval("2011_1Q", "2010/12/31 22:00", "2011/03/31 21:00"));
      intervalList.add(new TestInterval("2011_2Q", "2011/03/31 21:00", "2011/06/30 21:00"));
       
       for (TestInterval testInterval : intervalList)
       {
            String reportPath = reportRoot +
                  testInterval.getName() +  ".html";
            final File reportFile = new File(reportPath);
            File reportDir = reportFile.getParentFile();
            if (!reportDir.exists())
            {
               reportDir.mkdirs();
            }
          
          //get the instance of the IClient interface
           final ITesterClient client = TesterFactory.getDefaultInstance();
           //set the listener that will receive system events
          
          
           // cache directory
           client.setCacheDirectory(new File(cacheDir));
          
           client.setSystemListener(new ISystemListener() {
               @Override
               public void onStart(long processId) {
                   LOGGER.info("Strategy started: " + processId);
               }
   
               @Override
               public void onStop(long processId) {
                   LOGGER.info("Strategy stopped: " + processId);
                   //File reportFile = new File("C:\\report.html");
                   try {
                       client.createReport(processId, reportFile);
                   } catch (Exception e) {
                       LOGGER.error(e.getMessage(), e);
                   }
//                   if (client.getStartedStrategies().size() == 0) {
//                       System.exit(0);
//                   }
               }
   
               @Override
               public void onConnect() {
                   LOGGER.info("Connected");
               }
   
               @Override
               public void onDisconnect() {
                   //tester doesn't disconnect
               }
           });
   
           LOGGER.info("Connecting...");
           //connect to the server using jnlp, user name and password
           //connection is needed for data downloading
           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);
           }
   
           //set instruments that will be used in testing
           Set<Instrument> instruments = new HashSet<Instrument>();
           instruments.add(Instrument.EURUSD);
           LOGGER.info("Subscribing instruments...");
           client.setSubscribedInstruments(instruments);
           //setting initial deposit
           client.setInitialDeposit(Instrument.EURUSD.getSecondaryCurrency(), 50000);
           //load data
           LOGGER.info("Downloading data");
           long startDate = testInterval.getStartTime();
           long endDate = testInterval.getEndTime();
           client.setDataInterval(ITesterClient.DataLoadingMethod.ALL_TICKS, startDate, endDate);
           Future<?> future = client.downloadData(null);
           //wait for downloading to complete
           future.get();
           //start the strategy
           LOGGER.info("Starting strategy");
           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;
               }
           });
           //now it's running
       }
    }
}


package singlejartest;

import java.util.*;
import java.text.SimpleDateFormat;
import java.text.ParseException;

public class TestInterval {
   private String name;
   private String startTimeStr;
   private String endTimeStr;
   private SimpleDateFormat dateFmt;
   
   private static final String DATE_PATTERN = "yyyy/MM/dd HH:mm";
   
   public TestInterval(String name, String startTimeStr, String endTimeStr)
   {
      this.name = name;
      this.startTimeStr = startTimeStr;
      this.endTimeStr = endTimeStr;
        this.dateFmt = new SimpleDateFormat(DATE_PATTERN);
        this.dateFmt.setTimeZone(TimeZone.getTimeZone("GMT"));
   }
   
   public String getName()
   {
      return this.name;
   }
   
   public long getStartTime() throws ParseException
   {
      return this.dateFmt.parse(this.startTimeStr).getTime();
   }
   
   public long getEndTime() throws ParseException
   {
      return this.dateFmt.parse(this.endTimeStr).getTime();
   }
}


I tested MA_Play strategy in the two intervals, but I got only one result report on the last interval.
What is wrong? Isn't there any good way?

Thank you in advance for your help

tnaka36


 
 Post subject: Re: Multi intervals test by using Eclipse Post rating: 0   Post Posted: Mon 05 Sep, 2011, 09:12 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
tnaka36 wrote:
What is wrong? Isn't there any good way?
Strategies get loaded and ran asynchronously, so in order not to overwrite the report file paths, consider setting the path in the thread that creates the report. Consider first chaning intervalList to final and changing ISystemListener to the following:
client.setSystemListener(new ISystemListener() {

   int intervalNr = 0;

   @Override
   public void onStart(long processId) {
      LOGGER.info("Strategy started: " + processId);
   }

   @Override
   public void onStop(long processId) {
      LOGGER.info("Strategy stopped: " + processId);
      try {
         String reportPath = reportRoot + intervalList.get(intervalNr).getName() + ".html";
         final File reportFile = new File(reportPath);
         File reportDir = reportFile.getParentFile();
         if (!reportDir.exists()) {
            reportDir.mkdirs();
         }
         LOGGER.info("reportPath: " + reportPath);
         client.createReport(processId, reportFile);
         intervalNr++;
      } catch (Exception e) {
         LOGGER.error(e.getMessage(), e);
      }
   }

   @Override
   public void onConnect() {
      LOGGER.info("Connected");
   }

   @Override
   public void onDisconnect() {
      // tester doesn't disconnect
   }
});


 
 Post subject: Re: Multi intervals test by using Eclipse Post rating: 0   Post Posted: Mon 19 Sep, 2011, 02:47 

User rating: 0
Joined: Sun 04 Sep, 2011, 12:12
Posts: 2
Location: JapanJapan
Hi, support

I confirmed that your code works.
Thanks a lot for your support.


 

Jump to:  

cron
  © 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