Can someone explain 1 minute ohlc with newbar?

 

Check this code

When you run this on ohlc and uncomment the newbar function it returns different results? Can anyone explain what it skips on 1minute ohlc??

#include <Trade\Trade.mqh>
#include <Trade\OrderInfo.mqh>
//+------------------------------------------------------------------+
CPositionInfo  m_position;
CTrade         trade;
COrderInfo m_order;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+

  //--- Detects when a "new bar" occurs, which is the same as when the previous bar has completed.
bool IsNewBar(const string symbol, const ENUM_TIMEFRAMES period)
  {
   bool isNewBar = false;
   static datetime priorBarOpenTime = NULL;

//--- SERIES_LASTBAR_DATE == Open time of the last bar of the symbol-period
   const datetime currentBarOpenTime = (datetime) SeriesInfoInteger(symbol, period, SERIES_LASTBAR_DATE);

   if(priorBarOpenTime != currentBarOpenTime)
     {
      //--- Don't want new bar just because EA started
      isNewBar = (priorBarOpenTime == NULL) ? false : true; // priorBarOpenTime is only NULL once

      //--- Regardless of new bar, update the held bar time
      priorBarOpenTime = currentBarOpenTime;
     }

   return isNewBar;
  }
void OnTick()
  {
//---
      //if (!IsNewBar(_Symbol, PERIOD_M1))return;
      
      double tp,stp, Lots;
      stp = SymbolInfoDouble(Symbol(), SYMBOL_BID) - 100 * _Point;
      tp = SymbolInfoDouble(Symbol(), SYMBOL_ASK) + 100 * _Point;
      Lots=0.01;
      if(PositionsTotal()==0)trade.Buy(Lots,Symbol(),SymbolInfoDouble(Symbol(), SYMBOL_ASK),stp, tp,ORDER_TIME_GTC);
 
  }
//+------------------------------------------------------------------+
 
Louis Stoltz:

Check this code

When you run this on ohlc and uncomment the newbar function it returns different results? Can anyone explain what it skips on 1minute ohlc??

what do you mean fun it on OHLC, you surely just mean a chart?

define the problem, what do you mean by different results? 

if you simply mean why does it not process all the code, that is because if it is not a new M1 bar it returns the function as written, but surely that is what you mean to do, so what is the problem?
 
Paul Anscombe #:

define the problem, what do you mean by different results...

if you simply mean why does it not process all the code, that is because if it is not a new M1 bar it returns the function but surely tha tis what you mean to do so what is the problem?

What im trying to see whats the difference when you add this function in 1 minute ohlc. When i add this it checks for new bar and then run the code, so when i deactivate this i get a different result. So my question is, is ohlc 1minute the same as newbar??

 
Louis Stoltz #:

What im trying to see whats the difference when you add this function in 1 minute ohlc. When i add this it checks for new bar and then run the code, so when i deactivate this i get a different result. So my question is, is ohlc 1minute the same as newbar??

no.... OHLC is short for Open, High, Low, Close  

a new bar is when a new bar starts to be formed, in your case on the M1 timeframe.

when you comment it out, it will process the rest of the code every tick instead of once every bar

 
Paul Anscombe #:

no.... OHLC is short for Open, High, Low, Close  

a new bar is when a new bar starts to be formed, in your case on the M1 timeframe.

when you comment it out, it will process the rest of the code every tick instead of once every bar

im running it on 1minute ohlc not every tick

 
Louis Stoltz #:im running it on 1minute ohlc not every tick
The event handler function name is "OnTick", which means it runs on "every tick", even if your chart is M1, M15, H1 or D1. It always runs on every tick. That is why there is a need for a "new bar" detection.
 
Louis Stoltz #:

im running it on 1minute ohlc not every tick

So you’re running it in the tester,  why didn’t you say that in the first place!

So now you still need to explain your problem, different results is meaningless…

If m1 is not the tester timeframe then you will need to handle data exceptions when requesting bar data
 
Paul Anscombe #:
So you’re running it in the tester,  why didn’t you say that in the first place!

So now you still need to explain your problem, different results is meaningless…

If m1 is not the tester timeframe then you will need to handle data exceptions when requesting bar data

Dont worry I did some reading and understand now and added this

Print("before new bar bid ",SymbolInfoDouble(Symbol(), SYMBOL_BID), " Open ", iOpen(_Symbol, TimePeriod, 0), " High ", iHigh(_Symbol, TimePeriod, 0), " Low ", iLow(_Symbol, TimePeriod, 0), " Close ", iClose(_Symbol, TimePeriod, 0));
if (!IsNewBar(_Symbol, PERIOD_M1))return;
Print("after new bar bid ",SymbolInfoDouble(Symbol(), SYMBOL_BID), " Open ", iOpen(_Symbol, TimePeriod, 1), " High ", iHigh(_Symbol, TimePeriod, 1), " Low ", iLow(_Symbol, TimePeriod, 1), " Close ", iClose(_Symbol, TimePeriod, 1));