I can't find why "Last exit deal is a buy" and "Last exit deal is a sell" is not getting printed in the journal. Any please help figure this out, Thanks.

 
#include <Trade\Trade.mqh>
CTrade trade;

datetime gBarTime;

void OnTick()
{
   datetime RightBarTime = iTime(_Symbol, _Period, 0);
   if (RightBarTime != gBarTime)
   {
      gBarTime = RightBarTime;
      OnBar();
   }
}

void OnBar()
{
   double open = iOpen(_Symbol, PERIOD_CURRENT, 1);
   double close = iClose(_Symbol, PERIOD_CURRENT, 1);

   if (close > open)
   {
      trade.PositionClose(_Symbol);
      Sleep(500);
      Comment("GOING LONG");
      trade.Buy(1, _Symbol);
   }

   if (close < open)
   {
      trade.PositionClose(_Symbol);
      Sleep(500);
      Comment("GOING SHORT");
      trade.Sell(1, _Symbol);
   }

   // Select the last month's deals
   datetime currentDate = TimeCurrent();
   datetime oneMonthAgo = TimeCurrent() - 60 * 60 * 24 * 7;
   //string deal_symbol = HistoryDealGetString(ticket_deal, DEAL_SYMBOL);
   
   if (HistorySelect(oneMonthAgo, currentDate))
   {
      // Get the number of deals
      int dealsCount = HistoryDealsTotal();
      
      if (dealsCount > 0)
      {
         // Get the ticket of the last deal
         ulong deal_ticket = HistoryDealGetTicket(dealsCount - 1);
         
         // Check the entry type of the last deal
         long entry_deal = HistoryDealGetInteger(deal_ticket, DEAL_ENTRY);
         long deal_type = HistoryDealGetInteger(deal_ticket, DEAL_TYPE);
         
         if (entry_deal == DEAL_ENTRY_OUT) Print("Last entry deal is: ", deal_ticket);
         
         if (deal_type==DEAL_TYPE_BUY) Print("Last exit deal is a buy");
         if (deal_type==DEAL_TYPE_SELL) Print("Last exit deal is a sell");
      }

      // Release the selected deals
      HistorySelect(0, 0);
   }
}
 
khulanimabila:
Because you are only checking one deal. You need to loop through until you have gone from deals_count to zero.

You need to wrap some of your code into a for() loop, or a while() loop.

Else you will continue to be checking only the last deal.
Reason: