Replies

ctid2568413
08 Oct 2020, 05:05

I still hope for an assistance of the Spotware team.


@ctid2568413

ctid2568413
02 Oct 2020, 05:54 ( Updated at: 21 Dec 2023, 09:22 )

Hi and thank you for your response.

If you let the indicator go for a few minutes with multiple symbols at one minute time frame, you will notice that it shows some forms correctly and some are either missing contents, the button, are freezing and impossible to be closed or even crashing cTrader. I haven't noticed that when there was no API's button and the forms were being shown directly.

I suspect the logic might need improvement. I am involved in several job projects based on the logic and would be very thankful for your support.

 


@ctid2568413

ctid2568413
29 Sep 2020, 05:00

Hi, this is an example of how the main structure is written. Is the logic of the task and its synchronization with the API button correct.

using System;
using cAlgo.API;
using cAlgo.API.Indicators;
using Forms = System.Windows.Forms;
using Drawing = System.Drawing;

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class asitest : Indicator
    {
        [Output("Main", LineColor = "#FFFFFFFF")]
        public IndicatorDataSeries asiResult { get; set; }

        AccumulativeSwingIndex asi;

        DateTime lastAlert;

        Button apiButton = new Button();

        Forms.Button button = new Forms.Button();
        Forms.Label label = new Forms.Label();
        Forms.Form form = new Forms.Form();

        protected override void Initialize()
        {
            asi = Indicators.AccumulativeSwingIndex(12);

            lastAlert = Bars.Last(1).OpenTime;

            ApiButton();
        }

        public override void Calculate(int index)
        {
            asiResult[index] = asi.Result[index];

            if (!IsLastBar)
                return;

            var isAbove = asi.Result.Last(1) <= 0 && asi.Result.Last(0) > 0;

            if (isAbove)
            {
                var subjectBuy = "Is Above";
                var bodyBuy = "Bullish";

                Notify("buy", subjectBuy, bodyBuy);
            }

            var isBelow = asi.Result.Last(1) >= 0 && asi.Result.Last(0) < 0;

            if (isBelow)
            {
                var subjectSell = "Is Below";
                var bodySell = "Bearish";

                Notify("sell", subjectSell, bodySell);
            }
        }

        private void Notify(string type, string subject, string body)
        {
            if (Bars.LastBar.OpenTime == lastAlert)
                return;

            lastAlert = Bars.LastBar.OpenTime;

            ShowForm(type, subject, body);
        }

        private void ShowForm(string type, string subject, string body)
        {
            Drawing.Color aColor = Drawing.Color.Yellow;

            switch (type)
            {
                case "buy":
                    aColor = Drawing.Color.Green;
                    break;
                case "sell":
                    aColor = Drawing.Color.Red;
                    break;
            }

            button.Location = new Drawing.Point(35, 50);
            button.ForeColor = Drawing.Color.White;
            button.BackColor = aColor;
            button.Text = "OK";
            button.DialogResult = Forms.DialogResult.OK;

            label.Location = new Drawing.Point(35, 25);
            label.Height = 75;
            label.Width = 75;
            label.ForeColor = Drawing.Color.White;
            label.BackColor = aColor;
            label.Text = body;
            label.TextAlign = Drawing.ContentAlignment.TopLeft;
            label.Font = new System.Drawing.Font("Arial", 10);

            form.StartPosition = Forms.FormStartPosition.CenterScreen;
            form.Height = 150;
            form.Width = 150;
            form.TopMost = true;
            form.BringToFront();
            form.FormBorderStyle = Forms.FormBorderStyle.FixedDialog;
            form.MinimizeBox = false;
            form.MaximizeBox = false;
            form.BackColor = aColor;
            form.Text = subject;
            form.AcceptButton = button;
            form.KeyPreview = true;

            form.Controls.Add(button);
            form.Controls.Add(label);

            apiButton.IsEnabled = true;
            apiButton.IsVisible = true;

            apiButton.Click += ButtonClickEventArgs =>
            {
                apiButton.IsEnabled = false;
                apiButton.IsVisible = false;

                var asyncShowForm = new System.Threading.Tasks.Task(() => { form.ShowDialog(); });
                asyncShowForm.Start();
            };
        }

        private void ApiButton()
        {
            apiButton.ForegroundColor = Color.White;
            apiButton.BackgroundColor = Color.Yellow;
            apiButton.Height = 100;
            apiButton.Width = 100;
            apiButton.HorizontalAlignment = HorizontalAlignment.Center;
            apiButton.VerticalAlignment = VerticalAlignment.Center;
            apiButton.HorizontalContentAlignment = HorizontalAlignment.Center;
            apiButton.VerticalContentAlignment = VerticalAlignment.Center;
            apiButton.IsEnabled = false;
            apiButton.IsVisible = false;

            IndicatorArea.AddControl(apiButton);
        }
    }
}

 


@ctid2568413

ctid2568413
28 Sep 2020, 05:31

When I want to start the task on an API's button click on the chart, the form is either missing its controls, disappearing quickly or crashing cTrader.

 

//apiButton declared and the properties are set

apiButton.Click += ButtonClickEventArgs =>
{
var asyncTask = new System.Threading.Tasks.Task(() => { form.ShowDialog(); });
asyncTask.Start();
};

 

Please advise.


@ctid2568413

ctid2568413
25 Sep 2020, 13:13

Thanks, I will be testing it shortly.

Edit: The link has been helpful. It looks like it works.


@ctid2568413

ctid2568413
15 Sep 2020, 14:01

@firemyst, thank you. I will see if this works for me or I will get back with the code.


@ctid2568413

ctid2568413
15 Sep 2020, 12:55

@firemyst, this might be what I am missing. Which part of the code is it to be defined?

 

@PanagiotisCharalampous, possibly the above will solve it.


@ctid2568413