How to run MQL4 or MQL5 code only one time for each bar

Поділитися
Вставка
  • Опубліковано 23 сер 2024
  • This is something I use all the time and I give a quick explanation in other videos but don't cover the topic completely.
    This video is a more complete explanation and the code is an improvement on the simple code in my other videos.
    I will be referring back to this video in future tutorials.
    If you are looking for a broker with good leverage, spreads and product range and want to help out our channel try OX Securities. We do receive commission from this broker.
    Demo Account: orchardforex.c...
    Live Account: orchardforex.c...
    Web: orchardforex.c...
    Discord: / discord
    Twitter: / orchardforex
    Telegram: t.me/orchardforex

КОМЕНТАРІ • 44

  • @dashoshea9540
    @dashoshea9540 Рік тому +1

    Great clear explanations, even for complete beginners like me. Without great clear explanations like this I wouldn't have found the confidence to start to start learning MT5. Ive scoured the internet and Its extremely hard to get great material for MQL5 learning online, and this stuff is great quality. Each step is well explained for those who have never attempted anything like coding before. Your efforts in providing this material are much appreciated mate, Thank you

  • @mike33812
    @mike33812 Рік тому

    Thank you so much for this wonderful video. Not only great coding but also clear explanation. Thank you

  • @yaserazizzadeh6925
    @yaserazizzadeh6925 Рік тому

    Tnx for video 👍 very useful. I learn new stuff everyday from your videos.

  • @reniervaneeden8682
    @reniervaneeden8682 Рік тому +1

    Thank you for this video, however in the OnInit() where the call NewBar(True) is made, the previous_time = 0, is set to D'1970.01.01.00:00:00, that differs with the current_time resulting in returning true. So OnInit() doesn't wait for the candle to finish, it returns true.

    • @OrchardForex
      @OrchardForex  Рік тому +1

      Yes. And what would happen next?

    • @reniervaneeden8682
      @reniervaneeden8682 Рік тому +1

      When I enter OnInit() the previous_time is set to the current_time and NewBar() returns true, which is correct
      When I enter OnTick() it picked up a later candle changed previous_time and NewBar() returned true.
      The 2nd time I enter OnTick(), only then did NewBar() returned false.
      Now I replayed this a few times and the same results occurred every time.
      My Strategic Tester is set for 1 week back testing on a EURUSD M30 chart@@OrchardForex

    • @reniervaneeden8682
      @reniervaneeden8682 Рік тому

      It seems that the problem is with the Strategy Tester and not the code, thank you for your instructional videos I have learned quite a lot from them.

  • @reach_perumal
    @reach_perumal 2 роки тому +1

    Great content as always ❤️

  • @PedroLeBoursicoteur
    @PedroLeBoursicoteur 8 місяців тому

    Hi Orchard,
    Thank you very much for the content of your video, we can tell you're passionate about programming, I've just joined your discord, is it possible to have a link to download the code for this video?

  • @LuELR22
    @LuELR22 8 місяців тому

    Hi Mr. Orchard, do you freelance code? I have seen your videos and even though I have learned and solved some "basic" problems with my codes I'm still quite newbie in this field.

  • @MQLWorld
    @MQLWorld Рік тому

    tnks a lot!

  • @simongraham2516
    @simongraham2516 7 місяців тому

    I have a question regarding CPU optimization.
    What I user NewBar() multiple times in an indicator (for different timeframes), and use that indicator on several (lets say 10) windows in the MetaTrader. Maybe I would even have a few MT open.
    Let's say I kick off my indicator code only on new bar.
    That means that NewBar() will be tested for every new tick, on every symbol/window in the few MT I have opened. Doesn't that use too much CPU?
    HOW CAN I OPTIMIZE THAT?
    Also is there a way to monitor CPU usage or RAM usage by a MT advisor or indicator that I make?

  • @user-ov9ec5jf5z
    @user-ov9ec5jf5z 10 місяців тому

    Hello professor, many thanks for teaching mql programming. I have a question, why you don't use something like this instead? "if(iVolume(Symbol(), ChartPeriod(0), 0)

  • @eversonmay
    @eversonmay 2 роки тому +1

    webcam - nice touch

  • @cooldeal
    @cooldeal 2 роки тому

    If you call NewBar without passing any variable it will not pass the first if statement and will always return without comparing the current and previous time

    • @OrchardForex
      @OrchardForex  2 роки тому

      That's correct. Call once at the beginning of OnTick passing in true and all other calls pass in nothing.

  • @franciscojunior6841
    @franciscojunior6841 2 роки тому

    Hey Professor,
    Could you give us a lesson on how to get information from MT5 into MT4? Also could you give us some examples how to use "continue" ?
    Thanks

    • @OrchardForex
      @OrchardForex  2 роки тому +1

      What sort of information would you want to transfer? Continue is easy, it just goes to the end of the loop and starts the next iteration (or you could say to the beginning, just depends on how you view it). In a for loop it will execute the iterator then go on to the exit condition, in a while loop it goes straight to the exit condition.

    • @franciscojunior6841
      @franciscojunior6841 2 роки тому

      @@OrchardForex My Idea was transfer informations from the book of offer built in MT5, so called DOM or Depth Of the Market..
      Thanks for your explanation 👍I really get it.

  • @joheinlambert1136
    @joheinlambert1136 2 роки тому

    Hi Sir i like your video content.Can you please make a video on a robot that use the zig zag indicator

  • @sermongate7994
    @sermongate7994 2 роки тому

    hi thanks for the great contents you provide. can you please show us how to code a simple MT5 copy trader EA. we will appreciate a lot. thanks

  • @DestoTrading
    @DestoTrading Рік тому

    Hi, i tried this in order to stop the EA from opening a position in the same candle if another position was closed in that same candle and it worked. However, the EA stopped opening some of the trades it is supposed to. Any suggestion how to fix that?

    • @OrchardForex
      @OrchardForex  Рік тому

      You probably have a strategy that tries to open mid candle. Then this isn't the right approach for you.

  • @rickyehmer2889
    @rickyehmer2889 2 роки тому

    Great video, just curious about declaring static variables.. I noticed in your video you’ve initialised previous_time to 0 in the first call, if initial values are not specified are they by default set at 0?

    • @OrchardForex
      @OrchardForex  2 роки тому +2

      If variables are not initialised they could contain anything that was in that memory space previously.

    • @rickyehmer2889
      @rickyehmer2889 2 роки тому

      @@OrchardForex okay thanks for letting me know

  • @greydragon100
    @greydragon100 2 роки тому

    Great content, just wondering how you will prevent a market close error on the daily timeframe if your broker does swap fee calculations during 23:58 to 00:02? If this new bar function is called for trade openings you will never place a trade due to the market close error, daily new bar being triggered at 00:00

    • @greydragon100
      @greydragon100 2 роки тому

      A possible solution I was pondering was to check if your brokers trade session is open using the SymbolInfoSessionTrade() function before running the IsNewBar function and then forcing a new bar calc on timeframes larger or equal to the daily, let's say every 12 hr timeframe...

    • @greydragon100
      @greydragon100 2 роки тому

      Meaning a daily will get processed twice a day at 00:00 and 12:00. If your broker does swap fees the 00:00 will get cancelled by the SymbolInfoSessionTrade function calc

    • @OrchardForex
      @OrchardForex  2 роки тому

      There is nothing you can do while the market is closed so the easy way out is to test for closed market in OnTick, even before testing for NewBar, then just return if market is closed.

    • @shaunbosch2262
      @shaunbosch2262 2 роки тому

      @@OrchardForex Yes agreed this will stop the market close error, but if a new order only gets executed on a new bar, new bar for the daily is at 00:00, meaning a daily will never place orders... A new bar calc for the daily, weekly and monthly needs to be checked on the 12 hr timeframe for it to work.

    • @OrchardForex
      @OrchardForex  2 роки тому

      @@shaunbosch2262 No, this works and new orders will be placed as soon as the market opens.

  • @oluagbaje8622
    @oluagbaje8622 2 роки тому

    Hello prof, you can help with a code for searching signal at the birth of a new bar on the current timeframe but if no positions were opened within one hour, signal should be searched at 15M timeframe.
    also is there a way to make ChartSetSymbolPeriod synchronous as I’ve read it’s asynchronous.
    Thanks.

    • @OrchardForex
      @OrchardForex  2 роки тому +1

      I don't have any videos like that. ChartSetSymbolPeriod is async. You will just have to check in a loop for the change.

  • @ultrasounds1842
    @ultrasounds1842 2 роки тому

    How can we make HFT EA please

  • @shaunbosch2262
    @shaunbosch2262 2 роки тому

    This seems to work for daily market close error, inp_timeframe being an input to the EA
    ENUM_DAY_OF_WEEK day_of_week;
    //+------------------------------------------------------------------+
    //| Function: Execute code only on a new bar, and not every tick |
    //+------------------------------------------------------------------+
    bool isNewBar(int symbol_loop)
    {
    return(Trade_Session_Open());
    static datetime last_time = 0;
    ENUM_TIMEFRAMES timeframe = inp_timeframe;
    if(timeframe == PERIOD_D1 || timeframe == PERIOD_W1 || timeframe == PERIOD_MN1)
    {
    timeframe = PERIOD_H12;
    }
    datetime lastbar_time = (datetime) SeriesInfoInteger(symbol_array[symbol_loop], timeframe, SERIES_LASTBAR_DATE);
    if(last_time != lastbar_time)
    {
    last_time = lastbar_time;
    return(true);
    }
    return(false);
    }
    //+------------------------------------------------------------------+
    //| Function: Check if trade session is open and excl. Sat and Sun |
    //+------------------------------------------------------------------+
    bool Trade_Session_Open()
    {
    datetime time_now = TimeCurrent();
    MqlDateTime time;
    TimeToStruct(time_now, time);
    uint week_day_now = time.day_of_week;
    uint seconds_now = (time.hour * 3600) + (time.min * 60) + time.sec;
    if(week_day_now == 0)
    day_of_week = SUNDAY;
    if(week_day_now == 1)
    day_of_week = MONDAY;
    if(week_day_now == 2)
    day_of_week = TUESDAY;
    if(week_day_now == 3)
    day_of_week = WEDNESDAY;
    if(week_day_now == 4)
    day_of_week = THURSDAY;
    if(week_day_now == 5)
    day_of_week = FRIDAY;
    if(week_day_now == 6)
    day_of_week = SATURDAY;
    datetime from, to;
    uint session = 0;
    while(SymbolInfoSessionTrade(_Symbol, day_of_week, session, from, to))
    {
    session++;
    }
    uint trade_session_open_seconds = uint(from) + 60;
    uint trade_session_close_seconds = uint(to) - 60;
    if(trade_session_open_seconds < seconds_now && trade_session_close_seconds > seconds_now && week_day_now >= 1 && week_day_now

    • @OrchardForex
      @OrchardForex  2 роки тому +1

      If you prefer this to my earlier suggestion then that's your choice but for anyone else reading this simple solutions are generally better. I recommend at the beginning of OnTick add one line to test for market open like
      if (SymbolInfoInteger(Symbol(), SYMBOL_TRADE_MODE) != SYMBOL_TRADE_MODE_FULL) return;
      bool newBar = NewBar( true );
      You can change or add more conditions as you like.

    • @greydragon100
      @greydragon100 2 роки тому

      Thanks 👌🏻

    • @shaunbosch2262
      @shaunbosch2262 2 роки тому

      @@OrchardForex Much simpler solution and works great thank you very much