Strg + Mouseclick refining

 

Hello Community,

I have recently developed an EA that can set all Stoplosses for all Trades on a Symbol on the position where the mouse cursor is while holding Ctrl-key. It works, but the thing is that I have to press the left mousebutton twice in order to activate the right price for the stoploss, the first time I click it sets the stoploss to a wrong place and I can't figure out why.

 Here is the Code:

 

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
double   price = 0;
void OnInit()
  {
//--- enable CHART_EVENT_MOUSE_MOVE messages
   ChartSetInteger(0,CHART_EVENT_MOUSE_MOVE,1);
  }
//+------------------------------------------------------------------+
//| MouseState                                                       |
//+------------------------------------------------------------------+
bool MouseState(uint state)
  {
   bool res;
   bool ml = (((state& 1)== 1)?true:false); // mouse left
   bool ctrl = (((state& 8)== 8)?true:false); // ctrl key
  
   if(ml == true && ctrl == true)res = true;
   else res = false;
   return(res);
  }
bool MouseState2(uint state)
  {
   bool res;
   bool ml = (((state& 1)== 1)?true:false); // mouse left
   bool ctrl = (((state& 4)== 4)?true:false); // ctrl key
  
   if(ml == true && ctrl == true)res = true;
   else res = false;
   return(res);
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
    
   if(id==CHARTEVENT_MOUSE_MOVE) {
  
   double total;
   int cnt;
  
      if(MouseState((uint)sparam)) {
        
            total = OrdersTotal();
            for (cnt = total-1; cnt >=0 ; cnt--)
            {
               if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
               {
                  if(OrderSymbol() == Symbol()) {
            
                  switch(OrderType())
                  {
                     case OP_BUY       :
                        if(!OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(price,Digits),OrderTakeProfit(),0))GetLastError();
                        break;
                  
                     case OP_SELL      :
                        if(!OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(price,Digits),OrderTakeProfit(),0))GetLastError();
                        break;
                  }
            
                  }            
               }
            }
        
         }
      
         if(MouseState2((uint)sparam)) {
        
            total = OrdersTotal();
            for (cnt = total-1; cnt >=0 ; cnt--)
            {
               if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
               {
                  if(OrderSymbol() == Symbol()) {
            
                  switch(OrderType())
                  {
                     case OP_BUY       :
                        if(!OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),NormalizeDouble(price,Digits),0))GetLastError();
                        break;
                  
                     case OP_SELL      :
                        if(!OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),NormalizeDouble(price,Digits),0))GetLastError();
                        break;
                  }
            
                  }            
               }
            }
         }
        
      }
  
      if(id==CHARTEVENT_CLICK)
     {
      //--- Prepare variables
      int      x     =(int)lparam;
      int      y     =(int)dparam;
      datetime dt    =0;
      int      window=0;
      price = NormalizeDouble(price,Digits);
      //--- Convert the X and Y coordinates in terms of date/time
      if(!ChartXYToTimePrice(0,x,y,window,dt,price))
         Print("ChartXYToTimePrice return error code: ",GetLastError());
     }
  
  }

I already tried something like 
 

if(id==CHARTEVENT_MOUSE_MOVE && id==CHARTEVENT_CLICK) {
//....
}

but that doesn't seem to work at all.


Any help appreciated ;)
 

 

You are very nearly there. You just need to put your bottom piece of code into its own function and call it each time MOUSE_MOVE traps the correct click. You can ignore CHARTEVENT_CLICK altogether

double GetPrice(double dparam)
     {
      //--- Prepare variables
      int      x     =0;
      int      y     =(int)dparam;
      datetime dt    =0;
      int      window=0;
      double price = 0;
      //--- Convert the X and Y coordinates in terms of date/time
      if(!ChartXYToTimePrice(0,x,y,window,dt,price)) Print("ChartXYToTimePrice return error code: ",GetLastError());
      return(price);
     }

 

void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
    
   if(id==CHARTEVENT_MOUSE_MOVE) {
  
   double total;
   int cnt;
  
      if(MouseState((uint)sparam)) {
         price=GetPrice(dparam); // then the rest of your code for the block
 
honest_knave:

You are very nearly there. You just need to put your bottom piece of code into its own function and call it each time MOUSE_MOVE traps the correct click. You can ignore CHARTEVENT_CLICK altogether

double GetPrice(double dparam)
     {
      //--- Prepare variables
      int      x     =0;
      int      y     =(int)dparam;
      datetime dt    =0;
      int      window=0;
      double price = 0;
      //--- Convert the X and Y coordinates in terms of date/time
      if(!ChartXYToTimePrice(0,x,y,window,dt,price)) Print("ChartXYToTimePrice return error code: ",GetLastError());
      return(price);
     }

 

void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
    
   if(id==CHARTEVENT_MOUSE_MOVE) {
  
   double total;
   int cnt;
  
      if(MouseState((uint)sparam)) {
         price=GetPrice(dparam); // then the rest of your code for the block
Thank you very much it works fine :)