Download MetaTrader 5

pls help !!! my ea open only sell order

To add comments, please log in or register
Do you want to order an application? Find out how to obtain the desired result
oilyjaah
28
oilyjaah 2015.04.08 18:39 

i tried to test my ea by the strategy tester , but it open only sell order.

what 's wrong?

 //+------------------------------------------------------------------+

//|                                                 200SMA_CCI25.mq4 |

//|                        Copyright 2015, MetaQuotes Software Corp. |

//|                                             https://www.mql5.com |

//+------------------------------------------------------------------+

#property copyright "Copyright 2015, MetaQuotes Software Corp."

#property link      "https://www.mql5.com"

#property version   "1.00"

#property strict

extern int    MGNo    = 555;

extern double risk=0.2;

extern   int    STEP=1;

extern   int    ConRisk=100;

extern int   TP   = 500;

bool   Trailing       = true;

extern double TrailingStop    = 100;

bool DisplayStatus=false;             

bool result;

double p,o,s,tp,markh,markl,marktp,target1,target2,mark,marksl,nsl,ntp;

int once=0,tarpip,onlyonce=0,cnt,order,ch;

string ord;



int start()

{

 static datetime tmp;

 

 double stl=MarketInfo(Symbol(),MODE_STOPLEVEL);

   

   tarpip=ConRisk;

   //if(ConRisk>0)tarpip=ConRisk;

   //if(MinRisk>0)tarpip=MinRisk;

   double maxl=MarketInfo(Symbol(),MODE_MAXLOT);

   double minl=MarketInfo(Symbol(),MODE_MINLOT);

   double tv=MarketInfo(Symbol(), MODE_TICKVALUE);

   double MoneyUse=risk*MathFloor(AccountBalance()/STEP)*STEP;

   //risk=0.2*AccountBalance();

   double x=MoneyUse/tarpip;

   double lot=x/tv;

   if(lot>maxl)lot=maxl;

   if(lot<minl)lot=minl; 

   

   double SMA200=iMA(Symbol(),0,200,0,MODE_SMA,PRICE_CLOSE,1);

   double c1=Close[1];

   double cci1=iCCI(Symbol(),0,25,PRICE_TYPICAL,1);

   double cci2=iCCI(Symbol(),0,25,PRICE_TYPICAL,2);

   

   if(c1>SMA200 && cci1>-100 && cci2<=-100)

   {

    order=1;

    ord="buy";

   }

   else {order=0;ord="";}

   

   if(c1<SMA200 && cci1<100 && cci2>=100)

   {

    order=-1;

    ord="sell";

   }

   else {order=0;ord="";}

   

   

  if (tmp < Time [0])

      {

       

       if(OrdersTotal()==0)

      { 

         if(order==1)

         //if(ord=="buy")

         { 

          ch=OrderSend(Symbol(),OP_BUY,lot,Ask,0,Ask-ConRisk*Point,Ask+TP*Point,"",555,0,clrNONE);

          if(ch>0){order=0;}

         }   

         

         if(order==-1)

         {

          ch=OrderSend(Symbol(),OP_SELL,lot,Bid,0,Bid+ConRisk*Point,Bid-TP*Point,"",555,0,clrNONE);

          if(ch>0){order=0;}

         }

      } 

       

       if(Trailing)

       {

        for(cnt=0;cnt<OrdersTotal();cnt++)

     {

      if(!OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))

         continue;

      if(OrderType()<=OP_SELL &&   // check for opened position 

         OrderSymbol()==Symbol()&& OrderMagicNumber()==MGNo)  // check for symbol

        {

         //--- long position is opened

         if(OrderType()==OP_BUY)

           {

            

            //--- check for trailing stop

            if(TrailingStop>0)

              {

               if(Bid-OrderOpenPrice()>Point*TrailingStop)

                 {

                  if(OrderStopLoss()<Bid-Point*TrailingStop)

                    {

                     //--- modify order and exit

                     if(!OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green))

                        Print("OrderModify error ",GetLastError());

                     return(0);

                    }

                 }

              }

           }

         else // go to short position

           {

            

            //--- check for trailing stop

            if(TrailingStop>0)

              {

               if((OrderOpenPrice()-Ask)>(Point*TrailingStop))

                 {

                  if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))

                    {

                     //--- modify order and exit

                     if(!OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red))

                        Print("OrderModify error ",GetLastError());

                     return(0);

                    }

                 }

              }

           }

        }

     }

       }

        tmp = Time [0];

      }

 

 Comment(c1,"/",SMA200,"/",cci1,"/",cci2);

 

 return(0);

}
Stuart Browne
5814
Stuart Browne 2015.04.09 00:24  

Please use the SRC button when posting code. I've fixed it for you this time 

 

 

oilyjaah
28
oilyjaah 2015.04.09 00:39  

sorry 

and

thank you 

Stuart Browne
5814
Stuart Browne 2015.04.09 00:54  
oilyjaah:

sorry 

and

thank you 

No problem :)

Are you seeing any errors in your journal?
oilyjaah
28
oilyjaah 2015.04.09 04:08  

no 

and realy would like to

what's wrong?

pls help 

Stuart Browne
5814
Stuart Browne 2015.04.09 04:26  
oilyjaah:

no 

and realy would like to

what's wrong?

pls help 

Your problem is the way you've coded the conditional logic for the buy sell. Currently, if the buy condition is met, it then drops down to check the sell condition. As this is false, it moves to the "else" of the sell condition which resets the order var to zero, hence no trade is opened. Try something like this instead:

   

   if(c1>SMA200 && cci1>-100 && cci2<=-100)
   {
    order=1;
    ord="buy";
   }
   else if(c1<SMA200 && cci1<100 && cci2>=100)
   {
    order=-1;
    ord="sell";
   }
   else 
   {
    order=0;
    ord="";
   }

You could also streamline your OrderSend() code so you don't need to check for buy/sell again but it should work OK as it is, just not as efficient.

Cheers
Stu 

oilyjaah
28
oilyjaah 2015.04.09 05:37  

thank you 

i will try 

Stuart Browne
5814
Stuart Browne 2015.04.09 05:54  
oilyjaah:

thank you 

i will try 

Very welcome :)
To add comments, please log in or register