import java.util.*;
/*
* The static import declaration is analogous to the normal import declaration.
* Where the normal import declaration imports classes from packages, allowing
* them to be used without package qualification, the static import declaration
* imports static members from classes, allowing them to be used without class
* qualification.
*/
/*
* So when should you use static import? Very sparingly! Only use it when you'd
* otherwise be tempted to declare local copies of constants, or to abuse
* inheritance (the Constant Interface Antipattern). In other words, use it when
* you require frequent access to static members from one or two classes. If you
* overuse the static import feature, it can make your program unreadable and
* unmaintainable, polluting its namespace with all the static members you import.
* Readers of your code (including you, a few months after you wrote it) will not
* know which class a static member comes from. Importing all of the static members
* from a class can be particularly harmful to readability; if you need only one or
* two members, import them individually. Used appropriately, static import can make
* your program more readable, by removing the boilerplate of repetition of class names.
* */
import static org.fusesource.jansi.Ansi.*;
import static org.fusesource.jansi.Ansi.Color.*;
import java.util.logging.*;
import java.util.Formatter;
class AlgoInteractiveBrokersShellEvents implements AlgoEvents {
public void historicalDataRetrieved(AlgoContract contract, Map<String, AlgoTechnicalOutput> technicals) {
//AlgoInteractiveBrokersShell.l.info("Data for contract " + contract.m_symbol + "." + contract.m_currency);
AlgoInteractiveBrokersShell.l.info("=============================================");
for (Map.Entry<String, AlgoTechnicalOutput> entry : technicals.entrySet()) {
String key = entry.getKey();
AlgoTechnicalOutput values = entry.getValue();
Double techValue = values.ret.get(values.ret.size() - 1);
int period = values.period;
Color color = BLUE;
String info = "";
if (key == "rsi") {
if (techValue >= 70 || techValue <= 30) {
color = RED;
}
} else if (key == "adx") {
info = " Non-Trend";
if (techValue > 25) {
color = GREEN;
info = " Trending";
} else if (techValue < 20) {
color = RED;
}
}
String symbol = contract.m_symbol + "." + contract.m_currency;
Formatter fmt = new Formatter();
AlgoInteractiveBrokersShell.l.info(
fmt.format("%s %s %s %s",
symbol,
ansi().fg(WHITE).a(key).reset() + "-" + period,
ansi().fg(color).a(techValue).reset(),
info).toString()
);
}
}
}
public class AlgoInteractiveBrokersShell {
private static IB ib = new IB();
private static List<AlgoContract> contracts = new ArrayList<AlgoContract>();
public static Logger l = Logger.getLogger("nameless");
public static void analyzeForex() {
String[] cts = {
"EUR.USD",
"GBP.USD",
"AUD.USD",
"NZD.USD",
"USD.CAD",
"USD.JPY",
"AUD.JPY",
"EUR.JPY",
"GBP.JPY",
"NZD.JPY",
"CAD.JPY",
"EUR.CHF",
"GBP.CHF",
"AUD.CHF",
"NZD.CHF",
"CAD.CHF"
};
for (String contract: cts) {
AlgoInteractiveBrokersShell.l.info(ansi().fg(WHITE).a("Analyzing ").reset() + "" + ansi().fg(GREEN).a(contract).reset());
String[] parts = contract.split("\\.");
contracts.add(AlgoContract.createFx(ib, parts[0], parts[1]));
}
AlgoInteractiveBrokersShellEvents events = new AlgoInteractiveBrokersShellEvents();
AlgoData data = new AlgoData(ib, events);
for (int i = 0; i < contracts.size(); i++) {
contracts.get(i).historicalBarSize = "1 day";
contracts.get(i).historicalDuration = "1 Y";
data.historicalData(contracts.get(i));
}
}
public static void main(String[] args) throws Exception {
/* Change console logging level in logging.properties */
l.setLevel(Level.ALL);
l.info("Trading shell started");
ib.connect("127.0.0.1",4001,1);
analyzeForex();
}
}