Download MetaTrader 5

Why cannot EA buy order ?

To add comments, please log in or register
hosoway
37
hosoway  

Hi,

I asked in this forum before, and I had my EA problem solved.

But later, EA ordered sell only when I operated EA.


Indicator ↓

-------------------------------------------------+

extern int                FractalPeriod   = 25;

extern double             Lots            = 0.1;

extern ENUM_APPLIED_PRICE PriceHigh       = PRICE_HIGH;

extern ENUM_APPLIED_PRICE PriceLow        = PRICE_LOW;



double UpperBuffer[];

double LowerBuffer[];

double trend[];

double GrandOrder[];



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

int init()

{ 

   IndicatorBuffers(12);

    //~~~omittion~~~

   SetIndexBuffer(8,UpperBuffer); 

   SetIndexBuffer(9,LowerBuffer);

   SetIndexBuffer(10,trend);

   SetIndexBuffer(11,GrandOrder);

   

     SetIndexStyle(8,DRAW_LINE);

     SetIndexStyle(9,DRAW_LINE);



return(0);}

int deinit() {  return(0); }

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

//

int start()

{

//

   int cnt, CurrentPosition;

   int i,limit,counted_bars=IndicatorCounted();

   if(counted_bars<0) return(-1);

   if(counted_bars>0) counted_bars--;

           limit=MathMin(MathMax(Bars-counted_bars,FractalPeriod),Bars-1);

   //

     int half = FractalPeriod/2;

     for(i=limit; i>=0; i--)

     {

         bool   found     = true;

         double compareTo = iMA(NULL,0,1,0,MODE_SMA,PriceHigh,i);

         for (int k=1;k<=half;k++)

            {

               if ((i+k)<Bars && iMA(NULL,0,1,0,MODE_SMA,PriceHigh,i+k)> compareTo) { found=false; break; }

               if ((i-k)>=0   && iMA(NULL,0,1,0,MODE_SMA,PriceHigh,i-k)>=compareTo) { found=false; break; }

            }

         if (found) UpperBuffer[i]=iMA(NULL,0,1,0,MODE_SMA,PriceHigh,i);

         else       UpperBuffer[i]=UpperBuffer[i+1];

         //

         found     = true;

         compareTo = iMA(NULL,0,1,0,MODE_SMA,PriceLow,i);

         for (k=1;k<=half;k++)

            {

               if ((i+k)<Bars && iMA(NULL,0,1,0,MODE_SMA,PriceLow,i+k)< compareTo) { found=false; break; }

               if ((i-k)>=0   && iMA(NULL,0,1,0,MODE_SMA,PriceLow,i-k)<=compareTo) { found=false; break; }

            }

         if (found) LowerBuffer[i]=iMA(NULL,0,1,0,MODE_SMA,PriceLow,i);  

         else       LowerBuffer[i]=LowerBuffer[i+1];



         trend[i] = trend[i+1];;

         if (Close[i]>UpperBuffer[i])                          trend[i] = 1;

         if (Close[i]<LowerBuffer[i])                          trend[i] =-1;

         if (Close[i]<UpperBuffer[i]&&Close[i]>LowerBuffer[i]) trend[i] = 0;

      

         if (trend[2] == 0)

         {         

         if (trend[1] == 1 && trend[2] == 0) 

           { GrandOrder[0] = 1; }

         if (trend[1] == -1 && trend[2] == 0) 

           { GrandOrder[0] = -1;}

         else 

           GrandOrder[0] = 0;

           }

           }

return(0);}

EA ↓

#property copyright "hosoway"

extern double             Lots            = 0.1;

extern int                TakeProfit      = 20;

extern int                StopLoss        = 20;



//---- input parameters

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



int init(){ return(0);}

int deinit() {  return(0); }



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

int start()

{

//

   int cnt, CurrentPosition;

   int Ticket;

   //

   //order check

   CurrentPosition = -1;

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

   OrderSelect(cnt,SELECT_BY_POS);

   if(OrderSymbol() == Symbol()) CurrentPosition=cnt;

   }

   //      

   double GrandOrder = iCustom(NULL,0,"Indicator name",11,11,0);

   

    if(CurrentPosition == -1)   //none position

    {  if (GrandOrder == 1)

    { Ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-(StopLoss*Point),Ask+(TakeProfit*Point),"Buy",0,0,Blue);             

            }

            }

    if(CurrentPosition == -1)   //none position

     {if (GrandOrder == -1) 

     {Ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+(StopLoss*Point),Bid-(TakeProfit*Point),"Sell",0,0,Red); 

            }

            } 

return(0);

}


EA's iCustom call the Parameter "GrandOrder" and ordersend runs. (GrandOrder=1 …buy    ,GrandOrder=-1 …sell )

Selling orders are executed collectely.

Can someone please tell me the reason why not Buy order?   Thank you.

whroeder1
17081
whroeder1  
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it

  2. Ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+(StopLoss*Point),Bid-(TakeProfit*Point),"Sell",0,0,Red); 
    Check your return codes and find out why. What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  3. Your stops are 20 points. That is 2 pips on a 5 digit broker but 20 pips on 4. You are not adjusting SL, TP, and slippage; for 4/5 digit brokers and for JPY pairs.
    double   pip          = StringFind(_Symbol,"JPY") < 00.010.0001;
    int      pipsToPoints = int(pip / _Point);
    int      pipDigits    = (int)MathLog10(pipsToPoints);
    int      slippage     = 3 * pipsToPoints;
  4. You can't move stops closer to the market than MODE_STOPLEVEL. Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial
hosoway
37
hosoway  

To whroeder1

Thanks again and again.And thank you for telling me the format to wright.

 

In some cases , I intend to enter The value of SL and TP manually. Thank you for your advise.

While referring the forum taught,I gonna find out the cause.

To add comments, please log in or register