Download MetaTrader 5

Programming question, MQL4 - page 3

To add comments, please log in or register
Hatem Abou Ouf
804
Hatem Abou Ouf 2015.01.02 22:22  
This part needs a cup of coffee :). Thanks "MQL4=5". I like so much your responses. You are a great tutor. 
Hatem Abou Ouf
804
Hatem Abou Ouf 2015.01.03 00:45  

so i deleted " FindNewBar () " function and added "isNewBar" as a bool variable under  the OnTick () event handler :). Now, i got only Sells and no buy. Also, i have a condition of " Once Sell closes , another opened". 

Sell only and continuous
//+-----------------------------------------------------------------------+
//|                                               spikySpike EA V1   mq4  |
//|                                                         EgyAlgoTrader |
//+-----------------------------------------------------------------------+
#property copyright "EgyAlgoTrader"
#property link      "https://forum.mql4.com/66171/page2"
#property version   "1.00"

//+Extern Variables---------------------------------------------------------------+
extern int                bar_volume_min        = 1;
extern int                spike_volume_min      = 1;
extern double             starting_LotSize      = 0.01;
extern int                spike_min_factor      = 2;
extern double             Slippage              = 5;
extern int                StopLoss              = 10;
extern int                TP                    = 5;
extern int                bMagic                = 12345;
extern int                sMagic                = 6789;

//+Global Variables-----------------------------------------------------------------+
double pt;
double stopLossB;
double stopLossS;

double absHighToClose5 ;
double absHighToOpen1;
double absLowToClose2 ;
double absLowToOpen4 ;
double absOpenToClose3;

  
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
Comment("Copyright © 2014, SpikySpike EA for Educational purposes");
   
  if(Digits==3 || Digits==5) pt=10*Point;  // when use at broker with 5 digits 
    else                       pt=Point;     // pricing, this function is useful 
    double stoplevel;                        // minimal pips for stop
    stoplevel=MarketInfo(Symbol(),MODE_STOPLEVEL);  // get broker's stoplevel
    if(StopLoss<=stoplevel) StopLoss=stoplevel;   // we compare our StopLoss with
                                             // stoplevel and adjust it when error occured
    if(TP<=stoplevel) TP=stoplevel;  // we compared our TakeProfit
                                             // as we compared our StopLoss
//----                                        
    return(0);
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {   
      absHighToOpen1 = MathAbs (High [1]-Open [1]);
      absLowToOpen4 = MathAbs (Low [1]-Open [1]);
      absOpenToClose3 = MathAbs (Open [1]-Close [1]);
      
      
      static datetime Time0=0;
      bool isNewBar = Time0 != Time[0]; Time0=Time[0];
  
     if ( isNewBar  == true && SignalBuy ()== true )
        { Comment( "\n", " Singal Buy Found");
        int buyTicket = OrderSend (Symbol(),OP_BUY,starting_LotSize,Ask,Slippage,StopLossB (),TargetProfitB (),"",bMagic,0,clrLawnGreen);
        if (buyTicket == -1){Comment ("\n\n", " Last buy order error  ",  GetLastError ());}
    
        }
    
    
    else if ( isNewBar  == false && SignalBuy ()== false)
    {Comment( "\n\n\n", " No Singal Buy Found"); }
    
    
    
    else if ( isNewBar == true && SignalSell ()== true )
    { Comment ("\n\n\n\n", " Sell signal Found");
    int sellTicket = OrderSend(Symbol(),OP_SELL,starting_LotSize,Bid,Slippage,StopLossS (),TargetProfitS (),"",sMagic,0,clrOrangeRed);
    if (sellTicket == -1) { Comment ("\n\n\n\n\n", " Last Sell Order error   ", GetLastError ());}
    }
    
    else if (isNewBar  == false && SignalSell ()== false)
    {Comment( "\n\n\n\n\n\n", " No Singal Sell Found"); }
    return;
    }
 

//+------------------------------------------------------------------+
//| Get Buy Signal                                                   |
//+------------------------------------------------------------------+

bool SignalBuy ()
{  
  for (int i=OrdersTotal ()-1;i>=0; i--)
   { 
   if (OrderSelect ( i, SELECT_BY_POS))
     { 
     if (OrderType () == OP_BUY && OrderMagicNumber()==bMagic)
        return(false);
     }
   }
   
   if(Close [1] > Open [1]                              &&
      Close [2] < Open [2]                              &&
      Close [3] < Open [3]                              &&
      Close [4] < Open [4]                              &&
      Low  [2] > Low [1]                                &&
      absLowToOpen4 >= spike_min_factor*absOpenToClose3 &&
      absOpenToClose3 >= bar_volume_min *pt )
         return (true);
     
}

//+------------------------------------------------------------------+
//| Get Sell Signal                                                  |
//+------------------------------------------------------------------+

bool SignalSell ()
{  
  for (int i=OrdersTotal ()-1;i>=0; i--)
   { 
   if (OrderSelect ( i, SELECT_BY_POS))
     { 
     if (OrderType () == OP_SELL && OrderMagicNumber()==sMagic)
        Print (" SignalSell False");
        return(false);
        
     }
   }
   
   if(Close [1] < Open [1]                              &&
      Close [2] > Open [2]                              &&
      High  [2] < High [1]                              &&
      absHighToOpen1 >= spike_min_factor*absOpenToClose3 &&
      absOpenToClose3 >= bar_volume_min *pt )
      Print (" SignalSell True");
      return (true);
}

//+------------------------------------------------------------------+
//| Calculate TP for Buy order                                       |
//+------------------------------------------------------------------+

double TargetProfitB ( )
{   
    double TPB =  High [2] + (TP*pt);
   return (TPB);
   
}
       
//+------------------------------------------------------------------+
//| Calculate TP for sell  order                                     |
//+------------------------------------------------------------------+

double TargetProfitS ( )
{  
    double TPS =  Low [2] - (TP*pt);
   return (TPS);
   
}

//+------------------------------------------------------------------+
//| Calculate StopLoss_B                                             |
//+------------------------------------------------------------------+

double StopLossB ( )
{  
    double SLB =  Low [1] - (StopLoss*pt);
   return (SLB);
   
}
       
//+------------------------------------------------------------------+
//| Calculate StopLoss_sell                                          |
//+------------------------------------------------------------------+

double StopLossS ( )
{  
    double SLS = High [1]+ (StopLoss*pt);
   return (SLS);
   
}

Hatem Abou Ouf
804
Hatem Abou Ouf 2015.01.03 00:51  

so i deleted " FindNewBar () " function and added "isNewBar" as a bool variable under  the OnTick () event handler :). Now, i got only Sells and no buy. Also, i have a condition of " Once Sell closes , another opened" on the new bar.

Sell only and continuous
//+-----------------------------------------------------------------------+
//|                                               spikySpike EA V1   mq4  |
//|                                                         EgyAlgoTrader |
//+-----------------------------------------------------------------------+
#property copyright "EgyAlgoTrader"
#property link      "https://forum.mql4.com/66171/page2"
#property version   "1.00"

//+Extern Variables---------------------------------------------------------------+
extern int                bar_volume_min        = 1;
extern int                spike_volume_min      = 1;
extern double             starting_LotSize      = 0.01;
extern int                spike_min_factor      = 2;
extern double             Slippage              = 5;
extern int                StopLoss              = 10;
extern int                TP                    = 5;
extern int                bMagic                = 12345;
extern int                sMagic                = 6789;

//+Global Variables-----------------------------------------------------------------+
double pt;
double stopLossB;
double stopLossS;

double absHighToClose5 ;
double absHighToOpen1;
double absLowToClose2 ;
double absLowToOpen4 ;
double absOpenToClose3;

  
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
Comment("Copyright © 2014, SpikySpike EA for Educational purposes");
   
  if(Digits==3 || Digits==5) pt=10*Point;  // when use at broker with 5 digits 
    else                       pt=Point;     // pricing, this function is useful 
    double stoplevel;                        // minimal pips for stop
    stoplevel=MarketInfo(Symbol(),MODE_STOPLEVEL);  // get broker's stoplevel
    if(StopLoss<=stoplevel) StopLoss=stoplevel;   // we compare our StopLoss with
                                             // stoplevel and adjust it when error occured
    if(TP<=stoplevel) TP=stoplevel;  // we compared our TakeProfit
                                             // as we compared our StopLoss
//----                                        
    return(0);
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {   
      absHighToOpen1 = MathAbs (High [1]-Open [1]);
      absLowToOpen4 = MathAbs (Low [1]-Open [1]);
      absOpenToClose3 = MathAbs (Open [1]-Close [1]);
      
      
      static datetime Time0=0;
      bool isNewBar = Time0 != Time[0]; Time0=Time[0];
  
     if ( isNewBar  == true && SignalBuy ()== true )
        { Comment( "\n", " Singal Buy Found");
        int buyTicket = OrderSend (Symbol(),OP_BUY,starting_LotSize,Ask,Slippage,StopLossB (),TargetProfitB (),"",bMagic,0,clrLawnGreen);
        if (buyTicket == -1){Comment ("\n\n", " Last buy order error  ",  GetLastError ());}
    
        }
    
    
    else if ( isNewBar  == false && SignalBuy ()== false)
    {Comment( "\n\n\n", " No Singal Buy Found"); }
    
    
    
    else if ( isNewBar == true && SignalSell ()== true )
    { Comment ("\n\n\n\n", " Sell signal Found");
    int sellTicket = OrderSend(Symbol(),OP_SELL,starting_LotSize,Bid,Slippage,StopLossS (),TargetProfitS (),"",sMagic,0,clrOrangeRed);
    if (sellTicket == -1) { Comment ("\n\n\n\n\n", " Last Sell Order error   ", GetLastError ());}
    }
    
    else if (isNewBar  == false && SignalSell ()== false)
    {Comment( "\n\n\n\n\n\n", " No Singal Sell Found"); }
    return;
    }
 

//+------------------------------------------------------------------+
//| Get Buy Signal                                                   |
//+------------------------------------------------------------------+

bool SignalBuy ()
{  
  for (int i=OrdersTotal ()-1;i>=0; i--)
   { 
   if (OrderSelect ( i, SELECT_BY_POS))
     { 
     if (OrderType () == OP_BUY && OrderMagicNumber()==bMagic)
        return(false);
     }
   }
   
   if(Close [1] > Open [1]                              &&
      Close [2] < Open [2]                              &&
      Close [3] < Open [3]                              &&
      Close [4] < Open [4]                              &&
      Low  [2] > Low [1]                                &&
      absLowToOpen4 >= spike_min_factor*absOpenToClose3 &&
      absOpenToClose3 >= bar_volume_min *pt )
         return (true);
     
}

//+------------------------------------------------------------------+
//| Get Sell Signal                                                  |
//+------------------------------------------------------------------+

bool SignalSell ()
{  
  for (int i=OrdersTotal ()-1;i>=0; i--)
   { 
   if (OrderSelect ( i, SELECT_BY_POS))
     { 
     if (OrderType () == OP_SELL && OrderMagicNumber()==sMagic)
        Print (" SignalSell False");
        return(false);
        
     }
   }
   
   if(Close [1] < Open [1]                              &&
      Close [2] > Open [2]                              &&
      High  [2] < High [1]                              &&
      absHighToOpen1 >= spike_min_factor*absOpenToClose3 &&
      absOpenToClose3 >= bar_volume_min *pt )
      Print (" SignalSell True");
      return (true);
}

//+------------------------------------------------------------------+
//| Calculate TP for Buy order                                       |
//+------------------------------------------------------------------+

double TargetProfitB ( )
{   
    double TPB =  High [2] + (TP*pt);
   return (TPB);
   
}
       
//+------------------------------------------------------------------+
//| Calculate TP for sell  order                                     |
//+------------------------------------------------------------------+

double TargetProfitS ( )
{  
    double TPS =  Low [2] - (TP*pt);
   return (TPS);
   
}

//+------------------------------------------------------------------+
//| Calculate StopLoss_B                                             |
//+------------------------------------------------------------------+

double StopLossB ( )
{  
    double SLB =  Low [1] - (StopLoss*pt);
   return (SLB);
   
}
       
//+------------------------------------------------------------------+
//| Calculate StopLoss_sell                                          |
//+------------------------------------------------------------------+

double StopLossS ( )
{  
    double SLS = High [1]+ (StopLoss*pt);
   return (SLS);
   
}

Mideel
74
Mideel 2015.01.03 01:42  

Hello, anyone knows why isn't there MqlTradeRequest struct in MQL4 ?

They say we can use full features of MQL5 in the new MQL4, but this half step only confuses us.

Want to learm the new OOP in MQL4 with MQL5 but MQL4 has incomplete stuffs in the end.

Hatem Abou Ouf
804
Hatem Abou Ouf 2015.01.03 15:07  

I am reading this part.. It has some info regarding flags and their proper usage. 

https://book.mql4.com/variables/arrays 

whroeder1
14742
whroeder1 2015.01.03 19:12  
EgyAlgoTrader: Now, i got only Sells
Reformatting and it's easy to see why
Your code
Reformatted
Simplified
bool SignalBuy(){  
   :   
   if(Close [1] > Open [1] ...
      Close [2] < Open [2] ...
      Close [3] < Open [3] ...
      Close [4] < Open [4] ...
      Low  [2] > Low [1]   ...
      absLowToOpen4 >= spik...
      absOpenToClose3 >= ba... )
         return (true);
     
}
bool SignalSell(){  
  :   
   if(Close [1] < Open [1] ...
      Close [2] > Open [2] ...
      High  [2] < High [1] ...
      absHighToOpen1 >= spi...
      absOpenToClose3 >= ba... )
      Print (" SignalSell True");
      return (true);
}
bool SignalBuy(){  
   :   
   if(Close [1] > Open [1] ...
      Close [2] < Open [2] ...
      Close [3] < Open [3] ...
      Close [4] < Open [4] ...
      Low  [2] > Low [1]   ...
      absLowToOpen4 >= spik...
      absOpenToClose3 >= ba... ){
         return (true);
   }
   <<< what value is returned here  
}
bool SignalSell(){  
   :   
   if(Close [1] < Open [1] ...
      Close [2] > Open [2] ...
      High  [2] < High [1] ...
      absHighToOpen1 >= spi...
      absOpenToClose3 >= ba... ){
         Print (" SignalSell True");
      }
   return (true); <<< Always a sell
}
 
bool SignalBuy(){  
   :   
   return Close [1] > Open [1] ...
          Close [2] < Open [2] ...
          Close [3] < Open [3] ...
          Close [4] < Open [4] ...
          Low  [2] > Low [1]   ...
          absLowToOpen4 >= spik...
          absOpenToClose3 >= ba...;
}
bool SignalSell(){  
   :   
   return Close [1] < Open [1] ...
          Close [2] > Open [2] ...
          High  [2] < High [1] ...
          absHighToOpen1 >= spi...
          absOpenToClose3 >= ba...;
}
123
To add comments, please log in or register