Custom Indicator - IndicatorAreaSample not working on cTrader 4.8.25 ?

Created at 30 Sep 2023, 12:10
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!
CT

ctid2948110

Joined 10.03.2021

Custom Indicator - IndicatorAreaSample not working on cTrader 4.8.25 ?
30 Sep 2023, 12:10


Just upgrading some custom C# indicators from a year ago to the latest cTrader frameworks

and having problems showing a Grid in the IndicatorArea in a Custom Indicator.

Tested the IndicatorAreaSample from https://help.ctrader.com/ctrader-automate/references/Chart/IndicatorArea/#examples

and found it doesn't work ?

Can somebody please help with a workaround for IndicatorArea.AddControl(grid); not working in the latest cTrader  ?


@ctid2948110
Replies

PanagiotisChar
02 Oct 2023, 06:04

Hi there,

If you share some code demonstrating what you are trying to do, it would be easier for somebody to help you.

 


@PanagiotisChar

ctid2948110
03 Oct 2023, 19:59 ( Updated at: 05 Oct 2023, 18:21 )

Here is the source from the cTrader example not working ?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo
{
   // This sample indicator shows how to use IndicatorArea
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class IndicatorAreaSample : Indicator
    {
        private TextBlock _indicatorAreaNumberTextBlock;
        protected override void Initialize()
        {
            var grid = new Grid(1, 2)
            {
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
                BackgroundColor = Color.Gold,
                Opacity = 0.7,
                Width = 200
            };
            grid.AddChild(new TextBlock { Text = "Indicator Area #", Margin = 5, FontWeight = FontWeight.ExtraBold, ForegroundColor = Color.Black }, 0, 0);
            _indicatorAreaNumberTextBlock = new TextBlock
            {
                Margin = 5,
                Text = Chart.IndicatorAreas.Count.ToString(),
                FontWeight = FontWeight.ExtraBold,
                ForegroundColor = Color.Black
            };
            grid.AddChild(_indicatorAreaNumberTextBlock, 0, 1);
            
// SHOW GRID ##########################################################################             
//             Chart.AddControl(grid);
            IndicatorArea.AddControl(grid);
            
            
            Chart.IndicatorAreaAdded += Chart_IndicatorAreaAdded;
            Chart.IndicatorAreaRemoved += Chart_IndicatorAreaRemoved;
        }
        private void Chart_IndicatorAreaRemoved(IndicatorAreaRemovedEventArgs obj)
        {
            _indicatorAreaNumberTextBlock.Text = Chart.IndicatorAreas.Count.ToString();
        }
        private void Chart_IndicatorAreaAdded(IndicatorAreaAddedEventArgs obj)
        {
            _indicatorAreaNumberTextBlock.Text = Chart.IndicatorAreas.Count.ToString();
        }
        public override void Calculate(int index)
        {
        }
    }

}


@ctid2948110

PanagiotisChar
04 Oct 2023, 06:03 ( Updated at: 04 Oct 2023, 06:09 )

Check your log, your indicator throws an exception. You need to fix that first.


@PanagiotisChar

ctid2948110
04 Oct 2023, 07:12 ( Updated at: 04 Oct 2023, 15:24 )

Panagiotis - Actually this code sample only has problems when "IsOverlay = false"

Hi Panagiotis, thanks for looking at this, I couldn't see any exceptions debugging in VS2022 or the cTrader > Tradewatch > Automate tab ?

(Which Log did you view and which exception did you see ?. I couldn't see what you mentioned :(  )

When "IsOverlay = false" and you add this Custom Indicator to a Chart it doesn't display correctly.

But if you close cTrader (with this Indicator active) & re-open it, this Custom Indicator displays correctly ?

Any ideas ??


@ctid2948110

PanagiotisChar
05 Oct 2023, 05:58 ( Updated at: 21 Dec 2023, 09:23 )

Hi again,

Here you go

If you debug your indicator, VS will break at the exact point of the exception


@PanagiotisChar

ctid2948110
05 Oct 2023, 21:07

RE: Here is the source from the cTrader example not working ?

ctid2948110 said: 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo
{
   // This sample indicator shows how to use IndicatorArea
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class IndicatorAreaSample : Indicator
    {
        private TextBlock _indicatorAreaNumberTextBlock;
        protected override void Initialize()
        {
            var grid = new Grid(1, 2)
            {
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
                BackgroundColor = Color.Gold,
                Opacity = 0.7,
                Width = 200
            };
            grid.AddChild(new TextBlock { Text = "Indicator Area #", Margin = 5, FontWeight = FontWeight.ExtraBold, ForegroundColor = Color.Black }, 0, 0);
            _indicatorAreaNumberTextBlock = new TextBlock
            {
                Margin = 5,
                Text = Chart.IndicatorAreas.Count.ToString(),
                FontWeight = FontWeight.ExtraBold,
                ForegroundColor = Color.Black
            };
            grid.AddChild(_indicatorAreaNumberTextBlock, 0, 1);
            
// SHOW GRID ##########################################################################             
//             Chart.AddControl(grid);
            IndicatorArea.AddControl(grid);
            
            
            Chart.IndicatorAreaAdded += Chart_IndicatorAreaAdded;
            Chart.IndicatorAreaRemoved += Chart_IndicatorAreaRemoved;
        }
        private void Chart_IndicatorAreaRemoved(IndicatorAreaRemovedEventArgs obj)
        {
            _indicatorAreaNumberTextBlock.Text = Chart.IndicatorAreas.Count.ToString();
        }
        private void Chart_IndicatorAreaAdded(IndicatorAreaAddedEventArgs obj)
        {
            _indicatorAreaNumberTextBlock.Text = Chart.IndicatorAreas.Count.ToString();
        }
        public override void Calculate(int index)
        {
        }
    }

}

Test


@ctid2948110

ctid2948110
05 Oct 2023, 21:15 ( Updated at: 21 Dec 2023, 09:23 )

RE: Custom Indicator - IndicatorAreaSample not working on cTrader 4.8.25 ?

PanagiotisChar said: 

Hi again,

Here you go

If you debug your indicator, VS will break at the exact point of the exception

Hi Again,

I was unable to reproduce the exception you confirmed, but thanks for explaining how you debugged it.

Here is a Link to my code with source of IndicatorAreaSample from the cTrader help, compiled in VS2022 for Debug with .Net 6.0.

Btw I noticed your running cTrader 4.8.28 while I'm on 4.8.25 both with Pepperstone Europe, could this be a problem ?

Any help would be appreciated ;)


@ctid2948110

PanagiotisChar
06 Oct 2023, 05:40 ( Updated at: 21 Dec 2023, 09:23 )

I tried it and works fine for me


@PanagiotisChar