show text on chart (user select color and position of text)

Created at 18 Jun 2021, 14:40
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!
IR

IRCtrader

Joined 17.06.2021

show text on chart (user select color and position of text)
18 Jun 2021, 14:40


i want to show a text on chart but user should select color, font size, and position text on chart.. please give me a sample code.

is there possible to do that without stackpanel?


@IRCtrader
Replies

amusleh
18 Jun 2021, 20:11

Hi,

cTrader Automate API gives you two option for drawing on charts:

  1. Chart Controls
  2. Chart Objects

The first one is static and doesn't depend on your chart price and time axis, the second one is more dynamic and you can position it based on chart price and time axis.

For your case I thing you should use ChartText not chart controls.

To allow user to interact with your chart object (change font size, color,...) set its IsInteractive property to True.

First call the Chart.DrawText and save the returning ChartText object on a variable, then set its IsInteractive property to True:

// You can also use time instead of BarIndex
var chartText = Chart.DrawText("Text", "My text", barIndex, price, color);

chartText.IsInteractive = True;

// If you want to lock the object position and only allow user to change // text properties set its IsLocked to True

chartText.IsLocked = True;

Checkout the ChartText example on API references and if you need a more advanced sample check the Pattern Drawing indicator code.


@amusleh

IRCtrader
19 Jun 2021, 21:24

RE: color option

amusleh said:

Hi,

cTrader Automate API gives you two option for drawing on charts:

  1. Chart Controls
  2. Chart Objects

The first one is static and doesn't depend on your chart price and time axis, the second one is more dynamic and you can position it based on chart price and time axis.

For your case I thing you should use ChartText not chart controls.

To allow user to interact with your chart object (change font size, color,...) set its IsInteractive property to True.

First call the Chart.DrawText and save the returning ChartText object on a variable, then set its IsInteractive property to True:

// You can also use time instead of BarIndex
var chartText = Chart.DrawText("Text", "My text", barIndex, price, color);

chartText.IsInteractive = True;

// If you want to lock the object position and only allow user to change // text properties set its IsLocked to True

chartText.IsLocked = True;

Checkout the ChartText example on API references and if you need a more advanced sample check the Pattern Drawing indicator code.

Could you give me a sample about color parameter for text. User should select text color.

 


@IRCtrader

IRCtrader
22 Jun 2021, 18:29

Sample code

finally i make it.

//Designed by AcademyWave Student



using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;

namespace cAlgo
{
    // This sample indicator shows how to add a text block control on your chart
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class AWSpread : Indicator
    {
        TextBlock tb = new TextBlock();
        [Parameter("FontSize", DefaultValue = 12)]
        public int FontSize { get; set; }
        [Parameter("Space to Corner", DefaultValue = 10)]
        public int Margin { get; set; }
        [Parameter("Horizental Alignment", DefaultValue = HorizontalAlignment.Right)]
        public HorizontalAlignment HAlignment { get; set; }
        [Parameter("Vertical Alignment", DefaultValue = VerticalAlignment.Bottom)]
        public VerticalAlignment VAlignment { get; set; }
        [Parameter("Color", DefaultValue = "Red")]
        public string Color1 { get; set; }


        protected override void Initialize()
        {

        }
        public override void Calculate(int index)
        {

            if (IsLastBar)
                DisplaySpreadOnChart();
        }

        public void DisplaySpreadOnChart()
        {

            var spread = Math.Round(Symbol.Spread / Symbol.PipSize, 2);
            string sp = string.Format("{0}", spread);
            tb.Text = sp;
            tb.FontSize = FontSize;
            tb.ForegroundColor = Color1.TrimEnd();
            tb.HorizontalAlignment = HAlignment;
            tb.VerticalAlignment = VAlignment;
            tb.Margin = Margin;
            Chart.AddControl(tb);
        }



    }


}

 


@IRCtrader