Coding help please

 
 
#include <stderror.mqh>
#include <stdlib.mqh>
 
extern double TakeProfit = 90;
extern double Lots = 1;
extern double TrailingStop = 55;

extern double MagicNumber = 23457;
extern int StopLoss = 190;
extern bool WaitForBarClose = false;

extern double MacdCurrent= 48;
extern double MacdPrevious = 24;


//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
 int sf;
      if(WaitForBarClose)sf = 1; else sf = 0;
      
        MacdCurrent=iMACD(NULL,60,4,2,9,PRICE_CLOSE,MODE_MAIN,0);
   MacdPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);
         
       
      
     if(MacdCurrent < MacdPrevious && TO() == 0)OB();
      if(MacdPrevious < MacdCurrent && TO() == 0)OS();
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
int TO()
{
   int count = 0;
 
      for(int f = 0; f < OrdersTotal();f++)
      {
         OrderSelect(f,SELECT_BY_POS,MODE_TRADES);
         if(OrderMagicNumber() == MagicNumber && OrderType() <= OP_SELL) //&&(OrderType() == OP_BUY))// makes buy and sell
         {
            count++;
            if(TrailingStop > 0)
            {
               if(OrderType() == OP_BUY)
               {
                   if(Bid - OrderOpenPrice() > 0 && OrderStopLoss()  < Bid - (TrailingStop*Point)) 
                   {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*Point,OrderTakeProfit(),0,Blue); 
                   }
               }
               if(OrderType() == OP_SELL)
               {
                   if(OrderOpenPrice()-Ask > 0 && (OrderStopLoss() > Ask + (TrailingStop*Point) || OrderStopLoss() == 0)) 
                   {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+TrailingStop*Point,OrderTakeProfit(),0,Blue);  
                   }
               }
            }
         }
      }
      
   for(int z = 0; z < OrdersHistoryTotal();z++)
   {
      OrderSelect(z,SELECT_BY_POS,MODE_HISTORY);
      if(OrderMagicNumber() == MagicNumber && iBarShift(Symbol(),0,OrderCloseTime(),false) == 0)
      {
         count++;
      }
   }
      
   return(count);
}
//+------------------------------------------------------------------+
int OB()
{
   double sl = 0;
   if(StopLoss > 0)sl = Ask-(StopLoss*Point);
   double tp = 0;
   if(TakeProfit > 0) tp = Ask+(TakeProfit*Point);
   int t = OrderSend(Symbol(),OP_BUY,Lots,Ask,0,sl,tp,"Buy",MagicNumber,0,Blue);
   if(t < 0){Print("Failed to open Buy with error :",ErrorDescription(GetLastError()));
      //double sl = 0;
   if(StopLoss > 0)sl = Bid+(StopLoss*Point);
  // double tp = 0;
   if(TakeProfit > 0) tp = Bid-(TakeProfit*Point);
  // int t = 
   OrderSend(Symbol(),OP_SELL,Lots,Bid,0,sl,tp,"Sell",MagicNumber,0,Red);
   if(t < 0){Print("Failed to open Sell with error :",ErrorDescription(GetLastError()));}
   }
 
   return(t);
}
//+------------------------------------------------------------------+

int OS()
{
   double sl = 0;
   if(StopLoss > 0)sl = Bid+(StopLoss*Point);
   double tp = 0;
   if(TakeProfit > 0) tp = Bid-(TakeProfit*Point);
   int t = OrderSend(Symbol(),OP_SELL,Lots,Bid,0,sl,tp,"Sell",MagicNumber,0,Red);
   if(t < 0){Print("Failed to open Sell with error :",ErrorDescription(GetLastError()));
      //double sl = 0;
   if(StopLoss > 0)sl = Ask-(StopLoss*Point);
  // double tp = 0;
   if(TakeProfit > 0) tp = Ask+(TakeProfit*Point);
   //int t =
    OrderSend(Symbol(),OP_BUY,Lots,Ask,0,sl,tp,"Buy",MagicNumber,0,Blue);
   if(t < 0){Print("Failed to open Buy with error :",ErrorDescription(GetLastError()));
   }
 
   return(t);
}
}
//+------------------------------------------------------------------+

Above is an EA that I have been trying to insert a "orderclose" portion, at the moment a trade is opened and closed after conditions met, however, a new trade is opened immediately in the same direction, because the same conditions are still intact, if i changed the conditions to follow the twist and turn of the MACD, as it goes up buy and as it turns and goes down sell, etc.. (will be able to figure this part of the code out myself) then where and how could I insert the "orderclose" keeping the code in a similar manner to the OB & OS if this is possible (this I understand).

Please keep in mind I am not a coder and my knowledge and understanding is limited, thanks in advance R

 

Please keep in mind I am not a coder and my knowledge and understanding is limited.

I would recommend getting a comprehensive understanding of mql4 following the book.

where and how could I insert the "orderclose"

You can create a function, then place it within the start(). I recommend naming your function something descriptive like Close_Buy_Orders_In_Reverse_Logic() instead of OC().

at the moment a trade is opened and closed after conditions met, however, a new trade is opened immediately in the same direction, because the same conditions are still intact

You can try limiting the Time. Can be achieve by searching for the Last_Order_Time. Then check if X-amount of time have elapsed or X-number Bars have elapsed.

 
Thanks Ubzen, I am currently reading the book, will have to go over this a few times but things are making a little sense, after reading a few times, i may be able to contribute a little more, which brings me to a puzzling question, you are the only contributor to my question, its beyond me that there are some threads on this forum, https://forum.mql4.com/50748 that have so much attention and help when clearly it's a p*ss take, (forgive me for stooping) thanks Ubzen my friend, for taking genuine time, Robbo
 
I have read your question about 15 times and my brain still can't work out what your saying - sorry!
 

You declare these variables extern:

extern double MacdCurrent= 48;
extern double MacdPrevious = 24;


but then you overwrite them in your start() function:

MacdCurrent=iMACD(NULL,60,4,2,9,PRICE_CLOSE,MODE_MAIN,0);
MacdPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);

Also, your MACD parameters are different. This will give you very strange results I think, unless you are looking for some cross over perhaps?

As Ubzen suggests, plenty of reading is in order. I suggest you find the "MACD sample" in the codebase and study it as I think the sample will do what you are trying to code.

 

I think you are asking about stopping orders from being opened while the opening conditions have not changed. The two ways to remember a status

1) is to use a static variable

2) keep notes in a file. File operations every tick will slow you down so avoid if posible.

Your code would look something like and written for clarity.

static bool AllowBuy = true ;

if ( Buytrigger()==true && AllowBuy==true ) 
  {
   OpenBuy();
   AlowBuy=false ;
  } // no more buy trades until
  
if ( BuyTrigger()==false )
  { 
   AlowBuy=true ;
  }
 
Thanks guys for the input, thinking about this a little more, what I have asked would not realy work, the trades are"always in the market" and this being so would have an organic effect on how they are made, something I understand that may not make sense to others, just a quick question though, how easy would this be to convert the code to MQL5 ? I suppose one answer would be to research the language and do it yourself.... but given the code above, is it easy to relate all commands in MQL4 to -5, thanks Robbo
Reason: