help with High and Low values

 

Hi guys,

Im trying to build a EA that calculate the High and Low, Im using standard devtion to determ when the market isnt moving then setting high and low values and want

to buy or sell if the price is crossing under the low or above the high

Im getting wrong values in high and low, someone know why?

Thanks for your help

#property link      ""
#include <stderror.mqh>
#include <stdlib.mqh>

#define IDLE 0
#define SIGNAL_BUY   1
#define SIGNAL_SELL  2
bool trade=false;
int tickets=0;
int ticketb=0;
int Order = IDLE;
bool update=true;
bool flagbuy = true;
bool flagsell = true;
double MA9, BigMA, BigMAShort, stand;
double Short_MA9;
extern double lotSize=0.1;
extern int TP=100;
extern int SL=100;
extern int Short_TP=100;
extern int Short_SL=100;
extern string a= "Standard Deviation setting";
extern int gapInPips=10;
extern int Stand_PER= 10;       /// set the number of bars back 
extern int MODE_MA= 0;
extern int stand_app= 0;
extern double DEV_POINT= 0.0005;
extern string b= "setting for ";
extern int MagicNumber=7272;
double low, hi, hiPt, lowPt;
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
//----
  if (OrdersTotal()==0)  
   {
    trade=false;
     Order = IDLE;
    
    
   }
   if (OrdersTotal()!=0)
   {
       for (int i=1; i<=OrdersTotal(); i++)       //Cycle for all orders..
      {                                        //displayed in the terminal
       if(OrderSelect(i-1,SELECT_BY_POS)==true)//If there is the next one
        {                                    
          if (OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
          {
           trade=true;
           update=true;
               break;
            }
          else {trade=false;}
          }
        }   
   }
//////////////////// INDICATORS
 stand=iStdDev(NULL,0,Stand_PER,0,MODE_EMA,PRICE_CLOSE,0);
 double gap= gapInPips; /// 
 hilow();///find the high and low price
 
 if(DEV_POINT>stand && update==true)      /// just if the Standart devtion is < than the open point than get high and low 
 {
   hi=hiPt;
   low= lowPt;
   update=false;        ///stop getting high and low and wait until the price will cross it
 }
//////////////////CHECK FOR BUY
if (trade==false && Order==IDLE && flagbuy== false && update==false)
{
   if( Ask >hi)
   {    
         Order=SIGNAL_BUY;
         flagbuy=true;
       
    } 
}
//////////////////CHECK FOR SELL
if (trade==false && Order==IDLE && flagsell==false && update==false)
{
   if(Bid <low)
   {      
         Order=SIGNAL_SELL;
        flagsell=true;
     
   } 
      
}

Comment("DEV = ", stand, " devpoint ", DEV_POINT, " hi ", hiPt, " low ", lowPt, " hi_dev= ", hi, " low_vev ", low);// "\n buy_pt " ,Ask+ (gap*Point), " sell_pt ",Bid- (gap*Point) );

/////////////////////////BUY_ order
if (trade==false)
{
  if (Order == SIGNAL_BUY  )
  {
  ticketb=OrderSend(Symbol(),OP_BUY,lotSize,Ask,3, 0, 0,"",MagicNumber ,0 ,Green);   //open buy

   if(OrderSelect( ticketb, SELECT_BY_TICKET )==true)
   OrderModify(ticketb,OrderOpenPrice(),Ask- SL*Point,Ask+ (TP * Point) ,0,Green);

      if(OrderSelect( ticketb, SELECT_BY_TICKET )==true)
      {
       Alert("LONG ORDER OPENED IN Stand DEV"," Symbol= ",Symbol()," Period=",Period(), " Ticket= ",ticketb, " HiPt= ",hi, " lowPt= ",low);
       
      trade= true;
      }
      else
      {
      Print("OrderSelect returned the error of ",GetLastError());
      Alert("EROR LONG ORDER IN Stand DEV"," Symbol= ",Symbol()," Period=",Period()," EROR= ",GetLastError());
      Order= IDLE;
      }
   }
}
 /////////////////////////sell
if (trade==false)
{
  if (Order == SIGNAL_SELL  )
  {
  tickets=OrderSend(Symbol(),OP_SELL,lotSize,Bid,3,0,0,"",MagicNumber,0,Green); // open sell
   if(OrderSelect( tickets, SELECT_BY_TICKET )==true)
   OrderModify(tickets,OrderOpenPrice(),Bid+ (Short_SL*Point),Bid-(Short_TP*Point),0,Blue);

      if(OrderSelect( tickets, SELECT_BY_TICKET )==true)
      {
       Alert("SHORT ORDER OPENED IN Stand DEV"," Symbol= ",Symbol()," Period=",Period(), " Ticket= ",ticketb);
      trade= true;
      }
      else
      {
      Print("OrderSelect returned the error of ",GetLastError());
      Alert("ERROR SHORT ORDER IN Stand DEV "," Symbol= ",Symbol()," Period=",Period()," EROR= ",GetLastError());
      Order= IDLE;
      }
   }

}
 /////////////////////////////////// END
/////////////////check order status buy
   
   if (stand>DEV_POINT){
   flagbuy=false;
   flagsell=false;}
   
//////////////////////////////////////////////////////////////
   return(0);
}
//+------------------------------------------------------------------+
void hilow()
{
    for(int size=Stand_PER; size>0; size--)    /// sort to find the high and low 
   {
      if(iHigh(NULL,0,size)>iHigh(NULL,0,size-1))     //// find high point 
      {
         hiPt=iHigh(NULL,0,size);
      }
      if(iHigh(NULL,0,size)<iHigh(NULL,0,size-1)) 
      {
          hiPt=iHigh(NULL,0,size-1);
      }
      ////////////////////////////////////////
      if(iLow(NULL,0,size)<iLow(NULL,0,size-1))        /// find low point 
      {
         lowPt=iLow(NULL,0,size);
      }
       if(iLow(NULL,0,size)>iLow(NULL,0,size-1))
      {
         lowPt=iLow(NULL,0,size-1);
      }
   }
}
 

Found the problem thank you

Reason: