Closing Button (日本語版) free

by summer in category Other at 20/01/2021
Description

「通貨ペア」ボタン = チャートの通貨ペアのポジションを全て決済します

「全て」ボタン = 通貨ペアに関わらずすべてのポジションを決済します

Warning! Executing the following cBot may result in loss of funds. Use it at your own risk.
Notification Publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section you may use the Copyright Infringement Notification form to submit a claim.
Formula / Source Code
Language: C#
Trading Platform: cAlgo
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 ClosingButton : Robot
    {
        protected override void OnStart()
        {
            var tradingPanel = new TradingPanel(this, Symbol);

            var border = new Border 
            {
                VerticalAlignment = VerticalAlignment.Bottom,
                HorizontalAlignment = HorizontalAlignment.Right,
                Style = Styles.CreatePanelBackgroundStyle(),
                Margin = "8 8 8 8",
                Width = 150,
                Child = tradingPanel
            };

            Chart.AddControl(border);
        }
    }

    public class TradingPanel : CustomControl
    {
        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 = 5
            };
            var grid = new Grid(1, 3);
            grid.Columns[1].SetWidthInPixels(5);

            var closeButton = CreateCloseButton("通貨ペア", Styles.CreateBuyButtonStyle());
            grid.AddChild(closeButton, 0, 0);

            var closeAllButton = CreateCloseAllButton("全て", Styles.CreateBuyButtonStyle());
            grid.AddChild(closeAllButton, 0, 2);

            contentPanel.AddChild(grid);

            return contentPanel;
        }

        private Button CreateCloseButton(string text, Style style)
        {
            var closeButton = new Button 
            {
                Text = text,
                Style = style,
                Height = 25
            };

            closeButton.Click += args => Close();

            return closeButton;
        }

        private ControlBase CreateCloseAllButton(string text, Style style)
        {
            var closeAllButton = new Button 
            {
                Text = text,
                Style = style,
                Height = 25
            };

            closeAllButton.Click += args => CloseAll();

            return closeAllButton;
        }

        private void Close()
        {
            foreach (var position in _robot.Positions)
            {
                if (position.SymbolName == _symbol.Name)
                {
                    _robot.ClosePositionAsync(position);
                }

            }

        }

        private void CloseAll()
        {
            foreach (var position in _robot.Positions)
                _robot.ClosePositionAsync(position);
        }
    }

    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 CreateBuyButtonStyle()
        {
            return CreateButtonStyle(Color.FromHex("#009345"), Color.FromHex("#10A651"));
        }

        public static Style CreateSellButtonStyle()
        {
            return CreateButtonStyle(Color.FromHex("#F05824"), Color.FromHex("#FF6C36"));
        }

        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, hoverColor, ControlState.DarkTheme | ControlState.Hover);
            style.Set(ControlProperty.BackgroundColor, hoverColor, ControlState.LightTheme | ControlState.Hover);
            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);
        }
    }
}
Comments
0