How to iterate over each candle

 

I really need your help, please. I'm still in the learning phase on MQL5 programming. I'm actually trying to write a simple program of what I've been learning so far. But there is one thing giving me headache.

This code enters a sell trade at the close of a candle. It exit trade if it goes above the close, and re-enters if it goes below. Now the problem I have with this program is it always enters a sell trade at the close of each candle formed. I know quite well that it has to do with this part of the code.

int Data = CopyRates(Symbol(),Period(),1,Bars(Symbol(),Period()),PriceInformation);

It enters trade at the close of the previous or second candle which is always set to "1" above.

Now, I've been trying my best to see how I can make it remain at the very first entry trade level even if newer candles are formed below the close. And when price redirect to the trade level and above, it should do its normal operation. It should not enter trades at the close of each candle which is always set to 1 below the the main entry point.

I think this has to do with the index number changing as newer candles are formed. But this is just the problem for me. I don't know how I can loop this and make it work. Attached is the file, and below is the code. Any help is highly appreciated.


#include <Trade/Trade.mqh>

CTrade trade;

int OnInit(){
   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason){
  }

void OnTick(){

   double Ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
   double Bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
   
   string signal ="";
      
   MqlRates PriceInformation[];

   ArraySetAsSeries(PriceInformation,true);

   int Data = CopyRates(Symbol(),Period(),1,Bars(Symbol(),Period()),PriceInformation);
   
   double close_Price = PriceInformation[0].close;
   
   if(Bid > close_Price){
      signal = "buy";
   }
   
   if(Bid <= close_Price){
      signal = "sell";
   }
   
   if(signal == "buy" && PositionsTotal() > 0)
   CloseAllSellPositionsThisPair();
   
   if (signal == "sell" && PositionsTotal() == 0)
   trade.Sell(0.03,_Symbol,Bid,NULL,NULL,NULL);

}
void CloseAllSellPositionsThisPair(){

   for(int i = PositionsTotal()-1; i >= 0; i--){
      int ticket = (int)PositionGetTicket(i);
      string CurrencyPair = PositionGetSymbol(i);
      
      int PositionDirection = (int)PositionGetInteger(POSITION_TYPE);
      
      if(PositionDirection == POSITION_TYPE_SELL)
      
      if(_Symbol == CurrencyPair){
         trade.PositionClose(ticket);
      }
      
      if(_Symbol == CurrencyPair){
      }
   }
}
Files:
EA.mq5  2 kb
 

Study the CopyRates help and think very hard:

  1. How many (number) items are you copying?
  2. WHY do you copy so many elements ???
Documentation on MQL5: Timeseries and Indicators Access / CopyRates
Documentation on MQL5: Timeseries and Indicators Access / CopyRates
  • www.mql5.com
CopyRates - Timeseries and Indicators Access - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Hi @ Kelly O,

I believe the nesting of your IF statements is not correct. 

Also it's not easy to read your code, you've made it more complicated than it needs to be.

Have a look at my solution below, follow the flow carefully and see if you can understand it.

It might be worth your time looking at the MQL5 documentation too.





CPositionInfo   m_Position;
CTrade  m_Trade;
void
CloseAllSellPositionsThisPair()     {         for(int i=PositionsTotal()-1; i>=0; i--)         {             if(m_Position.SelectByIndex(i))             {                 if(m_Position.PositionType()==POSITION_TYPE_SELL)                 {                     m_Trade.PositionClose(m_Position.Ticket());                 }             }         }     }
Reason: