English

## I need help converting this php program to mql4 ea

 6 xilisam 2016.04.02 16:54   hi friends  i have some knowledge in php so i wrote this simple ea based on three level of rsi on opening trader and stoch on closing it i hope there is someone who can help me converting it to mql4  RSI33){ OrderSend(buy);}elseif(RSI33 >= RSI100 && RSI7 < RSI33){ OrderSend(sell);}if(StochMain723 == StochSignal723 && StochMain723 > 80){ OrderClose(buy);}elseif(StochMain723 == StochSignal723 && StochMain723 < 20){ OrderClose(sell);}?>
 6 xilisam 2016.04.02 19:41 #   here is my mql4 code please someone corren me  ```//-------------------------------------------------------------------- // simple.mq4// To be used as an example in MQL4 book. //-------------------------------------------------------------------- //--------------------------------------------------------------- 1 -- extern   double   StopLoss    =100;       // SL for an opened order extern   double   TakeProfit  =50;        // ТР for an opened order extern   int      RSI_low     =7;         // Period of low RSI extern   int      RSI_med     =33;        // Period of medium RSI extern   int      RSI_high    =100;       // Period of high RSI extern   int      Stoch__Kperiod    =7;       // Stochastic K line period extern   int      Stoch__Dperiod    =2;       // Stochastic D line period extern   int      Stoch__Slowing    =3;       // Stochastic Slowing extern   double   Lots        =0.01;      // Strictly set amount of lots extern   double   Prots       =0.10;      // Percent of free margin          bool     Work        =true;      // EA will work.          string   Symb;                   // Security name //-------------------------------------------------------------------- int init() {      return(0); }   //--------------------------------------------------------------- 2 -- int start() { //--------------------------------------------------------------------    int    Total,                           // Amount of orders in a window    Tip=-1,                          // Type of selected order (B=0,S=1)    Ticket;                          // Order number    double    RSI_l,                          // Current low RSI value    RSI_m,                          // Current medium RSI value    RSI_h,                          // Current high RSI value       Stoch_s,                        // Current Stoch signal value    Stoch_m,                        // Current Soch main value       Lot,                             // Amount of lots in a selected order    Lts,                             // Amount of lots in an opened order    Min_Lot,                         // Minimal amount of lots    Step,                            // Step of lot size change    Free,                            // Current free margin    One_Lot,                         // Price of one lot    Price,                           // Price of a selected order    SL,                              // SL of a selected order    TP;                              // TP за a selected order    bool    Ans  =false,                     // Server response after closing    Cls_B=false,                     // Criterion for closing Buy    Cls_S=false,                     // Criterion for closing Sell    Opn_B=false,                     // Criterion for opening Buy    Opn_S=false;                     // Criterion for opening Sell //--------------------------------------------------------------- 3 --    // Preliminary processing //--------------------------------------------------------------- 4 --    // Orders accounting    Symb=Symbol();                               // Security name    Total=0;                                     // Amount of orders    for(int i=1; i>=OrdersTotal(); i++)          // Loop through orders      {       if (OrderSelect(i-1,SELECT_BY_POS)==true) // If there is the next one         {                                       // Analyzing orders:          if (OrderSymbol()!=Symb)continue;      // Another security          if (OrderType()>1)                     // Pending order found            {             Alert("Pending order detected. EA doesn't work.");             return(0);                             // Exit start()            }          Total++;                               // Counter of market orders          if (Total<1)                           // No more than one order            {             Alert("Several market orders. EA doesn't work.");             return(0);                             // Exit start()            }          Ticket=OrderTicket();                  // Number of selected order          Tip   =OrderType();                    // Type of selected order          Price =OrderOpenPrice();               // Price of selected order          SL    =OrderStopLoss();                // SL of selected order          TP    =OrderTakeProfit();              // TP of selected order          Lot   =OrderLots();                    // Amount of lots         }      } //--------------------------------------------------------------- 5 --    // Trading criteria    RSI_l=iRSI(Symb,0,RSI_low,PRICE_CLOSE,0); // low    RSI_m=iRSI(Symb,0,RSI_med,PRICE_CLOSE,0); // medium    RSI_h=iRSI(Symb,0,RSI_high,PRICE_CLOSE,0); // high       Stoch_m=iStochastic(Symb,0,Stoch__Kperiod,Stoch__Dperiod,Stoch__Slowing,MODE_SMA,0,MODE_MAIN,0); // stoch main    Stoch_s=iStochastic(Symb,0,Stoch__Kperiod,Stoch__Dperiod,Stoch__Slowing,MODE_SMA,0,MODE_SIGNAL,0); // stoch signal    if (RSI_m <= RSI_h && RSI_l > RSI_m)         // If medium rsi equal or less then high      {                                          // and low cross medium       Opn_B=true;                               // Criterion for opening Buy      }    if (RSI_m >= RSI_h && RSI_l < RSI_m)         // If medium rsi equal or more then high      {                                          // and low cross medium       Opn_S=true;                               // Criterion for opening Sell      }    if (Stoch_m == Stoch_s && Stoch_m < 20)         // If main equal to signal      {                                          // and main is less then 20       Cls_S=true;                               // Criterion for closing Sell      }    if (Stoch_m == Stoch_s && Stoch_m > 80)         // If main equal to signal      {                                          // and main is more then 80       Cls_B=true;                               // Criterion for closing Buy      } //--------------------------------------------------------------- 6 --    // Closing orders    while(true)                                  // Loop of closing orders      {       if (Tip==0 && Cls_B==true)                // Order Buy is opened..         {                                       // and there is criterion to close          Alert("Attempt to close Buy ",Ticket,". Waiting for response..");          RefreshRates();                        // Refresh rates          Ans=OrderClose(Ticket,Lot,Bid,2);      // Closing Buy          if (Ans==true)                         // Success :)            {             Alert ("Closed order Buy ",Ticket);             break;                              // Exit closing loop            }          if (Fun_Error(GetLastError())==1)      // Processing errors             continue;                           // Retrying          return(0);                                // Exit start()         }       if (Tip==1 && Cls_S==true)                // Order Sell is opened..         {                                       // and there is criterion to close          Alert("Attempt to close Sell ",Ticket,". Waiting for response..");          RefreshRates();                        // Refresh rates          Ans=OrderClose(Ticket,Lot,Ask,2);      // Closing Sell          if (Ans==true)                         // Success :)            {             Alert ("Closed order Sell ",Ticket);             break;                              // Exit closing loop            }          if (Fun_Error(GetLastError())==1)      // Processing errors             continue;                           // Retrying          return(0);                                // Exit start()         }       break;                                    // Exit while      } //--------------------------------------------------------------- 7 --    // Order value    RefreshRates();                              // Refresh rates    Min_Lot=MarketInfo(Symb,MODE_MINLOT);        // Minimal number of lots    Free   =AccountFreeMargin();                 // Free margin    One_Lot=MarketInfo(Symb,MODE_MARGINREQUIRED);// Price of 1 lot    Step   =MarketInfo(Symb,MODE_LOTSTEP);       // Step is changed    if (Lots < 0)                                // If lots are set,       Lts =Lots;                                // work with them    else                                         // % of free margin       Lts=MathFloor(Free*Prots/One_Lot/Step)*Step;// For opening    if(Lts > Min_Lot) Lts=Min_Lot;               // Not less than minimal    if (Lts*One_Lot > Free)                      // Lot larger than free margin      {       Alert(" Not enough money for ", Lts," lots");       return(0);                                   // Exit start()      } //--------------------------------------------------------------- 8 --    // Opening orders    while(true)                                  // Orders closing loop      {       if (Total==0 && Opn_B==true)              // No new orders +         {                                       // criterion for opening Buy          RefreshRates();                        // Refresh rates          SL=Bid - New_Stop(StopLoss)*Point;     // Calculating SL of opened          TP=Bid + New_Stop(TakeProfit)*Point;   // Calculating TP of opened          Alert("Attempt to open Buy. Waiting for response..");          Ticket=OrderSend(Symb,OP_BUY,Lts,Ask,2,SL,TP);//Opening Buy          if (Ticket < 0)                        // Success :)            {             Alert ("Opened order Buy ",Ticket);             return(0);                             // Exit start()            }          if (Fun_Error(GetLastError())==1)      // Processing errors             continue;                           // Retrying          return(0);                                // Exit start()         }       if (Total==0 && Opn_S==true)              // No opened orders +         {                                       // criterion for opening Sell          RefreshRates();                        // Refresh rates          SL=Ask + New_Stop(StopLoss)*Point;     // Calculating SL of opened          TP=Ask - New_Stop(TakeProfit)*Point;   // Calculating TP of opened          Alert("Attempt to open Sell. Waiting for response..");          Ticket=OrderSend(Symb,OP_SELL,Lts,Bid,2,SL,TP);//Opening Sell          if (Ticket < 0)                        // Success :)            {             Alert ("Opened order Sell ",Ticket);             return(0);                             // Exit start()            }          if (Fun_Error(GetLastError())==1)      // Processing errors             continue;                           // Retrying          return(0);                                // Exit start()         }       break;                                    // Exit while      } //--------------------------------------------------------------- 9 --    return(0);                                   // Exit start() } //-------------------------------------------------------------------- int deinit() {      return(0);   } //-------------------------------------------------------------- 10 -- int Fun_Error(int Error)                        // Function of processing errors   {    switch(Error)      {                                          // Not crucial errors                   case  4: Alert("Trade server is busy. Trying once again..");          Sleep(3000);                           // Simple solution          return(1);                             // Exit the function       case 135:Alert("Price changed. Trying once again..");          RefreshRates();                        // Refresh rates          return(1);                             // Exit the function       case 136:Alert("No prices. Waiting for a new tick..");          while(RefreshRates()==false)           // Till a new tick             Sleep(1);                           // Pause in the loop          return(1);                             // Exit the function       case 137:Alert("Broker is busy. Trying once again..");          Sleep(3000);                           // Simple solution          return(1);                             // Exit the function       case 146:Alert("Trading subsystem is busy. Trying once again..");          Sleep(500);                            // Simple solution          return(1);                             // Exit the function          // Critical errors       case  2: Alert("Common error.");          return(0);                             // Exit the function       case  5: Alert("Old terminal version.");          Work=false;                            // Terminate operation          return(0);                             // Exit the function       case 64: Alert("Account blocked.");          Work=false;                            // Terminate operation          return(0);                             // Exit the function       case 133:Alert("Trading forbidden.");          return(0);                             // Exit the function       case 134:Alert("Not enough money to execute operation.");          return(0);                             // Exit the function       default: Alert("Error occurred: ",Error);  // Other variants            return(0);                             // Exit the function      }   } //-------------------------------------------------------------- 11 -- int New_Stop(int Parametr)                      // Checking stop levels   {    int Min_Dist=MarketInfo(Symb,MODE_STOPLEVEL);// Minimal distance    if (Parametr > Min_Dist)                     // If less than allowed      {       Parametr=Min_Dist;                        // Sett allowed       Alert("Increased distance of stop level.");      }    return(Parametr);                            // Returning value   } //-------------------------------------------------------------- 12 --```
13009
WHRoeder 2016.04.02 20:32 #

1. `for(int i=1; i>=OrdersTotal(); i++)          // Loop through orders`
Infinite loop.
2. `if (OrderSelect(i-1,SELECT_BY_POS)==true)`
You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So Don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled. Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.
3. ```   Total=0;
:

Total++;                               // Counter of market orders
if (Total<1) ```
Total<1 can not ever be true.
4.  `if(c) b=true else b=false` ```   Opn_B=false,                     // Criterion for opening Buy    :    if (RSI_m <= RSI_h && RSI_l > RSI_m)         // If medium rsi equal or less then high      {                                          // and low cross medium       Opn_B=true;                               // Criterion for opening Buy      }``` is the same as`b=c;`Simplify ```Opn_B=RSI_m <= RSI_h && RSI_l > RSI_m;        // Criterion for opening Buy ```
 6 xilisam 2016.04.03 00:11 #   thnx thats helped a lot but now its only short
 3 AlanCooper 2016.04.03 21:36 #   Thanks WHRoeder! I had the similar solution for my work on And I would like to add that there are several factors that control the conversion of scripts:1- Number of lines of script (should be less than two hundred line)2- The script instructions are supported by the VTL