Pls help me with this simple EA! Particularly, Buy oder function

 

I created a simple EA base on RSI and CCi! Besides, this EA will open order at bar's open price and close order at close price of the same bar or open price of next bar! 

However, when i backtest it only work with sell order! Buy order didn't work

Pls help me

//+------------------------------------------------------------------+
//|                                                        test1.mq4 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+

extern int  maxmp = 3;
extern double lot = 0.01,
              mp = 2.0,
              rsihigh = 70.0,
              rsilow = 30.0,
              ccihigh = 195.0,
              ccilow = -195.0;
double lota = lot;
double lotb = lot * (pow(mp,maxmp));

   



                         
void OnTick()
  {
//---
  if (( iRSI(Symbol(),NULL,14,PRICE_OPEN,0) > rsihigh) && ( iCCI(Symbol(),NULL,14,PRICE_OPEN,0) > ccihigh))
   { 
     if (OrdersTotal() < 1)
     {
        OrderSend(Symbol(),OP_SELL,lota,Open[0],NULL,NULL,NULL,NULL,NULL,NULL,Red);
     }
   }
   if ((iRSI(Symbol(),NULL,14,PRICE_OPEN,0) < rsilow) && (iCCI(Symbol(),NULL,14,PRICE_OPEN,0) < ccilow))
   {
     if (OrdersTotal() < 1)
     {
   
        OrderSend(Symbol(),OP_BUY,lota,Open[0],NULL,NULL,NULL,NULL,NULL,NULL,Green);
     }
   }
 //Closeorder Function----------------------------

   for(int iPos=OrdersTotal() - 1; iPos >=0; iPos--) 
        if (OrderSelect(iPos,SELECT_BY_POS) && OrderSymbol() == Symbol())
        {
           int duration = iBarShift(NULL,NULL, OrderOpenTime());
           if (duration < Period()) continue;
              if (!OrderClose(OrderTicket(),OrderLots(), OrderClosePrice(), NULL, MediumSeaGreen))
              Alert("OrderClose Failed: ", GetLastError());
        }

 //----------------------------------------------   
//Martingale Function ------
 if(OrderProfit() < 0)
 {
   lota = OrderLots() * mp;
   if (OrderLots() >= lotb)
      {
         lota = lot;
      }
 }
 else
 {
   lota = lot;
 }
 //----------------------------------------------   
 }
  
//+------------------------------------------------------------------+
 
quagpz0:

I created a simple EA base on RSI and CCi! Besides, this EA will open order at bar's open price and close order at close price of the same bar or open price of next bar! 

However, when i backtest it only work with sell order! Buy order didn't work

Pls help me

Open[0] in your OrderSend function is Bid price and you can't use it at BUY (you have to use Ask price).
You should check your condition only on a new bar event and instead of Close[0] in your OrderSend use Bid at SELL and Ask at BUY.
 
Petr Nosek:
Open[0] in your OrderSend function is Bid price and you can't use it at BUY (you have to use Ask price).
You should check your condition only on a new bar event and instead of Close[0] in your OrderSend use Bid at SELL and Ask at BUY.

If i use Bid or Ask, it would have a problem! As u see in this picture, i want to open order at 1 but it open at the middle of bar! Is there any other way to do it?


 
quagpz0:

If i use Bid or Ask, it would have a problem! As u see in this picture, i want to open order at 1 but it open at the middle of bar! Is there any other way to do it?


Of course. It is because of spread. Chart prices are Bid prices but if you want to open a long trade you have to buy at Ask price. If I can advise you something don't trade on an 1 minute chart. There is often spread greater than some candles.
Reason: