Time Filter

 

HI i have this time filter and i want to close all trades when at the specified time but doesn't happen .... im doing something wrong .... the leaves the positions open .... i want all position close


can someone help ?


regards


bool TimeFilter()
{
   if (!UseTime) return(true);
   
   string StartTime, EndTime;
   
   datetime TimeStart = StringToTime(TimeToString(TimeCurrent(), TIME_DATE) + " " + timestart_);
   datetime TimeEnd = StringToTime(TimeToString(TimeCurrent(), TIME_DATE) + " " + timeend_);
   if (TimeEnd == TimeStart) return(true);
   else if (TimeEnd < TimeStart) 
   {
      if (TimeCurrent() >= TimeStart || TimeCurrent() < TimeEnd) return(true);
   }
   else if (TimeEnd > TimeStart) 
   {
      if (TimeCurrent() >= TimeStart && TimeCurrent() < TimeEnd) return(true);
   }
   
   return(false);
}


int CountBuys() {
   int count = 0;
   for (int i = OrdersTotal() - 1; i >= 0; i--) {
       ticket=OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
         if (OrderType() == OP_BUY) count++;
   }
   return (count);
}

int CountSells() {
   int count = 0;
   for (int i = OrdersTotal() - 1; i >= 0; i--) {
       ticket=OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
         if (OrderType() == OP_SELL) count++;
   }
   return (count);
}
bool NewBar()
  {
   static datetime lastbar;
   datetime curbar=Time[0];
   if(lastbar!=curbar)
     {
      lastbar=curbar;
      return (true);
     }
   else
     {
      return(false);
     }
  }



   
int CountTrades() {
   int count = 0;
   for (int trade = OrdersTotal() - 1; trade >= 0; trade--) {
      ticket=OrderSelect(trade, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
         if (OrderType() == OP_SELL || OrderType() == OP_BUY) count++;
   }
   return (count);
 
living the: . im doing something wrong .... t
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2019)
              Messages Editor

  2.        ticket=OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

    Perhaps you should read the manual. OrderSelect does not return a ticket number.
       How To Ask Questions The Smart Way. (2004)
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

    Check your return codes, and report your errors (including market prices and your variables). Don't look at GLE/LE unless you have an error. Don't just silence the compiler (MT5/MT4+strict), it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 programming forum (2012)
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles (2014)

  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)

 
William Roeder #:
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2019)
              Messages Editor

  2. Perhaps you should read the manual. OrderSelect does not return a ticket number.
       How To Ask Questions The Smart Way. (2004)
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

    Check your return codes, and report your errors (including market prices and your variables). Don't look at GLE/LE unless you have an error. Don't just silence the compiler (MT5/MT4+strict), it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 programming forum (2012)
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles (2014)

  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)

Edited the post .... but still i don't know what to modify in the code ....

 
living the #: Edited the post .... but still i don't know what to modify in the code ....

Two days ago you left feedback for a developer making modifications to code. It is safe to assume then, that you don't know how to code and therefore are unable to understand the suggestions that William has provided.

You not having this understanding means that we are unable to communicate to you what you have to fix. So I suggest, you contact your freelance developer and have it fix it for you.

 
Fernando Carreiro #:

Two days ago you left feedback for a developer making modifications to code. It is safe to assume then, that you don't know how to code and therefore are unable to understand the suggestions that William has provided.

You not having this understanding means that we are unable to communicate to you what you have to fix. So I suggest, you contact your freelance developer and have it fix it for you.

I didn't  ask you to give me life lessons .... if you can help with this code its much appreciated 


regards

 
living the #: if you can help with this code its much appreciated

There is no helping you. If you don't learn MQL4/5, there is no common language for us to communicate. If we tell you what you need, you can't code it. If we give you the code, you don't know how to integrate it into your code.

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)

or pay (Freelance) someone to code it. Top of every page is the link Freelance.
          Hiring to write script - General - MQL5 programming forum (2019)

 
living the:

HI i have this time filter and i want to close all trades when at the specified time but doesn't happen .... im doing something wrong .... the leaves the positions open .... i want all position close

can someone help ?

Post all relevant code if you are asking for help.

There is nothing in your posted code that attempts to close any trades at a specified time .

We can assume that timestart_ and timeend_ are string input variables, but we cannot know for sure.


string StartTime, EndTime;

Remove unused variables, especially when there are used variable with similar names.


You have 3 functions to count buy, sell or all trades, this is probably inefficient. You could create a single function and pass variables by reference.

In all 3 of those functions you use

      if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)

Why use the 2 lines? You only need 1.

ticket=OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

Variable ticket is not declared anywhere and seems to me to be a strange name for a boolean variable. Also nothing is done with the result, so obviously only used to prevent the compiler giving a warning. Bad idea! The compiler warning can help you. Don't continue if the OrderSelect() fails.



Topics concerning MT4 and MQL4 have their own section.

In future please post in the correct section.

I have moved your topic to the MQL4 and Metatrader 4 section.

 
Keith Watford #:

Post all relevant code if you are asking for help.

There is nothing in your posted code that attempts to close any trades at a specified time .

We can assume that timestart_ and timeend_ are string input variables, but we cannot know for sure.


Remove unused variables, especially when there are used variable with similar names.


You have 3 functions to count buy, sell or all trades, this is probably inefficient. You could create a single function and pass variables by reference.

In all 3 of those functions you use

Why use the 2 lines? You only need 1.

Variable ticket is not declared anywhere and seems to me to be a strange name for a boolean variable. Also nothing is done with the result, so obviously only used to prevent the compiler giving a warning. Bad idea! The compiler warning can help you. Don't continue if the OrderSelect() fails.



Topics concerning MT4 and MQL4 have their own section.

In future please post in the correct section.

I have moved your topic to the MQL4 and Metatrader 4 section.




Solved 


Thank you !