Why isn't this working...

 

How is it possible that the alert "total trades" is returning '3', but "have we had two trades?" is returning 'false'. This makes no sense... my head hurts. Please help.

   bool haveWeHadTwoTrades;
   int totalTrades = 0;
   
   for(int i = OrdersHistoryTotal()-1 ; i >= 0 ; i--)
   {
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
      {
         if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic_Number && TimeLocal() > iTime(Symbol(),Validation_TimeFrame,0)) totalTrades = totalTrades + 1;
            if(totalTrades >= 2) haveWeHadTwoTrades==true;
      }
   }
   Alert("total trades: ", totalTrades);
   Alert("have we had two trades? :", haveWeHadTwoTrades);
 
dreaming MT6 with easy language...
 
JoeMSC.FX:

How is it possible that the alert "total trades" is returning '3', but "have we had two trades?" is returning 'false'. This makes no sense... my head hurts. Please help.

You are checking if it is BIGGER OR equals:

if(totalTrades >= 2) haveWeHadTwoTrades=true;

If you want to check if its exactly 2, you have to use ==:

if(totalTrades == 2) haveWeHadTwoTrades=true;

Read the documentation or some tutorials first before coding or you'll encounter many issues like this along the road:

https://www.mql5.com/en/docs/basis/operations/relation

Documentation on MQL5: Language Basics / Operations and Expressions / Operations of Relation
Documentation on MQL5: Language Basics / Operations and Expressions / Operations of Relation
  • www.mql5.com
Operations of Relation - Operations and Expressions - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Jox90 #:
dreaming MT6 with easy language...

It's a balance, if a language is too simplistic then it would prevent the programmers from doing complex stuff,
I'm glad they designed it like C++, although is not easy for beginner developers, it gives those that know how
to code unlimited potential and I still miss some C++'s features like metaprograming.

 
  1. if(totalTrades >= 2) haveWeHadTwoTrades==true;
    haveWeHadTwoTrades has no initial value; result meaningless.
  2. That's not an assignment; bool value not used.

  3. Compute your result after you count all orders.

    bool haveWeHadTwoTrades=totalTrades >= 2;

 
Alexandre Borela #:

It's a balance, if a language is too simplistic then it would prevent the programmers from doing complex stuff,
I'm glad they designed it like C++, although is not easy for beginner developers, it gives those that know how
to code unlimited potential and I still miss some C++'s features like metaprograming.

yeah but there are some who just want to be traders and not expert programmers...for me it should maintain mql language as you said you can do everything, but it should integrate easy language scripts, in fact there are platforms that were not born with easy language but it has been integrated.
 
Jox90 #:
yeah but there are some who just want to be traders and not expert programmers...for me it should maintain mql language as you said you can do everything, but it should integrate easy language scripts, in fact there are platforms that were not born with easy language but it has been integrated.

They are already doing this, there's python:

https://www.mql5.com/en/docs/integration/python_metatrader5

Documentation on MQL5: Integration / MetaTrader for Python
Documentation on MQL5: Integration / MetaTrader for Python
  • www.mql5.com
MetaTrader for Python - Integration - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Alexandre Borela #:

You are checking if it is BIGGER OR equals:

If you want to check if its exactly 2, you have to use ==:

Read the documentation or some tutorials first before coding or you'll encounter many issues like this along the road:

https://www.mql5.com/en/docs/basis/operations/relation

I understand this, the issues is that the bool haveWeHadTwoTrades is returning false when totalTrades is more than or equal to 2, I don't understand how this is possible.
 

Thanks for everyone's comments, here's the solution I used:


  bool HaveWeHadTwoTrades(int Magic_Number, ENUM_TIMEFRAMES Validation_TimeFrame)
   {
   
      int totalTrades;
            
      for(int i = OrdersHistoryTotal()-1 ; i >= 0 ; i--)
      {
         if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
         {
            if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic_Number && OrderOpenTime() > iTime(Symbol(),Validation_TimeFrame,0)) totalTrades = totalTrades + 1;
         }
      }
      if(totalTrades == 2) return true;
         return false;
   }