Need help, MQL4 first timer...

 

Hello everybody, thank you very much for any support you would kindly give me.

I am a newbi in programming and I am trying  to make this few lines work... It is very simple stuff: when I press the "u" button, it opens a buy order, when I press the "n" button, a sell order is triggered.

Then, it checks which kind of order is opened (but or sell) and it applies a trailing stop according to the parameters ("spostamento/shifting" and "profitto/prifit" I put in an editable field together with the lots number "lotti").

This is the "Frankenstein code" I put together :), I run it and... nothign happens!! :D

string CampoTesto[1000];
double Profitto;
double Spostamento;
double Lotti;

double NewSL=0;


int OnInit()
  {    
    creazioneSfondo("Sfondo");
    creaCampoEditabile("Profitto","Pips di profitto",75,150,150,30,clrWhite,clrGray);
    creaCampoEditabile("Spostamento","Spostamento SL [pips]",75,200,150,30,clrWhite,clrGray);
    creaCampoEditabile("Lotti","Numero di Lotti",75,250,150,30,clrWhite,clrGray);
    
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
    
         if(!ObjectDelete(0,"Profitto")){
            Print("Errore eliminazione pulsante");
         }
         if(!ObjectDelete(0,"Lotti")){
            Print("Errore eliminazione pulsante");
         }
            
         if(!ObjectDelete(0,"Spostamento")){
            Print("Errore eliminazione pulsante");
         }
         
         if(!ObjectDelete(0,"Sfondo")){
            Print("Errore eliminazione pulsante");
         }        
   
  }
//+------------------------------------------------------------------+
//| Operazione con un tasto                                             |
//+------------------------------------------------------------------+

void OnChartEvent(const int id, const long& lparam, const double& dparam, const string& sparam)
  {
   
     if(id==CHARTEVENT_KEYDOWN)
      
       if(lparam==85)
       OrderSend(Symbol(),OP_BUY,"Lotti",Bid,0,Bid-0.0001,0,"Ordine di Vendita",123,0,clrAliceBlue);
       
       if(lparam==78)
       OrderSend(Symbol(),OP_SELL,"Lotti",Ask,0,Ask+0.0001,0,"Ordine di Acquisto",123,0,clrRed);
       
      
        
  }
    

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+


void OnTick()
  {
   
     if(OrdersTotal()>0)
      
      if(!OrderSelect(0,SELECT_BY_TICKET,MODE_TRADES)){
       Print("Errore Ordine Select #",GetLastError());
      }
     
     if(OrderType()==OP_BUY){ 
     
         
         if( (Bid-OrderOpenPrice())>Profitto*Point*10 && Bid-OrderStopLoss()>Spostamento*Point*10){

             NewSL=Bid-Spostamento*Point*10;
             
             if(!OrderModify(OrderTicket(),OrderOpenPrice(),NewSL,OrderTakeProfit(),OrderExpiration(),clrAliceBlue)){
               Print("Errore Ordine Select #",GetLastError());
             }         
         }   
     }
     
     else{
     
                if( (OrderOpenPrice()-Ask)>Profitto*Point*10 && OrderStopLoss()-Ask>Spostamento*Point*10){

             NewSL=Ask+Spostamento*Point*10;
             
             if(!OrderModify(OrderTicket(),OrderOpenPrice(),NewSL,OrderTakeProfit(),OrderExpiration(),clrAliceBlue)){
               Print("Errore Ordine Select #",GetLastError());
             }
         }     
     }
    }
   
//+------------------------------------------------------------------+


// funzione che crea lo sfondo

void creazioneSfondo(const string name){
   if(!ObjectCreate(0,name,OBJ_RECTANGLE_LABEL,0,0,0)){
      Print("Errore creazione sfondo");
      return;
   }
   ObjectSetInteger(0,name,OBJPROP_CORNER,CORNER_LEFT_UPPER);
   ObjectSetInteger(0,name,OBJPROP_XDISTANCE,20);
   ObjectSetInteger(0,name,OBJPROP_YDISTANCE,140);
   ObjectSetInteger(0,name,OBJPROP_XSIZE,250);
   ObjectSetInteger(0,name,OBJPROP_YSIZE,150);
   
//--- set background color
   ObjectSetInteger(0,name,OBJPROP_BGCOLOR,clrGray);
   
//--- display in the foreground (false) or background (true)
   ObjectSetInteger(0,name,OBJPROP_BACK,false);
   ObjectSetInteger(0,name,OBJPROP_SELECTABLE,false);
   ObjectSetInteger(0,name,OBJPROP_SELECTED,false);
   ObjectSetInteger(0,name,OBJPROP_HIDDEN,false);
}


void creaCampoEditabile(string NomeOggetto, string objText,int xDistance,int yDistance,int xSize,int ySize,color clrIns,color clrTextIns){
   
   if(!ObjectCreate(0,NomeOggetto,OBJ_EDIT,0,0,0)){ 
      Print("Errore creazione pulsante edit");
      return;
   }
   
   ObjectSetInteger(0,NomeOggetto,OBJPROP_CORNER,CORNER_LEFT_UPPER);
   
   ObjectSetInteger(0,NomeOggetto,OBJPROP_XDISTANCE,xDistance); 
   ObjectSetInteger(0,NomeOggetto,OBJPROP_YDISTANCE,yDistance); 

   ObjectSetInteger(0,NomeOggetto,OBJPROP_XSIZE,xSize); 
   ObjectSetInteger(0,NomeOggetto,OBJPROP_YSIZE,ySize);
   
//--- set the text 
   ObjectSetString(0,NomeOggetto,OBJPROP_TEXT,objText);
   ObjectSetString(0,NomeOggetto,OBJPROP_FONT,"Arial");
   ObjectSetInteger(0,NomeOggetto,OBJPROP_FONTSIZE,8);
   ObjectSetInteger(0,NomeOggetto,OBJPROP_ALIGN,ALIGN_CENTER);
   
   
   ObjectSetInteger(0,NomeOggetto,OBJPROP_READONLY,false);
   
   ObjectSetInteger(0,NomeOggetto,OBJPROP_BGCOLOR,clrIns);
   ObjectSetInteger(0,NomeOggetto,OBJPROP_COLOR,clrTextIns);
   
   ObjectSetInteger(0,NomeOggetto,OBJPROP_BORDER_COLOR,clrDarkGreen);
   
//--- display in the foreground (false) or background (true) 
   ObjectSetInteger(0,NomeOggetto,OBJPROP_BACK,false); 
//--- set button state 
   ObjectSetInteger(0,NomeOggetto,OBJPROP_STATE,false);
   ObjectSetInteger(0,NomeOggetto,OBJPROP_SELECTABLE,false);
   ObjectSetInteger(0,NomeOggetto,OBJPROP_SELECTED,false);
   ObjectSetInteger(0,NomeOggetto,OBJPROP_HIDDEN,false);
}



Any help would be much appreciated... thanks so much.


***

Documentation on MQL5: Constants, Enumerations and Structures / Objects Constants / Object Types
Documentation on MQL5: Constants, Enumerations and Structures / Objects Constants / Object Types
  • www.mql5.com
Object Types - Objects Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
SimoMarietta :

Hello everybody, thank you very much for any support you would kindly give me.

I am a newbi in programming and I am trying  to make this few lines work... It is very simple stuff: when I press the "u" button, it opens a buy order, when I press the "n" button, a sell order is triggered.

Then, it checks which kind of order is opened (but or sell) and it applies a trailing stop according to the parameters ("spostamento/shifting" and "profitto/prifit" I put in an editable field together with the lots number "lotti").

This is the "Frankenstein code" I put together :), I run it and... nothign happens!! :D


Any help would be much appreciated... thanks so much.


***

Please insert the code correctly - using the button Code

 
SimoMarietta:


Please use the code button (Alt+S) when pasting code.

Topics concerning MT4 and MQL4 have their own section.

In future please post in the correct section.

I have moved your topic to the MQL4 and Metatrader 4 section.

 
Keith Watford:

Please use the code button (Alt+S) when pasting code.

Topics concerning MT4 and MQL4 have their own section.

In future please post in the correct section.

I have moved your topic to the MQL4 and Metatrader 4 section.

I willl paste the code correctly, sorry for mistake and thanks for moving me to the right section :)
 
Vladimir Karputov:

Please insert the code correctly - using the button

Done, sorry for not doing it but I am a real newbi to the forum too.
 

Always use 

#property strict

Read the documentation for OrderSend and check the return value. Print the error if necessary.

if(OrdersTotal()>0)
      
      if(!OrderSelect(0,SELECT_BY_TICKET,MODE_TRADES)){
       Print("Errore Ordine Select #",GetLastError());
      }

There will never be a trade with the ticket #0

 
Keith Watford:

Always use 

Read the documentation for OrderSend and check the return value. Print the error if necessary.

There will never be a trade with the ticket #0

Thank you Keith,

yes I used #propery strict but didn't mention it.

You are right, I should selcet by position and not by trade... 


Why do you think the keyboard event is not working? 

 
SimoMarietta:

Why do you think the keyboard event is not working? 

Keith Watford:

Read the documentation for OrderSend and check the return value. Print the error if necessary.

Also print something when the key is pressed in order to check.

 
Keith Watford:

Also print something when the key is pressed in order to check.

Thank you Keith for your help, but I am still not getting the point... Is it a ridicuolous idea to ask you exactly what should I do?