Topics

Forum Topics not found

Replies

Emmanuel.evrard2
26 Oct 2020, 10:57

RE:

Thank you for your answer

 

PanagiotisCharalampous said:

Hi Emmanuel,

Unfortunately we do not have such an example at the moment.

Best Regards,

Panagiotis 

Join us on Telegram 

 

 


@Emmanuel.evrard2

Emmanuel.evrard2
25 Oct 2020, 23:30

RE:

Hi PanagiotisCharalampous,

I tried to use Pipe to connect Calgo to an application

It works initially with

 [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]

but it don't work anymore, It seem not to be able to connect

                NamedPipeClientStream pipeStream = new NamedPipeClientStream(".", PipeName, PipeDirection.Out, PipeOptions.Asynchronous);

                pipeStream.Connect(TimeOut);                              DON'T WORK ANYMORE

 

Do you have an example of Pipe or sharememory in Ctrader ?

 

Thank you

 

 

PanagiotisCharalampous said:

Hi GammaQuant,

Named Pipes and Memory Mapped Files are indeed advanced C# subjects. Named Pipes are used for inteprocess communication i.e. one application sending messages to another while Memory Mapped Files are used to share memory between different processes. So it all depends on the application you are building. If for example you need one application to inform another about something then use Named Pipes. If you need an application to write something in memory and another application to read this information whenever needed then use Memory-Mapped files. I don't know if I can help you more than this at this stage. If you have more specific questions, feel free to ask.

Best Regards,

Panagiotis

 


@Emmanuel.evrard2

Emmanuel.evrard2
25 Oct 2020, 23:19

RE:

JHtrader,

 

Where is the example ? how can we download it ?

 

jhtrader said:

Hi,

Can anyone help me code this (without using sycware).  If we share libraries and things this would help everyone focus better on more powerful strategies.

I want to create a simple example that anyone can then expand on.

There are 2 robots, robot 1 and robot 2.

Robot 1  - checks a condition If the time is 10am, on this event it communicates a random pair and the fact that the 10am hourly bar has been completed to  to Robot 2.

Robot 2 - Receives information from Robot 1 and checks the SMA of the pair given.  If the SMA is rising robot 2 buys the pair else it sells the pair.

This is just a example to illustrate the communication process between robots using pipes....

 

 

 


@Emmanuel.evrard2

Emmanuel.evrard2
25 Oct 2020, 23:15

RE:

SpotWare, 

 

Can we use pipe to connect to Calgo? (with AccessRights = AccessRights.FullAccess)

Thank you

 

 

Spotware said:

Dear ales.sobotka@gmail.com,

There is no way to send external commands to cAlgo. As solark suggested, you can use Connect API to modify positions. You can find more information about Connect API here.

Best Regards,

cTrader Team

 


@Emmanuel.evrard2

Emmanuel.evrard2
25 Oct 2020, 22:35

RE: RE:

I tried again this solution , but it doesn't work anymore.

 

Ctrader is the same. it works the first day but not the next day.

 

If someone have an idea ?

 

Emmanuel.evrard2 said:

cAlgo_Fanatic said:

System.IO.Pipes is included in cAlgo. We will add a sample in this section: /forum/calgo-reference-samples.

 

Hello

 

I tried to used System.IO.Pipes in CTrader and it works !

 

I used this example : 

 

I had to add at the beginning in Calgo :

 

using System.IO.Pipes;
using System.Diagnostics;
using System.Text;

 

  [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]

 

for the server side : the form1 : 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;

namespace PipesServerTest
{
    public partial class Form1 : Form
    {
        public delegate void NewMessageDelegate(string NewMessage);
        private PipeServer _pipeServer;

        public Form1()
        {
            InitializeComponent();
            _pipeServer = new PipeServer();
            _pipeServer.PipeMessage += new DelegateMessage(PipesMessageHandler);
        }

        private void cmdListen_Click(object sender, EventArgs e)
        {
            try
            {
                _pipeServer.Listen("TestPipe");
                txtMessage.Text = "Listening - OK";
                cmdListen.Enabled = false;
            }
            catch (Exception)
            {
                txtMessage.Text = "Error Listening";
            }
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void PipesMessageHandler(string message)
        {
            try
            {
                if (this.InvokeRequired)
                {
                    this.Invoke(new NewMessageDelegate(PipesMessageHandler), message);
                }
                else
                {
                    txtMessage.Text = message;
                }
            }
            catch (Exception ex)
            {

                Debug.WriteLine(ex.Message);
            }

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            _pipeServer.PipeMessage -= new DelegateMessage(PipesMessageHandler);
            _pipeServer = null;

        }


    }
}

and a class  PipeServer

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Pipes;
using System.Diagnostics;

namespace PipesServerTest
{
    // Delegate for passing received message back to caller
    public delegate void DelegateMessage(string Reply);

    class PipeServer
    {
        public event DelegateMessage PipeMessage;
        string _pipeName;

        public void Listen(string PipeName)
        {
            try
            {
                // Set to class level var so we can re-use in the async callback method
                _pipeName = PipeName;
                // Create the new async pipe 
                NamedPipeServerStream pipeServer = new NamedPipeServerStream(PipeName, PipeDirection.In, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);

                // Wait for a connection
                pipeServer.BeginWaitForConnection(new AsyncCallback(WaitForConnectionCallBack), pipeServer);
            }
            catch (Exception oEX)
            {
                Debug.WriteLine(oEX.Message);
            }
        }

        private void WaitForConnectionCallBack(IAsyncResult iar)
        {
            try
            {
                // Get the pipe
                NamedPipeServerStream pipeServer = (NamedPipeServerStream)iar.AsyncState;
                // End waiting for the connection
                pipeServer.EndWaitForConnection(iar);

                byte[] buffer = new byte[255];

                // Read the incoming message
                pipeServer.Read(buffer, 0, 255);
                
                // Convert byte buffer to string
                string stringData = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
                Debug.WriteLine(stringData + Environment.NewLine);

                // Pass message back to calling form
                PipeMessage.Invoke(stringData);

                // Kill original sever and create new wait server
                pipeServer.Close();
                pipeServer = null;
                pipeServer = new NamedPipeServerStream(_pipeName, PipeDirection.In, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);

                // Recursively wait for the connection again and again....
                pipeServer.BeginWaitForConnection(new AsyncCallback(WaitForConnectionCallBack), pipeServer);
            }
            catch
            {
                return;
            }
        }
    }
}
 

 

for the client side  in CAlgo :

        Send("test Ctrader ", "TestPipe", 1000);

        

        public void Send(string SendStr, string PipeName, int TimeOut = 1000)
        {
            try
            {
                NamedPipeClientStream pipeStream = new NamedPipeClientStream(".", PipeName, PipeDirection.Out, PipeOptions.Asynchronous);

                // The connect function will indefinitely wait for the pipe to become available
                // If that is not acceptable specify a maximum waiting time (in ms)
                pipeStream.Connect(TimeOut);
                //  Debug.WriteLine("[Client] Pipe connection established");

                byte[] _buffer = Encoding.UTF8.GetBytes(SendStr);
                pipeStream.BeginWrite(_buffer, 0, _buffer.Length, AsyncSend, pipeStream);
            } catch (TimeoutException oEX)
            {
                // Debug.WriteLine(oEX.Message);
            }
        }
        private void AsyncSend(IAsyncResult iar)
        {
            try
            {
                // Get the pipe
                NamedPipeClientStream pipeStream = (NamedPipeClientStream)iar.AsyncState;

                // End the write
                pipeStream.EndWrite(iar);
                pipeStream.Flush();
                pipeStream.Close();
                pipeStream.Dispose();
            } catch (Exception oEX)
            {
                // Debug.WriteLine(oEX.Message);
            }
        }

 


@Emmanuel.evrard2

Emmanuel.evrard2
24 Oct 2020, 18:43

RE:

cAlgo_Fanatic said:

System.IO.Pipes is included in cAlgo. We will add a sample in this section: /forum/calgo-reference-samples.

 

Hello

 

I tried to used System.IO.Pipes in CTrader and it works !

 

I used this example : 

 

I had to add at the beginning in Calgo :

 

using System.IO.Pipes;
using System.Diagnostics;
using System.Text;

 

  [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]

 

for the server side : the form1 : 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;

namespace PipesServerTest
{
    public partial class Form1 : Form
    {
        public delegate void NewMessageDelegate(string NewMessage);
        private PipeServer _pipeServer;

        public Form1()
        {
            InitializeComponent();
            _pipeServer = new PipeServer();
            _pipeServer.PipeMessage += new DelegateMessage(PipesMessageHandler);
        }

        private void cmdListen_Click(object sender, EventArgs e)
        {
            try
            {
                _pipeServer.Listen("TestPipe");
                txtMessage.Text = "Listening - OK";
                cmdListen.Enabled = false;
            }
            catch (Exception)
            {
                txtMessage.Text = "Error Listening";
            }
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void PipesMessageHandler(string message)
        {
            try
            {
                if (this.InvokeRequired)
                {
                    this.Invoke(new NewMessageDelegate(PipesMessageHandler), message);
                }
                else
                {
                    txtMessage.Text = message;
                }
            }
            catch (Exception ex)
            {

                Debug.WriteLine(ex.Message);
            }

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            _pipeServer.PipeMessage -= new DelegateMessage(PipesMessageHandler);
            _pipeServer = null;

        }


    }
}

and a class  PipeServer

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Pipes;
using System.Diagnostics;

namespace PipesServerTest
{
    // Delegate for passing received message back to caller
    public delegate void DelegateMessage(string Reply);

    class PipeServer
    {
        public event DelegateMessage PipeMessage;
        string _pipeName;

        public void Listen(string PipeName)
        {
            try
            {
                // Set to class level var so we can re-use in the async callback method
                _pipeName = PipeName;
                // Create the new async pipe 
                NamedPipeServerStream pipeServer = new NamedPipeServerStream(PipeName, PipeDirection.In, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);

                // Wait for a connection
                pipeServer.BeginWaitForConnection(new AsyncCallback(WaitForConnectionCallBack), pipeServer);
            }
            catch (Exception oEX)
            {
                Debug.WriteLine(oEX.Message);
            }
        }

        private void WaitForConnectionCallBack(IAsyncResult iar)
        {
            try
            {
                // Get the pipe
                NamedPipeServerStream pipeServer = (NamedPipeServerStream)iar.AsyncState;
                // End waiting for the connection
                pipeServer.EndWaitForConnection(iar);

                byte[] buffer = new byte[255];

                // Read the incoming message
                pipeServer.Read(buffer, 0, 255);
                
                // Convert byte buffer to string
                string stringData = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
                Debug.WriteLine(stringData + Environment.NewLine);

                // Pass message back to calling form
                PipeMessage.Invoke(stringData);

                // Kill original sever and create new wait server
                pipeServer.Close();
                pipeServer = null;
                pipeServer = new NamedPipeServerStream(_pipeName, PipeDirection.In, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);

                // Recursively wait for the connection again and again....
                pipeServer.BeginWaitForConnection(new AsyncCallback(WaitForConnectionCallBack), pipeServer);
            }
            catch
            {
                return;
            }
        }
    }
}
 

 

for the client side  in CAlgo :

        Send("test Ctrader ", "TestPipe", 1000);

        

        public void Send(string SendStr, string PipeName, int TimeOut = 1000)
        {
            try
            {
                NamedPipeClientStream pipeStream = new NamedPipeClientStream(".", PipeName, PipeDirection.Out, PipeOptions.Asynchronous);

                // The connect function will indefinitely wait for the pipe to become available
                // If that is not acceptable specify a maximum waiting time (in ms)
                pipeStream.Connect(TimeOut);
                //  Debug.WriteLine("[Client] Pipe connection established");

                byte[] _buffer = Encoding.UTF8.GetBytes(SendStr);
                pipeStream.BeginWrite(_buffer, 0, _buffer.Length, AsyncSend, pipeStream);
            } catch (TimeoutException oEX)
            {
                // Debug.WriteLine(oEX.Message);
            }
        }
        private void AsyncSend(IAsyncResult iar)
        {
            try
            {
                // Get the pipe
                NamedPipeClientStream pipeStream = (NamedPipeClientStream)iar.AsyncState;

                // End the write
                pipeStream.EndWrite(iar);
                pipeStream.Flush();
                pipeStream.Close();
                pipeStream.Dispose();
            } catch (Exception oEX)
            {
                // Debug.WriteLine(oEX.Message);
            }
        }


@Emmanuel.evrard2