cAlgo-Open new form.

Created at 28 Apr 2018, 16:17
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
SzymonC's avatar

SzymonC

Joined 28.04.2018

cAlgo-Open new form.
28 Apr 2018, 16:17


Hi,

I'm new to cAlgo,but I've got some experience using C#.

I created new blank form and want to show it when my robot is on.The problem i've got is that when I use:

Form1 f1 = new Form1();
f1.Show();

the form freezes(not responding) and I can't find the reason for that,however,when I use:

Form1 f1 = new Form1();
f1.ShowDialog();

It works,but it looks ugly!

It is blank form without any extra code on main thread,so it should work.

Is there any thing I'm missing?

 

Also I'd like to mention a bug on android which sometime not updating "free margin" and it allows for opening extra positions,even if free margin is under $0.

Kind Regards

            Szymon C.


@SzymonC
Replies

PanagiotisCharalampous
08 May 2018, 10:04

Dear Szymon,

Thanks for posting in our forum. In order to launch a form from a cBot and avoid freezing, you should do in a new thread. See an example below

using System;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class NewcBot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }
        private Thread _thread;
        private Form _f1;
        protected override void OnStart()
        {
            _f1 = new Form();
            _thread = new Thread(() => _f1.ShowDialog());
            _thread.SetApartmentState(ApartmentState.STA);
            _thread.Start();
        }

        protected override void OnTick()
        {
            // Put your core logic here
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

Regarding the bug you mentioned, could you please send us some more information please e.g. screenshot, so that we can investigate further?

Best Regards,

Panagiotis


@PanagiotisCharalampous

kontodospamu5
01 Nov 2018, 02:09

RE:

Dear Panagiotis,

 

could you please give an example how to construct in "OnTick()" section a cross-thread reference to a label1 placed in the "_f1" form?

Best Regards,

PiotrW


@kontodospamu5

PanagiotisCharalampous
01 Nov 2018, 09:52

Hi kontodospamu5,

You can expose a public method that will update the form's label and call it on tick as below

using System;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
 
namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class NewcBot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }
        private Thread _thread;
        private Form _f1;
        protected override void OnStart()
        {
            _f1 = new Form();
            _thread = new Thread(() => _f1.ShowDialog());
            _thread.SetApartmentState(ApartmentState.STA);
            _thread.Start();
        }
 
        protected override void OnTick()
        {
            _thread = new Thread(() => _f1.UpdateLabel("label"));
            _thread.SetApartmentState(ApartmentState.STA);
            _thread.Start();
        }
 
        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

Let me know if this helps,

Best Regards,

Panagiotis


@PanagiotisCharalampous

kontodospamu5
03 Nov 2018, 02:07

Dear Panagiotis,

Thank you for a quick response indeed.

Sorry to tell you but the solution you proposed is not working.

I received “Error CS1061  'Form' does not contain a definition for 'UpdateLabel' and no extension method 'UpdateLabel' accepting a first argument of type 'Form' could be found (are you missing a using directive or an assembly reference?)” although I applied:

using System.Windows.Forms;

using System.Threading;

 

I know how to build a Windows Form in cBot, but I have problems with updating label in the Form.

Panagiotis I have request, if it is not a problem, could you please present an exemplary but full script of how to construct a Windows Form with one label in a cBot in OnStart() and how to update that label.Text with Symbol.Bid in OnTick(), please.

 

Best Regards,

kontodospamu5


@kontodospamu5

PanagiotisCharalampous
05 Nov 2018, 10:01

Hi kontodospamu5,

The above is just an example and assumes that you will develop your UpdateLabel() function. The function will be a part of the form and will update your label or anything else you wish it to do. 

Best Regards,

Panagiotis

 


@PanagiotisCharalampous

lec0456
22 Feb 2019, 00:41

I tried this but instead of show dialog, I used show.  The window comes up quickly and then disapears.  Is there some reason why you must use .showdialog?


@lec0456

PanagiotisCharalampous
22 Feb 2019, 10:07

Hi lec0456,

See here why.

Best Regards,

Panagiotis


@PanagiotisCharalampous

lec0456
22 Feb 2019, 17:35

Thank you!!

I changed  _form.ShowDialog();

to:

Application.Run(_form); inside ThreadProc. and it worked!!


@lec0456

trader.calgo
03 Mar 2019, 21:38

RE:

Panagiotis Charalampous said:

Hi kontodospamu5,

You can expose a public method that will update the form's label and call it on tick as below

using System;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
 
namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class NewcBot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }
        private Thread _thread;
        private Form _f1;
        protected override void OnStart()
        {
            _f1 = new Form();
            _thread = new Thread(() => _f1.ShowDialog());
            _thread.SetApartmentState(ApartmentState.STA);
            _thread.Start();
        }
 
        protected override void OnTick()
        {
            _thread = new Thread(() => _f1.UpdateLabel("label"));
            _thread.SetApartmentState(ApartmentState.STA);
            _thread.Start();
        }
 
        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

Let me know if this helps,

Best Regards,

Panagiotis

Please, tell me how I can execute an order by pressing a button in this new form.


@trader.calgo

lec0456
03 Mar 2019, 22:57

You should check out the cBot I posted here: https://ctrader.com/algos/cbots/show/1849

You have to pass the cBot object to the form:

counter = new frmCandleCountdown(this, paramOnTop);

the "this" key word is the object.

So, you have to modify the default windows method something like this:

        public frmCandleCountdown(myCandleCountDown _caller, bool _alwaysOnTop)
        {
            alwaysOnTop = _alwaysOnTop;
            caller = _caller;
            InitializeComponent();
        }

You also have to declare your cBot obkect in the windows form like this:

myCandleCountDown caller;

Then create a function in your cBot to execute a trade.

Then call the function OnClick

 


@lec0456

trader.calgo
03 Mar 2019, 23:20

RE:

lec0456 said:

You should check out the cBot I posted here: https://ctrader.com/algos/cbots/show/1849

You have to pass the cBot object to the form:

counter = new frmCandleCountdown(this, paramOnTop);

the "this" key word is the object.

So, you have to modify the default windows method something like this:

        public frmCandleCountdown(myCandleCountDown _caller, bool _alwaysOnTop)
        {
            alwaysOnTop = _alwaysOnTop;
            caller = _caller;
            InitializeComponent();
        }

You also have to declare your cBot obkect in the windows form like this:

myCandleCountDown caller;

Then create a function in your cBot to execute a trade.

Then call the function OnClick

 

I appreciate your answer. Thanks you.


@trader.calgo