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]>:
> ĵaŭ, 31 Dec 2009, Matthew Brand skribis:
>> >>Did you use the regedit.exe in syswow64?
>> I did not that time.
>>
>> I just did though and when I do, there is niether an AppID or an
>> HKEY_CLASSES_ROOT\CLSID entry. I guess this is what is going wrong?
>>
>> But when I run try to register it using:
>> C:\Windows\SysWOW64>regsvr32 "c:\Program Files (x86)\NinjaTrader
>> 6.5\bin\Interop.JDLLServerLib.dll"
>
> As Oleg had pointed out, this is only a proxy, you should reg the
> j.dll itself.
> try
> 1. copy j.dll to the ninja bin folder
> 2. type "cmd" in run-window, press shift-enter or other method to run as
>   an admin.
> 3. reg the j.dll
>  C:\Windows\SysWOW64>regsvr32 "c:\Program Files (x86)\NinjaTrader 
> 6.5\bin\j.dll"
>
> 4. see what will be the error message or use syswow64/regedit to verify the
>   clsid has been written.
>
> untested.
>
> --
> 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