Help With Buy/Sell Stop & TP/SL Panel

 

Greetings

Please assist with my panel I created. When I press the Buy/Sell stop, "Pending stop pips" & "No. of Trades" buttons everything works perfectly but as for my TP & SL they do not show my take profit & stop loss levels at all. Below is my code please assist if you know the problem. 

#property copyright "Copyright 2023"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
#include <Trade\PositionInfo.mqh>
#include <Trade\AccountInfo.mqh>
#include <Trade\OrderInfo.mqh>

#include <Controls/Button.mqh>
#include <Controls/Dialog.mqh>
#include <Controls/Panel.mqh>
#include <Controls/Edit.mqh>

CTrade trade;
CPositionInfo m_position;
CSymbolInfo m_symbol;
COrderInfo m_order;
CAccountInfo account;

CButton ButtonBuy,ButtonSell,ButtonBuyStop,ButtonSellStop,ButtonBuyLimit,ButtonSellLimit,ButtonCloseAll,
ButtonDelete,ButtonTakeProfit,ButtonStopLoss,ButtonRisk;

CAppDialog AppWindow;
CPanel Panel;
CEdit EditModify,EditTakeProfit,EditStopLoss,EditNoTrades,EditLotSize,EditDistance;

input double Points=1000;
input int digit=2;

int MagicNumber;
double TakeProfit=5;
double StopLoss=5;
double Risk=1;
int PanelWidth=200;
int PanelHeight=250;
int RowHeight=20;

int NoTrades;
int NumberOfTrades=NoTrades;
int PendingDistance=0;


double Ask=SymbolInfoDouble(Symbol(),SYMBOL_ASK); 
double Bid=SymbolInfoDouble(Symbol(),SYMBOL_BID);
int StopLevel=(int)SymbolInfoInteger(Symbol(),SYMBOL_TRADE_STOPS_LEVEL);
int FreezeLevel=(int)SymbolInfoInteger(Symbol(),SYMBOL_TRADE_FREEZE_LEVEL);
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
 {
  ResetLastError();
  
  if(!m_symbol.Name(Symbol()))
  {
   return(INIT_FAILED);
  }
   
  
  trade.SetExpertMagicNumber(MagicNumber);
  trade.SetMarginMode();
  trade.SetTypeFillingBySymbol(m_symbol.Name());
  
  MagicNumber=(int)TimeCurrent();

  AppWindow.Create(0,"MyPanel",0,2,220,4+PanelWidth+8,220+PanelHeight);
  AppWindow.Run(); 
  
  ButtonBuyStop.Create(0,"ButtonBuyStop",0,0,0,0,0);
  ButtonBuyStop.Width(PanelWidth/2);
  ButtonBuyStop.Height(RowHeight);
  ButtonBuyStop.Text("BuyStop");
  ButtonBuyStop.Color(clrBlack);
  ButtonBuyStop.ColorBackground(clrLightBlue);
  ButtonBuyStop.FontSize(8);
  AppWindow.Add(ButtonBuyStop);
  
  ButtonSellStop.Create(0,"ButtonSellStop",0,PanelWidth/2,0,0,0);
  ButtonSellStop.Width(PanelWidth/2);
  ButtonSellStop.Height(RowHeight);
  ButtonSellStop.Text("SellStop");
  ButtonSellStop.Color(clrBlack);
  ButtonSellStop.ColorBackground(clrDarkRed);
  ButtonSellStop.FontSize(8);
  AppWindow.Add(ButtonSellStop);
  
  EditDistance.Create(0,"EditDistance",0,0,RowHeight,0,0);
  EditDistance.Width(PanelWidth);
  EditDistance.Height(RowHeight);
  EditDistance.Text("PendingDistancePips:");
  EditDistance.ColorBackground(clrDarkOrange);
  EditDistance.Color(clrBlack);
  AppWindow.Add(EditDistance);
  
  ButtonTakeProfit.Create(0,"ButtonTakeProfit",0,0,RowHeight*2,0,0);
  ButtonTakeProfit.Width(PanelWidth/2);
  ButtonTakeProfit.Height(RowHeight);
  ButtonTakeProfit.Text("TakeProfit");
  ButtonTakeProfit.Color(clrBlack);
  ButtonTakeProfit.ColorBackground(clrGreen);
  ButtonTakeProfit.FontSize(8);
  AppWindow.Add(ButtonTakeProfit);
  
  EditTakeProfit.Create(0,"ButtonTakeProfitModify",0,PanelWidth/2,RowHeight*2,0,0);
  EditTakeProfit.Width(PanelWidth/2);
  EditTakeProfit.Height(RowHeight);
  AppWindow.Add(EditTakeProfit);
  
  ButtonStopLoss.Create(0,"ButtonStopLoss",0,0,RowHeight*3,0,0);
  ButtonStopLoss.Width(PanelWidth/2);
  ButtonStopLoss.Height(RowHeight);
  ButtonStopLoss.Text("StopLoss");
  ButtonStopLoss.Color(clrBlack);
  ButtonStopLoss.ColorBackground(clrRed);
  ButtonStopLoss.FontSize(8);
  AppWindow.Add(ButtonStopLoss);
  
  EditStopLoss.Create(0,"ButtonStopLossModify",0,PanelWidth/2,RowHeight*3,0,0);
  EditStopLoss.Width(PanelWidth/2);
  EditStopLoss.Height(RowHeight);
  AppWindow.Add(EditStopLoss);
  
  EditNoTrades.Create(0,"EditNoTrades",0,0,RowHeight*4,0,0);
  EditNoTrades.Width(PanelWidth);
  EditNoTrades.Height(RowHeight);
  EditNoTrades.Text("No.Of Trades:");
  EditNoTrades.ColorBackground(clrDarkGoldenrod);
  EditNoTrades.Color(clrBlack);
  AppWindow.Add(EditNoTrades);
  
  return(INIT_SUCCEEDED);
 } 
 
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
 {
  AppWindow.Destroy(reason);
 } 
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnChartEvent(const int Click,const long &Adjust,const double &Adjust2,const string &ButtonPressed)
 {
  AppWindow.ChartEvent(Click,Adjust,Adjust2,ButtonPressed);
  
  if(Click==CHARTEVENT_OBJECT_CLICK)
  {
   NumberOfTrades=(int)StringToInteger(ObjectGetString(0,"EditNoTrades",OBJPROP_TEXT));
   
   if(ButtonPressed==ButtonBuyStop.Name())
   {
    for(int a=0;a<NumberOfTrades;a++)
    {
     BuyStop();
    } 
   }
   
   if(ButtonPressed==ButtonSellStop.Name())
   {
    for(int b=0;b<NumberOfTrades;b++)
    {
     SellStop();
    }
   }
   
   if(ButtonPressed==ButtonTakeProfit.Name())
   {
    TakeProfit=StringToDouble(EditTakeProfit.Text());
    double PriceOrder=StringToDouble(EditDistance.Text());
    
    for(int j=OrdersTotal()-1;j>=0;j--)
    {
     if(m_order.SelectByIndex(j))
     if(m_order.Symbol()==Symbol())
     {
      int Type=m_order.Type();
      double TPBuy,TPSell=0;
      switch(Type)
      {
       case ORDER_TYPE_BUY_STOP:
       case ORDER_TYPE_BUY_LIMIT:
       TPBuy=Bid+TakeProfit*Pips();
       if(TPBuy-Bid>StopLevel*Pips())
       {
        trade.OrderModify(m_order.Ticket(),m_order.PriceOpen(),m_order.StopLoss(),TPBuy,0,0,0);break;
       }

       case ORDER_TYPE_SELL_STOP:
       case ORDER_TYPE_SELL_LIMIT:
       TPSell=Bid-TakeProfit*Pips();
       if(Ask-TPSell>StopLevel*Pips())
       {
        trade.OrderModify(m_order.Ticket(),m_order.PriceOpen(),m_order.StopLoss(),TPSell,0,0,0);break;
       }
      }
      
     }
    } 
  }
  
  if(ButtonPressed==ButtonStopLoss.Name())
  {
   StopLoss=StringToDouble(EditStopLoss.Text());
   double PriceOrder=StringToDouble(EditDistance.Text());
   
   for(int l=OrdersTotal()-1;l>=0;l--)
   {
    if(m_order.SelectByIndex(l))
    if(m_order.Symbol()==Symbol())
    {
     int Type=m_order.Type();
     double PriceOpen=m_order.PriceOpen();
     double SLBuy,SLSell=0;
     switch(Type)
     {
      case ORDER_TYPE_BUY_STOP:
      case ORDER_TYPE_BUY_LIMIT:
      
      SLBuy=PriceOrder-StopLoss*Pips();
      if(Bid-SLBuy>StopLevel*Pips())
      {
       trade.OrderModify(m_order.Ticket(),m_order.PriceOpen(),SLBuy,m_order.TakeProfit(),0,0,0);break;
      }

      case ORDER_TYPE_SELL_STOP:
      case ORDER_TYPE_SELL_LIMIT:
      SLSell=PriceOrder+StopLoss*Pips();
      if(SLSell-Ask>StopLevel*Pips())
      {
       trade.OrderModify(m_order.Ticket(),m_order.PriceOpen(),SLSell,m_order.TakeProfit(),0,0,0);break;
      }
     }
    }
   }
  }
 } 
   
 }  

double Pips()
 {
  double PipPoint=0;
  int Digit=(int)SymbolInfoInteger(Symbol(),SYMBOL_DIGITS);
  if(Digit==digit){PipPoint=Point()*Points;}
  return(PipPoint);
 }
 
void BuyStop()
 { 
  int Dis=(int)StringToInteger(EditDistance.Text());
  double Distance=Dis*Pips();
  double EntryPrice=Ask+Distance;
    
  double MinDistance=StopLevel*Pips();
  if((EntryPrice-Ask)<MinDistance)
  {
   EntryPrice=Ask+MinDistance;
  }
  
  EntryPrice=NormalizeDouble(EntryPrice,Digits());
  trade.BuyStop(0.01,EntryPrice,Symbol(),0,0,0,0,"MyPanel");
 }
 
void SellStop()
 {
  int Dis=(int)StringToInteger(EditDistance.Text());
  double Distance=Dis*Pips();
  double EntryPrice=Bid-Distance;
    
  double MinDistance=StopLevel*Pips();
  if((Bid-EntryPrice)<MinDistance)
  {
   EntryPrice=Bid-MinDistance;
  }
  
  EntryPrice=NormalizeDouble(EntryPrice,Digits());
  trade.SellStop(0.01,EntryPrice,Symbol(),0,0,0,0,"MyPanel");
 } 
 
RedAlgo:

Greetings

Please assist with my panel I created. When I press the Buy/Sell stop, "Pending stop pips" & "No. of Trades" buttons everything works perfectly but as for my TP & SL they do not show my take profit & stop loss levels at all. Below is my code please assist if you know the problem. 

Have a look at the code posted at:

Articles

How to Create an Interactive MQL5 Dashboard/Panel Using the Controls Class (Part 1): Setting Up the Panel

Allan Munene Mutiiria, 2024.10.16 11:30

In this article, we create an interactive trading dashboard using the Controls class in MQL5, designed to streamline trading operations. The panel features a title, navigation buttons for Trade, Close, and Information, and specialized action buttons for executing trades and managing positions. By the end of the article, you will have a foundational panel ready for further enhancements in future installments.

 
Ryan L Johnson #:

Have a look at the code posted at:


Thanks for the link but I don't want to use enter the price for my TP & SL instead by entering a number (pips) of my choice. Btw it does not work & I prefer if I can enter a number(pips) for my TP & SL.  
 
RedAlgo #:
Thanks for the link but I don't want to use enter the price for my TP & SL instead by entering a number (pips) of my choice. Btw it does not work & I prefer if I can enter a number(pips) for my TP & SL.  

That was actually a multi-part series of Articles where the link is merely to Part 1.

Anyway, here's another trading panel that uses pips for SL and TP (AR_Trading_Panel_For_MT5/AR_Trading_Panel_For_MT5.mq5 at main · Andrukas8/AR_Trading_Panel_For_MT5 · GitHub),