I am trying to use an Adaptive Algo from Interactive Brokers. It seems like IBrokers package for R (https://cran.r-project.org/web/packages/IBrokers/IBrokers.pdf - pg37 and 38) was not completed as my order does not go through when I execute the code below.
tws <- twsConnect()
stockEquity <- twsEquity("AAPL")
parentLongId <- reqIds(tws)
parentLongOrder <- twsOrder(parentLongId, action="BUY", totalQuantity = 100, orderType = "MKT", transmit=TRUE,
algoStrategy ="Adaptive", algoParams = "Normal")
I found API Guide on GitHub (http://interactivebrokers.github.io/tws-api/ibalgos.html) for JAVA, Python, C# and C++. I was wondering if anyone knows how to convert the codes into R.
Example of Java,
Order baseOrder = OrderSamples.LimitOrder("BUY", 1000, 1);
AvailableAlgoParams.FillAdaptiveParams(baseOrder, "Normal");
client.placeOrder(nextOrderId++, ContractSamples.USStockAtSmart(), baseOrder);
public static void FillAdaptiveParams(Order baseOrder, String priority) {
baseOrder.algoStrategy("Adaptive");
baseOrder.algoParams(new ArrayList<>());
baseOrder.algoParams().add(new TagValue("adaptivePriority", priority));
}
I think the easiest way for R users to access all of IBALGO is to install the Reticulate package which allows you to use Python in R. Then install ib_insync Python module. Now you can use R to manage IB API in almost every way as if working within its native Python thanks to Reticulate.
Just remember that the syntax to use the equivalent of Python's TagValue translate in R looks like this:
algoStrategy = 'Adaptive',
algoParams = list(insync$TagValue('adaptivePriority', 'Normal')))
Related
I use Twitter anomaly detection algorithm in my project. For this purpose I use Rserve library to run R code in my Java application.
My Java code:
RConnection connection = new RConnection();
connection.voidEval("library(AnomalyDetection)");
connection.eval("res <- AnomalyDetectionTs(data.frame(/*list of timestamps*/,/*list of values*/), direction='both', plot=FALSE, longterm=TRUE)");
And, as a result, I got this output:
$anoms
timestamp anoms
1 1980-09-25 16:05:00 21.3510
2 1980-09-29 06:40:00 193.1036
3 1980-09-29 21:44:00 148.1740
To get results now I'm using this not nice solution:
connection.eval("write.csv(res[['anoms']],file='anom.csv')");
Then I open this file in Java and parse the results.
So, how to get the output results in Java using Rserve possibilities for data.frame structure?
Simply write the R command such that it returns the desired result back to Java:
RList l = c.eval("AnomalyDetectionTs(data, direction='both',
plot=FALSE, longterm=TRUE)$anoms").asList();
What you get is the data frame (as a list) with the two variables timestamp and anoms.
However, AnomalyDetectionTs returns dates in a horribly annoying and inefficient format so you may want to actually return a more sane result which is easier to work with in Java, e.g.:
RList l = c.eval("{ res <- AnomalyDetectionTs(data, direction='both', plot=FALSE,
longterm=TRUE)$anoms;
list(as.POSIXct(res$timestamp), res$anoms) }").asList();
double ts[] = l.at(0).asDoubles();
double anom[] = l.at(1).asDoubles();
for (int i = 0; i < ts.length; i++)
System.out.println(new java.util.Date((long)(ts[i]*1000.0)) + ": " + anom[i]);
PS: the right place for Rserve questions is the stats-rosuda-devel mailing list which will give you answers much faster.
I'm trying to create a program to compare the amount of time it takes various haskell scripts to run, which will later be used to create graphs and displayed in a GUI. I've tried to create said GUI using Haskell libraries but I haven't had much luck, especially since I'm having trouble finding up to date GUI libraries for Windows. I've tried to use Java to get these results but either get errors returned or simply no result.
I've constructed a minimal example to show roughly what I'm doing at the moment:
import java.io.*;
public class TestExec {
public static void main(String[] args) {
try {
Process p = Runtime.getRuntime().exec("ghc test.hs 2 2");
BufferedReader in = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
And here is the Haskell script this is calling, in this case a simple addition:
test x y = x + y
Currently there simply isn't any result stored or printed. Anyone have any ideas?
Since you're attempting to run this as an executable, you need to provide a main. In you're case it should look something like
import System.Environment
test :: Integer -> Integer -> Integer
test = (+)
main = do
[x, y] <- map read `fmap` getArgs
print $ x `test` y
This just reads the command line arguments, adds them, then prints them. Though I did something like a while ago, it's much easier to do the benchmarking/testing in Haskell, and dump the output data to a text file in a more structured format, then parse/display it in Java or whatever language you like.
This is mostly a Java question. Search for Runtime.getRuntime().exec().
On the Haskell side, you need to write a stand-alone Haskell script. The one by #jozefg is OK. You should be able to run it as
runghc /path/to/script.hs 1 2
from the command line.
Calling it from Java is no different than running any other external process in Java. In Clojure (a JVM language, I use it for brevity) I do:
user=> (def p (-> (Runtime/getRuntime) (.exec "/usr/bin/runghc /tmp/test.hs 1 2")))
#'user/p
user=> (-> p .getInputStream input-stream reader line-seq)
("3")
Please note that I use runghc to run a script (not ghc). Full paths are not necessary, but could be helpful. Your Java program can be modified this way:
--- TestExec.question.java
+++ TestExec.java
## -2,7 +2,7 ##
public class TestExec {
public static void main(String[] args) {
try {
- Process p = Runtime.getRuntime().exec("ghc test.hs 2 2");
+ Process p = Runtime.getRuntime().exec("/usr/bin/runghc /tmp/test.hs 2 2");
BufferedReader in = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line = null;
The modified version runs the Haskell script just fine. You may have to change paths to you runghc and test.hs locations.
At first to read from output you need to use OutputStreamReader(p.getOutputStream()) instead of InputStreamReader
As I said in comment such a benchmark is simply incorrect. While benchmarking one should eliminate as many side coasts as possible. The best solution is to use the criterion package. It produces nice graphical output as you desire.
Small example:
import Criterion
import Criterion.Main
import Criterion.Config
fac 1 = 1
fac n = n * (fac $ n-1)
myConfig = defaultConfig {
cfgReport = ljust "report.html"
}
main = defaultMainWith myConfig (return ()) [
bench "fac 30" $ whnf fac 30
]
After execution it produces a file "report.html" with neat interactive plots.
I have a problem in using the apache commons math library.
I just want to create functions like f(x) = 4x^2 + 2x and I want to compute the derivative of this function --> f'(x) = 8x + 2
I read the article about Differentiation (http://commons.apache.org/proper/commons-math/userguide/analysis.html, section 4.7).
There is an example which I don't understand:
int params = 1;
int order = 3;
double xRealValue = 2.5;
DerivativeStructure x = new DerivativeStructure(params, order, 0, xRealValue);
DerivativeStructure y = f(x); //COMPILE ERROR
System.out.println("y = " + y.getValue();
System.out.println("y' = " + y.getPartialDerivative(1);
System.out.println("y'' = " + y.getPartialDerivative(2);
System.out.println("y''' = " + y.getPartialDerivative(3);
In Line 5 a compile error occurs of course. The function f(x) is called and not defined. What I am getting wrong?
Has anyone any experience with the differentiation/derivation with the apache commons math library or does anyone know another library/framework which can help me?
Thanks
In the paragraph below that example, the author describes ways to create DerivativeStructures. It isn't magic. In the example you quoted, someone was supposed to write the function f. Well, that wasn't very clear.
There are several ways a user can create an implementation of the UnivariateDifferentiableFunction interface. The first method is to simply write it directly using the appropriate methods from DerivativeStructure to compute addition, subtraction, sine, cosine... This is often quite straigthforward and there is no need to remember the rules for differentiation: the user code only represent the function itself, the differentials will be computed automatically under the hood. The second method is to write a classical UnivariateFunction and to pass it to an existing implementation of the UnivariateFunctionDifferentiator interface to retrieve a differentiated version of the same function. The first method is more suited to small functions for which user already control all the underlying code. The second method is more suited to either large functions that would be cumbersome to write using the DerivativeStructure API, or functions for which user does not have control to the full underlying code (for example functions that call external libraries).
Use the first idea.
// Function of 1 variable, keep track of 3 derivatives with respect to that variable,
// use 2.5 as the current value. Basically, the identity function.
DerivativeStructure x = new DerivativeStructure(1, 3, 0, 2.5);
// Basically, x --> x^2.
DerivativeStructure x2 = x.pow(2);
//Linear combination: y = 4x^2 + 2x
DerivativeStructure y = new DerivativeStructure(4.0, x2, 2.0, x);
System.out.println("y = " + y.getValue());
System.out.println("y' = " + y.getPartialDerivative(1));
System.out.println("y'' = " + y.getPartialDerivative(2));
System.out.println("y''' = " + y.getPartialDerivative(3));
The following thread from the Apache mailing list seems to illustrate the two possible ways of how the derivative of a UnivariateDifferentiableFunction can be defined. I am adding a new answer as I'm unable to comment on the previous one (insufficient reputation).
The used sample specification of the function is f(x) = x^2.
(1) Using a DerivativeStructure:
public DerivativeStructure value(DerivativeStructure t) {
return t.multiply(t);
}
(2) By writing a classical UnivariateFunction:
public UnivariateRealFunction derivative() {
return new UnivariateRealFunction() {
public double value(double x) {
// example derivative
return 2.*x;
}
}
}
If I understand well, the advantage of the first case is that the derivative does not need to be obtained manually, as in the second case. In case the derivative is known, there should thus be no advantage of defining a DerivativeStructure, right? The application I have in mind is that of a Newton-Raphson solver, for which generally the function value and its derivative need to be known.
The full example is provided on the aforementioned web site (authors are Thomas Neidhart and Franz Simons). Any further comments are most welcome!
I have a linear problem modelled in IBM ILOG CPLEX Optimization Studio, that returns correct solutions, i.e. objective values.
For simulation purposes I use an ILOG model model file and a data file that I both call from java:
IloOplFactory.setDebugMode(false);
IloOplFactory oplF = new IloOplFactory();
IloOplErrorHandler errHandler = oplF.createOplErrorHandler(System.out);
IloOplModelSource modelSource = oplF.createOplModelSource("CDA_Welfare_Examination_sparse2.mod");
IloCplex cplex = oplF.createCplex();
IloOplSettings settings = oplF.createOplSettings(errHandler);
IloOplModelDefinition def=oplF.createOplModelDefinition(modelSource,settings);
IloOplModel opl=oplF.createOplModel(def,cplex);
String inDataFile = path;
IloOplDataSource dataSource=oplF.createOplDataSource(inDataFile);
opl.addDataSource(dataSource);
opl.generate();
opl.convertAllIntVars(); // converts integer bounds into LP compatible format
if (cplex.solve()){
}
else{
System.out.println("Solution could not be achieved, probably insufficient memory or some other weird problem.");
}
Now, I would like to access the actual decision variable match[Matchable] from java.
In ILOG CPLEX Optimization Studio I use the following nomenclatura:
tuple bidAsk{
int b;
int a;
}
{bidAsk} Matchable = ...;
dvar float match[Matchable];
In Java I access the objective value in the following way (which works fine):
double sol = new Double(opl.getSolutionGetter().getObjValue());
Now, how do I access the decision variable "match"? So far I have started with
IloOplElement dVarMatch = opl.getElement("match");
but I can't seem to get any further. Help is very much appreciated! Thanks a lot!
You're on the right track. You need to get tuples which represent each valid bidAsk in Matchable, then use the tuple as an index into the decision variable object. Here's some sample code in Visual Basic (what I happen to be writing in right now, should be easy to translate to java):
' Get the tuple set named "Matchable"
Dim matchable As ITupleSet = opl.GetElement("Matchable").AsTupleSet
' Get the decision variables named "match"
Dim match As INumVarMap = opl.GetElement("match").AsNumVarMap
' Loop through each bidAsk in Matchable
For Each bidAsk As ITuple In matchable
' This is the current bidAsk's 'b' value
Dim b As Integer = bidAsk.GetIntValue("b")
' This is the current bidAsk's 'a' value
Dim a As Integer = bidAsk.GetIntValue("a")
' this is another way to get bidAsk.b and bidAsk.a
b = bidAsk.GetIntValue(0)
a = bidAsk.GetIntValue(1)
' This is the decision variable object for match[<b,a>]
Dim this_variable As INumVar = match.Get(bidAsk)
' This is the value of that decision variable in the current solution
Dim val As Double = opl.Cplex.GetValue(this_variable)
Next
You can get the variable values through the IloCplex-Object like that:
cplex.getValue([variable reference]);
I never imported a model like that. When you create the model in java, references to the decision variables are easily at hand, but there should be a way to obtain the variables. You could check the documentation:
cplex docu
This question already has answers here:
How to evaluate a math expression given in string form?
(26 answers)
Closed 6 years ago.
Is there a way in Java to get the result from this mathematical expression:
String code = "5+4*(7-15)";
In other hand what's the best way to parse an arithmetic expression?
You can pass it to a BeanShell bsh.Interpreter, something like this:
Interpreter interpreter = new Interpreter();
interpreter.eval("result = 5+4*(7-15)");
System.out.println(interpreter.get("result"));
You'll want to ensure the string you evaluate is from a trusted source and the usual precautions but otherwise it'll work straight off.
If you want to go a more complicated (but safer) approach you could use ANTLR (that I suspect has a math grammar as a starting point) and actually compile/interpret the statement yourself.
i recently developed a expression parser and released it under the apache license. you can grab it at http://projects.congrace.de/exp4j/index.html
hope that helped
You can use the ScriptEngine class and evaluate it as a javascript string
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
Object result = engine.eval("5+4*(7-15)");
Indeed , yu should know that the result of the following instruction in javascript :
eval('var aa=5+4*(7-15)')
aa // -27
There may be a better way, but this one works.
Probably not in as straight forward a manner as you are hoping!
But perhaps you could use a javax.script.ScriptEngine and treat the string as a ECMAScript expression, for example?
Take a look at: Scripting for the Java Platform.
Recently I was using very mature math expression parser library, open source, giving the same API for JAVA and .NET. The library name is mXparser. mXparser provides basic functionalities (simple formulas parsing and calculation) and more advanced ones (i.e. user defined arguments, functions). Additionally it is worth to notice that mXparser has rich built-in math collection (meaning operators, unary / binary / variadic functions, iterated operators such as summation and product).
https://mathparser.org/
https://mathparser.org/mxparser-tutorial/
Please find below a few examples to have more clear view on the syntax.
Example 1 - simple formula
Expression e = new Expression("2+3");
double v = e.calculate();
Example 2 - built-in function
Expression e = new Expression("2+sin(3)");
double v = e.calculate();
Example 3 - built-in constants
Expression e = new Expression("2+sin(pi)");
double v = e.calculate();
Example 4 - user defined arguments and constants
Argument x = new Argument("x = 5");
Constant a = new Constant("a = 2 + sin(3)");
Expression e = new Expression("a + x^2", x, a);
double v1 = e.calculate();
x.setArgumentValue(10);
double v2 = e.calculate();
Example 5 - user defined functions
Function f = new Function("f(x,y) = x^2 + cos(y)");
Expression e = new Expression("f(10,pi) - 3", f);
double v = e.calculate();
Example 6 - user defined recursion
Function factorial = new Function("fact(n) = if( n > 0; n*fact(n-1); 1)");
Expression e = new Expression("fact(10) - 10!", factorial);
double v = e.calculate();
Found recntly - in case you would like to try the syntax (and see the advanced use case) you can download the Scalar Calculator app that is powered by mXparser.
Best regards
LK
There is no builtin way of doing that. But you can use one of the many many open source calculators available.
There is no direct support in the Java SDK for doing this.
You will either have to implement it yourself (possibly using a parser generator such as JavaCC), or use an existing library.
One option would be JEP (commercial), another JEval (free software).
You coul use that project
How to use:
double result = 0;
String code = "5+4*(7-15)";
try {
Expr expr = Parser.parse(code);
result = expr.value();
} catch (SyntaxException e) {
e.printStackTrace();
}
System.out.println(String.format("Result: %.04f", result));
There's an open-source tool called formula4j that does that job.
To take your example expression, it would be evaluated like this using formula4j:
Formula formula = new Formula("5+4*(7-15)");
Decimal answer = formula.getAnswer(); //-27
public static int calc(String string){
int result=0;
String numbers="0123456789";
String operations="+-/*";
for (int i=0;i<string.length();i++){
if (numbers.contains(string.charAt(i)+"")){
result=result*10+(Integer.parseInt(string.charAt(i)+""));
}
else {
if (string.charAt(i)=='+'){ result+=calc(string.substring(i+1));}
if (string.charAt(i)=='-'){ result-=calc(string.substring(i+1));}
if (string.charAt(i)=='*'){ result*=calc(string.substring(i+1));}
if (string.charAt(i)=='/'){ try{result/=calc(string.substring(i+1));}
catch (ArithmeticException e){
System.err.println("You cannot devide by Zero!");}
}
break;
}
}
return result;
}