2010/1/3 bill lam <[email protected]>:
> I guess you should have a loop for send/recv.

In NinjaTrader, OnBarUpdate() is called for each time step (i.e.
effectively inside a loop).

The loop:
// } while (bytes > 0);

to recieve messages in multiple parts where they are too long for the
buffer gets stuck for me, even when I am only sending 15 bytes. I gave
up in the end and decided to comment it out and use a huge buffer.

> Did you try the
> client/server example in J.
I used them to get going. They are very useful (you might notice that
most of the J code is copy pasted from the lab).

I do not know much about socket in c#, is
> it synchronous or asysc ?
The one I am using is sync and blocking. C# sends a message, and waits
until J sends a message back. J waits for a message and sends one back
after processing. It is a bit crap really, I am not good at
programming these types of things - but it works for me for now :-).



>
> sab, 02 Jan 2010, Matthew Brand skribis:
>> I could not get the .NET/COM/DLL to talk to NinjaTrader c# so I am
>> using a socket connection instead.
>>
>> Below is some C# code and some J code which gets NT communicating with
>> J in case anybody wants to do that. I am not a very good programmer so
>> only use with caution, but it works.
>>
>> c#/Ninjatrader:
>>
>> #region Using declarations
>> using System;
>> using System.ComponentModel;
>> using System.Diagnostics;
>> using System.Drawing;
>> using System.Drawing.Drawing2D;
>> using System.Xml.Serialization;
>> using NinjaTrader.Cbi;
>> using NinjaTrader.Data;
>> using NinjaTrader.Indicator;
>> using NinjaTrader.Gui.Chart;
>> using NinjaTrader.Strategy;
>> using System.Net;
>> using System.Net.Sockets;
>> #endregion
>>
>> // This namespace holds all strategies and is required. Do not change it.
>> namespace NinjaTrader.Strategy
>> {
>>     /// <summary>
>>     /// Sends samples to a J server and maintains its required positions.
>>     /// </summary>
>>     [Description("Sendsmessages to J server and outputs replies")]
>>     public class connectJ : Strategy
>>     {
>>         #region Variables
>>                       Socket JSocket = null;
>>                       const string server = "127.0.0.1";
>>                       const int port = 1500;
>>                       const int bufferLength = 1024*10;
>>       #endregion
>>               private string messageJ(string message)
>>               {
>>                       // Send a message to J on server:port and return the 
>> string based output.
>>                       // Algo is: Connect if not connected, send message, 
>> return reply.
>>                       // Socket code based on:
>>                       // 
>> http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.aspx
>>                       bool connected = false;
>>                       if (JSocket == null) {
>>                                       connected = false;
>>                       } else {
>>                               connected = JSocket.Connected;
>>                       }
>>                       if (! connected) {
>>                               Print("Not connected, connecting...");
>>                               IPHostEntry hostEntry = null;
>>                               hostEntry = Dns.GetHostEntry(server);
>>                               foreach(IPAddress address in 
>> hostEntry.AddressList)
>>                               {
>>                                       IPEndPoint ipe = new 
>> IPEndPoint(address, port);
>>                                       Socket tempSocket =
>>                                               new Socket(ipe.AddressFamily, 
>> SocketType.Stream, ProtocolType.Tcp);
>>                                       tempSocket.Connect(ipe);
>>                                       if(tempSocket.Connected)
>>                                       {
>>                                               JSocket = tempSocket;
>>                                               break;
>>                                       }
>>                                       else
>>                                       {
>>                                               continue;
>>                                       }
>>                               }
>>                       }
>>                       Byte[] byData = 
>> System.Text.Encoding.ASCII.GetBytes(message);
>>                       JSocket.Send(byData);
>>                       Byte[] buffer = new Byte[bufferLength];
>>                       string reply = "";
>>                       int bytes = 0;
>>                       // do {
>>                               try {
>>                                       bytes = JSocket.Receive(buffer, 
>> buffer.Length, 0);
>>                       reply = reply +
>> System.Text.Encoding.ASCII.GetString(buffer, 0, bytes);
>>                                       // Print("reply was:" + reply);
>>                               }
>>                               catch (System.Net.Sockets.SocketException ex) {
>>                                       Print("Socket exception on read:" + 
>> ex.SocketErrorCode);
>>                               }
>>
>>                       // } while (bytes > 0); Loop causes a hang. Do not 
>> know why!
>> So made buffer huge.
>>                       return reply;
>>               }
>>
>>
>>               public override void Dispose()
>>               {
>>               // Clean up your resources here
>>                       messageJ("NT closing connection now...");
>>                       
>> JSocket.Shutdown(System.Net.Sockets.SocketShutdown.Both);
>>                       JSocket.Close();
>>               base.Dispose();
>>               }
>>
>>         /// <summary>
>>         /// This method is used to configure the strategy and is
>> called once before any strategy method is called.
>>         /// </summary>
>>         protected override void Initialize()
>>         {
>>             CalculateOnBarClose = true;
>>         }
>>
>>         /// <summary>
>>         /// Called on each bar update event (incoming tick)
>>         /// </summary>
>>         protected override void OnBarUpdate()
>>         {
>>                       Print(messageJ("hello!"));
>>               }
>>
>>         #region Properties
>>         #endregion
>>     }
>> }
>>
>>
>> J:
>>
>> 0 : 0
>> Starts a server ready to accept trading comms from NinjaTrader.
>> )
>>
>>
>> load'socket'
>> coinsert 'jsocket'
>>
>> connect =: 3 : 0
>> NB. close all open sockets
>> sdcleanup ''
>> NB. create a socket to LISTEN
>> SKLISTEN =: 0 pick sdcheck sdsocket''
>> NB. bind an address to the SKLISTEN socket
>> sdcheck sdbind SKLISTEN ; AF_INET_jsocket_ ; '' ; y
>> NB. tell the socket to listen for 1 message at a time
>> sdcheck sdlisten SKLISTEN , 1
>> NB. poll until connection recieved (must be a better way?)
>> smoutput ''
>> smoutput ''
>> smoutput ''
>> smoutput 'Waiting for client to connect...'
>> while. ((3$a:) -: sdc =. sdcheck sdselect'') do. end.
>> smoutput 'Connected.'
>> NB. accept the connection
>> skserver=: 0 pick sdcheck sdaccept SKLISTEN
>> NB. close the listening socket
>> sdcheck sdclose SKLISTEN
>> )
>>
>> messageLoop =: 3 : 0
>> NB. wait for and echo messages
>> NB. if connection drops then try to reestablish
>> msg =. ''
>> while. msg -.@:-: 'exit' do.
>>       try. smoutput msg =. ; sdcheck sdrecv skserver,1000,0
>>       catch.
>>               smoutput 'Client not connected.'
>>               connect 1500 NB. go to reconnect if any problems
>>       end.
>>       try. sdcheck ('You said:', msg) sdsend skserver , 0
>>       catch.
>>               connect 1500
>>       end.
>> end.
>> NB. kill the socket
>> smoutput 'Client sent exit command ... closing socket.'
>> sdcleanup '' NB. only if msg was exit.
>> )
>>
>> messageLoop ''
>>
>> 2009/12/31 bill lam <[email protected]>:
>> [---=| TOFU protection by t-prot: 32 lines snipped |=---]
>
> --
> regards,
> ====================================================
> GPG key 1024D/4434BAB3 2008-08-24
> gpg --keyserver subkeys.pgp.net --recv-keys 4434BAB3
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>



-- 
"There is a 75 per cent chance that Al Gore, during the summer months,
could be completely fact-free within five to seven years.”
http://www.youtube.com/watch?v=X-5XwlcBqF0&feature=player_embedded
http://www.youtube.com/watch?v=t8O-E_GN0Kg&feature=player_embedded

Climate Scientists predicted UK snowfall thing of the past 10 years ago:
http://www.independent.co.uk/environment/snowfalls-are-now-just-a-thing-of-the-past-724017.html
Yet here we are, covered in snow!
Say no to fascism! Say no to the Carbon tax!
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to