is possible do that in MT4 ? - page 2

 

hi i want  modify this  code when i push shift  and click  must do something , i use this combo because if i  use only left click mouse , appear also when push over the button , i just did do a modify but  not  work anyone  can  helpme  ?   thanks

//+------------------------------------------------------------------+
//|                                     STA.mq4                      |
//|                                      Copyright © 2023, Faustf    |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright   "Copyright © 2023, Faustf"
#property link        ""
#property version     "1.0"      // Current version of the Expert Advisor
#property strict 

extern string   b10="=== CROSS CORRELATO ===";
extern string   Cross_Correlato="NZDUSD";
extern string   b11="=== LA CORRELAZIONE E' INVERSA ? ===";
extern int      Correlaz_Inv=0;

int ctrlclick=0;
double PricePrimario;
double PriceCorrelato;
uint state;

void OnTick()
{
   // create the object 
   ObjectCreate(_Symbol,"Apri",OBJ_BUTTON,0,0,0);
   
   

//set distance from border x
ObjectSetInteger(_Symbol,"Apri",OBJPROP_XDISTANCE,200);
// set width
ObjectSetInteger(_Symbol,"Apri",OBJPROP_XSIZE,50);
// set distance fromborder y
ObjectSetInteger(_Symbol,"Apri",OBJPROP_YDISTANCE,200);
// set hight
ObjectSetInteger(_Symbol,"Apri",OBJPROP_YSIZE,21);
// set chart corner 
ObjectSetInteger(_Symbol,"Apri",OBJPROP_CORNER,4);


}

string MouseState()
  {
   string res;
   res+="\nML: "   +(((state& 1)== 1)?"DN":"UP");   // mouse left
   res+="\nMR: "   +(((state& 2)== 2)?"DN":"UP");   // mouse right 
   res+="\nMM: "   +(((state&16)==16)?"DN":"UP");   // mouse middle
   res+="\nMX: "   +(((state&32)==32)?"DN":"UP");   // mouse first X key
   res+="\nMY: "   +(((state&64)==64)?"DN":"UP");   // mouse second X key
   res+="\nSHIFT: "+(((state& 4)== 4)?"DN":"UP");   // shift key
   res+="\nCTRL: " +(((state& 8)== 8)?"DN":"UP");   // control key
   return(res);
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
    //--- Show the event parameters on the chart
    Comment(__FUNCTION__, ": id=", id, " lparam=", lparam, " dparam=", dparam, " sparam=", sparam);
    
    //--- If this is an event of a mouse click on the chart
    if (id == CHARTEVENT_CLICK)
    {
        //--- Prepare variables
        int      x     = (int)lparam;
        int      y     = (int)dparam;
        datetime dt    = 0;
        double   price = 0;
        int      window = 0;
        
        //--- Check if the Shift key is pressed
        if ((state& 4)== 4)  //  corrispond at button Shift
        {
            ctrlclick++;
            
            //--- Convert the X and Y coordinates in terms of date/time
            if (ChartXYToTimePrice(0, x, y, window, dt, price))
            {
                PrintFormat("Window=%d X=%d  Y=%d  =>  Time=%s  Price=%G", window, x, y, TimeToString(dt), price);
                
                //--- Perform reverse conversion: (X,Y) => (Time,Price)
                if (ChartTimePriceToXY(0, window, dt, price, x, y))
                {
                    if (ctrlclick == 1)
                    {
                        int shift = iBarShift(Cross_Correlato, 0, dt);
                        Print("index of the bar for the time ", TimeToStr(dt), " is ", shift);
                        Print("NZD  ", iOpen(Cross_Correlato, 0, shift));
                        PrintFormat("Time=%s  Price=%G  =>  X=%d  Y=%d", TimeToString(dt), price, x, y);
                        PricePrimario = iOpen(Cross_Correlato, 0, shift);
                    }
                    else if (ctrlclick == 2)
                    {
                        int shift2 = iBarShift(Cross_Correlato, 0, dt);
                        Print("index of the bar for the time ", TimeToStr(dt), " is ", shift2);
                        Print("NZD2  ", iOpen(Cross_Correlato, 0, shift2));
                        PrintFormat("Time=%s  Price=%G  =>  X=%d  Y=%d", TimeToString(dt), price, x, y);
                        PriceCorrelato = iOpen(Cross_Correlato, 0, shift2);
                        ctrlclick = 0;
                    }
                }
                else
                {
                    Print("ChartTimePriceToXY return error code: ", GetLastError());
                }
                
                //--- Delete lines
                ObjectDelete(0, "V Line");
                ObjectDelete(0, "H Line");
                
                //--- Create horizontal and vertical lines of the crosshair
                ObjectCreate(0, "H Line", OBJ_HLINE, window, dt, price);
                ObjectCreate(0, "V Line", OBJ_VLINE, window, dt, price);
                
                ChartRedraw(0);
            }
            else
            {
                Print("ChartXYToTimePrice return error code: ", GetLastError());
            }
            
            Print("+--------------------------------------------------------------+");
        }
    }
}

 

Your code has many problems.

  • you are creating a button on every tick
    • this will work first time, each subsequent call to ObjectCreate will fail with an error. 
    • further commands - setting coordinates and size will be executed on every tick
  • you should create a button in OnInit() and remove it in OnDeinit() event
    • alternatively you could check on every tick if button still exists and recreate it if the user deletes it from the chart, but I wouldn't bother.  
  • check this page and search for the ObjectCreate() section - you will find a code sample that creates label in the OnInit and removes it in the OnDeinit

Next problem is in the OnChartEvent() function

  • you use global variable "state" that never gets value
  • since the "state" variable is global, it is initialized to zero (0) and it will never be equal 4, so your code will never execute 
  • if you copied this snippet from somewhere, please check that example again and find out how to set value of the "state" variable
Resources - MQL4 programs - MQL4 Reference
Resources - MQL4 programs - MQL4 Reference
  • docs.mql4.com
Resources - MQL4 programs - MQL4 Reference
Reason: