How to Deal with Bar0 ?

 

Hi coders, I have a technical issue that I cannot resolve it. I have an entry condition as follows :-


double Hi0=iHigh(sym,60,0), Hi1=iHigh(sym,60,1), Lo0=iLow(sym,60,0), Lo1=iLow(sym,60,1);

bool BuySignal= ... && Lo0>Lo1 && ...;

bool SelSignal= ... && Hi0<Hi1 && ...;


To place a BUY (BuySignal), it must be a "Lo0>Lo1" condition, otherwise my EA will not proceed to BUY, and vice versa for SELL signal.


。 。 。 。 。


Now, during my demo testing, I found that my EA yet placed BUY/SELL orders even through "Lo0<=Lo1" and "Hi0>=Hi1" happened respectively, and this should not happen at all.


。 。 。 。 。


In my opinion, the problem is on the Lo0 and Hi0 because the Bar0 is yet not a complete full bar.


I need coder's help and advice, how to resolve the Bar0 issue ? Thanks.

 

>> In my opinion, the problem is on the Lo0 and Hi0 because the Bar0 is yet not a complete full bar.


You got it right!

Bar0 is "forming candle", so HIGH, LOW and CLOSE are keep on changing / updating until this candle is then closed.

If no price changes, i.e. no new price tick arrives, then you might see only OPEN price, where HIGH, LOW and CLOSE might be all having the same price.


Modify your condition or try to work with lower timeframe.

Good luck.

 
Soewono Effendi #:

>> In my opinion, the problem is on the Lo0 and Hi0 because the Bar0 is yet not a complete full bar.


You got it right!

Bar0 is "forming candle", so HIGH, LOW and CLOSE are keep on changing / updating until this candle is then closed.

If no price changes, i.e. no new price tick arrives, then you might see only OPEN price, where HIGH, LOW and CLOSE might be all having the same price.


Modify your condition or try to work with lower timeframe.

Good luck.

Thanks Soewono, pertaining to your statement "HIGH, LOW and CLOSE might be all having the same price as OPEN ", but if I run a print :-

Print(Symbol()+" /Lo0 "+DoubleToStr(Lo0,Digits)+" /Hi0 "+DoubleToStr(Hi0,Digits));

The output of the Lo0 and Hi0 are exactly the same as chart, but not having the same price as OPEN.


Do you have any idea how to deal with "Lo0>Lo1" and "Hi0<Hi1" for the TF=60 ? If possible, kindly provide your codes, appreciated.

 
Chin Min Wan #: Do you have any idea how to deal with "Lo0>Lo1" and "Hi0<Hi1" for the TF=60 ? If possible, kindly provide your codes, appreciated.
double Hi0=iHigh(sym,60,0), Hi1=iHigh(sym,60,1), Lo0=iLow(sym,60,0), Lo1=iLow(sym,60,1);
  1. Do not hard code constants. Use the proper symbols (PERIOD_H1). Code fails on MT5.

  2. On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - MQL4 programming forum - Page 3 #26.4 (2019)

    I recommend: Do not trade multiple currencies in one EA.

    1. You can't use any {MT4: predefined variables, MT5: predefined variables,}

    2. A multi-asset EA runs in one thread so that one asset blocks all the others, while each EA for a single asset runs in its own thread,

    3. Must poll (not OnTick, unless you use specific indicators)
                The Implementation of a Multi-currency Mode in MetaTrader 5 - MQL5 Articles (2011)

    4. and usually other problems, e.g. A problem with iBarShift - MQL4 programming forum - Page 2 (2016)

    5. You must handle History {MT4:4066/4073 errors: Download history in MQL4 EA - MQL4 programming forum, MT5: Timeseries and Indicators Access /  Data Access - Reference on algorithmic/automated trading language for MetaTrader 5.}

    6. Code it to trade the chart pair only. Look at the others if you must. Don't assume that Time[i] == iTime(otherPair, TF, i) always use iBarShift.

    Then put it on other charts to trade the other pairs. Done.

  3. Show us your attempt (using the CODE button) and state the nature of your difficulty.
              No free help (2017)

    Or pay someone. Top of every page is the link Freelance.
              Hiring to write script - General - MQL5 programming forum (2018)

    We're not going to code it for you (although it could happen if you are lucky or the issue is interesting).
              No free help (2017)

 
William Roeder #:
  1. Do not hard code constants. Use the proper symbols (PERIOD_H1). Code fails on MT5 ...

Noted with thanks
 

Hi

IMO this is the correct logic. Unless some other conditions are not overruling those rules this should work fine.

Of course you should use PERIOD_H1 instead of “60” – but either way the values should be correctly obtained and block trades at the moment of signal. Just make sure that you have updated data for this symbol and timeframe, it’s best to use this EA on the chart that data are from.

Have a nice day👍📊

 
Marzena Maria Szmit #:

Hi

IMO this is the correct logic. Unless some other conditions are not overruling those rules this should work fine.

Of course you should use PERIOD_H1 instead of “60” – but either way the values should be correctly obtained and block trades at the moment of signal. Just make sure that you have updated data for this symbol and timeframe, it’s best to use this EA on the chart that data are from.

Have a nice day👍📊

Hi Marzena, what is IMO ?

 
Chin Min Wan #:

Hi Marzena, what is IMO ?

In My Opinion.

 
Soewono Effendi #:

You got it right!

Bar0 is "forming candle", so HIGH, LOW and CLOSE are keep on changing / updating until this candle is then closed.

If no price changes, i.e. no new price tick arrives, then you might see only OPEN price, where HIGH, LOW and CLOSE might be all having the same price.

William Roeder #:
  1. Do not hard code constants. Use the proper symbols (PERIOD_H1). Code fails on MT5.

Marzena Maria Szmit #:


IMO this is the correct logic. Unless some other conditions are not overruling those rules this should work fine.


Hi, Soewono, William and Marzena, is there any command in mql4 to turn the Lo0 and Hi0 temporary into "invalid double value" ? If I set a condition that at any point of time if Lo0 and Hi0 are detected and equal to either open price (iOpen) or zero double value ? Additionally, the so-called "invalid double value" will not create any error to my EA so that my EA will function as usual but just temporary stops for a second, and the EA is just unable to do entry and exit only ... The moment Lo0 and Hi0 come back to the correct prices, then the EA can function correctly to do the entry and exit thereafter.

Hope you're able to advise me, thank you.

 
search for 'EMPTY_VALUE'
 
Soewono Effendi #:
search for 'EMPTY_VALUE'
I found the "EMPTY_VALUE" at https://docs.mql4.com/constants/namedconstants/otherconstants , from my understanding, it yet has a value of 2147483647 and not really absolutely "EMPTY_VALUE", am I right ? In my opinion, if it yet has a value, it doesn't fit the purpose, please advise, thanks.