General confusion

 
I seem to be missing something in my understanding of code

I read the lessons and documents and feel I have a sort of basic knowledge of coding an EA, yet I seem to be lacking some major understanding.
This is very hard for me to describe the question because I don't know exactly where I am missing information.

I guess I should just ask this for now and learn from here:
When you have an if statement and you open an OP_BUY for example:
I thought that I understood that there should be a for loop, that will close the trade if the opposite trade would occur with an OP_SELL order.


I continue to learn from the lessons,forums,documents,book etc; and am sure there are many ways of doing this.

It would seem that even the lessons EA (MY_FIRST_EA) does not directly trade in the other direction.

It would seem to close the trade and then pickup the next signal but not really reverse the trade on the reverse signal directly.
So that the reverse is not really reversing, but actually closing and then trading the next cross.

So although I understand the lesson I do not believe the EA should be working this way or at least from what I understand in the lesson it should reverse directly.

Should it not ?

//+------------------------------------------------------------------+
//|                                            My_First_EA_edits.mq4 |
//|                                        Agent86's Dirty Rat Trade |
//|                                     http://www.iclbiz.com/joomla |
//+------------------------------------------------------------------+
#property copyright "Agent86 My First EA"
#property link      "www.iclbiz.com/joomla"

//---- input parameters
extern double    TakeProfit=200.0;
extern double    Lots=0.1;
extern double    StopLoss=120.0;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//---- 
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 
   
//----
   return(0);
  }

int Crossed (double line1 , double line2)
   {
      static int last_direction = 0;
      static int current_direction = 0;
      
      if(line1>line2)current_direction = 1; //up
      if(line1<line2)current_direction = 2; //down



      if(current_direction != last_direction) //changed 
      {
            last_direction = current_direction;
            return (last_direction);
      }
      else
      {
            return (0);
      }
   } 
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//---- 

   
   int cnt, ticket, total;
   double faster, slower;
   
   
   if(Bars<100)
     {
      Print("bars less than 100");
      return(0);  
     }
   if(TakeProfit<10)
     {
      Print("TakeProfit less than 10");
      return(0);  // check TakeProfit
     }
     
     
   faster = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_UPPER,0); //MODE_MAIN
   slower = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_LOWER,0); //MODE_SIGNAL
   
   int isCrossed  = Crossed (faster,slower);
   
   total  = OrdersTotal(); 
   if(total < 1) 
     {
       if(isCrossed == 1)
         {
            ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,"Agent86",12345,0,Green);
            if(ticket>0)
              {
               if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
              }
            else Print("Error opening BUY order : ",GetLastError()); 
            return(0);
         }
         if(isCrossed == 2)
         {

            ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Ask+StopLoss*Point,Bid-TakeProfit*Point,"Agent86",12345,0,Red);
            if(ticket>0)
              {
               if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
              }
            else Print("Error opening SELL order : ",GetLastError()); 
            return(0);
         }
         return(0);
     }
for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
        {
         if(OrderType()==OP_BUY)   // long position is opened
           {
            // should it be closed?
           if(isCrossed == 2)
                {
                 OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
                 return(0); // exit
                }        
           
            // should it be closed?
            if(isCrossed == 1)
              {
               OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
               return(0); // exit
              }
           }
        }
     }
   return(0);
  }
//+------------------------------------------------------------------+
 
  1. Don't DOUBLE POST
  2. total  = OrdersTotal(); 
       if(total < 1) 
    already told that was incompatible with other EAs
  3. already told to count down when closing
  4. You HAD 4/5 digit broker code, why have you un-done.
  5. MACD may cross and uncross several times during bar zero. Use bar one: macd(... 1)
 
WHRoeder:
  1. Don't DOUBLE POST
  2. already told that was incompatible with other EAs
  3. already told to count down when closing
  4. You HAD 4/5 digit broker code, why have you un-done.
  5. MACD may cross and uncross several times during bar zero. Use bar one: macd(... 1)
Sorry about that.
  1. I didn't realized the answer was the same subject, since the question was different.
  2. same
  3. same
  4. I posted the sample more closely related to the original MY_FIRST_EA in the lessons and the 4/5 digit topic is not related to this topic so I figured I would remove for this subject.
  5. I thought that the PRICE_CLOSE would have addressed this. Also the macd(....1) I thought was related to the shift, not the Bar. But I think I get it now, I should use (macd.....1) since this will refer to the closed bar price and actually mark the crosses more properly instead of (macd......0) which may be in flux the whole time causing multiple trades. Once (macd...1) is closed it can't move and the crosses will stay fixed. OK I see.

OK thanks,

So, how to handle the trade if(Close[1]>Open[1] && High[0]>High[1]) ? This will be in flux all the time due to High[0]>High[1] on more then one occasion.
Basically attempting to place a trade at the break of High[1]

Is this also related to the same subject ? Or something different ?
Perhaps I'm mixing multitudes of topics and confusing myself which is why I'm failing to write good NOOB codes :)

Please advise.

Reason: