ERROR 4112 Work around

 

I wrote this to trade "manually" using an EA for a broker that doesn't allow auto trading... ERROR 4112. My experience with interacting with Windows using USER32.DLL very is limited, first time actually. I managed to make it work, but with some issues...

First, the work around uses static locations for the buttons instead of getting the dynamic location from Windows. I don't know how to get the coordinates for the places I need to click, other than trial and error. That works, but it would be much better to have it dynamic.

Second, I'm still working on a feedback system so once an order is placed it waits for the server to respond before moving along in the code. This would be built into the Main EA calling FU4112 functions. During my tests, it would open a second order and then the scroll bar would appear in the trade terminal and the order close button would move and cause fun problems.

Lastly, I added a button object in the main EA to act as a switch to turn off calls to FU4112. I have a touch screen, but would also like to add a keyboard kill switch. I made the mistake of moving my cursor every second and forgot to limit the amount of times it happened... had to force shutdown to regain control.

The coordinates are based on the upper left corner of the New Order window being at the center of the cross hair button.

NeworderWindow

//+------------------------------------------------------------------+
//|                                                       FU4112.mqh |
//|                                    Copyright 2019, Chad Magruder |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, Chad Magruder"
#property version   "1.00"
#property strict
#include <WinUser32.mqh>
#import "user32.dll"
   int SetCursorPos(int dx,int dy);
#import

//NUETRAL SPACE ON CHART
   int NEUTRALx=1545;
   int NEUTRALy=180;
//NEW ORDER BUTTON
   int NEWORDERx=500;
   int NEWORDERy=100;
//VOLUME FIELD IN NEWORDER POPUP WINDOW
   int VOLUMEx=1000;
   int VOLUMEy=310;
//SL FIELD IN NEWORDER POPUP WINDOW
   int SLx=1000;
   int SLy=360;
//BUY BUTTON IN NEWORDER POPUP WINDOW
   int BUYx=1350;
   int BUYy=675;
//OSELL BUTTON IN NEWORDER POPUP WINDOW
   int SELLx=950;
   int SELLy=675;
//TRADE TERMINAL ORDER CLOSE BUTTON
   int CLOSEx=2705;
   int CLOSEy=1600;
//CLOSE POPUP WINDOW
   int PUx=1545;
   int PUy=180;
//+------------------------------------------------------------------+
//                       FU4112 Functions                            |
//+------------------------------------------------------------------+
void TestCoords(int x,int y){
   SetCursorPos(x,y);
}//TestCoords()
//+------------------------------------------------------------------+
void CloseOrder(){
   SetCursorPos(CLOSEx,CLOSEy);//TRADE TERMINAL ORDER CLOSE BUTTON
   mouse_event(0x0002,0,0,0,0);//Left button down
   Sleep(10);
   mouse_event(0x0004,0,0,0,0);//Left button up  
   Sleep(100);
   SetCursorPos(NEUTRALx,NEUTRALy);//NEUTRAL
   Sleep(1000);
}
//+------------------------------------------------------------------+
void NewOrder(int i,double s, double l){
char ls[5];
StringToCharArray(DoubleToStr(l,2),ls,0,5," ");
char sl[7];
StringToCharArray(DoubleToStr(s,2),sl,0,7," ");

   SetCursorPos(NEWORDERx,NEWORDERy);
   Sleep(10);
   mouse_event(0x0002,0,0,0,0);//Left button down
   Sleep(100);
   mouse_event(0x0004,0,0,0,0);//Left button up  
   Sleep(100);  
//Popup Window opened
   
   SetCursorPos(VOLUMEx,VOLUMEy);
   Sleep(10);
   mouse_event(0x0002,0,0,0,0);//Left button down
   Sleep(100);
   mouse_event(0x0004,0,0,0,0);//Left button up  
   Sleep(100);  
//Volume field selected
   
   for(int a=0;a<5;a++){
      switch(ls[a]){
       case 46:{
          keybd_event(0x6E,0x45,KEYEVENTF_EXTENDEDKEY | 0,0);
          keybd_event(0x6E,0x45,KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,0);
       }break;//"."
       case 48:{
          keybd_event(0x30,0x45,KEYEVENTF_EXTENDEDKEY | 0,0);
          keybd_event(0x30,0x45,KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,0);
       }break;//0
       case 49:{
          keybd_event(0x31,0x45,KEYEVENTF_EXTENDEDKEY | 0,0);
          keybd_event(0x31,0x45,KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,0);
       }break;//1
       case 50:{
          keybd_event(0x32,0x45,KEYEVENTF_EXTENDEDKEY | 0,0);
          keybd_event(0x32,0x45,KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,0);
       }break;//2
       case 51:{
          keybd_event(0x33,0x45,KEYEVENTF_EXTENDEDKEY | 0,0);
          keybd_event(0x33,0x45,KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,0);
       }break;//3
       case 52:{
         keybd_event(0x34,0x45,KEYEVENTF_EXTENDEDKEY | 0,0);
         keybd_event(0x34,0x45,KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,0);
       }break;//4
       case 53:{
         keybd_event(0x35,0x45,KEYEVENTF_EXTENDEDKEY | 0,0);
         keybd_event(0x35,0x45,KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,0);
       }break;//5
       case 54:{
         keybd_event(0x36,0x45,KEYEVENTF_EXTENDEDKEY | 0,0);
         keybd_event(0x36,0x45,KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,0);
       }break;//6
       case 55:{
         keybd_event(0x37,0x45,KEYEVENTF_EXTENDEDKEY | 0,0);
         keybd_event(0x37,0x45,KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,0);
       }break;//7
       case 56:{
         keybd_event(0x38,0x45,KEYEVENTF_EXTENDEDKEY | 0,0);
         keybd_event(0x38,0x45,KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,0);
       }break;//8
       case 57:{
         keybd_event(0x39,0x45,KEYEVENTF_EXTENDEDKEY | 0,0);
         keybd_event(0x39,0x45,KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,0);
       }break;//9
       default:{break;}
      }//switch
   }//loop a
//Volume adjusted
   
   SetCursorPos(SLx,SLy);//SL input field
   mouse_event(0x0002,0,0,0,0);//Left button down
   Sleep(100);
   mouse_event(0x0004,0,0,0,0);//Left button up  
   Sleep(100);
   mouse_event(0x0002,0,0,0,0);//Left button down
   Sleep(100);
   mouse_event(0x0004,0,0,0,0);//Left button up  
   Sleep(100);
//SL field selected
   
   for(int a=0;a<7;a++){
      switch(sl[a]){
       case 46:{
          keybd_event(0x6E,0x45,KEYEVENTF_EXTENDEDKEY | 0,0);
          keybd_event(0x6E,0x45,KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,0);
       }break;//"."
       case 48:{
          keybd_event(0x30,0x45,KEYEVENTF_EXTENDEDKEY | 0,0);
          keybd_event(0x30,0x45,KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,0);
       }break;//0
       case 49:{
          keybd_event(0x31,0x45,KEYEVENTF_EXTENDEDKEY | 0,0);
          keybd_event(0x31,0x45,KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,0);
       }break;//1
       case 50:{
          keybd_event(0x32,0x45,KEYEVENTF_EXTENDEDKEY | 0,0);
          keybd_event(0x32,0x45,KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,0);
       }break;//2
       case 51:{
          keybd_event(0x33,0x45,KEYEVENTF_EXTENDEDKEY | 0,0);
          keybd_event(0x33,0x45,KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,0);
       }break;//3
       case 52:{
         keybd_event(0x34,0x45,KEYEVENTF_EXTENDEDKEY | 0,0);
         keybd_event(0x34,0x45,KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,0);
       }break;//4
       case 53:{
         keybd_event(0x35,0x45,KEYEVENTF_EXTENDEDKEY | 0,0);
         keybd_event(0x35,0x45,KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,0);
       }break;//5
       case 54:{
         keybd_event(0x36,0x45,KEYEVENTF_EXTENDEDKEY | 0,0);
         keybd_event(0x36,0x45,KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,0);
       }break;//6
       case 55:{
         keybd_event(0x37,0x45,KEYEVENTF_EXTENDEDKEY | 0,0);
         keybd_event(0x37,0x45,KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,0);
       }break;//7
       case 56:{
         keybd_event(0x38,0x45,KEYEVENTF_EXTENDEDKEY | 0,0);
         keybd_event(0x38,0x45,KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,0);
       }break;//8
       case 57:{
         keybd_event(0x39,0x45,KEYEVENTF_EXTENDEDKEY | 0,0);
         keybd_event(0x39,0x45,KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,0);
       }break;//9
       default:{break;}
      }//switch
   }//loop a
//SL entered
   
   if(i==1){
      SetCursorPos(BUYx,BUYy);//SL input field
      mouse_event(0x0002,0,0,0,0);//Left button down
      Sleep(100);
      mouse_event(0x0004,0,0,0,0);//Left button up  
      Sleep(100);
   }
//BUY order placed
   
   if(i==-1){
      SetCursorPos(SELLx,SELLy);//SL input field
      mouse_event(0x0002,0,0,0,0);//Left button down
      Sleep(100);
      mouse_event(0x0004,0,0,0,0);//Left button up  
      Sleep(100);
   }
//SELL order placed
   Sleep(2000);
   SetCursorPos(NEUTRALx,NEUTRALy);
   mouse_event(0x0002,0,0,0,0);//Left button down
   Sleep(100);
   mouse_event(0x0004,0,0,0,0);//Left button up
   Sleep(100);
//Cursor moved to NEUTRAL
}//NewOrder()
Reason: