Invoking a Button Click Event
Created at 01 Dec 2021, 03:12
Invoking a Button Click Event
01 Dec 2021, 03:12
Hi Guys,
I think this is a simple thing to do, but i'm not sure of the approach.
I'd like to be able to create a Button Click Event on my Toggle Buttons when certain conditions are met. The conditions will be set in OnTick() .
i.e. If ( x > y)
{
SToggleButton_Checked.Click;
}
Any help would be greatly appreciated.
using System;
using System.Collections.Generic;
using cAlgo.API;
using cAlgo.API.Internals;
using System.Linq;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
using System.Windows;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class TESTPOS_Original : Robot
{
[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; }
[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; }
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()
{
// Here i'd like to place the Button Click event, via an if statement. I.e.
// if(x > y)
// {ButtonClickevent for SToggleButton_Checked}
// if(y > x)
// {ButtonClickevent for LToggleButton_Checked}
}
protected override void OnStop()
{
}
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 static void SToggleButton_Checked(ToggleButtonEventArgs obj)
{
//_robot.ExecuteMarketOrder(TradeType.Sell, _symbol.Name, 1000);
obj.ToggleButton.Text = ("SHORT POSITIONS ON");
obj.ToggleButton.Style = Styles.ShortON();
ShortsEnabled = true;
}
public static void SToggleButton_Unchecked(ToggleButtonEventArgs obj)
{
obj.ToggleButton.Text = ("SHORT POSITIONS OFF");
obj.ToggleButton.Style = Styles.TradeOFF();
ShortsEnabled = false;
}
public static void LToggleButton_Checked(ToggleButtonEventArgs obj)
{
//_robot.ExecuteMarketOrder(TradeType.Buy, _symbol.Name, 1000);
obj.ToggleButton.Text = ("LONG POSITIONS ON");
obj.ToggleButton.Style = Styles.LongON();
LongsEnabled = true;
}
public static void LToggleButton_Unchecked(ToggleButtonEventArgs obj)
{
obj.ToggleButton.Text = ("LONG POSITIONS OFF");
obj.ToggleButton.Style = Styles.TradeOFF();
LongsEnabled = false;
}
}
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;
}
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);
}
}
}
}
amusleh
01 Dec 2021, 09:47
Hi,
You can use properties for your buttons and then you will be able to access them from class instance and do whatever you want to with them.
Try this (Change the OnTick if conditions):
@amusleh