Risk management


Good day, I was wondering if anyone can help me, I am developing an EA and it's almost done but I am struggling with one thing: I need a working if statment that can check that

A) the current local time is not 9AM, or 1PM or 4PM,

B) check that there is only one or less active trade on the currency,

C) that the position type is a sell or a buy

D) if a sell and the current price is equal or greater than the open price (meaning its a loss) it must close the trade

E) and if the position type is abuy and the current price is smaller or equal to the open price (also meaning a loss) it must close the trade.

Below is my code can someone please help me take a look at it and help me fix it to do the above mentioned things, many thanks in advance :)

#include <Trade\Trade.mqh>
CTrade trade;
void OnTick()
   datetime time=TimeLocal(); //getting local time
   string HoursAndMin=TimeToString(time,TIME_MINUTES);  //format the time and create a string
   double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits); //get the ask price
   double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits); //get the bid price
   long PositionDirection = PositionGetInteger(POSITION_TYPE); //get the position type
   trade.Buy(0.02,_Symbol,Ask,Ask-170 * _Point,Ask+200 * _Point,NULL); 
   trade.Sell(0.02,_Symbol,Bid,Bid+170 * _Point,Bid-200 * _Point,NULL);

            if(StringSubstr(HoursAndMin,0,5)!="09:00" || StringSubstr(HoursAndMin,0,5)!="13:00"
               || StringSubstr(HoursAndMin,0,5)!="16:00" && PositionsTotal() =<1 &&
               PositionDirection == POSITION_TYPE_SELL
               && PositionGetDouble(POSITION_PRICE_CURRENT)>= PositionGetDouble(POSITION_PRICE_OPEN))
               else if (StringSubstr(HoursAndMin,0,5)!="09:00" || StringSubstr(HoursAndMin,0,5)!="13:00"
               || StringSubstr(HoursAndMin,0,5)!="16:00" && PositionsTotal() =<1 && PositionDirection == POSITION_TYPE_BUY
               && PositionGetDouble(POSITION_PRICE_CURRENT)<= PositionGetDouble(POSITION_PRICE_OPEN))
                  ClosePosition(); // calling the close position function
 void ClosePosition() // function to close position
      //count down till there are no open position left
      for (int i = PositionsTotal ()-1; i>=0; i--)
         //get the ticket number for the current position
         int ticket = PositionGetTicket(i);


Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
  • www.mql5.com
Position Properties - Trade Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5

1. I highly recommend using the MQL5 Wizard:

- in this way you will get the correct file (blank).

2. Please create a new file using the MQL5 Wizard and I will show you (step by step) how to code the Expert Advisor correctly.