Why is this example does not work (JForex can not compare a string)
package jforex;
import java.util.*;
import java.awt.Color;
import java.awt.List;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.Writer;
import java.lang.reflect.Array;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Set;
import javax.net.ssl.HttpsURLConnection;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.net.MalformedURLException;
import java.io.IOException;
import java.io.*;
import java.sql.Timestamp;
import java.util.Date;
import javax.swing.JFrame;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import com.dukascopy.api.*;
import com.dukascopy.api.IEngine.OrderCommand;
import com.dukascopy.api.IMessage.Type;
@RequiresFullAccess
public class Strategy implements IStrategy {
private IEngine engine;
private IConsole console;
private IHistory history;
private IContext context;
private IIndicators indicators;
private IUserInterface userInterface;
public void onStart(IContext context) throws JFException {
this.engine = context.getEngine();
this.console = context.getConsole();
this.history = context.getHistory();
this.context = context;
this.indicators = context.getIndicators();
this.userInterface = context.getUserInterface();
Set<Instrument> instruments = new HashSet<Instrument>();
instruments.add(Instrument.GBPJPY);
instruments.add(Instrument.GBPUSD);
instruments.add(Instrument.EURJPY);
instruments.add(Instrument.EURUSD);
instruments.add(Instrument.USDJPY);
instruments.add(Instrument.USDJPY);
context.setSubscribedInstruments(instruments, true);
}
public void onAccount(IAccount account) throws JFException {
}
public void onMessage(IMessage message) throws JFException {
String[][] data = new String[100][10];
try {
int xx = 0;
String onePos = "";
for(IOrder o : engine.getOrders()){
if(o.getProfitLossInUSD() != 987654231){
onePos = onePos + "" + o.getInstrument() + "#" + o.getOrderCommand().isLong() + "#" + o.getAmount() + "#" + o.getId() + "#" + o.getFillTime() + "#" + + o.getOpenPrice() + "ZZZ";
//console.getOut().println(onePos);
}
}
saveToFile(onePos);
logToFile(onePos);
} catch (Exception e) {
console.getOut().println(e.getMessage());
// context.stop();
}
}
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 {
String sCurrentLine = "";
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("d:\\positions.db"));
while ((sCurrentLine = br.readLine()) != null) {
// log file operation get timestamp
java.util.Date date= new java.util.Date();
// time to string
String time = new Timestamp(date.getTime()).toString();
console.getOut().println("From file : " + sCurrentLine + "ZZZ" + time);
br.close();
break;
}
} catch (IOException e) {
console.getOut().println(e);
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
console.getOut().println(ex);
}
}
double price = 0;
int slippage = 2;
String posCurr = "";
String posLong = "";
String[][] out = new String [10][6];
// Booooooo
// split line to array
if(sCurrentLine != ""){
// line back to array
String[] xline = sCurrentLine.split("ZZZ");
//console.getOut().println(Arrays.deepToString(xline));
int ss = 1;
for (String str : xline) {
out[ss] = (String[]) str.split("#");
console.getOut().println("string line>>>" + str);
ss++;
//console.getOut().println(Arrays.deepToString(out));
}
}
//======================================================================================================== GBPJPY
try{
for(int i = 0; i < out.length; i++){
String ff = out[i][0];
//console.getOut().println(ff);
String nn = "null";
if(out[i][0] != null){
console.getOut().println(out[i][0]);
// sprawdzić czy istnieje w pliku z juz kiedyś otwartymi
console.getOut().println("SET POSITION");
String sd = out[i][0];
console.getOut().println(sd);
///?????????????????????????????????????????????????????????????????????????????????????????????????????
if(sd == "GBP/USD"){
if(out[i][0] == "true"){
// label p + time nano
//engine.submitOrder("label1", Instrument.GBPUSD, OrderCommand.BUY, 0.1, price, slippage);
console.getOut().println("nnnnnnnnnnnnnnnnnnnnnnnnn");
console.getOut().println("OPEN BUY" + out[i][0] + "BUY" + out[i][2] + "" + out[i][3]);
}else{
//engine.submitOrder("label2", Instrument.GBPUSD, OrderCommand.SELL, 0.1, price, slippage);
console.getOut().println("nnnnnnnnnnnnnnnnnnnnnnnnn");
console.getOut().println("OPEN SELL" + out[i][0] + "SELL" + out[i][2] + "" + out[i][3]);
}
}
//======================================================================================================== GBPJPY
}
}
}catch(Exception e){
console.getOut().println(e);
}
}
// Save to file
private void saveToFile(String txt){
try{
String Path = "d:\\positions.db";
// log file operation get timestamp
java.util.Date date= new java.util.Date();
//System.out.println(new Timestamp(date.getTime()));
// time to string
String time = new Timestamp(date.getTime()).toString();
// log file catch all response
PrintWriter log = new PrintWriter(new BufferedWriter(new FileWriter(Path)));
log.println(txt.toString());
log.close();
//print position
console.getOut().println("Save to file : " + txt.toString());
}catch(Exception ss)
{
console.getOut().println(ss);
}
}
// Log to file
private void logToFile(String txt){
try{
String Path = "d:\\logpositions.db";
// log file operation get timestamp
java.util.Date date= new java.util.Date();
//System.out.println(new Timestamp(date.getTime()));
// time to string
String time = new Timestamp(date.getTime()).toString();
// log file catch all response
PrintWriter log = new PrintWriter(new BufferedWriter(new FileWriter(Path, true)));
log.println(txt.toString()+ "ZZZ" + time);
log.close();
//print position
console.getOut().println("Save to log : " + txt.toString() + "ZZZ" + time);
}catch(Exception ss)
{
console.getOut().println(ss);
}
}
private int fileExist(String Path){
File f = new File(Path);
if(f.exists()){
return 1;
}else{
return 0;
}
}
private void set(String posCurr, String posLong, String posVolume, String posId){
//======================================================================================================== GBPJPY
if(posCurr == "GBP/USD"){
if(posLong == "true"){
// label p + time nano
//engine.submitOrder("label1", Instrument.GBPUSD, OrderCommand.BUY, 0.1, price, slippage);
console.getOut().println("nnnnnnnnnnnnnnnnnnnnnnnnn");
console.getOut().println("OPEN BUY" + posCurr + "BUY" + posVolume + "" + posId);
}else{
//engine.submitOrder("label2", Instrument.GBPUSD, OrderCommand.SELL, 0.1, price, slippage);
console.getOut().println("OPEN SELL " + posCurr + " SELL " + posVolume + " " + posId);
}
}
//======================================================================================================== GBPJPY
}
}// end
Best Regards
zix