使用WPF与MultiCharts .Net(交易计划)中的指标进行交互

时间:2013-05-15 21:41:12

标签: c# trading

我正在尝试使用Visual Studio C#创建一个WPF表单,它将与我为MultiCharts .Net中的交易图表创建的指标进行交互

我在解决方案中添加了一个WPF项目,并添加了指标的命名空间。但是,我无法弄清楚如何操作指标对象的输入。任何使用此程序的人的帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

你必须创建一个本地网络连接才能使它工作 - 我使用套接字,但任何类似的方式都会支持相同的目标:

制作一个新的MC指标,如下所示:

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);
        }
    }
}

在您的应用中(控制台应用示例,扩展为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();
        }

将它们一起运行,您将发现它们已连接并且已收到信息。

我使用此example开始,以防我的示例不充分。

确保包含所有必需的命名空间:

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

答案 1 :(得分:0)

参见documentation我相信第三章向您展示了如何使用指标