Using the togglebutton with a function
Using the togglebutton with a function
26 Nov 2021, 23:39
Hi Guys,
I have the following code, it creates 2 toggle buttons that are either inactive, on or off.
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);
var border = new Border
{
VerticalAlignment = PanelVerticalAlignment,
HorizontalAlignment = PanelHorizontalAlignment,
Style = Styles.CreatePanelBackgroundStyle(),
Margin = "20 40 20 20",
Width = 225,
Child = tradingPanel
};
Chart.AddControl(border);
}
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());
}
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();
}
public void SToggleButton_Unchecked(ToggleButtonEventArgs obj)
{
obj.ToggleButton.Text = ("SHORT POSITIONS OFF");
obj.ToggleButton.Style = Styles.TradeOFF();
}
public void LToggleButton_Checked(ToggleButtonEventArgs obj)
{
obj.ToggleButton.Text = ("LONG POSITIONS ON");
obj.ToggleButton.Style = Styles.LongON();
}
public void LToggleButton_Unchecked(ToggleButtonEventArgs obj)
{
obj.ToggleButton.Text = ("LONG POSITIONS OFF");
obj.ToggleButton.Style = Styles.TradeOFF();
}
}
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);
}
}
}
}
I'd like to update the global parameters "ShortsEnabled" or "LongsEnabled" when the toggle buttons are pressed, either updating the boolean value to "True" if checked, or "False" if unchecked.
Is there a way this is possible?
Thanks for your time,
Replies
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
amusleh
29 Nov 2021, 09:16
Hi,
There are multiple ways to do that, here is an example by using Action delegates:
@amusleh