whats the best approach to detect button click and then open trades ?

 
I have written a simple trading pannel which will open trades based on button clicks but I just noticed that sometimes it would open trades automatically , even though I havent clicked any button.

here is what the code looks like :

void OnChartEvent(const int id,const long& lparam,const double& dparam,const string& sparam)
  { 
   string btnclick = sparam;
   string btntoreset = sparam;
   if(Risk != 0  )
     {
      LotSize = GetLots(Risk,Sl);
      if(StringFind(btnclick,"Buy ",0)>=0) //I have multiple buy buttons and Here I am finding if a buy button was clicked.
        {
         StringReplace(btnclick,"Buy ",""); // Here I am replacing the "Buy " word from the name of the button because the names of the buy buttons are like this: Buy 2, Buy 4, Buy 6 
         int NbrOfTrades =(int) btnclick; // Here I am trying to see how many trades to open
         for(int i=0; i<NbrOfTrades; i++)
           {
            RefreshRates();
            if(!OrderSend(Symbol(),0,LotSize,Ask,100,Ask-Sl*Point,Ask+Tp*Point,NULL,(int)magicnum))
              {
               Print("Error: ",GetLastError()," during opening the market order.");
              }
           }
         ObjectSetInteger(0,btntoreset, OBJPROP_STATE, 0);
        }
      if(StringFind(btnclick,"Sell ",0)>=0)
        {
         StringReplace(btnclick,"Sell ","");
         int NbrOfTrades =(int) btnclick;
         for(int i=0; i<NbrOfTrades; i++)
           {
            RefreshRates();
            if(!OrderSend(Symbol(),1,LotSize,Ask,100,Bid+Sl*Point,Ask-Tp*Point,NULL,(int)magicnum))
              {
               Print("Error: ",GetLastError()," during opening the market order.");
              }
           }
         ObjectSetInteger(0,btntoreset, OBJPROP_STATE, 0);
        }
     }  
  }

I have tried to recreate the issue , but I have no Idea what causes this problem , and which way I should go to solve this , I know some thing is wrong with my approach , but if some one can point out what I am doing wrong I will really appreciate it 
 
  1. The very first thing is finding out what event has occurred. You are assuming that it is a button click.

    void OnChartEvent(const int id,
                      const long &lparam,
                      const double &dparam,
                      const string &sparam){
       if(      id == CHARTEVENT_KEYDOWN){
          ⋮
       }  // CHARTEVENT_KEYDOWN
       else  if(id == CHARTEVENT_MOUSE_MOVE){
          // Check for dragging levels.
          ⋮
       }                                                        // Mouse move.
       else  if(direction == 0)         // GUI not visible; Ignore clicks,
          return;                       // Including a double click on order button.
       else  if(id == CHARTEVENT_OBJECT_CLICK){
          if(      sparam == "MMGT_RiskSizeButtonPlus"){
             ⋮
       }  // CHARTEVENT_OBJECT_CLICK
       else  if(id == CHARTEVENT_OBJECT_CREATE){
          ⋮
  2.             if(!OrderSend(Symbol(),0,LotSize,Ask,100,Ask-Sl*Point,Ask+Tp*Point,NULL,(int)magicnum))
    
                if(!OrderSend(Symbol(),1,LotSize,Ask,100,Bid+Sl*Point,Ask-Tp*Point,NULL,(int)magicnum))

    You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)

      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes.
      My GBPJPY shows average spread = 26 points, average maximum spread = 134.
      My EURCHF shows average spread = 18 points, average maximum spread = 106.
      (your broker will be similar).
                Is it reasonable to have such a huge spreads (20 PIP spreads) in EURCHF? - General - MQL5 programming forum (2022)

 
William Roeder #:
  1. The very first thing is finding out what event has occurred. You are assuming that it is a button click

ahhh so it was a blunder on my part to try and catch the button click by sparam alone 

Reason: