Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I'd like to connect Mathematica with my Italian electronic trading platform.

To connect I need to open a TCP socket client towards the trading platform and send a string command.

The high-level description (pseudo code) is like this:

- create new tcp socket on hostname="localhost and port ="5333"
- create out channel
- create input channel

- send request in string format 
   "function=subscribe|item=<stockCode>|schema=ask1;bid1;last_price"

- read response: if response is like outcom=OK then wait the push update

- read push update  

- close socket

But how I can do this with Mathematica?

Using JLink I should write something like this:

socket = new Socket(TCP_address, TCP_port);
out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(),
"UTF-8"));
in = new BufferedReader(new InputStreamReader(socket.getInputStream(),
"UTF-8"));
//send request to T3 .
out.println(messagge);
//Wait response 
String response = in.readLine();
//[...]
//If response is outcome=OK wait push data
while(true){
String update = in.readLine();
//Thread to manage push data
}
} catch (UnknownHostException e) {
//[...]
} catch (IOException e) {
//[...]
}
//[...]

But I'd like to know if is possible to avoid java and .net !

share|improve this question
    
What do you mean by "create out channel" and "create input channel"? Is this something different than what one gets anyway with a TCP socket? –  Oleksandr R. Jun 12 '13 at 11:08
    
@OleksandrR. -- Often in these types of applications one may want high speed throughput, so one doesn't want two way traffic on a TCP socket. –  Jagra Jun 12 '13 at 11:34
    
This sounds like a job for JLink –  Jagra Jun 12 '13 at 11:35
    
@Jagra that is fair enough, but really my point was that pseudocode ought to provide a precise description of how to do something, not just a broad overview of what is desired. Since we don't know anything about what these channels are specifically, it's not really feasible to say how to create them using Mathematica. For all we know they could be messages passed over a Myrinet connection that need to contain timestamps accurate to the nanosecond. While this is possible, any practical implementation will have to explicitly take it into account. –  Oleksandr R. Jun 12 '13 at 11:44
    
So the only way is to use java library or .Net library ? –  msalese Jun 12 '13 at 14:19

1 Answer 1

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.}}
share|improve this answer
    
now that you proofed that this is working it is maybe time to clean up your code a bit. avoid magic numbers inside your function (use options instead) and maybe move the whole thing into a package. –  Stefan Jun 28 '13 at 13:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.