Open 1 order on each new bar

 
Hi
Firstly, sorry for my bad English.
Im trying to make my EA. But im a newbie MT4 code, so it's hard for me. Would you please help me?
These are what im trying to to:
- It works on many pairs same time
- If the Buy (or Sell) condition is right, it will open 1 order per 1 bar (new bar new 1 order)
- If Close condition is right, it will close all orders of the pair that opened order

- After close order, it can open new order after that (include the bar that close order)

:)

 
jordantran: Would you please help me?
  1. Help you with what? You haven't stated a problem, you stated a want.
    You have only four choices:
    1. Search for it,
    2. Beg at Will code your trading systems for free - Free Expert Advisors - Trading Systems - MQL5 programming forum, or Coding help - MQL4 and MetaTrader 4 - MQL4 programming forum, or Need help with coding - General - MQL5 programming forum, or Free MQL4 To MQL5 Converter - General - MQL5 programming forum, or Requests & Ideas (MQL5 only!).
    3. learn to code it. 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 yours.
    4. or pay (Freelance) someone to code it.
    We're not going to code it for you (although it could happen if you are lucky or the problem is interesting.) We are willing to help you when you post your attempt (using CODE button) and state the nature of your problem.
              No free help
              urgent help.

  2. It works on many pairs same time
    No need for that. Code it to trade the current pair, put it on multiple charts to trade the others.
 

Hi whroeder1,

Thanks for your reply. And sorry about this. I know there are no free help. But in my case, I have to try.

This is my code. It can only open 1 order and alway miss order

void OnTick()

  {

//---

   if(OrdersTotal() <1)

   {

      if(Close[1] > Open[1] && Close[0] > Open[0]) {ticket=OrderSend(_Symbol,OP_BUY,0.01,Ask,30,0,0,NULL,MagicNumber,0,Blue); return;}

      if(Close[1] < Open[1] && Close[0] < Open[0]) {ticket=OrderSend(_Symbol,OP_SELL,0.01,Bid,30,0,0,NULL,MagicNumber,0,Red); return;}

   }

   else

   {

      if(IsNewBar() && TotalOpenOrders())

      {

         if(Close[1] > Open[1] && Close[0] > Open[0]) {ticket=OrderSend(_Symbol,OP_BUY,0.01,Ask,30,0,0,NULL,MagicNumber,0,Blue); return;}

         if(Close[1] < Open[1] && Close[0] < Open[0]) {ticket=OrderSend(_Symbol,OP_SELL,0.01,Bid,30,0,0,NULL,MagicNumber,0,Red); return;}

         Comment("Total Open Order: ",TotalOpenOrders(), " - ", _Symbol);       

      }

   }

   return;

  }

//+------------------------------------------------------------------+



// Check if there is a new bar

bool IsNewBar()   

{        

      static datetime RegBarTime=0;

      datetime ThisBarTime = Time[0];

      

      if (ThisBarTime == RegBarTime)

      {

         return(false);

      }

      else

      {

         RegBarTime = ThisBarTime;

         return(true);

      }

}  

 



// Returns the number of total open orders for this Symbol and MagicNumber

int TotalOpenOrders()

{

   int total_orders = 0;

   

   for(int order = 0; order < OrdersTotal(); order++) 

   {

      if(OrderSelect(order,SELECT_BY_POS,MODE_TRADES)==false) break; 

      if(OrderMagicNumber() == MagicNumber && OrderSymbol() == _Symbol)

         {

            total_orders++;

         }

   }

   return(total_orders);

}

 
jordantran:
Hi
Firstly, sorry for my bad English.
Im trying to make my EA. But im a newbie MT4 code, so it's hard for me. Would you please help me?
These are what im trying to to:
- It works on many pairs same time
- If the Buy (or Sell) condition is right, it will open 1 order per 1 bar (new bar new 1 order)
- If Close condition is right, it will close all orders of the pair that opened order

- After close order, it can open new order after that (include the bar that close order)

:)

jordantran:
Hi
Firstly, sorry for my bad English.
Im trying to make my EA. But im a newbie MT4 code, so it's hard for me. Would you please help me?
These are what im trying to to:
- It works on many pairs same time
- If the Buy (or Sell) condition is right, it will open 1 order per 1 bar (new bar new 1 order)
- If Close condition is right, it will close all orders of the pair that opened order

- After close order, it can open new order after that (include the bar that close order)

:)

bool NewBar()
{
   static datetime lastbar;
   datetime curbar = Time[0];
   if(lastbar!=curbar)
   {
      lastbar=curbar;
      return (true);
   }
   else
   {
      return(false);
   }

if(NewBar())
 {
 // Your Code
  }
 
jordantran:
Hi
Firstly, sorry for my bad English.
Im trying to make my EA. But im a newbie MT4 code, so it's hard for me. Would you please help me?
These are what im trying to to:
- It works on many pairs same time
- If the Buy (or Sell) condition is right, it will open 1 order per 1 bar (new bar new 1 order)
- If Close condition is right, it will close all orders of the pair that opened order

- After close order, it can open new order after that (include the bar that close order)

:)

int prevtime; // at the top


if(Time[0] == prevtime)  return(0);
        prevtime = Time[0];
// Your Code..
 
Mehmet Bastem:
Thanks for your help. But it doesn't work with me.
Reason: