my increment not working right MQL5

 

i created 2 statements

if a new bar opens set newb to 1 and if the latest tick is not a new bar set newb to 0

if(isNewBar()){newb=1;}else if(isNewBar()==false){newb=0;}

 

if((renkoC[2]>renkoO[2]) && newb==1){numhigh++;}

even when new bar is false, numhigh keeps incrementing

 
newb is set to 1 elsewhere, or numhigh is increased elsewhere.
 

Alain Verleyen:
newb is set to 1 elsewhere, or numhigh is increased elsewhere.

 

 

it is not being incremented else where 

   if(numhigh>=BuyRange1){buynow=1;}
   if(numhigh>=SellRange1){sellnow=1;}
   
      if((renkoC[2]>renkoO[2]) && newb==1){numhigh++;}

      
      if((renkoC[2]<renkoO[2]) && newb==1){numlow++;}
      
      newb=0;
   
   if(numhigh>=BuyRange1 && buynow==1){
   numhigh=0;
   buynow=0;
   trade.Buy(Lot,Symbol(),ask,0,0,"Donchain Trade");
   }
   
   if(numlow>=SellRange1 && sellnow==1){
   numlow=0;
   sellnow=0;
   trade.Sell(Lot,Symbol(),bid,0,0,"Donchain Trade");
   }
 
is there a better way i can write the if statements?
 
Nurudeen Amedu:

Useless code, we don't see where newb is set.

Post relevant all code if you want help, otherwise it's just waste of time.

 
Alain Verleyen:

Useless code, we don't see where newb is set.

Post relevant all code if you want help, otherwise it's just waste of time.

//+------------------------------------------------------------------+
//|                                                      RenkoEA.mq5 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"


input int BuyRange1  = 3;
input int BuyRange2 = 4;
input int BuyRange3  = 5;
input int BuyRange4 = 7;
input int SellRange1  = 3;
input int SellRange2 = 4;
input int SellRange3  = 5;
input int SellRange4 = 7;
  
input double BuyStop = 70;
input double BuyGain = 120;
input double SellStop = 70;
input double SellGain = 120;  
input double Lot = 0.01;
input int Magic = 54636;
input int InpBoxSize = 100;

int buynow=0;
int sellnow=0;


#include<Trade\Trade.mqh>
CTrade  trade;
double bid, ask;
double renkoH[], renkoO[],renkoL[], renkoC[], renkoS[];
int RenkoH;
double tpoint;
int num, numhigh, numlow, newb;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   tpoint=SymbolInfoDouble(Symbol(),SYMBOL_POINT);
   //--- set MagicNumber for your orders identification
   int MagicNumber=Magic;
   trade.SetExpertMagicNumber(MagicNumber);
//--- set available slippage in points when buying/selling
   int deviation=50;
   trade.SetDeviationInPoints(deviation);
//--- order filling mode, the mode allowed by the server should be used
   trade.SetTypeFilling(ORDER_FILLING_RETURN);
//--- logging mode: it would be better not to declare this method at all, the class will set the best mode on its own
   trade.LogLevel(1); 
//--- what function is to be used for trading: true - OrderSendAsync(), false - OrderSend()
   trade.SetAsyncMode(false);

   //MA1=iCustom(NULL,0,"Examples\\Zigzag"); 
   //SetIndexBuffer(0,Label1Buffer,INDICATOR_DATA); 
   //ResetLastError(); 
   RenkoH=iCustom(NULL,0,"Market\\Median and Turbo renko indicator bundle"); 


   Print("MA_handle = ",RenkoH,"  error = ",GetLastError()); 
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   if(CopyBuffer(RenkoH,5,1,3,renkoO)<0){Print("CopyBuffer1 error =",GetLastError());}
   if(CopyBuffer(RenkoH,6,1,3,renkoH)<0){Print("CopyBuffer2 error =",GetLastError());}
   if(CopyBuffer(RenkoH,7,1,3,renkoL)<0){Print("CopyBuffer3 error =",GetLastError());}
   if(CopyBuffer(RenkoH,8,1,3,renkoC)<0){Print("CopyBuffer4 error =",GetLastError());}
   if(CopyBuffer(RenkoH,4,1,3,renkoS)<0){Print("CopyBuffer4 error =",GetLastError());}
   
   //Comment( NormalizeDouble(renkoO[1],9), "  ", NormalizeDouble(renkoH[1],9), "  ", NormalizeDouble(renkoL[1],9), "  ", NormalizeDouble(renkoC[1],9), " ", SymbolInfoInteger(Symbol(),SYMBOL_SPREAD));
//isNewBar();   
   if(isNewBar()){newb=1;
                  Print("Newbar");
   }else if(isNewBar()==false){newb=0;}
    
FindSignal();

  Comment(isNewBar(), "  ", newb, "  ", numhigh, "  ", renkoC[2], " ", renkoO[2]);
   
  }
//+------------------------------------------------------------------+


void FindSignal(){

   if(numhigh>=BuyRange1){buynow=1;}
   if(numhigh>=SellRange1){sellnow=1;}
   
      if((renkoC[2]>renkoO[2]) && newb==1){numhigh++;}

      
      if((renkoC[2]<renkoO[2]) && newb==1){numlow++;}
      
      newb=0;
   
   if(numhigh>=BuyRange1 && buynow==1){
   numhigh=0;
   buynow=0;
   Comment("Buy now");
  // trade.Buy(Lot,Symbol(),ask,0,0,"Donchain Trade");
   }
   
   if(numlow>=SellRange1 && sellnow==1){
   numlow=0;
   sellnow=0;
   ("Sell Now");
  // trade.Sell(Lot,Symbol(),bid,0,0,"Donchain Trade");
   }
}

//+------------------------------------------------------------------+
//insuring its a new candle function
//+------------------------------------------------------------------+
bool IsNewCandle()
{
   static int BarsOnChart=0;
        if (Bars(_Symbol,PERIOD_CURRENT) == BarsOnChart)
        return (false);
        BarsOnChart = Bars(_Symbol,PERIOD_CURRENT);
        return(true);
}

void OpenOrders(){
   num =0;
   int _tp=PositionsTotal();
   for(int i=_tp-1; i>=0; i--)
     {
      string _p_symbol=PositionGetSymbol(i);
      if(_p_symbol==_Symbol){
      if(Magic==PositionGetInteger(POSITION_MAGIC)){
      
      //---
         num++;
   
      }
   }

}
}

//+------------------------------------------------------------------+
//| Returns true if a new bar has appeared for a symbol/period pair  |
//+------------------------------------------------------------------+
bool isNewBar()
  {
//--- memorize the time of opening of the last bar in the static variable
   static datetime last_time=0;
//--- current time
   datetime lastbar_time=SeriesInfoInteger(Symbol(),Period(),SERIES_LASTBAR_DATE);

//--- if it is the first call of the function
   if(last_time==0)
     {
      //--- set the time and exit
      last_time=lastbar_time;
      return(false);
     }

//--- if the time differs
   if(last_time!=lastbar_time)
     {
      //--- memorize the time and return true
      last_time=lastbar_time;
      return(true);
     }
//--- if we passed to this line, then the bar is not new; return false
   return(false);
  }
 
Alain Verleyen:
newb is set to 1 elsewhere, or numhigh is increased elsewhere.
did you find anything?
 
Nurudeen Amedu:

i created 2 statements

if a new bar opens set newb to 1 and if the latest tick is not a new bar set newb to 0

even when new bar is false, numhigh keeps incrementing

Simplify your code, making it easier to spot problems and to debug:

bool newb;

...

newb = isNewBar();

...

if( newb )
{
   if( renkoC[2] > renkoO[2] ) numhigh++;
   if( renkoC[2] < renkoO[2] ) numlow++;
   newb = false;
}

...

bool isNewBar()
{
   static datetime last_time = EMPTY;
   datetime lastbar_time = SeriesInfoInteger( _Symbol, _Period, SERIES_LASTBAR_DATE );

   if( last_time != EMPTY )
   {
      if( last_time != lastbar_time )
      {
         last_time = lastbar_time;
         return( true );
      }
   }
   else
      last_time = lastbar_time;

   return( false );
}
 
Fernando Carreiro:

Simplify your code, making it easier to spot problems and to debug:

it works better now but it counts with every tick instead of once every bar open
 
Nurudeen Amedu:
it works better now but it counts with every tick instead of once every bar open

I was actually answering your "is there a better way i can write the if statements" query! I just gave examples of simplifying code to make it easier to spot problems.

So, simplify the rest of your code in order to see where the problem of the logic exists. For example, your "buynow" and "sellnow" should also be made into "bool" and not "int".

Also, post your updated code, so we can see if changes were made correctly!

 
Fernando Carreiro:

I was actually answering your "is there a better way i can write the if statements" query! I just gave examples of simplifying code to make it easier to spot problems.

So, simplify the rest of your code in order to see where the problem of the logic exists. For example, your "buynow" and "sellnow" should also be made into "bool" and not "int".

Also, post your updated code, so we can see if changes were made correctly!

does MQL5 code work better when most of the codes are within the onTick function
Reason: