iTime () function is not working with iHigh () and iLow ()

 

Hello there...

When i want an order to be executed once per candle it just stops when I add " iTime () " function.
And the problem seems to be only with " iHigh () " and " iLow () " functions...
i will appreciate if anyone would kindly give me a hand, thanks.

//+------------------------------------------------------------------+
//|                                                         test.mq5 |
//|                                  Copyright 2022, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

#include <Trade\Trade.mqh>
CTrade   *Trade;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
input ENUM_TIMEFRAMES timeFrame = PERIOD_M15;
static datetime candleTime = 0;
bool newCandle = false;
double High = 0;
double Low = 0;
double bidPrice = 0;

int OnInit()
  {
//---
   Trade = new CTrade;
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   newCandle = false;
if (candleTime != iTime(_Symbol, timeFrame, 0)){
candleTime = iTime(_Symbol, timeFrame, 0);
newCandle = true;
}
if (newCandle == true){

High = NormalizeDouble (iHigh(_Symbol, timeFrame, 1), _Digits);
Low = NormalizeDouble (iLow(_Symbol, timeFrame, 1), _Digits);
bidPrice = NormalizeDouble (SymbolInfoDouble(_Symbol, SYMBOL_BID), _Digits);

if ( bidPrice > High )
{
Trade.Buy(0.1, _Symbol, NULL, NULL, NULL, NULL);
}
else if ( bidPrice < Low )
{
Trade.Sell(0.1, _Symbol, NULL, NULL, NULL, NULL);
} 
}
  }
//+------------------------------------------------------------------+



 

Code Base

Detecting the start of a new bar or candle

Fernando Carreiro, 2022.04.24 00:38

Detecting the start of a new bar or candle, in the OnTick() event handler of an expert advisor.
 
Fernando Carreiro #:

I put my code in your code but still not working.
May i ask you to paste the code in your MetaTrader and check what could the problem possibly be ? thank you

 
Kourosh Emami #: I put my code in your code but still not working.

“Doesn't work” is meaningless — just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires — meaningless.
     How To Ask Questions The Smart Way. (2004)
          When asking about code
          Be precise and informative about your problem

Do you really expect an answer? There are no mind readers here and our crystal balls are cracked. Always post all relevant code (using Code button) or attach the source file.
     How To Ask Questions The Smart Way. (2004)
          Be precise and informative about your problem

We can't see your broken code.

 
William Roeder #:

“Doesn't work” is meaningless — just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires — meaningless.
     How To Ask Questions The Smart Way. (2004)
          When asking about code
          Be precise and informative about your problem

Do you really expect an answer? There are no mind readers here and our crystal balls are cracked. Always post all relevant code (using Code button) or attach the source file.
     How To Ask Questions The Smart Way. (2004)
          Be precise and informative about your problem

We can't see your broken code.

Hello William

Yes i've tried Fernando solution but when I wrap my condition with " iTime () " function, it just doesn't let any position correctly works.
it's just a simple condition which anytime Bid Price passes the High of the candle number 1 it opens a buy position per each candle or vice versa....
but it just doesn't happen...

i've sent a picture that shows the price passes High of candle 1 but there is no Buy position opened....

Files:
Captureoooo.PNG  94 kb
 
Kourosh Emami #: I put my code in your code but still not working. May i ask you to paste the code in your MetaTrader and check what could the problem possibly be ? thank you
Show your new code with mine! How do you wish for me to check it, if you don't show it?
 
  1. Kourosh Emami #: i've sent a picture that shows the price passes High of candle 1 but there is no Buy position opened....

    But that price is not “pass the High of candle 1” on the first tick of the new bar. You added new bar code.

  2. Kourosh Emami: When i want an order to be executed once per candle i

    You don't want it once per candle. You don't want it once per condition (open on each new bar). You want it once!

  3. See if you have a position open. If not and Bid>high then open.

 
Fernando Carreiro #:
Show your new code with mine! How do you wish for me to check it, if you don't show it?
//+------------------------------------------------------------------+
//|                                                         test.mq5 |
//|                                  Copyright 2022, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

#include <Trade\Trade.mqh>
CTrade   *Trade;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
input ENUM_TIMEFRAMES timeFrame = PERIOD_M15;
static datetime candleTime = 0;
bool newCandle = false;
double High = 0;
double Low = 0;
double bidPrice = 0;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   Trade = new CTrade;
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   static datetime dtBarCurrent  = WRONG_VALUE;
   datetime dtBarPrevious = dtBarCurrent;
   dtBarCurrent  = iTime(_Symbol, _Period, 0);
   bool     bNewBarEvent  = (dtBarCurrent != dtBarPrevious);

// React to a new bar event and handle it.
   if(bNewBarEvent)
     {
      // Detect if this is the first tick received and handle it.
      /* For example, when it is first attached to a chart and
         the bar is somewhere in the middle of its progress and
         it's not actually the start of a new bar. */
      if(dtBarPrevious == WRONG_VALUE)
        {
         // Do something on first tick or middle of bar ...
        }
      else
        {
         // Do something when a normal bar starts ...
         High = NormalizeDouble(iHigh(_Symbol, timeFrame, 1), _Digits);
         Low = NormalizeDouble(iLow(_Symbol, timeFrame, 1), _Digits);
         bidPrice = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_BID), _Digits);

         if(bidPrice > High)
           {
            Trade.Buy(0.1, _Symbol, NULL, NULL, NULL, NULL);
           }
         else
            if(bidPrice < Low)
              {
               Trade.Sell(0.1, _Symbol, NULL, NULL, NULL, NULL);
              }
        };

      // Do something irrespective of the above condition ...
     }
   else
     {
      // Do something else ...
     };



  }
//+------------------------------------------------------------------+

 

Here is an example! Try it and let use know!

#include <Trade\Trade.mqh>

input ENUM_TIMEFRAMES   i_eTimeframe   = PERIOD_M15;
input double            i_dbVolume     = 0.1;

CTrade oTrade;

int OnInit() {
   return INIT_SUCCEEDED;
};

void OnTick() {
   static datetime dtBarCurrent  = WRONG_VALUE;
          datetime dtBarPrevious = dtBarCurrent;
                   dtBarCurrent  = iTime( _Symbol, i_eTimeframe, 0 );
          bool     bNewBarEvent  = ( dtBarCurrent != dtBarPrevious );
   if( bNewBarEvent ) {
      double
         dbHigh = iHigh( _Symbol, i_eTimeframe, 1 ),
         dbLow  = iLow(  _Symbol, i_eTimeframe, 1 ),
         dbAsk  = SymbolInfoDouble( _Symbol, SYMBOL_BID ),
         dbBid  = SymbolInfoDouble( _Symbol, SYMBOL_BID );
      if( dbBid > dbHigh )
         oTrade.Buy(  i_dbVolume, _Symbol, dbAsk );
      else if( dbBid < dbLow )
         oTrade.Sell( i_dbVolume, _Symbol, dbBid );
   };
};

And please don't use NormaliseDouble on quote prices provided by the terminal. They are already normalised.

NB! Please note, that trades are only placed if the opening bid is above or below the previous candle. If during the lifetime of the current candle the bid price moves above or below the previous candle, no trades are placed. Only at the open of the candle. That is the logic you have presented and that is the logic of the code above.

 
Fernando Carreiro #:

Here is an example! Try it and let use know!

And please don't use NormaliseDouble on quote prices provided by the terminal. They are already normalised.

Doesn't work...
there was a little mistake in second if i changed it to ...... 

else if( dbBid < dbLow )
Files:
Capture666.PNG  107 kb
 
William Roeder #:
  1. But that price is not “pass the High of candle 1” on the first tick of the new bar. You added new bar code.

  2. You don't want it once per candle. You don't want it once per condition (open on each new bar). You want it once!

  3. See if you have a position open. If not and Bid>high then open.

i've removed that iTime() as soon as price passes the position opened...

that PositionsTotal () is just preventing opening too many positions

Files:
Capture12.PNG  106 kb
Reason: