my expert not working correctly

 

hi guys

i'm new in mt4 

i want an expert that:

if 2 candles in timeframe m1 are ascending and in timeframe m5 an ascending candle is forming then open a buy position

if in m5 ascending candle change to descending candle then close position

if 2 candles in timeframe m1 are descending and in timeframe m5 a Descending candle is forming then open a sell position

if in m5 descending candle change to ascending candle then close position

trade between 8am and 11pm

my code is here but just open buy position and never close it

pleas help me


//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2018, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+

#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

input double lot=0.4;
int BuyMagic=1474,SellMagic=3087;
string chkcandels;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {

   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {

   void OnTick()
     {
      int ordrsnd;
      if(iVolume(Symbol(),PERIOD_H1,0)<1 && Hour() <= 23 && Hour() >= 8)
        {
         close_pos();
         if(!chkTrade())
           {
            chkcandels=chkCandels();
            if(iClose(Symbol(),PERIOD_M5,0)>iOpen(Symbol(),PERIOD_M5,0) && chkcandels=="UP")
              {
               ordrsnd = OrderSend(Symbol(),OP_BUY,lot,Ask,5,Ask-50*Point,Ask+50*Point,"Afkariaus",BuyMagic,0,clrGreen);
              }
            if(iClose(Symbol(),PERIOD_M5,0)<iOpen(Symbol(),PERIOD_M5,0) && chkcandels=="DOWN")
              {
               ordrsnd = OrderSend(Symbol(),OP_SELL,lot,Bid,5,Bid-50*Point,Bid+50*Point,"Afkariaus",SellMagic,0,clrGreen);
              }
           }
        }
     }

//+------------------------------------------------------------------+
   bool chkTrade()
     {
      bool Orderslct;
      for(int i=OrdersTotal()-1; i>=0; i--)
        {
         Orderslct=OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         if((OrderMagicNumber() == 1474 && OrderSymbol() == Symbol()) || (OrderMagicNumber() == 3087 && OrderSymbol() == Symbol()))
            return true;
        }
      return false;
     }
//+------------------------------------------------------------------+
   string chkCandels()
     {

      if(iOpen(Symbol(),PERIOD_M1,1)>iClose(Symbol(),PERIOD_M1,1) && iOpen(Symbol(),PERIOD_M1,2)>iClose(Symbol(),PERIOD_M1,2))
         return "DOWN";
      if(iOpen(Symbol(),PERIOD_M1,1)<iClose(Symbol(),PERIOD_M1,1) && iOpen(Symbol(),PERIOD_M1,2)<iClose(Symbol(),PERIOD_M1,2))
         return "UP";
      else
         return "NOTHING";
     }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
   string chkOrders()
     {
      bool orderslct;
      for(int i = OrdersTotal()-1; i>=0; i--)
        {
         orderslct=OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         if(OrderType()==0 && OrderSymbol()==Symbol() && OrderMagicNumber()==BuyMagic)
            return "BUY";
         if(OrderType()==1 && OrderSymbol()==Symbol() && OrderMagicNumber()==SellMagic)
            return "SELL";
        }
      return "NOTHING";
     }
//+------------------------------------------------------------------+
   void close_pos()
     {
      bool ordrcls;
      if(iClose(Symbol(),PERIOD_M5,0)>iOpen(Symbol(),PERIOD_M5,0))
        {
         for(int i= OrdersTotal()-1; i>=0; i--)
           {
            if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
               if(OrderMagicNumber()==SellMagic)
                  ordrcls = OrderClose(i,OrderLots(),OrderClosePrice(),5,clrRed);
           }
        }
      if(iClose(Symbol(),PERIOD_M5,0)<iOpen(Symbol(),PERIOD_M5,0))
        {
         for(int i= OrdersTotal()-1; i>=0; i--)
           {
            if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
               if(OrderSymbol()==Symbol() && OrderMagicNumber()==BuyMagic)
                  ordrcls = OrderClose(i,OrderLots(),OrderClosePrice(),5,clrRed);
           }
        }
     }
//+------------------------------------------------------------------+
   
 
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.
 
is there anyone to help me?
 
afkariaus:

Do not double post!!

I have deleted your duplicate topic which incidentally, you placed in the wrong section again!

Keith Watford:
Topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.
 
  1.       if(iVolume(Symbol(),PERIOD_H1,0)<1

    For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart), volume is unreliable (miss ticks), Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
              New candle - MQL4 programming forum #3 2014.04.04

    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
              Running EA once at the start of each bar - MQL4 programming forum 2011.05.06

  2. ordrsnd = OrderSend(Symbol(),OP_BUY,lot,Ask,5,Ask-50*Point,Ask+50*Point
    You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit and open at the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY (OANDA) shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).

  3. 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.05.20
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles 25 March 2014

  4. iClose(Symbol(),PERIOD_M5,0)

    On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26.4 2019.05.20

 
William Roeder:
  1. For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart), volume is unreliable (miss ticks), Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
              New candle - MQL4 programming forum #3 2014.04.04

    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
              Running EA once at the start of each bar - MQL4 programming forum 2011.05.06

  2. You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit and open at the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY (OANDA) shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).

  3. 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.05.20
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles 25 March 2014

  4. On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
    zDownload history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26.4 2019.05.20So what should i d

So What should i do?

where is my mistake?

 
Comments that do not relate to this topic, have been moved to "Off Topic Posts".