Here is my sample code using JLink.
It's not a foolproof code, but if someone know haw to do better, please let me know.
The following code let Mathematica connect with T3Open Trading Software (Italian Broker WeBank), it expose some api that you can call with tcp socket. In this example I'm going to pass a list of options code (Terna Call) and take the ask/bid price.
Needs["JLink`"];
InstallJava[];
t3OpenRequest [exchange_, market_, code_, schema_] := Module[
{funSub, request, unsub, tcpaddress, tcpport, socket, out, in,
response},
response = {};
funSub = "function=subscribe|item=";
unsub = "function=unsubscribe";
(*ip addess and port where T3Open is running*)
tcpaddress = "192.168.0.78";
tcpport = 5333;
(*build the string request to send*)
request =
StringJoin[{funSub, exchange, ".", market, ".", code, "|schema=",
schema}];
(*create a Socket object*)
socket = JavaNew["java.net.Socket", tcpaddress, tcpport];
out = JavaNew["java.io.PrintWriter", socket@getOutputStream[], True];
in = JavaNew["java.io.BufferedReader",
JavaNew["java.io.InputStreamReader", socket@getInputStream[]]];
(*send the request*)
out@println[request];
(*read the first response*)
in@readLine[];
(*read the second response, here are the data that I need*)
response = Append[response , in@readLine[]];
(*usubscribe the service*)
out@println[unsub];
(*close the socket*)
socket@close[];
Take[Flatten[StringSplit[response, "|"]], -1]
]
To use the function I prepare the options code list:
In[4]:= codeList01 = {{791308., 791309., 791310., 791311., 791312.,
791313., 791314., 791315., 791316., 791317., 791318.,
791319.}, {"TRN3G2.90", "TRN3G3", "TRN3G3.10", "TRN3G3.20",
"TRN3G3.30", "TRN3G3.40", "TRN3G3.50", "TRN3G3.60", "TRN3G3.70",
"TRN3G3.80", "TRN3G3.90", "TRN3G4"}}
Out[4]= {{791308., 791309., 791310., 791311., 791312., 791313.,
791314., 791315., 791316., 791317., 791318., 791319.}, {"TRN3G2.90",
"TRN3G3", "TRN3G3.10", "TRN3G3.20", "TRN3G3.30", "TRN3G3.40",
"TRN3G3.50", "TRN3G3.60", "TRN3G3.70", "TRN3G3.80", "TRN3G3.90",
"TRN3G4"}}
In[5]:= codeList02 = Transpose[codeList01]
Out[5]= {{791308., "TRN3G2.90"}, {791309., "TRN3G3"}, {791310.,
"TRN3G3.10"}, {791311., "TRN3G3.20"}, {791312.,
"TRN3G3.30"}, {791313., "TRN3G3.40"}, {791314.,
"TRN3G3.50"}, {791315., "TRN3G3.60"}, {791316.,
"TRN3G3.70"}, {791317., "TRN3G3.80"}, {791318.,
"TRN3G3.90"}, {791319., "TRN3G4"}}
And here call the function using Table on all options:
In[10]:= data =
Table[t3OpenRequest["MI", "DER", ToString[code], schema], {code,
codeList02[[All, 1]]}, {schema, {"best_ask1", "best_bid1"}}]
Out[10]= {{{"0.0"}, {"0.0"}}, {{"0.1825"}, {"0.1405"}}, {{"0.089"}, \
{"0.0805"}}, {{"0.0325"}, {"0.027"}}, {{"0.0085"}, {"0.004"}}, \
{{"0.003"}, {"0.0005"}}, {{"0.003"}, {"0.0005"}}, {{"0.0015"}, \
{"0.0005"}}, {{"0.0015"}, {"0.0005"}}, {{"0.0"}, {"0.0"}}, {{"0.0"}, \
{"0.0"}}, {{"0.0"}, {"0.0"}}}
In[11]:= data01 = ToExpression[Flatten[data]]
Out[11]= {0., 0., 0.1825, 0.1405, 0.089, 0.0805, 0.0325, 0.027, \
0.0085, 0.004, 0.003, 0.0005, 0.003, 0.0005, 0.0015, 0.0005, 0.0015, \
0.0005, 0., 0., 0., 0., 0., 0.}
Here the couples of ask and bid:
In[12]:= data02 = Partition[data01, 2]
Out[12]= {{0., 0.}, {0.1825, 0.1405}, {0.089, 0.0805}, {0.0325,
0.027}, {0.0085, 0.004}, {0.003, 0.0005}, {0.003, 0.0005}, {0.0015,
0.0005}, {0.0015, 0.0005}, {0., 0.}, {0., 0.}, {0., 0.}}