#property copyright "Your Name"
#property link      "yourwebsite.com"
#property version   "1.00"
#property strict
#property description "Advanced Trading Panel with Risk Management Features"

#include <Trade/Trade.mqh>
#include <Trade/PositionInfo.mqh>
#include <Controls/Dialog.mqh>
#include <Controls/Button.mqh>
#include <Controls/Edit.mqh>
#include <Controls/CheckBox.mqh>

//+------------------------------------------------------------------+
//| Constants                                                        |
//+------------------------------------------------------------------+
#define ROW_HEIGHT         25
#define COLUMN_WIDTH       80
#define PANEL_OFFSET       50
#define MARGIN             10

//+------------------------------------------------------------------+
//| Global Variables and Objects                                     |
//+------------------------------------------------------------------+
CAppDialog MainWindow;
CTrade trade;
CPositionInfo position_info;

// Main buttons
CButton btnTrade, btnClose, btnBreakeven;

// Trade panel components
CEdit edtLots, edtTP, edtSL;
CButton btnBuyMarket, btnSellMarket, btnBuyLimit, btnSellLimit, btnBuyStop, btnSellStop;

// Close panel components
CButton btnCloseAllBuy, btnCloseAllSell, btnCloseProfit, btnCloseLoss, btnCloseAll;

// Breakeven panel components
CButton btnBreakevenExecute;
CCheckBox chkAutoBreakeven;

// Panel management
int activePanel = -1; // 0-Trade, 1-Close, 2-Breakeven

//+------------------------------------------------------------------+
//| Expert Initialization Function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
    // Create main window
    if(!MainWindow.Create(0,"Trading Panel",0,5,5,350,500))
        return(INIT_FAILED);

    CreateMainButtons();
    CreateTradePanel();
    CreateClosePanel();
    CreateBreakevenPanel();
    ShowPanel(0); // Show Trade panel by default

    ChartRedraw();
    return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Create Main Navigation Buttons                                   |
//+------------------------------------------------------------------+
void CreateMainButtons()
{
    int x = MARGIN;
    int y = MARGIN;
    
    // Trade Button
    if(!btnTrade.Create(0,"btnTrade",0,x,y,COLUMN_WIDTH,ROW_HEIGHT))
        return;
    btnTrade.Text("Trade");
    MainWindow.Add(btnTrade);
    x += COLUMN_WIDTH + MARGIN;

    // Close Button
    if(!btnClose.Create(0,"btnClose",0,x,y,COLUMN_WIDTH,ROW_HEIGHT))
        return;
    btnClose.Text("Close");
    MainWindow.Add(btnClose);
    x += COLUMN_WIDTH + MARGIN;

    // Breakeven Button
    if(!btnBreakeven.Create(0,"btnBreakeven",0,x,y,COLUMN_WIDTH*1.5,ROW_HEIGHT))
        return;
    btnBreakeven.Text("Breakeven");
    MainWindow.Add(btnBreakeven);
}

//+------------------------------------------------------------------+
//| Create Trade Panel Elements                                      |
//+------------------------------------------------------------------+
void CreateTradePanel()
{
    int y = PANEL_OFFSET;
    
    // Lot Size Input
    if(!edtLots.Create(0,"edtLots",0,MARGIN,y,COLUMN_WIDTH,ROW_HEIGHT))
        return;
    edtLots.Text("0.01");
    MainWindow.Add(edtLots);
    y += ROW_HEIGHT + MARGIN;

    // TP Input
    if(!edtTP.Create(0,"edtTP",0,MARGIN,y,COLUMN_WIDTH,ROW_HEIGHT))
        return;
    edtTP.Text("0");
    MainWindow.Add(edtTP);
    y += ROW_HEIGHT + MARGIN;

    // SL Input
    if(!edtSL.Create(0,"edtSL",0,MARGIN,y,COLUMN_WIDTH,ROW_HEIGHT))
        return;
    edtSL.Text("0");
    MainWindow.Add(edtSL);
    y += ROW_HEIGHT + MARGIN;

    // Order Buttons
    int btnWidth = (COLUMN_WIDTH*2 + MARGIN)/2 - MARGIN/2;
    
    if(!btnBuyMarket.Create(0,"btnBuyMarket",0,MARGIN,y,btnWidth,ROW_HEIGHT))
        return;
    btnBuyMarket.Text("Buy Market");
    MainWindow.Add(btnBuyMarket);

    if(!btnSellMarket.Create(0,"btnSellMarket",0,MARGIN+btnWidth+MARGIN,y,btnWidth,ROW_HEIGHT))
        return;
    btnSellMarket.Text("Sell Market");
    MainWindow.Add(btnSellMarket);
    y += ROW_HEIGHT + MARGIN;

    if(!btnBuyLimit.Create(0,"btnBuyLimit",0,MARGIN,y,btnWidth,ROW_HEIGHT))
        return;
    btnBuyLimit.Text("Buy Limit");
    MainWindow.Add(btnBuyLimit);

    if(!btnSellLimit.Create(0,"btnSellLimit",0,MARGIN+btnWidth+MARGIN,y,btnWidth,ROW_HEIGHT))
        return;
    btnSellLimit.Text("Sell Limit");
    MainWindow.Add(btnSellLimit);
    y += ROW_HEIGHT + MARGIN;

    if(!btnBuyStop.Create(0,"btnBuyStop",0,MARGIN,y,btnWidth,ROW_HEIGHT))
        return;
    btnBuyStop.Text("Buy Stop");
    MainWindow.Add(btnBuyStop);

    if(!btnSellStop.Create(0,"btnSellStop",0,MARGIN+btnWidth+MARGIN,y,btnWidth,ROW_HEIGHT))
        return;
    btnSellStop.Text("Sell Stop");
    MainWindow.Add(btnSellStop);
}

//+------------------------------------------------------------------+
//| Create Close Panel Elements                                      |
//+------------------------------------------------------------------+
void CreateClosePanel()
{
    int y = PANEL_OFFSET;
    int btnWidth = COLUMN_WIDTH*2;
    
    if(!btnCloseAllBuy.Create(0,"btnCloseAllBuy",0,MARGIN,y,btnWidth,ROW_HEIGHT))
        return;
    btnCloseAllBuy.Text("Close All Buy");
    MainWindow.Add(btnCloseAllBuy);
    y += ROW_HEIGHT + MARGIN;

    if(!btnCloseAllSell.Create(0,"btnCloseAllSell",0,MARGIN,y,btnWidth,ROW_HEIGHT))
        return;
    btnCloseAllSell.Text("Close All Sell");
    MainWindow.Add(btnCloseAllSell);
    y += ROW_HEIGHT + MARGIN;

    if(!btnCloseProfit.Create(0,"btnCloseProfit",0,MARGIN,y,btnWidth,ROW_HEIGHT))
        return;
    btnCloseProfit.Text("Close Profit");
    MainWindow.Add(btnCloseProfit);
    y += ROW_HEIGHT + MARGIN;

    if(!btnCloseLoss.Create(0,"btnCloseLoss",0,MARGIN,y,btnWidth,ROW_HEIGHT))
        return;
    btnCloseLoss.Text("Close Loss");
    MainWindow.Add(btnCloseLoss);
    y += ROW_HEIGHT + MARGIN;

    if(!btnCloseAll.Create(0,"btnCloseAll",0,MARGIN,y,btnWidth,ROW_HEIGHT))
        return;
    btnCloseAll.Text("Close All");
    MainWindow.Add(btnCloseAll);
}

//+------------------------------------------------------------------+
//| Create Breakeven Panel Elements                                  |
//+------------------------------------------------------------------+
void CreateBreakevenPanel()
{
    int y = PANEL_OFFSET;
    
    if(!btnBreakevenExecute.Create(0,"btnBreakevenExecute",0,MARGIN,y,COLUMN_WIDTH*2,ROW_HEIGHT))
        return;
    btnBreakevenExecute.Text("Set Breakeven");
    MainWindow.Add(btnBreakevenExecute);
    y += ROW_HEIGHT + MARGIN;

    if(!chkAutoBreakeven.Create(0,"chkAutoBreakeven",0,MARGIN,y,COLUMN_WIDTH*2,ROW_HEIGHT))
        return;
    chkAutoBreakeven.Text("Adjust for Spread/Commission");
    MainWindow.Add(chkAutoBreakeven);
}

//+------------------------------------------------------------------+
//| Show/Hide Panels                                                 |
//+------------------------------------------------------------------+
void ShowPanel(int panelIndex)
{
    activePanel = panelIndex;
    
    // Trade Panel
    edtLots.Hide();
    edtTP.Hide();
    edtSL.Hide();
    btnBuyMarket.Hide();
    btnSellMarket.Hide();
    btnBuyLimit.Hide();
    btnSellLimit.Hide();
    btnBuyStop.Hide();
    btnSellStop.Hide();

    // Close Panel
    btnCloseAllBuy.Hide();
    btnCloseAllSell.Hide();
    btnCloseProfit.Hide();
    btnCloseLoss.Hide();
    btnCloseAll.Hide();

    // Breakeven Panel
    btnBreakevenExecute.Hide();
    chkAutoBreakeven.Hide();

    switch(panelIndex)
    {
        case 0: // Trade
            edtLots.Show();
            edtTP.Show();
            edtSL.Show();
            btnBuyMarket.Show();
            btnSellMarket.Show();
            btnBuyLimit.Show();
            btnSellLimit.Show();
            btnBuyStop.Show();
            btnSellStop.Show();
            break;
            
        case 1: // Close
            btnCloseAllBuy.Show();
            btnCloseAllSell.Show();
            btnCloseProfit.Show();
            btnCloseLoss.Show();
            btnCloseAll.Show();
            break;
            
        case 2: // Breakeven
            btnBreakevenExecute.Show();
            chkAutoBreakeven.Show();
            break;
    }
}

//+------------------------------------------------------------------+
//| Chart Event Handler                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam)
{
    if(id == CHARTEVENT_OBJECT_CLICK)
    {
        // Handle main buttons
        if(sparam == "btnTrade") ShowPanel(0);
        else if(sparam == "btnClose") ShowPanel(1);
        else if(sparam == "btnBreakeven") ShowPanel(2);

        // Handle order execution
        HandleTradePanelEvents(sparam);
        HandleClosePanelEvents(sparam);
        HandleBreakevenEvents(sparam);
    }
}

//+------------------------------------------------------------------+
//| Handle Trade Panel Events                                        |
//+------------------------------------------------------------------+
void HandleTradePanelEvents(const string sparam)
{
    double lots = StringToDouble(edtLots.Text());
    double tp = StringToDouble(edtTP.Text());
    double sl = StringToDouble(edtSL.Text());
    
    if(sparam == "btnBuyMarket")
        ExecuteOrder(ORDER_TYPE_BUY, lots, 0, sl, tp);
    else if(sparam == "btnSellMarket")
        ExecuteOrder(ORDER_TYPE_SELL, lots, 0, sl, tp);
    // Add similar handlers for other order types
}

//+------------------------------------------------------------------+
//| Execute Order Function                                           |
//+------------------------------------------------------------------+
void ExecuteOrder(ENUM_ORDER_TYPE order_type, double lots, double price=0, double sl=0, double tp=0)
{
    if(order_type <= ORDER_TYPE_SELL) // Market orders
    {
        if(!trade.PositionOpen(_Symbol, order_type, lots, 
           order_type==ORDER_TYPE_BUY ? SymbolInfoDouble(_Symbol,SYMBOL_ASK) : SymbolInfoDouble(_Symbol,SYMBOL_BID),
           sl, tp))
            Print("Order failed: ", GetLastError());
    }
    else // Pending orders
    {
        if(!trade.OrderOpen(_Symbol, order_type, lots, 0, price, sl, tp))
            Print("Order failed: ", GetLastError());
    }
}

//+------------------------------------------------------------------+
//| Handle Close Panel Events                                        |
//+------------------------------------------------------------------+
void HandleClosePanelEvents(const string sparam)
{
    if(sparam == "btnCloseAllBuy")
        CloseAllPositions(POSITION_TYPE_BUY);
    else if(sparam == "btnCloseAllSell")
        CloseAllPositions(POSITION_TYPE_SELL);
    else if(sparam == "btnCloseAll")
        CloseAllPositions();
}

//+------------------------------------------------------------------+
//| Close All Positions                                              |
//+------------------------------------------------------------------+
void CloseAllPositions(int type=-1)
{
    for(int i=PositionsTotal()-1; i>=0; i--)
    {
        if(position_info.SelectByIndex(i))
        {
            if(type == -1 || position_info.PositionType() == type)
                trade.PositionClose(position_info.Ticket());
        }
    }
}

//+------------------------------------------------------------------+
//| Handle Breakeven Events                                          |
//+------------------------------------------------------------------+
void HandleBreakevenEvents(const string sparam)
{
    if(sparam == "btnBreakevenExecute")
        AdjustBreakeven();
}

//+------------------------------------------------------------------+
//| Breakeven Adjustment Function                                    |
//+------------------------------------------------------------------+
void AdjustBreakeven()
{
    for(int i=PositionsTotal()-1; i>=0; i--)
    {
        if(position_info.SelectByIndex(i))
        {
            double new_sl = position_info.PriceOpen();
            if(chkAutoBreakeven.Checked())
            {
                double spread = SymbolInfoInteger(_Symbol,SYMBOL_SPREAD)*_Point;
                new_sl += (position_info.PositionType()==POSITION_TYPE_BUY) ? spread : -spread;
            }
            if(!trade.PositionModify(position_info.Ticket(), new_sl, position_info.TakeProfit()))
                Print("Breakeven failed: ", GetLastError());
        }
    }
}

//+------------------------------------------------------------------+
//| Expert Deinitialization Function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
    MainWindow.Destroy(reason);
}

//+------------------------------------------------------------------+
//| Expert Tick Function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
    // Update dynamic data here
}