Template code
using System; using cAlgo.API; using cAlgo.API.Collections; using cAlgo.API.Indicators; using cAlgo.API.Internals; using System.Linq;
namespace cAlgo.Robots { [Robot(AccessRights = AccessRights.None, AddIndicators = true)] public class ChartTemplatesTest : Robot { protected override void OnStart() { var templatesComboBox = new ComboBox();
templatesComboBox.AddItem("Click to view available templates");
templatesComboBox.SelectedItem = "Click to view available templates";
foreach (var template in ChartTemplates)
{
templatesComboBox.AddItem(template.Name);
}
ChartTemplates.Added += args =>
{
templatesComboBox.AddItem(args.ChartTemplate.Name);
Print($"A new template was added: {args.ChartTemplate.Name}");
};
ChartTemplates.Removed += args =>
{
templatesComboBox.RemoveItem(args.ChartTemplate.Name);
Print($"A template was removed: {args.ChartTemplate.Name}");
};
var showTemplateInfoButton = new Button { Text = "Show the template information" };
showTemplateInfoButton.Click += args =>
{
if (string.IsNullOrWhiteSpace(templatesComboBox.SelectedItem))
{
Print("No template is selected.");
return;
}
var template = ChartTemplates.Get(templatesComboBox.SelectedItem);
Print($"Name: {template.Name} | Bots #: {template.Robots.Count} | Indicators #: {template.Indicators.Count}");
};
var showDefaultTemplateInfoButton = new Button { Text = "Show the default template information" };
showDefaultTemplateInfoButton.Click += args =>
{
Print($"Name: {ChartTemplates.DefaultTemplate.Name} | Bots #: {ChartTemplates.DefaultTemplate.Robots.Count} | Indicators #: {ChartTemplates.DefaultTemplate.Indicators.Count}");
};
var removeTemplateButton = new Button { Text = "Remove the selected template" };
removeTemplateButton.Click += _ => ChartTemplates.Remove(ChartTemplates.Get(templatesComboBox.SelectedItem));
var applyTemplateButton = new Button { Text = "Apply the selected template" };
applyTemplateButton.Click += _ => ChartTemplates.Get(templatesComboBox.SelectedItem).Apply(Chart);
var duplicateTemplateButton = new Button { Text = "Duplicate the selected template" };
duplicateTemplateButton.Click += _ => ChartTemplates.Duplicate($"{templatesComboBox.SelectedItem} Duplicate", ChartTemplates.Get(templatesComboBox.SelectedItem));
var createTemplateTextBox = new TextBox { Text = "Click to type in a template name" };
var createEmptyTemplateButton = new Button { Text = "Create an empty template" };
createEmptyTemplateButton.Click += _ => ChartTemplates.Create(createTemplateTextBox.Text);
var createTemplateBasedOnChartButton = new Button { Text = "Create a template from the current chart" };
createTemplateBasedOnChartButton.Click += _ => ChartTemplates.Create(createTemplateTextBox.Text, Chart);
var changeDefaultTemplateToSelectedButton = new Button { Text = "Set the selected template as the default" };
changeDefaultTemplateToSelectedButton.Click += _ => ChartTemplates.DefaultTemplate = ChartTemplates.Get(templatesComboBox.SelectedItem);
var checkSelectedTemplateExistsButton = new Button { Text = "Check whether the selected template exists" };
checkSelectedTemplateExistsButton.Click += _ => Print($"The template '{templatesComboBox.SelectedItem}' exists: {ChartTemplates.Contains(templatesComboBox.SelectedItem)}");
var removeSelectedTemplateIndicator = new Button { Text = "Remove an indicator from the selected template" };
removeSelectedTemplateIndicator.Click += _ =>
{
if (string.IsNullOrWhiteSpace(templatesComboBox.SelectedItem))
{
Print("No template is selected.");
return;
}
var template = ChartTemplates.Get(templatesComboBox.SelectedItem);
if (template.Indicators.Count == 0)
return;
Print($"Removal result: {template.Indicators.Remove(template.Indicators.First())}");
};
var removeSelectedTemplateBot = new Button { Text = "Remove a cBot from the selected template" };
removeSelectedTemplateBot.Click += _ =>
{
if (string.IsNullOrWhiteSpace(templatesComboBox.SelectedItem))
{
Print("No template is selected.");
return;
}
var template = ChartTemplates.Get(templatesComboBox.SelectedItem);
if (template.Robots.Count == 0)
return;
Print($"Removal result: {template.Robots.Remove(template.Robots.First())}");
};
var indicatorsCombobox = new ComboBox();
indicatorsCombobox.AddItem("Click to select an indicator");
indicatorsCombobox.SelectedItem = "Click to select an indicator";
foreach (var indicator in AlgoRegistry.Indicators.OrderBy(i => i.Name))
{
indicatorsCombobox.AddItem(indicator.Name);
}
var addIndicatorWithDefaultParametersToSelectedTemplate = new Button { Text = "Add the selected indicator to the selected template using default parameters" };
addIndicatorWithDefaultParametersToSelectedTemplate.Click += _ =>
{
if (string.IsNullOrWhiteSpace(templatesComboBox.SelectedItem))
{
Print("No template is selected.");
return;
}
var template = ChartTemplates.Get(templatesComboBox.SelectedItem);
if (string.IsNullOrWhiteSpace(indicatorsCombobox.SelectedItem))
{
Print("No indicator is selected.");
return;
}
Print($"Adding the indicator {indicatorsCombobox.SelectedItem}.");
template.Indicators.Add(indicatorsCombobox.SelectedItem);
};
var robotsCombobox = new ComboBox();
robotsCombobox.AddItem("Click to select a cBot");
robotsCombobox.SelectedItem = "Click to select a cBot";
foreach (var robot in AlgoRegistry.Robots.OrderBy(i => i.Name))
{
robotsCombobox.AddItem(robot.Name);
}
var addRobotWithDefaultParametersToSelectedTemplate = new Button { Text = "Add the selected cBot to the selected template using default parameters" };
addRobotWithDefaultParametersToSelectedTemplate.Click += _ =>
{
if (string.IsNullOrWhiteSpace(templatesComboBox.SelectedItem))
{
Print("No template is selected.");
return;
}
var template = ChartTemplates.Get(templatesComboBox.SelectedItem);
if (string.IsNullOrWhiteSpace(robotsCombobox.SelectedItem))
{
Print("No cBot is selected.");
return;
}
Print($"Adding the cBot {robotsCombobox.SelectedItem}.");
template.Robots.Add(robotsCombobox.SelectedItem);
};
var addEmaIndicatorWithDifferentParametersToSelectedTemplate = new Button { Text = "Add an EMA indicator to the selected template using custom parameters" };
addEmaIndicatorWithDifferentParametersToSelectedTemplate.Click += _ =>
{
if (string.IsNullOrWhiteSpace(templatesComboBox.SelectedItem))
{
Print("No template is selected.");
return;
}
var template = ChartTemplates.Get(templatesComboBox.SelectedItem);
var emaType = AlgoRegistry.Get("Exponential Moving Average", AlgoKind.StandardIndicator) as IndicatorType;
Print($"Adding the indicator {emaType.Name} with a period of 100.");
template.Indicators.Add(emaType, Bars.OpenPrices, 100);
};
var changeEmaIndicatorParametersForSelectedTemplate = new Button { Text = "Change the EMA indicator parameters in the selected template" };
changeEmaIndicatorParametersForSelectedTemplate.Click += _ =>
{
if (string.IsNullOrWhiteSpace(templatesComboBox.SelectedItem))
{
Print("No template is selected.");
return;
}
var template = ChartTemplates.Get(templatesComboBox.SelectedItem);
var emaType = AlgoRegistry.Get("Exponential Moving Average", AlgoKind.StandardIndicator) as IndicatorType;
if (template.Indicators.FirstOrDefault(i => i.Type == emaType) is not { } templateIndicator)
{
Print($"The EMA indicator was not found in the template '{template.Name}'.");
return;
}
Print($"Changing the indicator {emaType.Name} period to 200.");
templateIndicator.Parameters["Periods"].Value = 200;
};
var setAllColorSettingsToRedForSelectedTemplate = new Button { Text = "Set all colour settings to red for the selected template" };
setAllColorSettingsToRedForSelectedTemplate.Click += _ =>
{
if (string.IsNullOrWhiteSpace(templatesComboBox.SelectedItem))
{
Print("No template is selected.");
return;
}
var template = ChartTemplates.Get(templatesComboBox.SelectedItem);
template.ColorSettings.AskPriceLineColor = Color.Red;
template.ColorSettings.BidPriceLineColor = Color.Red;
template.ColorSettings.BackgroundColor = Color.Red;
template.ColorSettings.BearFillColor = Color.Red;
template.ColorSettings.BearOutlineColor = Color.Red;
template.ColorSettings.BullFillColor = Color.Red;
template.ColorSettings.BullOutlineColor = Color.Red;
template.ColorSettings.BuyColor = Color.Red;
template.ColorSettings.ForegroundColor = Color.Red;
template.ColorSettings.GridLinesColor = Color.Red;
template.ColorSettings.LosingDealColor = Color.Red;
template.ColorSettings.PeriodSeparatorColor = Color.Red;
template.ColorSettings.SellColor = Color.Red;
template.ColorSettings.TickVolumeColor = Color.Red;
template.ColorSettings.WinningDealColor = Color.Red;
template.ColorSettings.AreaFillColor = Color.Red;
};
var enableAllDisplayOptionsForSelectedTemplate = new Button { Text = "Enable all display options for the selected template" };
enableAllDisplayOptionsForSelectedTemplate.Click += _ =>
{
if (string.IsNullOrWhiteSpace(templatesComboBox.SelectedItem))
{
Print("No template is selected.");
return;
}
var template = ChartTemplates.Get(templatesComboBox.SelectedItem);
template.DisplaySettings.AskPriceLine = true;
template.DisplaySettings.Bars = true;
template.DisplaySettings.BidPriceLine = true;
template.DisplaySettings.ChartScale = true;
template.DisplaySettings.DealMap = true;
template.DisplaySettings.Grid = true;
template.DisplaySettings.IndicatorTitles = true;
template.DisplaySettings.MarketSentiment = true;
template.DisplaySettings.Orders = true;
template.DisplaySettings.PeriodSeparators = true;
template.DisplaySettings.Positions = true;
template.DisplaySettings.PriceAlerts = true;
template.DisplaySettings.PriceAxisOverlayButtons = true;
template.DisplaySettings.QuickTradeButtons = true;
template.DisplaySettings.Targets = true;
template.DisplaySettings.TickVolume = true;
template.DisplaySettings.AddRobotButton = true;
};
var disableAllDisplayOptionsForSelectedTemplate = new Button { Text = "Disable all display options for the selected template" };
disableAllDisplayOptionsForSelectedTemplate.Click += _ =>
{
if (string.IsNullOrWhiteSpace(templatesComboBox.SelectedItem))
{
Print("No template is selected.");
return;
}
var template = ChartTemplates.Get(templatesComboBox.SelectedItem);
template.DisplaySettings.AskPriceLine = false;
template.DisplaySettings.Bars = false;
template.DisplaySettings.BidPriceLine = false;
template.DisplaySettings.ChartScale = false;
template.DisplaySettings.DealMap = false;
template.DisplaySettings.Grid = false;
template.DisplaySettings.IndicatorTitles = false;
template.DisplaySettings.MarketSentiment = false;
template.DisplaySettings.Orders = false;
template.DisplaySettings.PeriodSeparators = false;
template.DisplaySettings.Positions = false;
template.DisplaySettings.PriceAlerts = false;
template.DisplaySettings.PriceAxisOverlayButtons = false;
template.DisplaySettings.QuickTradeButtons = false;
template.DisplaySettings.Targets = false;
template.DisplaySettings.TickVolume = false;
template.DisplaySettings.AddRobotButton = false;
};
var changeChartTypeForSelectedTemplate = new Button { Text = "Change the chart type for the selected template" };
changeChartTypeForSelectedTemplate.Click += _ =>
{
if (string.IsNullOrWhiteSpace(templatesComboBox.SelectedItem))
{
Print("No template is selected.");
return;
}
var template = ChartTemplates.Get(templatesComboBox.SelectedItem);
template.DisplaySettings.ChartType = template.DisplaySettings.ChartType switch
{
ChartType.Area => ChartType.Bars,
ChartType.Bars => ChartType.Candlesticks,
ChartType.Candlesticks => ChartType.Dots,
ChartType.Dots => ChartType.Hlc,
ChartType.Hlc => ChartType.Line,
_ => ChartType.Area
};
Print($"Changed the template '{template.Name}' chart type to '{template.DisplaySettings.ChartType}'.");
};
var changeZoomeLevelForSelectedTemplate = new Button { Text = "Change the zoom level for the selected template" };
changeZoomeLevelForSelectedTemplate.Click += _ =>
{
if (string.IsNullOrWhiteSpace(templatesComboBox.SelectedItem))
{
Print("No template is selected.");
return;
}
var template = ChartTemplates.Get(templatesComboBox.SelectedItem);
template.DisplaySettings.ZoomLevel = template.DisplaySettings.ZoomLevel switch
{
5 => 400,
400 => 5,
_ => 5
};
Print($"Changed the template '{template.Name}' zoom level to '{template.DisplaySettings.ZoomLevel}'.");
};
var panel = new StackPanel { Orientation = Orientation.Vertical, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center };
panel.AddChild(templatesComboBox);
panel.AddChild(showTemplateInfoButton);
panel.AddChild(showDefaultTemplateInfoButton);
panel.AddChild(removeTemplateButton);
panel.AddChild(applyTemplateButton);
panel.AddChild(duplicateTemplateButton);
panel.AddChild(createTemplateTextBox);
panel.AddChild(createEmptyTemplateButton);
panel.AddChild(createTemplateBasedOnChartButton);
panel.AddChild(changeDefaultTemplateToSelectedButton);
panel.AddChild(checkSelectedTemplateExistsButton);
panel.AddChild(removeSelectedTemplateIndicator);
panel.AddChild(removeSelectedTemplateBot);
panel.AddChild(indicatorsCombobox);
panel.AddChild(addIndicatorWithDefaultParametersToSelectedTemplate);
panel.AddChild(robotsCombobox);
panel.AddChild(addRobotWithDefaultParametersToSelectedTemplate);
panel.AddChild(addEmaIndicatorWithDifferentParametersToSelectedTemplate);
panel.AddChild(changeEmaIndicatorParametersForSelectedTemplate);
panel.AddChild(setAllColorSettingsToRedForSelectedTemplate);
panel.AddChild(enableAllDisplayOptionsForSelectedTemplate);
panel.AddChild(disableAllDisplayOptionsForSelectedTemplate);
panel.AddChild(changeChartTypeForSelectedTemplate);
panel.AddChild(changeZoomeLevelForSelectedTemplate);
Chart.AddControl(panel);
}
}
}