Move OANDA account info into separate file

This commit is contained in:
2018-09-06 16:06:50 -07:00
parent f9dd57c0e5
commit 45a215c15b
7 changed files with 85 additions and 30 deletions

View File

@ -1,16 +0,0 @@
public class AccountInfo {
// TODO use properties file
public String accountID() {
return "101-001-7935538-001";
}
public boolean isLiveAccount() {
return false;
}
public String accessToken() {
return "9a480f0b83e987f4015cf0846790c7d9-695ced635526744abd61bdf0e2ae8b71";
}
}

View File

@ -48,11 +48,7 @@ public class App {
String csv = cmdline.getOptionValue("historical");
reader = new CSVReader(new File(csv));
log.info("Using CSV file {}", csv);
} else {
// TODO: separate
AccountInfo acctInfo = new AccountInfo();
String[] instruments = getInstruments(cmdline);
reader = new OANDAReader(acctInfo, instruments);
log.info("Reading from stream.");
}
@ -122,4 +118,6 @@ public class App {
formatter.printHelp("java -jar ATS_Esper-all.jar [args] rules.epl", options);
System.exit(1);
}
Config config = new Config();
reader = new OANDAReader(config, instruments);
}

57
src/main/java/Config.java Normal file
View File

@ -0,0 +1,57 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Config provides access to runtime configuration properties defined
* in a Java properties file. The file is located in
* "config/config.properties" relative to the installation root.
*/
public class Config {
static final Logger log = LoggerFactory.getLogger("Config");
private Properties properties = new Properties();
/**
* Load the configuration properties file.
*/
public Config() {
try {
String cwd = System.getProperty("user.dir");
String configFile = cwd + File.separator + "config" +
File.separator + "config.properties";
log.info("Loading config file " + configFile);
properties.load(new FileInputStream(configFile));
} catch (IOException e) {
log.error("Error loading config.properties", e);
System.exit(1);
}
}
/**
* Return the OANDA account id.
*/
public String accountID() {
return properties.getProperty("OANDA.accountID");
}
/**
* Return true if the OANDA account is on the live ('fxlive') site.
* Return false if it is on the practice ('fxpractice') site.
*/
public boolean isLiveAccount() {
return Boolean.parseBoolean(properties.getProperty("OANDA.isLiveAccount"));
}
/**
*
*/
public String accessToken() {
return properties.getProperty("OANDA.accessToken");
}
}

View File

@ -21,13 +21,13 @@ import org.slf4j.LoggerFactory;
public class OANDAReader implements TickStreamReader {
final Logger log = LoggerFactory.getLogger(OANDAReader.class);
ObjectMapper mapper;
AccountInfo accountInfo;
String[] instruments;
Config config;
public OANDAReader(AccountInfo accountInfo, String[] instruments) {
this.accountInfo = accountInfo;
this.instruments = instruments;
public OANDAReader(Config config, String[] instruments) {
this.config = config;
this.instruments = instruments;
mapper = new ObjectMapper();
mapper.registerModule(new JodaModule());
@ -38,7 +38,6 @@ public class OANDAReader implements TickStreamReader {
}
private String streamURL() {
String type = accountInfo.isLiveAccount() ? "fxlive" : "fxpractice";
String instrList = String.join(",", instruments);
String query = "";
try {
@ -46,10 +45,11 @@ public class OANDAReader implements TickStreamReader {
} catch (UnsupportedEncodingException e) {
log.error("Creating stream URL", e);
}
String type = config.isLiveAccount() ? "fxlive" : "fxpractice";
return String.format("https://stream-%s.oanda.com/v3/accounts/%s/pricing/stream?%s",
type, accountInfo.accountID(), query);
}
return String.format("https://stream-%s.oanda.com/v3/accounts/%s/pricing/stream?%s",
type, config.accountID(), query);
}
private void readConnection(String urlStr, TickProcessor processor) {
HttpsURLConnection httpConn = null;
@ -65,8 +65,8 @@ public class OANDAReader implements TickStreamReader {
//httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
//httpConn.setReadTimeout(50 * 1000);
httpConn.setRequestProperty("Authorization", "Bearer 9a480f0b83e987f4015cf0846790c7d9-695ced635526744abd61bdf0e2ae8b71");
BufferedReader is =
httpConn.setRequestProperty("Authorization", "Bearer " + config.accessToken());
new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
while ((line = is.readLine( )) != null) {