I am trying to code a MA crossover EA-Looking for Assistance with the Code

 

Hi, I have tried to create the code for this EA to buy when the MA_High is greater then the MA_Low and sell when the MA_Low is less then the MA_High, I can't get it to send the orders and close the orders. Any assistance would be appreciated. Thanks

Files:
2attemp21.mq4  4 kb
 
barnacle7:

Hi, I have tried to create the code for this EA to buy when the MA_High is greater then the MA_Low and sell when the MA_Low is less then the MA_High, I can't get it to send the orders and close the orders. Any assistance would be appreciated. Thanks

Your Order Accounting does nothing . . .

   //Order accounting
   Total=0;
   for(int i=1; i>=OrdersTotal(); i++)        //  start at 1 loop while i >= OrdersTotal() ? ?
   {
   if(OrderSelect(i-1,SELECT_BY_POS)==true)   //  if the OrderSelect works . . .
      {
      return;                                 //  . . .  return ? ?
   
      Total++;
          
      ticket=OrderTicket();
      Lot =OrderLots();
      }
   }

 the loop only runs for 1 loop  if  you have just one Order,  if you have 2 the loop will not run as OrdersTotal() will be equal to 2 and 2 will be greater than 1 so the loop exits.  If you have 1 Order and the OrderSelect() works the loop is immediately terminated due to the return from start()

 

   int 
     Total,
     Tip = -1,    // Tip declared as  -1



   if(  Tip == 0 && Cls_B == True)   //  why will Tip ever be anything other tan  -1 ? ?
      {
      //to close

 

   //Order accounting
   Total = 0;             //  Total is always 0

//opening Orders
while(true)
   {
   if(Total == 0 && Opn_B==True)  // Total is always 0
      {
      RefreshRates();
      Alert("Attempt to open Buy.Waiting for Response..");  
  
      ticket = OrderSend(Symbol(), OP_BUY, 0.1, Ask, 5,"", "", "");  //  strings for SL and TP ? ?  why ?

 Please read the documentation for OrderSend()  what variable types does it use for TP and SL ?

 
barnacle7:

Hi, I have tried to create the code for this EA to buy when the MA_High is greater then the MA_Low and sell when the MA_Low is less then the MA_High, I can't get it to send the orders and close the orders. Any assistance would be appreciated. Thanks

 

 


Hello barnacle7,

I have not read the code you attached to your thread (thanks for attaching the code anyhow, forum members likey-likey (: ). The thing you may consider when using moving averages for order sends and order closes is this one very important concept...

Lets begin ;) Hahaha!

The moving average indicator is drawn to a chart's history (shifts 1,2,3,...) and is being drawn (shift 0 only) as current price moves up or down. I don't believe I must even view your attached code but will view it if you wish this of me.

iMA(NULL,0,5,0,MODE_EMA,PRICE_CLOSE,0);

// exponential moving average, period five, BEING drawn at the time frame close at shift zero.

iMA(NULL,0,5,0,MODE_EMA,PRICE_CLOSE,1);

// exponential moving average, period five, drawn at the time frame close at shift one.

iMA(NULL,0,5,0,MODE_EMA,PRICE_CLOSE,2);

// exponential moving average, period five, drawn at the time frame close at shift two.

You can choose to use a moving average shift zero to open/close orders but ;) I don't smile on it generally... What puts a smile on my face is when I use shift values of one and two as decisions to open/close orders.

You may wish for this...

if ( (iMA(NULL,0,5,0,MODE_EMA,PRICE_CLOSE,2) < (iMA(NULL,0,40,0,MODE_SMMA,PRICE_CLOSE,2))) && (iMA(NULL,0,5,0,MODE_EMA,PRICE_CLOSE,1) > (iMA(NULL,0,40,0,MODE_SMMA,PRICE_CLOSE,1))) )

OrderSend(Symbol(),OP_BUY,1,Ask,3,Ask-250*Point,Ask+250*Point,"",16384,0,Blue);

This should take care of order send signals. Order closes are a total "playground" all to their own. Once the above is understood you may consider taking the "next-step" towards order closes.

Recommended sources:

Offline sources: MetaEditor dictionary (a coder can quickly locate documentation regarding ANY MQL4 question springing to his/her mind) HIGHLY RECOMMENDED and my personal favorite. Hahaha ;)

Online sources: Documention straight off this site: Documentation

The MQL4 book, just for kicks ;) MQL4 book

Finally, there is the MQL4 forum. I only "come out to play" on the forum if I see something I can help with or wish for an outside experienced coding advice from the other kids (no disrespect intended) ;)


Hopefully I provided something of value towards your future.

Thank you
 

Thanks RaptorUk, Yes I understand what you have mentioned regarding the String in the Ordersend(), (that was helpful) ,the above was my attempt to try and create something which seems that it may have too many issues for me to resolve at this stage of my level of understanding in programing.

So i have looked at the MQL4:"tradingexpert.mq4", I have attached it but with the ALerts changed to english. This is mean't to be simple for newbies but when I attach it to the chart it generartes only Alerts. I was expecting it to generate Long/Short ,open / Close orders ? I was suprised that it doesn't generate the orders, Is there coding missing ? Any assistance to get it to work would be greatly appreciated. Thanks.

Also Thanks, WhooDoo22 for your input i will take note of that.

Files:
 
barnacle7:

Thanks RaptorUk, Yes I understand what you have mentioned regarding the String in the Ordersend(), (that was helpful) ,the above was my attempt to try and create something which seems that it may have too many issues for me to resolve at this stage of my level of understanding in programing.

So i have looked at the MQL4:"tradingexpert.mq4", I have attached it but with the ALerts changed to english. This is mean't to be simple for newbies but when I attach it to the chart it generartes only Alerts. I was expecting it to generate Long/Short ,open / Close orders ? I was suprised that it doesn't generate the orders, Is there coding missing ? Any assistance to get it to work would be greatly appreciated. Thanks.

The EA does have the code for trading,  how long did you wait for it to place an order ?  how frequently did it place trades when you ran it in the Strategy Tester ?  you did run it in the Strategy Tester ?  if not,  why not ?
 
RaptorUK:
The EA does have the code for trading,  how long did you wait for it to place an order ?  how frequently did it place trades when you ran it in the Strategy Tester ?  you did run it in the Strategy Tester ?  if not,  why not ?

Yes It does have code for trading,thats why I was suprised it wouldn't place a trade.  I had removed the Rastvor and set the trade criteia for MA'S 4 & 7 .I have had it on the chart with the MA's on the chart as well but no trade is triggered after they cross either up or down. I only get Alerts. I was looking at putting it on to a STP MT4 platform demo account not a live account. I am more interested in learning and understanding MQL4 coding and have decided to start with this EA being that its  a "Simple EA" but as it doesn't work , so I am questioning the quality of the coding in it. To know what is stopping it from placing trades would be great to Know.  
 
RaptorUK:
The EA does have the code for trading,  how long did you wait for it to place an order ?  how frequently did it place trades when you ran it in the Strategy Tester ?  you did run it in the Strategy Tester ?  if not,  why not ?

You tried it in the Strategy Tester ?
 
What alerts do you get ?  any that say "Error occurred" ?
 

RaptorUK:
What alerts do you get ?  any that say "Error occurred" ?

 

I am Getting :

when 4ma is above 7MA,   Error Occured:130  , "Attempt to Open buy,waiting response".

when 7ma is less then 4ma, Error Occured:130, "Attempt to sell .wiating response."

 

 

 
barnacle7:

I am Getting :

when 4ma is above 7MA,   Error Occured:130  , "Attempt to Open buy,waiting response".

when 7ma is less then 4ma, Error Occured:130, "Attempt to sell .wiating response."

The code that follows the line that produces that alert is this .. . .

         Alert("Attempt to open Sell. Waiting for response..");
         Ticket=OrderSend(Symb,OP_SELL,Lts,Bid,2,SL,TP);//Îòêðûòèå Sel
         if (Ticket > 0)                        // Ïîëó÷èëîñü :)
           {
            Alert ("Opened order  Sell ",Ticket);
            return;                             // Âûõîä èç start()
           }
         if (Fun_Error(GetLastError())==1)      // Îáðàáîòêà îøèáîê

. . . . and should result in an Order being placed and an Alert that says:  "Opened order Sell 123456"  or an Alert with one of these errors:

      case  4: Alert("Trade server is busy. Trying once again..");
         Sleep(3000);                           // Ïðîñòîå ðåøåíèå
         return(1);                             // Âûõîä èç ôóíêöèè
      case 135:Alert("Price Changed. Trying once again.");
         RefreshRates();                        // Îáíîâèì äàííûå
         return(1);                             // Âûõîä èç ôóíêöèè
      case 136:Alert("No Prices. Waiting for a new tick..");
         while(RefreshRates()==false)           // Äî íîâîãî òèêà
            Sleep(1);                           // Çàäåðæêà â öèêëå
         return(1);                             // Âûõîä èç ôóíêöèè
      case 137:Alert("Broker is Busy.Trying once again..");
         Sleep(3000);                           // Ïðîñòîå ðåøåíèå
         return(1);                             // Âûõîä èç ôóíêöèè
      case 146:Alert("Trading Subsystem is busy.Trying once again..");
         Sleep(500);                            // Ïðîñòîå ðåøåíèå
         return(1);                             // Âûõîä èç ôóíêöèè
         // // critical Errors
      case  2: Alert("Common Error.");
         return(0);                             // Âûõîä èç ôóíêöèè
      case  5: Alert("Old Terminal Version.");
         Work=false;                            // Áîëüøå íå ðàáîòàòü
         return(0);                             // Âûõîä èç ôóíêöèè
      case 64: Alert("Account Blocked.");
         Work=false;                            // Áîëüøå íå ðàáîòàòü
         return(0);                             // Âûõîä èç ôóíêöèè
      case 133:Alert("Trading Forbidden.");
         return(0);                             // Âûõîä èç ôóíêöèè
      case 134:Alert("Not Enough money to execute operation");
         return(0);                             // Âûõîä èç ôóíêöèè
      default: Alert("Error occured:  ",Error); // Äðóãèå âàðèàíòû   
         return(0);                             // Âûõîä èç ôóíêöèè

 

What do you get next ? 

 

barnacle7

amend from "When 7ma is less then 4ma" .  It  Should read  7MA is greater then 4MA , (Price is going down) ,Error 130 ....


Reason: