This article complements our extensive list of indicator code samples. We elaborate on some concepts mentioned in these code samples and discuss several advanced features you can implement when creating new indicators.
usingcAlgo.API;usingcAlgo.API.Indicators;usingSystem;namespacecAlgo{/// <summary>/// This indicator shows how to make a built-in cTrader indicator multi time frame and how to use cloud attribute/// </summary>[Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None), Cloud("Top", "Bottom", Opacity = 0.2)]publicclassBollingerBandsMTFCloudSample:Indicator{privateBollingerBands_bollingerBands;privateBars_baseBars;[Parameter("Base TimeFrame", DefaultValue = "Daily")]publicTimeFrameBaseTimeFrame{get;set;}[Parameter("Source", DefaultValue = DataSeriesType.Close)]publicDataSeriesTypeDataSeriesType{get;set;}[Parameter("Periods", DefaultValue = 14, MinValue = 0)]publicintPeriods{get;set;}[Parameter("Standard Deviation", DefaultValue = 2, MinValue = 0)]publicdoubleStandardDeviation{get;set;}[Parameter("MA Type", DefaultValue = MovingAverageType.Simple)]publicMovingAverageTypeMaType{get;set;}[Output("Main", LineColor = "Yellow", PlotType = PlotType.Line, Thickness = 1)]publicIndicatorDataSeriesMain{get;set;}[Output("Top", LineColor = "Red", PlotType = PlotType.Line, Thickness = 1)]publicIndicatorDataSeriesTop{get;set;}[Output("Bottom", LineColor = "Red", PlotType = PlotType.Line, Thickness = 1)]publicIndicatorDataSeriesBottom{get;set;}protectedoverridevoidInitialize(){_baseBars=MarketData.GetBars(BaseTimeFrame);varbaseSeries=GetBaseSeries();_bollingerBands=Indicators.BollingerBands(baseSeries,Periods,StandardDeviation,MaType);}publicoverridevoidCalculate(intindex){varbaseIndex=_baseBars.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);Main[index]=_bollingerBands.Main[baseIndex];Top[index]=_bollingerBands.Top[baseIndex];Bottom[index]=_bollingerBands.Bottom[baseIndex];}privateDataSeriesGetBaseSeries(){switch(DataSeriesType){caseDataSeriesType.Open:return_baseBars.OpenPrices;caseDataSeriesType.High:return_baseBars.HighPrices;caseDataSeriesType.Low:return_baseBars.LowPrices;caseDataSeriesType.Close:return_baseBars.ClosePrices;default:thrownewArgumentOutOfRangeException("DataSeriesType");}}}publicenumDataSeriesType{Open,High,Low,Close}}
The CloudAttribute constructor takes two output line names. Afterward, it automatically plots the 'cloud' to match the space between these two outputs.
You can also set the opacity of the 'cloud' by using the Opacity property. To set up the 'cloud' color, use the FirstColor property. By default, the color of the 'cloud' will match the color of the first line in the indicator output.
The Automate API includes the Colorenum that can be used to configure the colors for Chart objects, chart controls, and outputs.
The Color class contains several constants representing commonly used colors. You can, therefore, use these colors without having to deal with hexadecimal or ARGB color codes.
When customizing outputs, you can set their colors by using the LineColor string property. As shown below, it accepts both named colors and hexadecimal color codes.
1234
_=Chart.DrawStaticText("NamedColor","This is text using Color class color name properties",VerticalAlignment.Center,HorizontalAlignment.Center,Color.Red);_=Chart.DrawStaticText("HexadecimalColor","This is text using Hexadecimal color",VerticalAlignment.Bottom,HorizontalAlignment.Center,Color.FromHex("#FF5733"));_=Chart.DrawStaticText("ARGBColor","This is text using ARGB color",VerticalAlignment.Top,HorizontalAlignment.Center,Color.FromArgb(255,200,100,60));_=Chart.DrawStaticText("ParsedNameColor","This is text using color name by parsing it from string",VerticalAlignment.Center,HorizontalAlignment.Left,Color.FromName("Yellow"));
1 2 3 4 5 6 7 8 910111213
varstackPanel=newStackPanel{Orientation=Orientation.Vertical,HorizontalAlignment=HorizontalAlignment.Center,VerticalAlignment=VerticalAlignment.Center};stackPanel.AddChild(newTextBlock{Text="Red Color property",BackgroundColor=Color.Red});stackPanel.AddChild(newTextBlock{Text="Hexadecimal Color code",BackgroundColor=Color.FromHex("#FF5733")});stackPanel.AddChild(newTextBlock{Text="ARGB Color",BackgroundColor=Color.FromArgb(200,100,40,80)});stackPanel.AddChild(newTextBlock{Text="Color Name",BackgroundColor=Color.FromName("Green")});Chart.AddControl(stackPanel);
Note that colors can also be treated as customizable parameters. To allow users to select custom colors (e.g., the colors of the lines drawn by an indicator), declare a parameter of the Color type.
In the below example, we create a simple 'high minus low' indicator that, upon being initialized, displays text on the trading chart to which it is attached.
1 2 3 4 5 6 7 8 910111213141516171819
usingcAlgo.API;usingcAlgo.API.Indicators;usingcAlgo.API.Collections;usingcAlgo.API.Internals;namespacecAlgo{[Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]publicclassHighMinusLowColor:Indicator{[Parameter("Text Color", DefaultValue="#f54242")]publicColorTextColor{get;set;}publicoverridevoidInitialize(){varstaticText=Chart.DrawStaticText("static","This text shows how color parameters work",VerticalAlignment.Center,HorizontalAlignment.Center,TextColor);}}}
The enum type is needed to create parameters that have several predefined options from which users could choose. We use the enum type in the following example.
Use the Bars.OpenTime collection to get the bar open times. The time zone will be based on the time zone you have specified in the TimeZone class attribute.
Use the Application.UserTimeOffset property to get the time zone of the user's platform. This is primarily used for converting your indicator time to the user's time zone.
The Application.UserTimeOffset property returns a TimeSpan object representing the time offset set by the user in their cTrader platform compared to UTC time.
You can also choose to get notified when a user changes their platform time offset. To do so, use the Application.UserTimeOffsetChanged event.