One position per hour. If one close, another one can be opened (condition is true). Just limit one position on the same time (hour)

 

Hi everyone,

As described in the title, I am having a bit of trouble when I try to control my EA to only open one position per hour.

I just want one position per hour. If for some reason this position is closed, another one can be opened (if the condition is still true) in the same period (hour).

I have tried many ideas but it doesn't work. I would be very grateful if anyone help me to solve this problem.

 
Your topic has been moved to the section: Expert Advisors and Automated Trading — In the future, please consider which section is most appropriate for your query.
 
  • If you show your attempts and describe your problem clearly, you will most probably receive an answer from the community. Use the CODE button (Alt-S) when inserting code.
  • You can also search the Codebase for something similar, so you will have to do your own research.
  • Finally, you also have the option to hire a programmer in the Freelance section.
 
Ngọc Dũng:

As described in the title, I am having a bit of trouble when I try to control my EA to only open one position per hour.

I just want one position per hour. If for some reason this position is closed, another one can be opened (if the condition is still true) in the same period (hour).

I have tried many ideas but it doesn't work. I would be very grateful if anyone help me to solve this problem.

  1. 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)

  2. When in doubt, think!

    void OnTick(){
       static datetime canOpen=0;
       datetime now=TimeCurrent(); 
       if(now < canOpen) return;
       ⋮
       if(!openConditions) return
       OrderSend(…)
       datetime topHour - now - now % 3600;
       canOpen = topHour + 3600; // Next hour
  3. Instead of restricting open to once an hour, perhaps you should restrict to a change in condition.
              Too many orders - MQL4 programming forum #1 (2017)

 
Fernando Carreiro #:
  • If you show your attempts and describe your problem clearly, you will most probably receive an answer from the community. Use the CODE button (Alt-S) when inserting code.
  • You can also search the Codebase for something similar, so you will have to do your own research.
  • Finally, you also have the option to hire a programmer in the Freelance section.

Hi, this is my first time posting on the forum. Thank you for guidance. I will notice next time

 
William Roeder #:
  1. 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)

  2. When in doubt, think!

  3. Instead of restricting open to once an hour, perhaps you should restrict to a change in condition.
              Too many orders - MQL4 programming forum #1 (2017)

Thanks for your support. After posting on the forum, I had an idea. I create a function that counts the number of positions opened per period as follows

#include <Trade\PositionInfo.mqh>
CPositionInfo PositionInfo;

int LimitPositionPerTime(string symbol_name, ENUM_TIMEFRAMES period_time)
{
   int Count= 0;

   for(int i = 0; i <= PositionsTotal(); i++)
      {
         if(PositionInfo.SelectByIndex(i) == false)                             continue;
         if(PositionGetString(POSITION_SYMBOL) != symbol_name)                  continue;
         if(PositionGetInteger(POSITION_TIME) > iTime(_Symbol, period_time, 0)) Count++;
      }
   return Count;
}

And I called this function as follows. If it counts there is one open position (>0). It will not continue to open more positions until the next period. And it worked for me.

if(LimitPositionPerTime(_Symbol, PERIOD_D1)>0) return;
Reason: