Topics
Replies
MTrade12
04 Dec 2021, 04:43
RE:
MTrade12 said:
Hi Guys,
Just a quick, and what i imagine to be a simple one!
I want to create a label that's displayed on my chart at the location of my Horizontal Line.
Here's the code that draws the line:
using System; using System.Collections.Generic; using cAlgo.API; using cAlgo.API.Internals; using System.Linq; using cAlgo.API.Indicators; using cAlgo.Indicators; namespace cAlgo.Robots { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class ChartDrawings : Robot { protected override void OnStart() { } protected override void OnTick() { var monthly = MarketData.GetBars(TimeFrame.Monthly, Symbol.Name); Chart.DrawHorizontalLine("MONTH OPEN", monthly.Last(0).Open, Color.Green, 2, LineStyle.Lines); //Chart.DrawText("MONTHLABEL", "MONTHLY OPEN", monthly.Last(0).OpenTime, monthly.Last(0).Close, Color.Purple); } } }
As you can see, i've tried to use Chart.DrawText to place the label at the same price as the Horizontal Line, but it doesn't show anywhere. Perhaps the Y axis is wrong?
Thank you for your help and time!
Hey Guys,
I figured it out! :-)
Here's the code i used in case anyone wants the same thing in the future:
var monthly = MarketData.GetBars(TimeFrame.Monthly, Symbol.Name);
DateTime today = System.DateTime.Now;
TimeSpan duration = new System.TimeSpan(0, 0, 10, 0);
DateTime answer = openTime.Add(duration);
Chart.DrawText("MONTHLABEL", "MONTHLY OPEN", answer, monthly.Last(0).Open, Color.Purple);
@MTrade12
MTrade12
01 Dec 2021, 03:15
RE:
amusleh said:
Hi,
There are multiple ways to do that, here is an example by using Action delegates:
using System; using System.Collections.Generic; using cAlgo.API; using cAlgo.API.Internals; namespace cAlgo.Robots { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class SampleTradingPanel : Robot { [Parameter("Vertical Position", Group = "Panel alignment", DefaultValue = VerticalAlignment.Top)] public VerticalAlignment PanelVerticalAlignment { get; set; } [Parameter("Horizontal Position", Group = "Panel alignment", DefaultValue = HorizontalAlignment.Left)] public HorizontalAlignment PanelHorizontalAlignment { get; set; } [Parameter("Trading Direction Short", Group = "Trading Direction", DefaultValue = false)] public bool ShortsEnabled { get; set; } [Parameter("Trading Direction Long", Group = "Trading Direction", DefaultValue = false)] public bool LongsEnabled { get; set; } protected override void OnStart() { var tradingPanel = new TradingPanel(this, Symbol) { ShortChecked = ShortChecked, ShortUnchecked = ShortUnchecked, LongChecked = LongChecked, LongUnchecked = LongUnchecked }; var border = new Border { VerticalAlignment = PanelVerticalAlignment, HorizontalAlignment = PanelHorizontalAlignment, Style = Styles.CreatePanelBackgroundStyle(), Margin = "20 40 20 20", Width = 225, Child = tradingPanel }; Chart.AddControl(border); } private void ShortChecked() { Print("ShortChecked"); } private void ShortUnchecked() { Print("ShortUnchecked"); } private void LongChecked() { Print("LongChecked"); } private void LongUnchecked() { Print("LongUnchecked"); } protected override void OnTick() { } public class TradingPanel : CustomControl { private readonly IDictionary<string, TextBox> _inputMap = new Dictionary<string, TextBox>(); private readonly Robot _robot; private readonly Symbol _symbol; public TradingPanel(Robot robot, Symbol symbol) { _robot = robot; _symbol = symbol; AddChild(CreateTradingPanel()); } public Action ShortChecked { get; set; } public Action ShortUnchecked { get; set; } public Action LongChecked { get; set; } public Action LongUnchecked { get; set; } private ControlBase CreateTradingPanel() { var mainPanel = new StackPanel(); var contentPanel = CreateContentPanel(); mainPanel.AddChild(contentPanel); return mainPanel; } private StackPanel CreateContentPanel() { var contentPanel = new StackPanel { Margin = 10 }; var grid = new Grid(4, 3); grid.Columns[1].SetWidthInPixels(5); var toggleButton = new ToggleButton { Text = "SHORT - AWAITING INPUT", Margin = 10, Style = Styles.TradeOFF() }; toggleButton.Checked += SToggleButton_Checked; toggleButton.Unchecked += SToggleButton_Unchecked; contentPanel.AddChild(toggleButton); var ltoggleButton = new ToggleButton { Text = "LONG - AWAITING INPUT", Margin = 10, Style = Styles.TradeOFF() }; ltoggleButton.Checked += LToggleButton_Checked; ltoggleButton.Unchecked += LToggleButton_Unchecked; contentPanel.AddChild(ltoggleButton); contentPanel.AddChild(grid); return contentPanel; } public void SToggleButton_Checked(ToggleButtonEventArgs obj) { //testprint(); obj.ToggleButton.Text = ("SHORT POSITIONS ON"); obj.ToggleButton.Style = Styles.ShortON(); if (ShortChecked != null) { ShortChecked.Invoke(); } } public void SToggleButton_Unchecked(ToggleButtonEventArgs obj) { obj.ToggleButton.Text = ("SHORT POSITIONS OFF"); obj.ToggleButton.Style = Styles.TradeOFF(); if (ShortUnchecked != null) { ShortUnchecked.Invoke(); } } public void LToggleButton_Checked(ToggleButtonEventArgs obj) { obj.ToggleButton.Text = ("LONG POSITIONS ON"); obj.ToggleButton.Style = Styles.LongON(); if (LongChecked != null) { LongChecked.Invoke(); } } public void LToggleButton_Unchecked(ToggleButtonEventArgs obj) { obj.ToggleButton.Text = ("LONG POSITIONS OFF"); obj.ToggleButton.Style = Styles.TradeOFF(); if (LongUnchecked != null) { LongUnchecked.Invoke(); } } } public static class Styles { public static Style CreatePanelBackgroundStyle() { var style = new Style(); style.Set(ControlProperty.CornerRadius, 3); style.Set(ControlProperty.BackgroundColor, GetColorWithOpacity(Color.FromHex("#292929"), 0.85m), ControlState.DarkTheme); style.Set(ControlProperty.BackgroundColor, GetColorWithOpacity(Color.FromHex("#FFFFFF"), 0.85m), ControlState.LightTheme); style.Set(ControlProperty.BorderColor, Color.FromHex("#3C3C3C"), ControlState.DarkTheme); style.Set(ControlProperty.BorderColor, Color.FromHex("#C3C3C3"), ControlState.LightTheme); style.Set(ControlProperty.BorderThickness, new Thickness(1)); return style; } public static Style CreateCommonBorderStyle() { var style = new Style(); style.Set(ControlProperty.BorderColor, GetColorWithOpacity(Color.FromHex("#FFFFFF"), 0.12m), ControlState.DarkTheme); style.Set(ControlProperty.BorderColor, GetColorWithOpacity(Color.FromHex("#000000"), 0.12m), ControlState.LightTheme); return style; } public static Style CreateHeaderStyle() { var style = new Style(); style.Set(ControlProperty.ForegroundColor, GetColorWithOpacity("#FFFFFF", 0.70m), ControlState.DarkTheme); style.Set(ControlProperty.ForegroundColor, GetColorWithOpacity("#000000", 0.65m), ControlState.LightTheme); return style; } public static Style CreateInputStyle() { var style = new Style(DefaultStyles.TextBoxStyle); style.Set(ControlProperty.BackgroundColor, Color.FromHex("#1A1A1A"), ControlState.DarkTheme); style.Set(ControlProperty.BackgroundColor, Color.FromHex("#111111"), ControlState.DarkTheme | ControlState.Hover); style.Set(ControlProperty.BackgroundColor, Color.FromHex("#E7EBED"), ControlState.LightTheme); style.Set(ControlProperty.BackgroundColor, Color.FromHex("#D6DADC"), ControlState.LightTheme | ControlState.Hover); style.Set(ControlProperty.CornerRadius, 3); return style; } public static Style TradeOFF() { var style = new Style(); style.Set(ControlProperty.BackgroundColor, GetColorWithOpacity(Color.FromHex("#292929"), 0.85m), ControlState.DarkTheme); style.Set(ControlProperty.BackgroundColor, GetColorWithOpacity(Color.FromHex("#FFFFFF"), 0.85m), ControlState.LightTheme); style.Set(ControlProperty.BorderColor, Color.FromHex("#3C3C3C"), ControlState.DarkTheme); style.Set(ControlProperty.BorderColor, Color.FromHex("#C3C3C3"), ControlState.LightTheme); style.Set(ControlProperty.BorderThickness, new Thickness(1)); return style; } public static Style LongON() { var style = new Style(); style.Set(ControlProperty.BackgroundColor, GetColorWithOpacity(Color.FromHex("#009345"), 0.85m), ControlState.DarkTheme); style.Set(ControlProperty.BackgroundColor, GetColorWithOpacity(Color.FromHex("#009345"), 0.85m), ControlState.LightTheme); style.Set(ControlProperty.BorderColor, Color.FromHex("#3C3C3C"), ControlState.DarkTheme); style.Set(ControlProperty.BorderColor, Color.FromHex("#C3C3C3"), ControlState.LightTheme); style.Set(ControlProperty.BorderThickness, new Thickness(1)); return style; } public static Style ShortON() { var style = new Style(); style.Set(ControlProperty.BackgroundColor, GetColorWithOpacity(Color.FromHex("#F05824"), 0.85m), ControlState.DarkTheme); style.Set(ControlProperty.BackgroundColor, GetColorWithOpacity(Color.FromHex("#F05824"), 0.85m), ControlState.LightTheme); style.Set(ControlProperty.BorderColor, Color.FromHex("#3C3C3C"), ControlState.DarkTheme); style.Set(ControlProperty.BorderColor, Color.FromHex("#C3C3C3"), ControlState.LightTheme); style.Set(ControlProperty.BorderThickness, new Thickness(1)); return style; } public static Style CreateBuyButtonStyle() { return CreateButtonStyle(Color.FromHex("#009345"), Color.FromHex("#009345")); } public static Style CreateSellButtonStyle() { return CreateButtonStyle(Color.FromHex("#F05824"), Color.FromHex("#F05824")); } public static Style CreateCloseButtonStyle() { return CreateButtonStyle(Color.FromHex("#F05824"), Color.FromHex("#FF6C36")); } private static Style CreateButtonStyle(Color color, Color hoverColor) { var style = new Style(DefaultStyles.ButtonStyle); style.Set(ControlProperty.BackgroundColor, color, ControlState.DarkTheme); style.Set(ControlProperty.BackgroundColor, color, ControlState.LightTheme); style.Set(ControlProperty.BackgroundColor, color, ControlState.DarkTheme); style.Set(ControlProperty.BackgroundColor, color, ControlState.LightTheme); style.Set(ControlProperty.ForegroundColor, Color.FromHex("#FFFFFF"), ControlState.DarkTheme); style.Set(ControlProperty.ForegroundColor, Color.FromHex("#FFFFFF"), ControlState.LightTheme); return style; } private static Color GetColorWithOpacity(Color baseColor, decimal opacity) { var alpha = (int)Math.Round(byte.MaxValue * opacity, MidpointRounding.AwayFromZero); return Color.FromArgb(alpha, baseColor); } } } }
Thanks for the reply!
I actually managed to resolve the issue by making the Parameters static, as here:
[Parameter("Trading Direction Short", Group = "Trading Direction", DefaultValue = false)]
public static bool ShortsEnabled { get; set; }
[Parameter("Trading Direction Long", Group = "Trading Direction", DefaultValue = false)]
public static bool LongsEnabled { get; set; }
Issue Sorted :)
@MTrade12
MTrade12
24 Mar 2022, 14:05
RE:
amusleh said:
Hi amusleh!
Sorry, i realise now i could've been clearer in what i meant!
Let's say I want to manually draw a horizontal line on GBPCHF chart at 1.2265.
By default, my manually drawn horizontal line is called "Horizontal Line 1", and each subsequent line is "Horizontal Line +1".
Am i able using Calgo, to identify "Horizontal Line 1", and if so, can then obtain the price or X Value of it?
Hope that clarfies!
@MTrade12