Execute one trade per signal

 

i'd like to only make one trade per signal in other words "per new bar"

here's my implementation with wrapping the logic within "IsNewCandle" function  but the problem is that the expert makes few trades compared to not using the "IsNewCandle" function and instead limiting buy and sell orders to one order send and one position per signal


bool IsNewCandle(void)
  {
   static datetime t_bar=iTime(_Symbol,_Period,0);
   datetime time=iTime(_Symbol,_Period,0);
//---
   if(t_bar==time)
      return false;
   t_bar=time;
//---
   return true;
  }
  
  
void OnTick()
  {
  
   if (IsNewCandle() == true)
   
    {
      // My Logic for buy & sell orders

    }

} 
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
//| Expert initialization function                                   | //| Expert deinitialization function                                 | //| Expert tick function                                             | //| test1                                                            |...
 
   static datetime t_bar=iTime(_Symbol,_Period,0);
  1. That is not an assignment; it's initialization of a common (globally declared,) or static variable with a constant. They work exactly the same way in MT4/MT5/C/C++.
    1. They are initialized once on program load.

    2. They don't update unless you assign to them.

    3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5, or MT4 with strict (which you should always use.)

      MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and don't try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:

      1. Terminal starts.
      2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
      3. OnInit is called.
      4. For indicators OnCalculate is called with any existing history.
      5. Human may have to enter password, connection to server begins.
      6. New history is received, OnCalculate called again.
      7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

    4. Unlike indicators, EAs are not reloaded on chart change so you must reinitialize them, if necessary.
                external static variable - MQL4 programming forum

  2. You don't update your static on change.

  3. For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
              New candle - MQL4 programming forum #3 2014.04.04

    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
              Running EA once at the start of each bar - MQL4 programming forum 2011.05.06

 
William Roeder:
  1. That is not an assignment; it's initialization of a common (globally declared,) or static variable with a constant. They work exactly the same way in MT4/MT5/C/C++.
    1. They are initialized once on program load.

    2. They don't update unless you assign to them.

    3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5, or MT4 with strict (which you should always use.)

      MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and don't try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:

      1. Terminal starts.
      2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
      3. OnInit is called.
      4. For indicators OnCalculate is called with any existing history.
      5. Human may have to enter password, connection to server begins.
      6. New history is received, OnCalculate called again.
      7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

    4. Unlike indicators, EAs are not reloaded on chart change so you must reinitialize them, if necessary.
                external static variable - MQL4 programming forum

  2. You don't update your static on change.

  3. For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
              New candle - MQL4 programming forum #3 2014.04.04

    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
              Running EA once at the start of each bar - MQL4 programming forum 2011.05.06

Thanks for your help , 

unfortunately , i have tried your suggestion but it produced same result!! 

i have also tried code mentioned in number 3 , "with modification of Time to itime for mql5" and still got same result!

 
andrw11:

Thanks for your help , 

unfortunately , i have tried your suggestion but it produced same result!! 

i have also tried code mentioned in number 3 , "with modification of Time to itime for mql5" and still got same result!

Probably you did not fix your code correctly, please show the code you're actually working on.

 
lippmaje:

Probably you did not fix your code correctly, please show the code you're actually working on.

sorry about that , it's me doing something wrong , i just can't figure what!


bool IsNewCandle(void)
  {
   static datetime t_bar;
   datetime time=iTime(_Symbol,_Period,0);
//---
   if(t_bar==time)
      return false;
   t_bar=time;
//---
   return true;
  }
  
  
void OnTick()
  {
  
   if (IsNewCandle() == true)
   
    {
      // My Logic for buy & sell orders

    }

} 

another solution proposed which also didn't work , (modified for mql5)

void OnTick(){
   static datetime timeCur; datetime timePre = timeCur; timeCur=iTime(_Symbol,_Period,0);
   bool isNewBar = timeCur != timePre;
   if(isNewBar){ 


      // My Logic for buy & sell orders
   }
}
 

Turn your entry signal criteria work with new bar formation only and create a function for this, example;

void OnTick()
{
 if(IsSignal()==1)
 //....do buy function
 if(IsSignal()==-1)
 //....do sell function
}
//------------------------
int IsSignal()
{
 int sigval=0; 

 if(!IsNewBar())
 return;

 if(blabla<blabla)
   sigval=1;
 if(blabla>blabla)
   sigval=-1

 return(sigval);
}
//-------------------------
bool IsNewBar()
{
 static datetime lastbar;
 datetime curbar = Time[0];
 if(lastbar!=curbar)
   {
    lastbar=curbar;
    return (true);
   }
  else
    return(false);
 }
 
Kenneth Parling:

Turn your entry signal criteria work with new bar formation only and create a function for this, example;

void OnTick()
{
 if(IsSignal()==1)
 //....do buy function
 if(IsSignal()==-1)
 //....do sell function
}
//------------------------
int IsSignal()
{
 int sigval=0; 

 if(!IsNewBar())
 return;

 if(blabla<blabla)
   sigval=1;
 if(blabla>blabla)
   sigval=-1

 return(sigval);
}
//-------------------------
bool IsNewBar()
{
 static datetime lastbar;
 datetime curbar = Time[0];
 if(lastbar!=curbar)
   {
    lastbar=curbar;
    return (true);
   }
  else
    return(false);
 }

You are calling IsSignal() twice and each call to IsSignal() will call IsNewBar().

IF IsNewBar() returns true the first call, it will return false the second. The second call will always return false.

 
Kenneth Parling:

Turn your entry signal criteria work with new bar formation only and create a function for this, example;

Thanks for your help , but still not working !!

 
Any help!
 

can someone help me to work n my indicator. for it to run one trade per signal entry or one trade for every 4hrs to 6 hrs. thanks

 
Onasanya Olawale #: can someone help me to work n my indicator. for it to run one trade per signal entry or one trade for every 4hrs to 6 hrs. thanks

You are looking at a signal. Act on a change of signal.
          MQL4 (in Strategy Tester) - double testing of entry conditions - MQL5 programming forum #1 (2017)