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.

Read Configurable default from file
 Post subject: Read Configurable default from file Post rating: 0   New post Posted: Mon 22 Dec, 2008, 14:00 

User rating: 0
Joined: Mon 22 Dec, 2008, 13:00
Posts: 17
Hi, I need a little help with trying to initialize a Configurable factor from a file. Let's call it defaultPath. I want to be able to read this configurable factor from a file beforehand. In my code, defaultPath is supposed to be a path to a settings file (which by default is to be written in C:\temp). It works fine if I set it to a string but I want to give the user the flexibility to enter a different path. This user defined path should be written to a file in the My Strategies folder, so that the next time the strategy is run, the configurable defaultPath should show what the user entered previously.

In other words, I need to be able to read the defaultPath string from a file (which will be found in the My Strategies folder) instead of hard-coding it. The problem is if i try to check if the file exists, i get the error "Cannot instantiate target class". The error seems to occur at the line marked in red. I'd really appreciate it if someone could please take a look at the code below; I can't figure out what I'm doing wrong:

...........
...........
@RequiresFullAccess

public class Strategy1 implements IStrategy {
private IContext context = null;
private IEngine engine = null;
private IConsole console = null;

@Configurable("Enter the path to the settings file")
//public String defaultPath = "C:\\temp"; // this is the default path
public String defaultPath = GetPath(); // try to get the user defined path from the file path.txt

//----------------------------------------------------------
public void onStart(IContext context) throws JFException {

}
//----------------------------------------------------------
public String GetPath() {

File StrategyFolder = getMyStrategiesDir(); //returns the full path to My Strategies folder inside My Documents
String strStratFolder = StrategyFolder.toString();
//print(strStratFolder);

String tmpPath = "C:\\temp";
File pathFile = new File(strStratFolder, "path.txt"); //should give the full path to the file in My Strategies
if (pathFile.exists()) {

try {
//print("path file exists");
FileReader fr = new FileReader(pathFile);
BufferedReader br = new BufferedReader(fr);
tmpPath = br.readLine();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
} else {
//print("path file does not exist");
}
return tmpPath;
}

......................
......................
......................
......................
}


 
 Post subject: Re: Need help - read Configurable default from file Post rating: 0   New post Posted: Wed 24 Dec, 2008, 10:25 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
After compiler compiles class, it checks that the class implements IStrategy interface and has default constructor. To do so, it instantiates your class and does instanceof. The problem is, that at the moment when class is instantiated it fills all variables and calls GetPath() method to initialize defaultPath and does so with standard permissions. To fix this, you can change path.txt to "files/path.txt". It will work because you have access to the files directory by default. Or you can use variable of type File, then you will have button to select path to file in files selection dialog
package jforex;

import java.util.*;
import java.io.*;
import javax.swing.filechooser.*;

import com.dukascopy.api.*;

@RequiresFullAccess
public class GetPathTest implements IStrategy {
   @Configurable("Enter the path to the settings file")
   public String defaultPath = GetPath(); // try to get the user defined path from the file path.txt

   /*
    * Will have button to open file selection dialog
    */
   @Configurable("My file")
   public File myFile;
   
   /*
    * All txt files in My Strategies\files directory
    */
   @Configurable(value="My txt files", fileType="*.txt")
   public File myTxtFiles;
   
   public void onStart(IContext context) throws JFException {
   }

   public void onAccount(IAccount account) throws JFException {
   }

   public void onMessage(IMessage message) throws JFException {
   }

   public void onStop() throws JFException {
   }

   public void onTick(Instrument instrument, ITick tick) throws JFException {
   }
   
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
    }
   
    public String GetPath() {
      File StrategyFolder = getMyStrategiesDir(); //returns the full path to My Strategies folder inside My Documents
      String strStratFolder = StrategyFolder.toString();
      //print(strStratFolder);

      String tmpPath = "C:\\temp";
      File pathFile = new File(strStratFolder, "files/path.txt"); //should give the full path to the file in My Strategies\files
      if (pathFile.exists()) {
         try {
            //print("path file exists");
            FileReader fr = new FileReader(pathFile);
            BufferedReader br = new BufferedReader(fr);
            tmpPath = br.readLine();
            fr.close();
         } catch (IOException e) {
            e.printStackTrace();
         }
      } else {
         //print("path file does not exist");
      }
      return tmpPath;
   }
   
    public static File getMyStrategiesDir() {
        File myDocs = FileSystemView.getFileSystemView().getDefaultDirectory();
        File dir = new File(myDocs.getPath() + File.separator + "My Strategies");
        return dir;
    }   
}


 

Jump to:  

  © 1998-2024 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