Category Other  Published on 29/03/2021

Trading Panel Experto

Description

This is a Simple Trading Panel, that allows you to backtest your strategies, by setting you Stop Loss in Pips, Take Profit in Pips, choosing the the number of Lots, and it also allows you to move your Take Profit and Stop Loss manual.

Grupo CTrader em Portugues -->>  https://t.me/ComunidadeCtrader

 

ly. Whenever you open a order with a Stop Loss or a Take Profit, the Cbot will plot horizontal lines on them so that you can manualy move them.


using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
using System.Text.RegularExpressions;


namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class TradePanelExperto : Robot
    {


        [Parameter("Vertical Position", Group = "Panel alignment", DefaultValue = VerticalAlignment.Top)]
        public VerticalAlignment PanelVerticalAlignment { get; set; }

        [Parameter("Horizontal Position", Group = "Panel alignment", DefaultValue = HorizontalAlignment.Center)]
        public HorizontalAlignment PanelHorizontalAlignment { get; set; }

        [Parameter("Take Profit Color", Group = "Take Profit / Stop Loss", DefaultValue = Colors.Green)]
        public Colors TPColor { get; set; }

        [Parameter("Stop Loss Color", Group = "Take Profit / Stop Loss", DefaultValue = Colors.Green)]
        public Colors SLColor { get; set; }

        [Parameter("TP/SL Line Style", Group = "Take Profit / Stop Loss", DefaultValue = LineStyle.Lines)]
        public LineStyle TPSLStyle { get; set; }

        [Parameter("TP/SL Line Size", Group = "Take Profit / Stop Loss", MinValue = 1, MaxValue = 5, DefaultValue = 2)]
        public int LineSize { get; set; }




        public bool TP = false;
        public bool SL = false;
        public double TP_Pips = 20.0;
        public double SL_Pips = 20.0;
        public double Volume = 1.0;



        public ChartHorizontalLine TP_Line;
        public ChartHorizontalLine SL_Line;

        public TextBox textboxtest;


        protected override void OnStart()
        {



            var playIcon = new Polygon 
            {
                Points = new Point[] 
                {
                    new Point(3, 0),
                    new Point(6, 5),
                    new Point(0, 5)
                },
                FillColor = Color.White
            };
            var button1 = new Button 
            {
                Content = playIcon,
                BackgroundColor = "#0E9247",
                VerticalAlignment = VerticalAlignment.Top,
                HorizontalAlignment = HorizontalAlignment.Left,
                Margin = "25 5 0 3",
                Padding = "4 2 4 2"
            };

            var playIconDown = new Polygon 
            {
                Points = new Point[] 
                {

                    new Point(0, 0),
                    new Point(6, 0),
                    new Point(3, 5)
                },
                FillColor = Color.White
            };
            var button2 = new Button 
            {
                Content = playIconDown,
                BackgroundColor = "#0E9247",
                VerticalAlignment = VerticalAlignment.Top,
                HorizontalAlignment = HorizontalAlignment.Left,
                Margin = "25 0",
                Padding = "4 2 4 2"
            };




            var panel = new StackPanel 
            {
                Orientation = Orientation.Vertical,
                VerticalAlignment = PanelVerticalAlignment,
                HorizontalAlignment = PanelHorizontalAlignment,
                Width = 150,
                Height = 140,
                Margin = "15 25",
                BackgroundColor = "#FF3F3F3F"
            };
            var textBox = new TextBox 
            {
                Width = 60,
                VerticalAlignment = VerticalAlignment.Top,
                HorizontalAlignment = HorizontalAlignment.Center,
                Margin = "5 -20 0 5",
                Text = "1.0"
            };
            textboxtest = textBox;

            var SellButtonBox = new Button 
            {
                Width = 70,
                Text = "SELL",
                FontWeight = FontWeight.Bold,
                VerticalAlignment = VerticalAlignment.Bottom,
                HorizontalAlignment = HorizontalAlignment.Left,
                Margin = "3 2 -5 0",
                BackgroundColor = "Red"
            };
            var BuyButtonBox = new Button 
            {
                Width = 70,
                Text = "BUY",
                FontWeight = FontWeight.Bold,
                VerticalAlignment = VerticalAlignment.Top,
                HorizontalAlignment = HorizontalAlignment.Right,
                Margin = "0 -20 3 0",
                //Margin = "0 0 10 10",
                BackgroundColor = "LimeGreen"
            };
            var checkBoxTP = new CheckBox 
            {
                Text = "TP",
                //IsChecked = Chart.DisplaySettings.TickVolume,
                VerticalAlignment = VerticalAlignment.Top,
                HorizontalAlignment = HorizontalAlignment.Left,
                Margin = 8
            };
            var textBoxTP = new TextBox 
            {
                Width = 80,
                VerticalAlignment = VerticalAlignment.Top,
                HorizontalAlignment = HorizontalAlignment.Right,
                Text = "20.0",
                Margin = "20 -26"
            };
            var checkBoxSL = new CheckBox 
            {
                Text = "SL",
                //IsChecked = Chart.DisplaySettings.TickVolume,
                VerticalAlignment = VerticalAlignment.Top,
                HorizontalAlignment = HorizontalAlignment.Left,
                Margin = "8 0 0 0"
            };
            var textBoxSL = new TextBox 
            {
                Width = 80,
                VerticalAlignment = VerticalAlignment.Top,
                HorizontalAlignment = HorizontalAlignment.Right,
                Text = "20.0",
                Margin = "20 -17 20 10"
            };
            var ClosePositionsButtonBox = new Button 
            {
                Width = 95,
                Text = "Close Orders",
                FontWeight = FontWeight.DemiBold,
                VerticalAlignment = VerticalAlignment.Bottom,
                HorizontalAlignment = HorizontalAlignment.Left,
                Margin = "30 0 0 0",
                BackgroundColor = "Orange"
            };
            panel.AddChild(button1);
            panel.AddChild(button2);
            panel.AddChild(textBox);
            panel.AddChild(SellButtonBox);
            panel.AddChild(BuyButtonBox);
            panel.AddChild(checkBoxTP);
            panel.AddChild(textBoxTP);

            panel.AddChild(checkBoxSL);
            panel.AddChild(textBoxSL);
            panel.AddChild(ClosePositionsButtonBox);
            Chart.AddControl(panel);

//**************************************************************
            BuyButtonBox.Click += OnBuyButtonBoxClick;
            SellButtonBox.Click += OnSellButtonBoxClick;

//**************************************************************

            ClosePositionsButtonBox.Click += OnClosePositionsButtonBoxClick;

//**************************************************************
            checkBoxTP.Checked += OnCheckBoxTPChecked;
            checkBoxTP.Unchecked += OnCheckBoxTPUnchecked;
            checkBoxSL.Checked += OnCheckBoxSLChecked;
            checkBoxSL.Unchecked += OnCheckBoxSLUnchecked;

//**************************************************************

            textBoxTP.TextChanged += OnTextBoxTPTextChanged;
            textBoxSL.TextChanged += OnTextBoxSLTextChanged;

//**************************************************************

            Chart.ObjectUpdated += OnChartObjectUpdated;

//**************************************************************

            button1.Click += OnButton1Click;
            button2.Click += OnButton2Click;

//**************************************************************

            Positions.Closed += OnPositionsClosed;

            //**************************************************************

            textBox.TextChanged += OnTextBoxTextChanged;


        }

        void OnPositionsClosed(PositionClosedEventArgs obj)
        {
            Print("Position with PID: " + obj.Position.Id + " Closed.");

            var lines = Chart.FindAllObjects<ChartHorizontalLine>();

            var PID = obj.Position.Id.ToString();

            foreach (var line in lines)
            {
                if (line.Name.Contains("SL Line") && line.Name.Contains(PID))
                {
                    Chart.RemoveObject(line.Name);
                }
                else if (line.Name.Contains("TP Line") && line.Name.Contains(PID))
                {
                    Chart.RemoveObject(line.Name);
                }
            }

        }

        void OnClosePositionsButtonBoxClick(ButtonClickEventArgs obj)
        {
            foreach (var pos in Positions)
            {
                ClosePosition(pos);
                if (pos != null)
                {
                    Print("Closing Position with PID: " + pos.Id);

                    var lines = Chart.FindAllObjects<ChartHorizontalLine>();

                    foreach (var line in lines)
                    {
                        Print(line.Name);

                        if (line.Name.Contains("SL Line"))
                        {
                            Chart.RemoveObject(line.Name);
                        }
                        else if (line.Name.Contains("TP Line"))
                        {
                            Chart.RemoveObject(line.Name);
                        }
                    }
                }
            }
        }



//*************************************************************************************
        void OnButton2Click(ButtonClickEventArgs obj)
        {

            Print("Button Down Clicked!");

            if (Volume - Symbol.VolumeInUnitsStep >= Symbol.VolumeInUnitsMin)
            {
                Volume -= Symbol.VolumeInUnitsToQuantity(Symbol.VolumeInUnitsStep);
                textboxtest.Text = Volume.ToString();
            }
        }

        void OnButton1Click(ButtonClickEventArgs obj)
        {
            Print("Up Button Clicked!");

            if (Volume + Symbol.VolumeInUnitsStep <= Symbol.VolumeInUnitsMax)
            {
                Volume += Symbol.VolumeInUnitsToQuantity(Symbol.VolumeInUnitsStep);
                textboxtest.Text = Volume.ToString();
            }

        }



//*************************************************************************************
        void OnChartObjectUpdated(ChartObjectUpdatedEventArgs obj)
        {

            if (obj.ChartObject.Name.Contains("Trade Buy"))
            {
                if (obj.ChartObject.Name.Contains("SL Line"))
                {
                    var orderNumber = Regex.Match(obj.ChartObject.Name, "\\d+").Value;
                    Print("Trade Buy Order Stop Loss Changed: " + orderNumber);
                    var order = Convert.ToInt64(orderNumber);

                    foreach (var AllPositions in Positions)
                    {
                        Print(AllPositions.Id);
                        if (AllPositions.Id == order)
                        {
                            var hline = (ChartHorizontalLine)obj.ChartObject;
                            AllPositions.ModifyStopLossPrice(hline.Y);
                        }
                    }



                }
                else if (obj.ChartObject.Name.Contains("TP Line"))
                {
                    var orderNumber = Regex.Match(obj.ChartObject.Name, "\\d+").Value;
                    Print("Trade Buy Order Take Profit Changed: " + orderNumber);
                    var order = Convert.ToInt64(orderNumber);

                    foreach (var AllPositions in Positions)
                    {
                        Print(AllPositions.Id);
                        if (AllPositions.Id == order)
                        {
                            var hline = (ChartHorizontalLine)obj.ChartObject;
                            AllPositions.ModifyTakeProfitPrice(hline.Y);
                        }
                    }
                }
            }
            else if (obj.ChartObject.Name.Contains("Trade Sell"))
            {
                if (obj.ChartObject.Name.Contains("SL Line"))
                {
                    var orderNumber = Regex.Match(obj.ChartObject.Name, "\\d+").Value;
                    //Print("Stop Loss Changed: " + orderNumber);
                    var order = Convert.ToInt64(orderNumber);
                    foreach (var AllPositions in Positions)
                    {
                        Print(AllPositions.Id);
                        if (AllPositions.Id == order)
                        {
                            var hline = (ChartHorizontalLine)obj.ChartObject;
                            AllPositions.ModifyStopLossPrice(hline.Y);
                        }
                    }
                }
                else if (obj.ChartObject.Name.Contains("TP Line"))
                {
                    var orderNumber = Regex.Match(obj.ChartObject.Name, "\\d+").Value;
                    //Print("Take Profit Changed: " + orderNumber);
                    var order = Convert.ToInt64(orderNumber);

                    foreach (var AllPositions in Positions)
                    {
                        Print(AllPositions.Id);
                        if (AllPositions.Id == order)
                        {
                            var hline = (ChartHorizontalLine)obj.ChartObject;
                            AllPositions.ModifyTakeProfitPrice(hline.Y);
                        }
                    }
                }
            }

        }



//*************************************************************************************
        void OnCheckBoxSLUnchecked(CheckBoxEventArgs obj)
        {
            if (obj.CheckBox.IsChecked == false)
            {
                Print("Stop Loss Unhecked");
                SL = false;
            }
        }

        void OnCheckBoxSLChecked(CheckBoxEventArgs obj)
        {

            if (obj.CheckBox.IsChecked == true)
            {
                Print("Stop Loss Checked");
                SL = true;
            }
        }

        void OnCheckBoxTPUnchecked(CheckBoxEventArgs obj)
        {
            if (obj.CheckBox.IsChecked.Value == false)
            {
                Print("Take Profit UnChecked: " + obj.CheckBox.IsChecked.Value);
                TP = false;
            }
        }

        void OnCheckBoxTPChecked(CheckBoxEventArgs obj)
        {
            if (obj.CheckBox.IsChecked.Value == true)
            {
                Print("Take Profit Checked: " + obj.CheckBox.IsChecked.Value);
                TP = true;
            }

        }

//******************************************************************************************************************************************

        void OnSellButtonBoxClick(ButtonClickEventArgs obj)
        {


            if (TP == false && SL == false)
            {
                Print("Selling Market Execution");

                //QuantityToVolumeInUnits(Volume)

                var trade_Result = ExecuteMarketOrder(TradeType.Sell, Symbol.Name, Symbol.QuantityToVolumeInUnits(Volume), "Buy Order");
                Print(trade_Result.Position.Id);
            }
            else if (TP == true && SL == true)
            {

                var trade_Result = ExecuteMarketOrder(TradeType.Sell, Symbol.Name, Symbol.QuantityToVolumeInUnits(Volume), "Buy Order", SL_Pips, TP_Pips);
                Print(trade_Result.Position);

                var id = trade_Result.Position.Id;

                Print("Selling Market Execution: TP State: " + TP + " SL State: " + SL);
                Print(TP_Pips + " " + SL_Line);

                TP_Line = Chart.DrawHorizontalLine("TP Line Trade Sell Pid:" + id, Symbol.Bid - (TP_Pips * Symbol.PipSize), Color.FromName(TPColor.ToString()), 3);
                TP_Line.IsInteractive = true;
                TP_Line.LineStyle = LineStyle.Lines;

                SL_Line = Chart.DrawHorizontalLine("SL Line Trade Sell Pid:" + id, Symbol.Bid + (SL_Pips * Symbol.PipSize), Color.FromName(SLColor.ToString()), 3);
                SL_Line.IsInteractive = true;
                SL_Line.LineStyle = LineStyle.Lines;
            }
            else if (TP == true && SL == false)
            {
                var trade_Result = ExecuteMarketOrder(TradeType.Sell, Symbol.Name, Symbol.QuantityToVolumeInUnits(Volume), "Buy Order", null, TP_Pips);
                Print(trade_Result.Position);

                var id = trade_Result.Position.Id;

                Print("Selling Market Execution: TP State: " + TP + " SL State: " + SL);

                TP_Line = Chart.DrawHorizontalLine("TP Line Trade Sell Pid:" + id, Symbol.Bid - (TP_Pips * Symbol.PipSize), Color.FromName(TPColor.ToString()), 3);
                TP_Line.IsInteractive = true;
                TP_Line.LineStyle = LineStyle.Lines;
            }
            else if (SL == true && TP == false)
            {
                var trade_Result = ExecuteMarketOrder(TradeType.Sell, Symbol.Name, Symbol.QuantityToVolumeInUnits(Volume), "Buy Order", SL_Pips, null);
                Print(trade_Result.Position);

                var id = trade_Result.Position.Id;

                Print("Selling Market Execution: TP State: " + TP + " SL State: " + SL);

                SL_Line = Chart.DrawHorizontalLine("SL Line Trade Sell Pid:" + id, Symbol.Bid + (SL_Pips * Symbol.PipSize), Color.FromName(SLColor.ToString()), 3);
                SL_Line.IsInteractive = true;
                SL_Line.LineStyle = LineStyle.Lines;
            }
        }


//******************************************************************************************************************************************

        void OnBuyButtonBoxClick(ButtonClickEventArgs obj)
        {
            if (TP == false && SL == false)
            {
                Print("Buying Market Execution.");


                var trade_Result = ExecuteMarketOrder(TradeType.Buy, Symbol.Name, Symbol.QuantityToVolumeInUnits(Volume), "Buy Order");
                Print(trade_Result.Position.Id);
            }
            else if (TP == true && SL == true)
            {


                Print("Buying Market Execution: TP State: " + TP + " SL State: " + SL);

                var trade_Result = ExecuteMarketOrder(TradeType.Buy, Symbol.Name, Symbol.QuantityToVolumeInUnits(Volume), "Buy Order", SL_Pips, TP_Pips);
                Print(trade_Result.Position.Id);



                var id = trade_Result.Position.Id;

                TP_Line = Chart.DrawHorizontalLine("TP Line Trade Buy Pid:" + id, Symbol.Ask + (TP_Pips * Symbol.PipSize), Color.FromName(TPColor.ToString()), 3);
                TP_Line.IsInteractive = true;
                TP_Line.LineStyle = LineStyle.Lines;

                SL_Line = Chart.DrawHorizontalLine("SL Line Trade Buy Pid:" + id, Symbol.Ask - (SL_Pips * Symbol.PipSize), Color.FromName(SLColor.ToString()), 3);
                SL_Line.IsInteractive = true;
                SL_Line.LineStyle = LineStyle.Lines;
                Print(TP_Pips + " " + SL_Line);
            }
            else if (TP == true && SL == false)
            {

                var trade_Result = ExecuteMarketOrder(TradeType.Buy, Symbol.Name, Symbol.QuantityToVolumeInUnits(Volume), "Buy Order", null, TP_Pips);
                Print(trade_Result.Position.Id);

                var id = trade_Result.Position.Id;

                Print("Buying Market Execution: TP State: " + TP + " SL State: " + SL);


                TP_Line = Chart.DrawHorizontalLine("TP Line Trade Buy Pid:" + id, Symbol.Ask + (TP_Pips * Symbol.PipSize), Color.FromName(TPColor.ToString()), 3);
                TP_Line.IsInteractive = true;
                TP_Line.LineStyle = LineStyle.Lines;
            }
            else if (SL == true && TP == false)
            {
                var trade_Result = ExecuteMarketOrder(TradeType.Buy, Symbol.Name, Symbol.QuantityToVolumeInUnits(Volume), "Buy Order", SL_Pips, null);
                Print(trade_Result.Position.Id);



                var id = trade_Result.Position.Id;

                Print("Buying Market Execution: TP State: " + TP + " SL State: " + SL);


                SL_Line = Chart.DrawHorizontalLine("SL Line Trade Buy Pid:" + id, Symbol.Ask - (SL_Pips * Symbol.PipSize), Color.FromName(SLColor.ToString()), 3);
                SL_Line.IsInteractive = true;
                SL_Line.LineStyle = LineStyle.Lines;
            }

        }

//***************************************************************************************************



        void OnTextBoxSLTextChanged(TextChangedEventArgs obj)
        {

            Print(obj.TextBox.Text);
            SL_Pips = Convert.ToDouble(obj.TextBox.Text);
        }

        void OnTextBoxTPTextChanged(TextChangedEventArgs obj)
        {
            Print(obj.TextBox.Text);
            TP_Pips = Convert.ToDouble(obj.TextBox.Text);

        }


//***************************************************************************************************

        void OnTextBoxTextChanged(TextChangedEventArgs obj)
        {
            //Print("Volume: " + obj.TextBox.Text);
            double vol;

            var isDouble = Double.TryParse(obj.TextBox.Text, out vol);
            if (isDouble)
            {
                //Print("Is Double");
                obj.TextBox.BackgroundColor = "#FF262626";
                var unit = Convert.ToDouble(obj.TextBox.Text);
                var p = Convert.ToDouble(obj.TextBox.Text);
                p = Symbol.QuantityToVolumeInUnits(p);
                //Print("Converted Volume: " + p);


                if (p >= Symbol.VolumeInUnitsMin && p <= Symbol.VolumeInUnitsMax)
                {
                    //Print("Volume is Ok");
                    Volume = unit;
                    obj.TextBox.BackgroundColor = "#FF262626";
                }
                else if (p < Symbol.VolumeInUnitsMin)
                {
                    //Print("Volume is too Low");
                    obj.TextBox.BackgroundColor = "#45FE0000";
                }
                else if (p > Symbol.VolumeInUnitsMax)
                {
                    //Print("Volume is too High");
                    obj.TextBox.BackgroundColor = "#45FE0000";
                }

            }
            else
            {
                Print("Is Not Double");
                obj.TextBox.BackgroundColor = "#45FE0000";
            }

            if (vol < Symbol.VolumeInUnitsMin)
            {
                //Print("The minimum volume is {0}", Symbol.VolumeInUnitsMin);
            }
        }
    }
}


TraderExperto's avatar
TraderExperto

Joined on 07.06.2019

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: Trade Panel Experto.algo
  • Rating: 0
  • Installs: 2147
Comments
Log in to add a comment.
NE
newbee · 8 months ago

Hello TraderExperto.

Thanks for this.  Is there any way you can show me a sample of the code to insert a ComboBox into this trading panel say where the close Orders button is. (ie Instead of the close Orders button-just replace that with the ComboBox that would have a drop down list of texts to choose from). I've been trying to do that for a very long time with no success.

Any help would be greatly appreciated. 

thanks

FA
farojusticia.oscar87 · 1 year ago

 

 

 

 

 

 

 

 

 

 

Buen trabajo funciona muy bien, solo necesita calcular el tamaño de la posición por los pips de la parada

 

 

 

 

JO
Jobauma · 2 years ago

EDIT:

- Playback stops if "Play" ( blue button - right ) is activated

- I meant left, but this is not case. Bug uncertain.

Best regards,

Johannes Hillestad Baumann

JO
Jobauma · 2 years ago

Hi.

Very simple. :)

Works well with backtesting ( playback / simulator mode / back-test ) in "Automate", from verifying/testing custom indicators.

Playback stops if "Play" ( blue button - right ) is activated - while "Play" with " Visual Mode " is activated. ( Interface disappears when changing values. )

Trading Panel Experto is missing limit order and trailing stop loss. Take profit and stop loss in pips is perfect.

Best regards,

Johannes Hillestad Baumann