Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 1956

William Roeder  
TeoDeo #:! Can you tell me why my  integer 'Prevent Order' is not being revalued to '0' in the bottom section? 
   double LineCeiling    = iMA(NULL, 0, 10/3, 0, MODE_LWMA, PRICE_HIGH + (PRICE_HIGH / 3000), 0);
  1. You are using a LWMA period of three.
              On MT4 v434, division quotient don't give floating point values(Bug??) - MQL4 programming forum (2012)
              build 2504 & 2506 Bug - General - MQL5 programming forum - Page 2 #17 (2020)

  2. The ENUM_APPLIED_PRICE PRICE_HIGH is two. Two+two/3000 =two=PRICE_HIGH

  3. Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
              Code debugging - Developing programs - MetaEditor Help
              Error Handling and Logging in MQL5 - MQL5 Articles (2015)
              Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
              Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)

Iggor995  
Hey there I'd like to purchase Trader advisor for a friend, his birthday is coming and id like to surprise him with 1year rental how do i purchase it for him? Thank you! 
memo_mqlcom  

Hello!

I'm trying to write a function that allows for trading within 5 min before the candle close (on D1).

After hours of head scratching, I noticed that Strategy tester is NOT evaluating all ticks toward the end of the candle close. As an example, see this transition from 1/4/21 to 1/5/21. I use Tick-level data downloaded from Dukascopy.

    0   12:22:26.043    2021.01.04 22:00:00  sandbox_QA EURUSD,Daily: Alert: === trade_window_start=86100 time_in_candle=79200 sec_in_day=86400
    0   12:22:26.043    2021.01.05 00:00:00  sandbox_QA EURUSD,Daily: Alert: === trade_window_start=86100 time_in_candle=0 sec_in_day=86400

24 hrs is 86400 seconds, so I want to start trading window after the 86100 th sec (5 min before candle close), but the latest recorded tick was "79200", so it doesn't work.

Here's the code to replicate the issue and I'd appreciate your help! Thanks :)

    void OnTick()
      {
       bool trading_window = isWithinTradeHours();
       return;
      }
     
    bool isWithinTradeHours()
      {
       int time_in_candle = int(TimeCurrent()-Time[0]);
       int sec_in_day = 24 * 60 * 60;
       int trade_window_start = sec_in_day - (5 * 60); // 5min before candle close
       
       // print every 100 sec to reduce the size of the log
       if((time_in_candle % 100) == 0)
         {
          Alert("=== trade_window_start=", trade_window_start, " time_in_candle=", time_in_candle, " sec_in_day=", sec_in_day);
          Sleep(100);
         }
     
       if((time_in_candle > trade_window_start))
         {
          Alert("====== Trading Time: ", TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS), " Time in candle:", int(TimeCurrent()-Time[0]));
          Sleep(100);
          return true;
         }
       else
         {
          return false;
         }
      }


I'm running with "Every Tick":

William Roeder  
memo_mqlcom #: NOT evaluating all ticks toward the end of the candle close.
       if((time_in_candle % 100) == 0)

This assumes that there are ticks during each second of the day — they don't. What if there are no ticks during a specific period? There can be minutes between ticks during the Asian session.

static datetime lastAlert=0;
datetime prevAlert=lastAlert; lastAlert=TimeCurrent();
if(lastAlert - prevAlert >= 100) …
memo_mqlcom  

Thank you @William Roeder, I believe you identified the root cause! I wasn't even considering the possibility of 'no Ticks' within the time window that I define.

That said, I ran a quick test without the Alerts for EURUSD, and get almost no Ticks in unless I extend the trade window as large as an hour before the candle close -- which is not realistic (?)

I tried using OnTimer() to use time as a trigger, and it looks great, but unfortunately it doesn't work with StrategyTester :(

Any other ideas that I can try?

TeoDeo  

Hello! I am having trouble using Array Elements in Boolean Inequalities for If Statements.

I have 14 moving averages. Hardcoded, they crash MT4.

In an array they work. I can't use iMAOnArray because there is no Price parameter, which is absolutely necessary.

When I compare hardcoded MA's in an inequality statement, in this case for a If parameter, it is equated as True or False.

But when I compare MA's as Array Elements the same way, there is no pass.

So I'm at an impass. I can't hardcode the MA's because it crashes MT4. I can't use iMAOnArray because there is no Price parameter. And I can't use the Array Elements in inequalities.

I've tried parsing the Array Elements into integers, and no go.

I've tried equating a new variable to the Arrayed moving average, and no go.

What do I do?

TeoDeo  
William Roeder #:
  1. You are using a LWMA period of three.
              On MT4 v434, division quotient don't give floating point values(Bug??) - MQL4 programming forum (2012)
              build 2504 & 2506 Bug - General - MQL5 programming forum - Page 2 #17 (2020)

  2. The ENUM_APPLIED_PRICE PRICE_HIGH is two. Two+two/3000 =two=PRICE_HIGH

  3. Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
              Code debugging - Developing programs - MetaEditor Help
              Error Handling and Logging in MQL5 - MQL5 Articles (2015)
              Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
              Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)

Thank you!