Need help for this naked Expert advisor that not open order while start

To add comments, please log in or register
forexsender
20
forexsender  
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

extern int BiggestCandleRange = 6 ;
extern bool RoomToTheLeft = true ;
extern int RoomToTheLeftCandle = 10 ;
extern double RoomThreshold = 5.0 ;

extern int EntryPips = 10 ;
extern int TakeProfitPips = 300 ;
extern double LotPerTrade = 1 ;
extern int Slippage = 3;
extern int ExpirationTime = 3600 ;




datetime CurrentBarTime;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
    if ( Time [0] > CurrentBarTime )
    {
       // new bar created
       CurrentBarTime = Time[0] ;
       
       // 1 - is it an engulfing candle
       if ( High[1] > High [2] && Low [1] > Low [2])
         {
          Alert(" there is an engulfing candle at ", CurrentBarTime );
          
          // 2 - check if the range is the biggest over the last x amount of candle
          int HighestRangeCandle = GetHighestPreviousRangeCandle ( BiggestCandleRange );
          
          if ( HighestRangeCandle == 1 )
          {
           //Alert ( " the previous candle is the biggest one over the last" + IntegerToString( BiggestCandleRange ) + " candles" );
                bool isBull = false ;
                if ( Open [1] < Close[1] )
                isBull = true ;
                
           if(RoomToTheLeft == true )
           {
           
           
             
              bool IsRoomToTheLeft = IsThereRoomToTheLeft(isBull);
              
               Alert("the Previous candle is the biggest ,engulfing and has room to the left ");
              if ( IsRoomToTheLeft == false )
              {
                return  ;
              }  
             
                 
           }
           
           if ( isBull == true )
           {
             // create buy order
             
             //double EntryPoint  = High [1] + EntryPips * Point(); // 40*0.0001 
             double StopLoss  = Open[1] ;
             double TakeProfit =  TakeProfitPips * Point () ;
              
             int ticket = OrderSend( Symbol(),OP_BUYSTOP , LotPerTrade ,Slippage ,StopLoss , TakeProfit ,"this is an bullish buy trade" , ExpirationTime ,clrGreen );
             if ( ticket == false )
             {
               Alert ("something wrong in buy order", GetLastError () ) ;
             }
           }
           
          /* else {
             // create sell order
              double EntryPoint  = Low [1] - EntryPips * Point(); // 40*0.0001 
              double StopLoss  = Open[1] ;
              double TakeProfit = EntryPoint - TakeProfitPips * Point () ;
              
             int ticket = OrderSend ( Symbol(),OP_SELLSTOP , LotPerTrade , EntryPoint , Slippage , StopLoss , TakeProfit ,"this is an bearish sell trade" ,false , CurTime()+ ExpirationTime );
             if ( ticket == false )
             {
               Alert ("something wrong in sell order", GetLastError () ) ;
             }
           }*/
           
          }
         }
    }
  }
  
  bool IsThereRoomToTheLeft(bool isBull )
  {
   double Range = High[1] - Low[1] ;
   if (isBull == false )
   {
      // is a bearish candle - look at the high of previous candle
      
      int HighestIndex = iHighest(NULL,0,MODE_HIGH,RoomToTheLeftCandle,2);
      double HighestValue = High[HighestIndex] ;
      if (HighestValue < High[1] )
      {
        // there is room to the left but how much?
        
        double RoomAvaliable = High[1] - HighestValue ;
        double HowMuchRoomIsThere = (RoomAvaliable / Range )* 100 ;
        if( HowMuchRoomIsThere > RoomThreshold )
        {
         return  true ;
        }
        else{
         return  false ;
        }
        }
        else {
         return  false ;
            }
            
      }
      
      
      else {
       // is a bullish candle - look at the high of previous candle
     
      int LowestIndex = iLowest(NULL,0,MODE_LOW,RoomToTheLeftCandle,2);
      double LowestValue = Low[LowestIndex] ;
      if (LowestValue > Low[1] )
      {
        // there is room to the left but how much?
        
        double RoomAvaliable =LowestValue - Low[1] ;
        double HowMuchRoomIsThere = (RoomAvaliable / Range )* 100 ;
        if( HowMuchRoomIsThere > RoomThreshold )
        {
         return  true ;
        }
        else{
         return  false ;
        }
        }
        else {
         return  false ;
            }
      }
   
  }
  
  
  int GetHighestPreviousRangeCandle ( int bars )
  {
     double Output [100] = {};
     for ( int counter = 1 ; counter <= bars; counter++ )
     {
       Output[counter] = High[counter] - Low [counter];
     }
     
     int HighestIdInOutput = ArrayMaximum(Output);
     return HighestIdInOutput ;
  }
//+------------------------------------------------------------------+
William Roeder
20207
William Roeder  
  1.     if ( Time [0] > CurrentBarTime )
        {
           // new bar created
           CurrentBarTime = Time[0] ;
            
    
    There was a case a few years back there a broker had the time correct but the date wrong. Once corrected the EA stops trading. Use ≠ instead.

  2.                 bool isBull = false ;
                    if ( Open [1] < Close[1] )
                    isBull = true ;
    Simplify: bool isBull = Open[1] < Close[1];

  3.            if(RoomToTheLeft == true )
    
    You should be able to read your code out loud and have it make sense. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled where as Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.

  4.               bool IsRoomToTheLeft = IsThereRoomToTheLeft(isBull);
                  
                   Alert("the Previous candle is the biggest ,engulfing and has room to the left ");
                  if ( IsRoomToTheLeft == false )
                  {
                    return  ;
                  }  
    
    Don't lie to yourself.

  5. Perhaps you should read the manual.
    Your code
     Documentation
    int ticket = OrderSend( 
       Symbol(),
       OP_BUYSTOP , 
       LotPerTrade ,
                                        
       Slippage ,
       StopLoss , 
       TakeProfit ,
       "this is an bullish buy trade" , 
                                        
       ExpirationTime ,
       clrGreen 
    );
    int  OrderSend(
       string   symbol,              // symbol
       int      cmd,                 // operation
       double   volume,              // volume
       double   price,               // price
       int      slippage,            // slippage
       double   stoploss,            // stop loss
       double   takeprofit,          // take profit
       string   comment=NULL,        // comment
       int      magic=0,             // magic number
       datetime expiration=0,        // pending order expiration
       color    arrow_color=clrNONE  // color
       );

To add comments, please log in or register