OrderSend / OrderClose Issue - page 2

 
Mosamba #:
Thanks very much for having edited my code...for a newbie it makes this much easier! Just one question - on your first "if" shouldn't it be "(OrdersTotal > 0) ?

No problem, if(OrdersTotal () == 0) is fine. 

 
Adj007 #: if(OrdersTotal () == 0) is fine. 

Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number/symbol filtering on your OrderSelect / Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
          Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum (2013)
          PositionClose is not working - MQL5 programming forum (2020)
          MagicNumber: "Magic" Identifier of the Order - MQL4 Articles (2006)
          Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles (2011)

You need one Magic Number for each symbol/timeframe/strategy. Trade current timeframe, one strategy, and filter by symbol requires one MN.

 
 I think I did reference that in accordance to magic numbers. Even though I dont use a magic number but use this Orders Total command, my eas work fine. Interesting concept however. So does that mean results are skewed when orderstotal without magic numbers is directly used  (only one currency pair) ? 
 
Adj007 #: (only one currency pair) ? 

What part of “on other charts and manual trading” was unclear?

 
Hey, listen I just asked for an opinion in regards to RESULTS and not incompatibility with the ea, you dont need to get sentimental behind the screen. 
 

Hello everyone!

I'm starting to work on a simple strategy (just placing SHORTS) but cannot figure out why the EA is behaving as per attached picture. It is closing the SELL completely against the programmed logic.
Your help is appreciated. Thanks in advance!!

#property strict

input string  iNote_1    = " --== EA Settings ==-- ";
input ENUM_TIMEFRAMES      iTF                     = PERIOD_CURRENT; //TimeFrame
input int                  iRC                     = 60;             //RANGING_CANDLES
input int                  iRL                     = 150;            //RANGING_LIMIT
input double               iLot                    = 0.01;           //Lot
input int                  iMagic                  = 201159;         //Mag

double sma_5;    //magenta
double sma_16;   //cyan
double sma_25;   //orange
double sma_70;   //dash white

int Ranging;
int ticket;
int close_order;

datetime gBarTime;

string _PR = "EAO_";
int _ae[4] = {6,136,138,129};
datetime _TimeInit = 0;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
      sma_5   = iMA (_Symbol,_Period,5,0,MODE_SMA,PRICE_OPEN,0);    //magenta
      sma_16  = iMA (_Symbol,_Period,16,0,MODE_SMA,PRICE_OPEN,0);   //cyan
      sma_25  = iMA (_Symbol,_Period,25,0,MODE_SMA,PRICE_OPEN,0);   //orange
      sma_70  = iMA (_Symbol,_Period,70,0,MODE_SMA,PRICE_OPEN,0);   //dash white 

      int HighestCandle = iHighest(_Symbol,iTF,MODE_CLOSE,iRC,0);
      ObjectDelete(_PR+"LH");
      ObjectCreate(_PR+"LH",OBJ_HLINE,0,_Time(0),_High(HighestCandle));
      int LowestCandle = iLowest(_Symbol,iTF,MODE_CLOSE,iRC,0);
      ObjectDelete(_PR+"LL");
      ObjectCreate(_PR+"LL",OBJ_HLINE,0,_Time(0),_Low(LowestCandle));

      Ranging = 0;
      double diff = _High(HighestCandle)-_Low(LowestCandle);
      if(diff<=iRL*_Point)
      {
      Ranging = 1;
      }

      // new local variable to hold the current open time of bar zero
      datetime rightBarTime = iTime(_Symbol, _Period, 0);
      // check if bar zero has the same open time as the global variable
      if(rightBarTime != gBarTime)
      {
         // set the global variable to be the time of the new bar
         gBarTime = rightBarTime;
         // call on bar function
         OnBar();
      }
  
  // placing SELL
  if(OrdersTotal() == 0)
    if(Ranging == 0)
       if((sma_5 < sma_16) && (sma_16 < sma_25) && (sma_25 < sma_70))
             ticket = OrderSend(Symbol(),OP_SELL,iLot,Bid,100,0,0,NULL,iMagic,0,clrRed);
  
  }

void OnBar()
   { 
      // closing SELL
      if(OrdersTotal() >= 1)
        if(sma_5 > sma_70)
//        Print("CLOSE SELL"); 
         if(OrderType()== OP_SELL)
            close_order = OrderClose(ticket,iLot,Ask,100,clrMagenta); 
       
   }

double _Close(int i)
  {
   return(iClose(_Symbol,iTF,i));
  }
double _Open(int i)
  {
   return(iOpen(_Symbol,iTF,i));
  }
double _High(int i)
  {
   return(iHigh(_Symbol,iTF,i));
  }
double _Low(int i)
  {
   return(iLow(_Symbol,iTF,i));
  }
datetime _Time(int i)
  {
   return(iTime(_Symbol,iTF,i));
  }

quest_1

Reason: