OrderClose error on three EMA crosses EA - page 2

 
deVries:


orderopening before opening a trade make TicketNumber = 0; otherwise your checking if a new trade opend is senseless

and some other things i can suggest

Is that necessary? If OrderSend succeeds, TicketNumber will == the ticket number, if it fails, Ticket number will == -1.
 

Following your suggestion no the EA is opening buy and sell orders, and it's closing them normally,

the only problem is that is loosing money :-D

this is the EA, it's missing a good managing, I'me using only the PRICE_OPEN cross with EMA1 at 9 days

//+------------------------------------------------------------------+
//|                                                          EMA.mq4 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

extern double Lots = 0.1;
  double EMA1;
  double EMA2;
  double EMA3;
  int i;
  int Ticket;
  int TicketNumber;
  int MagicNo = 12345;
  int PositionIndex;    //  <-- this variable is the index used for the loop
  int TotalNumberOfOrders;   //  <-- this variable will hold the number of orders currently in the Trade pool
  int Slippage= 3;
//  int Counted_bars;                // Êîëè÷åñòâî ïðîñ÷èòàííûõ áàðîâ 
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
  Print(OrdersTotal());

//--------------------------------------------------------------------
//   Counted_bars=IndicatorCounted(); // Êîëè÷åñòâî ïðîñ÷èòàííûõ áàðîâ 
//   i=Bars-Counted_bars-1;           // Èíäåêñ ïåðâîãî íåïîñ÷èòàííîã
//   while(i>=0)                      // Öèêë ïî íåïîñ÷èòàííûì áàðà
//     {
       EMA1 = iMA(NULL, 0, 9, 0, MODE_EMA, PRICE_MEDIAN, 0);
       EMA2 = iMA(NULL, 0, 27, 0, MODE_EMA, PRICE_MEDIAN, 0);
//       EMA3 = iMA(NULL, 0, 50, 0, MODE_EMA, PRICE_MEDIAN, 0);     
//      i--;                          // Ðàñ÷¸ò èíäåêñà ñëåäóþùåãî áàðà
//     }
   //-----section for close the orders -----------------------
      
      TotalNumberOfOrders = OrdersTotal();
      if((PRICE_OPEN<EMA1)){
   //   Print("TotalNumberOfOrders is "+TotalNumberOfOrders);
      for(PositionIndex = TotalNumberOfOrders - 1; PositionIndex >= 0 ; PositionIndex --)  //  <-- for loop to loop through all Orders . .   COUNT DOWN TO ZERO !
      {
      if( ! OrderSelect(PositionIndex, SELECT_BY_POS, MODE_TRADES) ) continue;   // <-- if the OrderSelect fails advance the loop to the next PositionIndex
   
      if( OrderMagicNumber() == MagicNo && OrderSymbol() == Symbol() && ( OrderType() == OP_BUY ) )      // <-- or is it a Sell Order ?
   
      if ( ! OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(), Slippage ) )               // <-- try to close the order
         Print("Order Close failed, order number: ", OrderTicket(), " Error: ", GetLastError() );  // <-- if the Order Close failed print some helpful information 
      
      } //  end of For loop
      }
      
      
      if((PRICE_OPEN>EMA1)){
      TotalNumberOfOrders = OrdersTotal();    // <-- we store the number of Orders in the variable
   //   Print("TotalNumberOfOrders is "+TotalNumberOfOrders);
      for(PositionIndex = TotalNumberOfOrders - 1; PositionIndex >= 0 ; PositionIndex --)  //  <-- for loop to loop through all Orders . .   COUNT DOWN TO ZERO !
      {
      if( ! OrderSelect(PositionIndex, SELECT_BY_POS, MODE_TRADES) ) continue;   // <-- if the OrderSelect fails advance the loop to the next PositionIndex
   
      if( OrderMagicNumber() == MagicNo && OrderSymbol() == Symbol() && ( OrderType() == OP_SELL ) )      // <-- or is it a Sell Order ?
    
        if ( ! OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(), Slippage ) )               // <-- try to close the order
           Print("Order Close failed, order number: ", OrderTicket(), " Error: ", GetLastError() );  // <-- if the Order Close failed print some helpful information 
      
       } //  end of For loop
      }
    
   //--------------------------------------------------

   //-----section for open the orders------------------------------------
   
      TotalNumberOfOrders = OrdersTotal();
      if(TotalNumberOfOrders==0){
//      Print("TotalNumberOfOrders is "+TotalNumberOfOrders);
      if((PRICE_OPEN>EMA1))
         {
         TicketNumber=OrderSend(Symbol(), OP_BUY, Lots, Ask, 3,0,0, "EMA_CROSS", MagicNo, 0, Green);
         if( TicketNumber > 0 )
            {
            Print("Order placed # ", TicketNumber + " Bought");
            }
            else
            {
            Print("Order Send failed, error # ", GetLastError() );
            }
         }
      }
      
      TotalNumberOfOrders = OrdersTotal();
      if(TotalNumberOfOrders==0){
   //   Print("TotalNumberOfOrders is "+TotalNumberOfOrders);
      if((PRICE_OPEN<EMA1))
         {
         TicketNumber=OrderSend(Symbol(), OP_SELL, Lots, Bid, 3,0,0, "EMA_CROSS", MagicNo, 0, Red);
         if( TicketNumber > 0 )
            {
            Print("Order placed # ", TicketNumber + " Sold");
            }
            else
            {
            Print("Order Send failed, error # ", GetLastError() );
            }
         }
      }
      
   return(0);
  }
// the end.
 

You made it now a 2EMA cross

With 3 EMA lines you could follow the instructions of WHRoeder

about changing signal next tick you were using Price_Median

that is (High+Low) / 2 so changing signal on next tick is almost not happening

if your close rule was ok

this is how a chart looks like with 3 ema price_median periods you started

easily to see moments start EMA1 < EMA2 with EMA2 < EMA3 or moment EMA1 > EMA2 with EMA2 > EMA3

if you follow that in coding

then it could be your code is gonna look like .....

//+------------------------------------------------------------------+
//|                                            Three_MA_Cross_EA.mq4 |
//|                                       Copyright ©  2013, deVries |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright ©  2013, deVries"
#property link      ""


extern double Lots = 0.1;
extern int MagicNo = 12345;
extern int Slippage = 3;


extern int      shift=0;
int      checkorder = 0;
int      BUYS,SELLS;
double   EMA1, EMA2, EMA3;

//++++ These are adjusted for 5 digit brokers.
int     pips2points;      // slippage  3 pips    3=points    30=points
double  pips2dbl;         // Stoploss 15 pips    0.015      0.0150
int     Digits.pips;      // DoubleToStr(dbl/pips2dbl, Digits.pips)
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   if(Digits % 2 == 1)  // DE30=1/JPY=3/EURUSD=5 forum.mql4.com/43064#515262
     {pips2dbl = Point*10; pips2points = 10;   Digits.pips = 1;}
     else {pips2dbl = Point;    pips2points =  1;   Digits.pips = 0;}
     // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl         
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
  int  TicketNumber;
//----
double fastCurr = iMA(NULL, 0,  9, 0, MODE_EMA, PRICE_MEDIAN, shift),
       medCurr  = iMA(NULL, 0, 27, 0, MODE_EMA, PRICE_MEDIAN, shift),
       slowCurr = iMA(NULL, 0, 50, 0, MODE_EMA, PRICE_MEDIAN, shift),
     
       fastPrev = iMA(NULL, 0,  9, 0, MODE_EMA, PRICE_MEDIAN, shift+1),
       medPrev  = iMA(NULL, 0, 27, 0, MODE_EMA, PRICE_MEDIAN, shift+1),
       slowPrev = iMA(NULL, 0, 50, 0, MODE_EMA, PRICE_MEDIAN, shift+1);     

bool   isBuy  = ((fastCurr > medCurr) && (medCurr > slowCurr)),
      wasBuy  = ((fastPrev > medPrev) && (medPrev > slowPrev)),
      isSell  = ((fastCurr < medCurr) && (medCurr < slowCurr)),
     wasSell  = ((fastPrev < medPrev) && (medCurr < slowPrev));


//----
   if(checkorder != OrdersTotal())
     {
     checkorder = OrdersTotal();
     BUYS = 0;
     SELLS = 0;
     for(int i= OrdersTotal()-1; i>=0 ; i--)
       {
       if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)break;
       if(OrderMagicNumber()!=MagicNo || OrderSymbol()!=Symbol()) continue;  
       if(OrderType()==OP_BUY)
          {
          BUYS++;
          if(isSell) 
            {
            checkorder = 0;
            if ( ! OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(), Slippage*pips2points )) // <-- try to close the order
            Print("Close Buy failed, order number: ", OrderTicket(), " Error: ", GetLastError() );  // <-- if the Order Close failed print some helpful information 
            }
          }  
       if(OrderType()==OP_SELL)
          {
          SELLS++;
          if(isBuy) 
            {
            checkorder = 0;
            if ( ! OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(), Slippage*pips2points )) // <-- try to close the order
            Print("Close Sell failed, order number: ", OrderTicket(), " Error: ", GetLastError() );  // <-- if the Order Close failed print some helpful information 
            }
          }
       } 
    }   
       
if(isBuy && !wasBuy)
   {
   if(SELLS > 0){checkorder = 0;}
   if(BUYS==0)   
         {
         RefreshRates();       
         TicketNumber=OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage*pips2points,0,0, "EMA_CROSS", MagicNo, 0, Green);
         if( TicketNumber > 0 )
            {
            Print("Buy Order placed # ", TicketNumber + " Bought");
            }
            else
            {
            Print("Order BuySend failed, error # ", GetLastError() );
            }
         checkorder = 0;          
         }               
   }    
       
if(isSell && !wasSell)
   {
   if(BUYS>0){checkorder = 0;} 
   if(SELLS<1)
         {   
         RefreshRates();       
         TicketNumber=OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage*pips2points,0,0, "EMA_CROSS", MagicNo, 0, Red);
         if( TicketNumber > 0 )
            {
            Print("Sell Order placed # ", TicketNumber + " Bought");
            }
            else
            {
            Print("Order SellSend failed, error # ", GetLastError() );
            } 
         checkorder = 0;
         }                  
   }       
           
           
   return(0);
  }
//+------------------------------------------------------------------+

GumRai:
Is that necessary? If OrderSend succeeds, TicketNumber will == the ticket number, if it fails, Ticket number will == -1.

Your right about this

Only you get then people gonna do easily

TicketNumberBuy = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage*pips2points,0,0, "EMA_CROSS", MagicNo, 0, Green);

OrderClose( TicketNumberBuy, OrderLots(), OrderClosePrice(), Slippage*pips2points )) // you easily loose that value ( restarting pc )

and you can open only one buy that way...

with loop and orderselect it is not needed to know ticketnumber of your trades

 

after this simple EMA cross indicator and EA, I'm trying to translate in MQL4a type of Dorsey indicator,

I've trouble to use the iLow for select the low and lowest value of an array of a variable PB values.


//+------------------------------------------------------------------+
//|                                              dorsey-corinnah.mq4 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Green
#property indicator_color2 Red
//--- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
extern int d;
extern int e;
  int cl;
  int cs;
  int BU[9];
  double PB;
  double M;
  double R;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,ExtMapBuffer2);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {

   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
  int counted_bars=IndicatorCounted();

  if (Bars < 2) {
  BU[0]=PRICE_CLOSE;
  }
  else {
  BU[0]=BU[1]-BU[2]/3.414+(1/3.414)*PRICE_CLOSE;
  }
  
  M=iMA(NULL, 0, d, 0, MODE_SMA, PRICE_CLOSE, 0);
  
  PB=MathAbs(BU[0]-M);
 
//  R=100*(PB-iLow[9](PB)/(iHigh[9](PB)-iLow[9](PB));
    R=100*(PB-iLow(9,PB,0)/(iHigh(9,PB,0)-iLow(9,PB,0));
  
  ExtMapBuffer1=M;
  ExtMapBuffer2=R;
   
  return(0);
  }
 
giuseppeloddo:

after this simple EMA cross indicator and EA, I'm trying to translate in MQL4a type of Dorsey indicator,

I've trouble to use the iLow for select the low and lowest value of an array of a variable PB values.

iLow() ? have you read the documentation at all ? so your Symbol is 9 your timeframe is PB and your shift is 0 ??

symbol has to be a string, so 9 won't work, the timeframe has to be an int so PB won't work . . . 0 as the shift is OK

By the way, PB is not an array . . . you do have one array, BU and it's an int which you give double values . . .

  BU[0]=BU[1]-BU[2]/3.414+(1/3.414)*PRICE_CLOSE;

. . . do you know what PRICE_CLOSE is ? it's zero . . . click here ---> PRICE_CLOSE


You need to read the Book . . .

 
RaptorUK:

iLow() ? have you read the documentation at all ? so your Symbol is 9 your timeframe is PB and your shift is 0 ??

symbol has to be a string, so 9 won't work, the timeframe has to be an int so PB won't work . . . 0 as the shift is OK

By the way, PB is not an array . . . you do have one array, BU and it's an int which you give double values . . .

. . . do you know what PRICE_CLOSE is ? it's zero . . . click here ---> PRICE_CLOSE


You need to read the Book . . .


instead of PRICE_CLOSE I need the Close price of the actual bar, and about the iLow function I've wrong the order of the fields,

I read bettere the book for modify the iLow, the PB into an array and for sostitute the PRICE_CLOSE with EURUSD Price on the Closed last bar.

 
//+------------------------------------------------------------------+
//|                                                            A.mq4 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Green
#property indicator_color2 Red
  extern int d;
  extern int e;
  int lowest[0];
  int highest[0];
  double ExtMapBuffer1[];
  double ExtMapBuffer2[];
  double BU[];
  double PB[];
  double M;
  double R;

int init()
  {

   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,ExtMapBuffer2);
   return(0);
  }

int deinit()
  {

   return(0);
  }

int start()
  {

  int counted_bars=IndicatorCounted();
  if (Bars<2)
  {
  BU[0]=Close[0];
  }
  else {
  BU[0]=(BU[1]-((BU[2]/3.414)+((1/3.414)*(Close[0]))));
  }
  ExtMapBuffer1=iMA(NULL, 0, d, 0, MODE_SMA, Close[0], 0);
  PB[0]=MathAbs(BU[0]-ExtMapBuffer1);
  lowest=ArraySort(PB[]);
  highest=ArraySort(PB[],WHOLE_ARRAY,0,MODE_DESCEND);
  ExtMapBuffer2=100*(PB[0]-lowest[0]/(highest[0]-lowest[0]);

   return(0);
  }
//+------------------------------------------------------------------+

I'm reading all the documentation about the arrays and about the array sorting,

the problem is focused into the "finding of the lowest and higher value of the last 9 values of the variable PB"

I understand that MQL give me parenthesis error because can't calulate the line:

  ExtMapBuffer2=100*(PB[0]-lowest[0]/(highest[0]-lowest[0]);

this the error of compiler

'\end_of_program' - unbalanced left parenthesis C:\Program Files (x86)\XM MT4\experts\indicators\A.mq4 (59, 1)

 
  ExtMapBuffer1=iMA(NULL, 0, d, 0, MODE_SMA, Close[0], 0);
  PB[0]=MathAbs(BU[0]-ExtMapBuffer1);
  lowest=ArraySort(PB[]);
  highest=ArraySort(PB[],WHOLE_ARRAY,0,MODE_DESCEND);
  ExtMapBuffer2=100*(PB[0]-lowest[0]/(highest[0]-lowest[0]);

You are using the buffers without [ ]

Does this default to [0] or not work?

 
GumRai:

You are using the buffers without [ ]

Does this default to [0] or not work?


it's giving me the same error also with [0]


//+------------------------------------------------------------------+
//|                                              dorsey-corinnah.mq4 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Green
#property indicator_color2 Red
//--- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
extern int d;
extern int e;
  int cl;
  int counted_bars;
  int cs;
double BU[];
double PB[];
  double M;
  double R;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,ExtMapBuffer2);
   return(0);
  }

int deinit()
  {
   return(0);
  }

int start()
  {
  int counted_bars=IndicatorCounted();

  if (Bars<2)
  {
  BU[0]=Close[0];
  }
  else {
  BU[0]=(BU[1]-((BU[2]/3.414)+((1/3.414)*(Close[0]))));
  }
  
  M=iMA(NULL, 0, d, 0, MODE_SMA, Close[0], 0);
  
  PB[0]=MathAbs(BU[0]-M);
 
//  R=100*(PB-iLow[9](PB)/(iHigh[9](PB)-iLow[9](PB));
    R=100*(PB[0]-iLow(PB[0],9,0)/(iHigh(PB[0],9,0)-iLow(PB[0],9,0));
  
  ExtMapBuffer1[0]=M;
  ExtMapBuffer2[0]=R;
   
  return(0);
  }
 
giuseppeloddo:

it's giving me the same error also with [0]

You still haven't read the documentation for iLow() . . . the first parameter is a string and is the Symbol, what symbol is PB[0] ? PB[] is a double not a string . . . do you understand why this is never going to work ?

This is where your missing bracket is . . . all you need to do is count them . . .

R = 100 *  ( PB[0]-iLow ( PB[0],9,0 ) / ( iHigh ( PB[0],9,0 ) - iLow ( PB[0],9,0 )   ) ;
Reason: