Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
205 views
in Technique[技术] by (71.8m points)

c# - NinjaTrader 8 recive command by lan

I try to create an indicator for ninjatrader 8, this indicator must open a port like a server and stay in LISTEN mode, a command (by tcp after connect) arrives it must execute it.

I looked how to create socket and was a good example for me, now I am trying to integrate it inside of indicator in NJ8 but unfortunately when I try to connect it does not connect, probably I am doing something wrong.

I have been many days trying to resolve the problem but dont find the solution, if anyone can help me or tell me the way I will be grateful. Thanks you all in advance.

#region Using declarations
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Gui;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Gui.SuperDom;
using NinjaTrader.Gui.Tools;
using NinjaTrader.Data;
using NinjaTrader.NinjaScript;
using NinjaTrader.Core.FloatingPoint;
using NinjaTrader.NinjaScript.DrawingTools;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Net;
using System.Net.Sockets;
#endregion

//This namespace holds Indicators in this folder and is required. Do not change it. 
namespace NinjaTrader.NinjaScript.Indicators
{
    public class ATMStrategyMonitor : Indicator
    {
        TcpListener server=null;
        private int nData = 0;
        private double bidPrice;
        private double askPrice;
        private long askVolume;
        private long bidVolume;
        private string LastTot;
              Int32 port = 15926;
      IPAddress localAddr = IPAddress.Parse("127.0.0.1");
  String data = null;

           // Buffer for reading data
      Byte[] bytes = new Byte[256];
        
        protected override void OnStateChange()
        {
            if (State == State.SetDefaults)
            {
                Description                                 = @"Enter the description for your new custom Indicator here.";
                Name                                        = "ATMStrategyMonitor";
                Calculate                                   = Calculate.OnBarClose;
                IsOverlay                                   = false;
                DisplayInDataBox                            = true;
                DrawOnPricePanel                            = true;
                DrawHorizontalGridLines                     = true;
                DrawVerticalGridLines                       = true;
                PaintPriceMarkers                           = true;
                ScaleJustification                          = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                //Disable this property if your indicator requires custom values that cumulate with each new market data event. 
                //See Help Guide for additional information.
                IsSuspendedWhileInactive                    = true;
            }
            else if (State == State.Configure)
            {
      server = new TcpListener(localAddr, port);
      server.Start();
            }
            else if (State == State.Terminated)
            {
                //file.Dispose();
            }
        }


        protected override void OnBarUpdate()
        {
            //Add your custom indicator logic here.
            try
                {
                    // Set the TcpListener on port 13000.

      while(true)
      {
       Print("Waiting for a connection... ");

        // Perform a blocking call to accept requests.
        // You could also use server.AcceptSocket() here.
        TcpClient client = server.AcceptTcpClient();
        Print("Connected!");

        data = null;

        // Get a stream object for reading and writing
        NetworkStream stream = client.GetStream();

        int I;

        // Loop to receive all the data sent by the client.
        while((i = stream.Read(bytes, 0, bytes.Length))!=0)
        {
          // Translate data bytes to a ASCII string.
          data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
          Print("Received: {0}"+ data);

          // Process the data sent by the client.
          data = data.ToUpper();

          byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
          // Send back a response.
          stream.Write(msg, 0, msg.Length);
          Print("Sent: {0}"+ data);
        }
        // Shutdown and end connection
        client.Close(); 
      }
                }
                catch
                {
 Print("SocketException: {0}");
                }
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...