Download MetaTrader 5

Logic not correct

To add comments, please log in or register
Patrick Burns
1750
Patrick Burns  
I have a function on an EA (mql4) to set price levels at which not to buy or sell. The function doesn't work correctly, once a threshold has been reached it won't trade at all. Obviously the idea is still to be able to Sell once the High_Level is breached and still be able to Buy when the Low_Level is breached. Thanks.

//Variables

input double   High_Level= 1.50001; // Do not buy above this level
input double   Low_Level = 1.40001; // Do not sell below this level




//Relevant Section of code


// Get a signal:
      if(Volume_OK(Volume_Period,Volume_Minimum))
        {
         if(Ask<High_Level)
           {
            if(Bid>Low_Level)
              {
               if(MA_Disable)
                 {
                  d_Level=Close[1]-Open[1];
                  i_Signal=0;
                  if(d_Level>0.0) i_Signal=1;
                  else if(d_Level<0.0) i_Signal=-1;
                 }
               else i_Signal=Get_Signal(MA_Fast_Period,MA_Mid_Period,MA_Slow_Period);
              }
           }
        }
Mary Clark
120
Mary Clark  

 where is the code that opens orders ?

Your if statement says don't set i_Signal if its above or below the High/Low Level. its executing exactly how programmed. 

Take the if statements out if you don't want to check. (if (Ask and if (Bid.... 

Patrick Burns
1750
Patrick Burns  
Mary Clark:

 where is the code that opens orders ?

Your if statement says don't set i_Signal if its above or below the High/Low Level. its executing exactly how programmed. 

Take the if statements out if you don't want to check. (if (Ask and if (Bid.... 

Thank you, Mary.
if(i_Signal==0) return(0); // no signal, wait for the next tick

                                 // Define order type & levels:
      if(i_Signal>0)
        {
         i_Signal= OP_BUY;
         d_Level = Ask;
         d_SL = d_Level - gd_SL;
         d_TP = d_Level + gd_TP;
           } else {
         i_Signal= OP_SELL;
         d_Level = Bid;
         d_SL = d_Level + gd_SL;
         d_TP = d_Level - gd_TP;
        }

      // Check & normalize lot size:
      d_Lot=Get_Lot(0,0,0,First_Lot,gs_Symbol);

      if(d_Lot>0.0)
        { // lot OK
         if(Write_Journal) Print("Cycle start");
         i_Order=Send_Order(gs_Symbol,
                            Magic_Number,
                            Market_Execution,gi_Try_To_Trade,gi_Connect_Wait,
                            i_Signal,
                            d_Lot,
                            d_Level,
                            gi_Slippage,
                            "0",// 1st level = 0 index in array
                            d_SL,
                            d_TP
                            );
         if(i_Order<0 && Write_Journal)
           { // got error, write to log
            s_String=" Buy "; if(i_Signal==OP_SELL) s_String=" Sell ";
            Print("First step"+s_String+" error, Lot=",d_Lot," Level=",DoubleToStr(d_Level,Digits)," SL=",DoubleToStr(d_SL,Digits)," TP=",DoubleToStr(d_TP,Digits)," Ask=",DoubleToStr(Ask,Digits)," Bid=",DoubleToStr(Bid,Digits));
           }
         else si_Phase=1;
        }
      else if(Write_Journal) Print("Not enough money");

      return(1); // done OK or not, sleep now
     }
Achmad Wijaya
794
Achmad Wijaya  
Patrick Burns:
Thank you, Mary.
if(i_Signal==0) return(0); // no signal, wait for the next tick

                                 // Define order type & levels:
      if(i_Signal>0)
        {
         i_Signal= OP_BUY;
         d_Level = Ask;
         d_SL = d_Level - gd_SL;
         d_TP = d_Level + gd_TP;
           } else {
         i_Signal= OP_SELL;
         d_Level = Bid;
         d_SL = d_Level + gd_SL;
         d_TP = d_Level - gd_TP;
        }

      // Check & normalize lot size:
      d_Lot=Get_Lot(0,0,0,First_Lot,gs_Symbol);

      if(d_Lot>0.0)
        { // lot OK
         if(Write_Journal) Print("Cycle start");
         i_Order=Send_Order(gs_Symbol,
                            Magic_Number,
                            Market_Execution,gi_Try_To_Trade,gi_Connect_Wait,
                            i_Signal,
                            d_Lot,
                            d_Level,
                            gi_Slippage,
                            "0",// 1st level = 0 index in array
                            d_SL,
                            d_TP
                            );
         if(i_Order<0 && Write_Journal)
           { // got error, write to log
            s_String=" Buy "; if(i_Signal==OP_SELL) s_String=" Sell ";
            Print("First step"+s_String+" error, Lot=",d_Lot," Level=",DoubleToStr(d_Level,Digits)," SL=",DoubleToStr(d_SL,Digits)," TP=",DoubleToStr(d_TP,Digits)," Ask=",DoubleToStr(Ask,Digits)," Bid=",DoubleToStr(Bid,Digits));
           }
         else si_Phase=1;
        }
      else if(Write_Journal) Print("Not enough money");

      return(1); // done OK or not, sleep now
     }

Hello, What if I give a suggest to use String to get signal, maybe it can help

Example like this

I see above using this

                                 // Define order type & levels:
      if(i_Signal>0)
        {
         i_Signal= OP_BUY;
         d_Level = Ask;
         d_SL = d_Level - gd_SL;
         d_TP = d_Level + gd_TP;
           } else {
         i_Signal= OP_SELL;
         d_Level = Bid;
         d_SL = d_Level + gd_SL;
         d_TP = d_Level - gd_TP;
        }

How about make improvement to this


//+------------------------------------------------------------------+
//| get signal                                                       |
//+------------------------------------------------------------------+
string getsignal()                      // we calculate trading signal here
{
   double indicator to get signal  // what indicator to get signal
   if( condition to buy ) return("buy");    // buy signal On
                                        
   if(condition to sell ) return("sell");   // sell signal On
                                        /
   return("wait and see");              // no signal returned
}
//+------------------------------------------------------------------+
//| send order                                                       |
//+------------------------------------------------------------------+    
int sendorder(int cmd,double lots,double prc,double sl,double tp,color ordercolor)
{
   return(OrderSend(Symbol(),cmd,lots,prc,2,sl,tp,"",MagicNumber,0,ordercolor));
}  

//+------------------------------------------------------------------+
//| order management                                                 |
//+------------------------------------------------------------------+
int ordermanagement()                   // we manage our orders here
{
   if(ordercount()==0)                  // when there is no opened orders
   {
      if(getsignal()=="buy")            // when buy signal is appeared
      {
        if(sendorder(OP_BUY,Lot,    // we send our buy order into
           Ask,Ask-StopLoss*pt,         // broker's server then we out
           Ask+TakeProfit*pt,           // from this cycle to avoid
           Blue)>0) return(0);          // errors possibility
      }
      if(getsignal()=="sell")           // same procces as buy signal
      {
        if(sendorder(OP_SELL,Lot,
           Bid,Bid+StopLoss*pt,
           Bid-TakeProfit*pt,
           Red)>0) return(0);
      }
   }

   return(0);
}

and for Lot Calculation, I don't know detail of your Methode, but I want to give you example

I See above only like this

      // Check & normalize lot size:
      d_Lot=Get_Lot(0,0,0,First_Lot,gs_Symbol);

What if improvement to this

//+-----------------------------------------------------------------------------------+
//| last lot Example if First_Lot I Consider it taken from the History of Order       |
//+-----------------------------------------------------------------------------------+
double lastlot()                        // we will find last lot size
{                                       // order with this function
  double lot=0;                         // lot size
  int i;                                // pos of our order
  for(i=0; i<OrdersHistoryTotal(); i++) // looping to check all of our
   {                                     // order history from pos 0 until
                                        // last pos
       OrderSelect(i,SELECT_BY_POS,MODE_HISTORY); // is it right our order?
       if(OrderSymbol()!=Symbol() ||    // if symbol of order didn't match
       OrderMagicNumber()!=MagicNumber) // with current chart symbol
       continue;                        // or the magic numb of order
                                        // didn't match with our magic
                                        // number, it is not target Lot
       lot=OrderLots();                 // last lot
  }
  return(lot);                          // last lot size returned
}

But if I see Back on the Case of the logic, I think it's only need to swicth with True False  Condition

" Obviously the idea is still to be able to Sell once the High_Level is breached and still be able to Buy when the Low_Level is breached"


  High_Level= 1.50001; // Do not buy above this level , How About Make Condition Buy  False and Sell True ?
  Low_Level = 1.40001; // Do not sell below this level, , How About Make Condition Buy True and Sell False ?


I Hope It Can Help :) Forgive me If I'm wrong, because I still amateur in the code.

Patrick Burns
1750
Patrick Burns  
finally worked it out after much size 10's verses wall. I have commented out old code.



 // Get a signal:
  if(Volume_OK(Volume_Period, Volume_Minimum)) {
  
  //if(Ask<High_Level){
    //  if(Bid>Low_Level) {                                                                
   
     if(MA_Disable) {
      d_Level = Close[1]-Open[1];
      i_Signal = 0;
      if(d_Level > 0.0) i_Signal = 1;
     
     
                       
      else if(d_Level < 0.0) i_Signal = -1;
     
     } else i_Signal = Get_Signal(MA_Fast_Period, MA_Mid_Period, MA_Slow_Period);
    }
  // }
  //}
  if(Ask>High_Level)i_Signal=0;
  if(Bid<Low_Level)i_Signal=0;
  if(Ask<Low_Level)i_Signal=2;
  if(Bid>High_Level)i_Signal=-1;
 
 
  if(i_Signal == 0) return(0); // no signal, wait for the next tick
To add comments, please log in or register