[TUTORIAL] Windows forms

Created at 12 Jun 2021, 21:12
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!
DelFonseca's avatar

DelFonseca

Joined 25.06.2017

[TUTORIAL] Windows forms
12 Jun 2021, 21:12


Hi,

There is little documentation how to interact with forms created in Visual Studio, somenone can create a "how to do this" tutorial for future references?

1) Create in VS a form (named "MainForm") with:
 - Gray background
 - ComboBox with some symbol's name's
 - TextBox for Volume
 - TextBox for Label
 - Current Ask and Bid price's for user reference
 - Buy and Sell button's (with the respective green and orange colors).

2) When the Buy or Sell button is pressed, another form appears (named "formTrade") saying "Trade was successfully executed.", this one will have a close button.

3) Load the form "MainForm" in cTrader and execute it on OnStart().

With this tutorial it will be possible learn:
- Load and interact with fully forms created in VS
- Load information contained in cTrader in the form (Ask and Bid prices)
- Execute cTrader instructions via form (Buy and Sell button's)

I really appreciate any suport!!

Sincerely,
DelFonseca


@DelFonseca
Replies

amusleh
13 Jun 2021, 08:27

Hi,

To use WinForms on your cBot/Indicator, you have to follow these steps:

  • Add System.Windows.Forms Reference to your cBot/Indicator project
  • Change your cBot/Indicator AccessRights to FullAccess
  • Add a Windows Form to your cBot/Indicator project by right clicking on your project in VS, then select Add -> Form
  • Design your form on VS WinFroms Designer
  • To communicate between your cBot/Indicator and Form you can pass your indicator/cBot to the form via its constructor parameter, or use an adapter object
  • You must execute the Form ShowDialog method inside a separate thread, because this method blocks the thread until the form is closed you have to use a separate thread, and not use the cBot/Indicator main execution thread, also the thread must have STA ApartmentState
  • Build your cBot/Indicator project from VS not cTrader, because of the changes you made on references and adding new form, you can build it from cTrader but the first build after adding the form and making changes on project references must be done by VS

As an example you can check my WinForms Test cBot sample:  

 

Clone it from Github or download it as a zip file, extract the content on a new folder inside your cTrader cAlgo/Robots folder, it will appear in your cTrader automate cBots list, re-build it and run it.

Open it with VS to see the cBot full code.


@amusleh

DelFonseca
14 Jun 2021, 20:27

RE:

amusleh said:

Hi,

To use WinForms on your cBot/Indicator, you have to follow these steps:

  • (...)

Open it with VS to see the cBot full code.

Thank you so much for your hel, it was perfect!!


@DelFonseca

jumel.donatien
20 Jun 2021, 09:44 ( Updated at: 20 Jun 2021, 09:45 )

RE:

Hi amusleh and thank you for your help!

In order to understand your "tutorial", I downloaded your sample code, added the extracted folder on my robots folder, but it doesn't appear on my robots list in cTrader.. 

Where is the .algo file? or how could I create it to test your robot?

Thank you for your help.


@jumel.donatien

amusleh
20 Jun 2021, 10:25

You can download the algo file from here: 

 


@amusleh

jumel.donatien
20 Jun 2021, 10:34

RE:

It works!!

Great! Thank you so much! 


@jumel.donatien

jumel.donatien
20 Jun 2021, 23:20

RE:

Hi amusleh,

I have another question: When I launch the robot, the form opens and if I want to change the action of the Buy Button (for example) to draw text on chart, it doesn't work.. I think, it's because when the form is launched, the chart is not activated..

Do you have a solution?

Here is the code changed in MainForm.cs:

private void BtnBuy_Click(object sender, System.EventArgs e)
        {
            _robot.ExecuteMarketOrder(TradeType.Buy, _robot.SymbolName, _robot.Symbol.VolumeInUnitsMin);

            _robot.ChartObjects.DrawText("Sample Text", "Sample Text", StaticPosition.Center);
        }

Thank you in advance!


@jumel.donatien

amusleh
21 Jun 2021, 14:51

RE: RE:

jumel.donatien said:

Hi amusleh,

I have another question: When I launch the robot, the form opens and if I want to change the action of the Buy Button (for example) to draw text on chart, it doesn't work.. I think, it's because when the form is launched, the chart is not activated..

Do you have a solution?

Here is the code changed in MainForm.cs:

private void BtnBuy_Click(object sender, System.EventArgs e)
        {
            _robot.ExecuteMarketOrder(TradeType.Buy, _robot.SymbolName, _robot.Symbol.VolumeInUnitsMin);

            _robot.ChartObjects.DrawText("Sample Text", "Sample Text", StaticPosition.Center);
        }

Thank you in advance!

Hi,

Invoke it on robot main thread:

        private void BtnBuy_Click(object sender, System.EventArgs e)
        {
            _robot.ExecuteMarketOrder(TradeType.Buy, _robot.SymbolName, _robot.Symbol.VolumeInUnitsMin);

            _robot.BeginInvokeOnMainThread(() => _robot.Chart.DrawStaticText("Sample Text", "Sample Text", VerticalAlignment.Bottom, API.HorizontalAlignment.Center, Color.Red));
        }

 


@amusleh

jumel.donatien
21 Jun 2021, 21:19

RE: RE: RE:

That's great! Thank you.

One last question: is it possible to open a new chart by clicking on this button from a windowsform?

I tried with "SendKeys.SendWait (" ^ (N) ");" like simulating a shortcut key: but it only works when the main ctrader window is active, but not when the Windows form is open.

Thanks to you, I am now able to call actions by clicking on a button like drawing text as you showed me, but unable to open a new graph or activate the main ctrader window from the form Windows.

Do you have a solution for this? Thank you


@jumel.donatien