Using WPF to Interact with Indicators in MultiCharts .Net (Trading Program)

StackOverflow https://stackoverflow.com/questions/16575456

  •  29-05-2022
  •  | 
  •  

Question

I am trying to create a WPF form using Visual Studio C# that will interact with indicators I create for my trading charts in MultiCharts .Net

I've added a WPF Project to the solution and added the namespace for the indicators. However, I can not figure out how I can manipulate inputs for the indicator object. Any help from someone who works with this program would be much appreciated.

Était-ce utile?

La solution

You have to create a local network connection to make this work--I used sockets, but anything in a similar way will support the same goal:

Make a new MC indicator like so:

public class tests_OwnApp : IndicatorObject {

    public tests_OwnApp(object _ctx):base(_ctx)
    {
        PortNumber = 5000;
    }

    [Input]
    public int PortNumber { get; set; }

    public static string Symbol;

    const string ServerIP = "127.0.0.1";


    IPAddress localAdd;
    TcpListener listener;
    TcpClient client;
    NetworkStream nwStrm;

    private IPlotObject plot1;

    protected override void Create() {
        // create variable objects, function objects, plot objects etc.
        plot1 = AddPlot(new PlotAttributes("", EPlotShapes.Line, Color.Red));
        localAdd = IPAddress.Parse(ServerIP);
        client = null;
    }

    protected override void StartCalc() {
        // assign inputs 

        // establish connection
        if (listener == null)
        {
            listener = new TcpListener(localAdd, PortNumber);

        }
        listener.Stop();
        Thread.Sleep(100);
        listener.Start();

    }

    protected override void CalcBar()
    {
        // indicator logic 
        if (Bars.LastBarTime <= Bars.Time[0] + new TimeSpan(2,0,0))
        {
            Symbol = Bars.Info.Name;

            if (client == null || !client.Connected)
                client = listener.AcceptTcpClient();

            // create network stream
            nwStrm = client.GetStream();

            // send symbol to app:
            Output.WriteLine("sending " + Symbol + " to application");
            byte[] bytesToSend = ASCIIEncoding.ASCII.GetBytes(Symbol);


            string closingPrice = Convert.ToString(Bars.Close[0]);
            string totalMsg = "Sym," + Symbol +
                "\nClose[0]," + closingPrice + "\nClose[1]," + Bars.Close[1].ToString();

            byte[] bytes2 = ASCIIEncoding.ASCII.GetBytes(totalMsg);
            nwStrm.Write(bytes2, 0, bytes2.Length);
        }
    }
}

In your app (Console App example, extends to WPF):

static void Main(string[] args)
        {
            const int PortNum = 5000;
            const string ServerIP = "127.0.0.1";

            DateTime startTime = DateTime.Now;

            TcpClient client = new TcpClient(ServerIP, PortNum);
            NetworkStream nwStream = client.GetStream();

            while (true)
            {
                if (client.ReceiveBufferSize != 0)
                {
                    nwStream = client.GetStream();

                    byte[] bytesToRead = new byte[client.ReceiveBufferSize];
                    int bytesRead = nwStream.Read(bytesToRead, 0, client.ReceiveBufferSize);

                    string msg = Encoding.ASCII.GetString(bytesToRead, 0, bytesRead);

                    Console.WriteLine("received: " + msg);

                    if (DateTime.Now - new TimeSpan(0, 0, 30) > startTime)
                    {
                        break;
                    }
                    Thread.Sleep(100);
                    Console.Write("-ping-");
                }
            }


            Console.ReadLine();
        }

Run them both together and you'll find they connect and the info is received.

I used this example to get started, in case my example is not sufficient.

Be sure to include all the necessary namespaces:

using System.Net;
using System.Net.Sockets;

Autres conseils

See the documentation I believe chapter three shows you how to work with indicators

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top