mql4 issues

 

Hi im writing an ea to check if moving average crossed then to check if the trigger candle is the highest or lowest candle out of 8 , there is only chance for two candles max after trigger to be swing high if not the trade will be false , but my code dos not work for some reason i know the problem is in isthiscandleswingh() and isthiscandleswingh() because the ea work fine without them can you please look at it i attach photo and the mql4 any help will be appreciated thanks  

//========================================================================================================================================================================//
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//========================================================================================================================================================================//
extern double lotsize;
extern int ma1 =6;
extern int ma2 =18;
extern int ma3 =50;
extern int ma4 =200;

extern double sl=10.0;
extern double tp=20.0;
extern double risk=10.0;
//========================================================================================================================================================================//
int minbar =ma1;
bool canbuy;
bool cansell;
bool buycross=false;
bool sellcross=false;
int crossup;
int crossdown;
int maxswingcandle;
int minswingcandle;

//========================================================================================================================================================================//
int OnInit()
  {

   return(INIT_SUCCEEDED);
  }
//========================================================================================================================================================================//
void OnDeinit(const int reason)
  {

  }
//========================================================================================================================================================================//
void OnTick()
  {
  if (isthisnewcandle()){ 
   isthereactiveorder();
   cheackmacross();
   if((canbuy==true)&&(crossup==0)&& (isthiscandleswingh())&&  maxswingcandle<=2)
     {
      opennew(OP_BUY);
      maxswingcandle=0;
     }
     else 
       {
         maxswingcandle++;                                 //max candles to compare and find swing high
       }
   if((cansell==true)&&(crossdown==0)&&(isthiscandleswingl()) &&  minswingcandle<=2)
     {
      opennew(OP_SELL);
      minswingcandle=0;
     }
     else 
       {
         minswingcandle++;                                 //max candles to compare and find swing low
       }

  }
  
  }
//========================================================================================================================================================================//
void isthereactiveorder()
  {
   crossup=0;
   crossdown=0;
//---
   for(int i = 0 ; i < OrdersTotal() ; i++)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false)
        {
         Print("ERROR - Unable to select the order - ",GetLastError());
         break;
        }
      if(OrderSymbol()==Symbol() && OrderType() == OP_BUY)
         crossup++;
      if(OrderSymbol()==Symbol() && OrderType() == OP_SELL)
         crossdown++;
     }
  }
//========================================================================================================================================================================//
double calculateLotSize(double stopLoss,double Risk)
  {
// Fetch some symbol properties
   double lotStep = MarketInfo(Symbol(), MODE_LOTSTEP);
   double minLot  = MarketInfo(Symbol(), MODE_MINLOT);
   double maxLot  = MarketInfo(Symbol(), MODE_MAXLOT);
   double tickVal = MarketInfo(Symbol(), MODE_TICKVALUE);
// Calculate the actual lot size
   double lotSize = AccountBalance() * Risk / 100 / (stopLoss * tickVal);
//---
   return MathMin(
             maxLot,
             MathMax(
                minLot,
                NormalizeDouble(lotSize / lotStep, 0) * lotStep
             )
          );
  }
//========================================================================================================================================================================//
void opennew(int ordertype)
  {
   RefreshRates();
   double openprice=0;
   double closeprice=0;
   double stopp=0;
   double takep=0;
   bool OrdrSend=false;
//---
   if(ordertype==OP_BUY)
     {
      openprice=Ask;
      stopp=openprice-(sl*10*Point);
      takep=openprice+(tp*10*Point);
     }
//---
   if(ordertype==OP_SELL)
     {
      openprice=Bid;
      stopp=openprice+(sl*10*Point);
      takep=openprice-(tp*10*Point);
     }
//---
   OrdrSend=OrderSend(Symbol(),ordertype,calculateLotSize(sl*10,risk),openprice,10,NormalizeDouble(stopp,Digits),NormalizeDouble(takep,Digits),NULL,0,0,clrBlue);
  }
//========================================================================================================================================================================//
void cheackmacross()
  {
   canbuy=false;
   cansell=false;
//---
   double currMA1=iMA(Symbol(),0,ma1,0,MODE_EMA,PRICE_CLOSE,1);
   double prevMA1=iMA(Symbol(),0,ma1,0,MODE_EMA,PRICE_CLOSE,2);
   double currMA2=iMA(Symbol(),0,ma2,0,MODE_EMA,PRICE_CLOSE,1);
   double prevMA2=iMA(Symbol(),0,ma2,0,MODE_EMA,PRICE_CLOSE,2);
   double currMA3=iMA(Symbol(),0,ma3,0,MODE_EMA,PRICE_CLOSE,1);
   double prevMA3=iMA(Symbol(),0,ma3,0,MODE_EMA,PRICE_CLOSE,2);
   double currMA4=iMA(Symbol(),0,ma4,0,MODE_SMA,PRICE_CLOSE,1);
   double prevMA4=iMA(Symbol(),0,ma4,0,MODE_SMA,PRICE_CLOSE,2);
   

//---
   if(prevMA2>prevMA1 && currMA1>currMA2 && currMA1>currMA4 && currMA1>currMA3 && prevMA1>prevMA4 && prevMA1>prevMA3 )
     {
      canbuy=true;
     }
//---
   if(prevMA1>prevMA2 && currMA2>currMA1 && currMA1<currMA4 && currMA1<currMA3 && prevMA1<prevMA4 && prevMA1<prevMA3)
     {
      cansell=true;
     }

  }
//========================================================================================================================================================================//
bool isthisnewcandle(){
static int barsonchart=0;
if (Bars==barsonchart)
return (false);
barsonchart=Bars;
return(true);}
//========================================================================================================================================================================//
bool isthiscandleswingh(){
cheackmacross();
double highestprevbar = High[iHighest(Symbol(),0,MODE_HIGH,8,1)];
if (crossup==true){
   double currentcandle=High[1];                 //check if cuurent candle is the hiest of previos 8 
   if (currentcandle>highestprevbar){
   return true;
   }
}
return false;
}
//========================================================================================================================================================================//

bool isthiscandleswingl(){
cheackmacross();
double lowestprevbar = Low[iLowest(Symbol(),0,MODE_LOW,8,1)];
if (crossdown==true){
   double currentcandle=Low[1];                 //check if cuurent candle is the lowestof previos 8 
   if (currentcandle<lowestprevbar){
   return true;
   }
}
return false;
}
 
If it compiles without any error(!) use the debugger and check where and why it doe not do what you want it to do.
 
Carl Schreiber:
If it compiles without any error(!) use the debugger and check where and why it doe not do what you want it to do.
It is on tester and yeh no errors
 
mo798ua and yeh no errors

If there are no errors, why are you posting? Just because it compiles doesn't mean it does what you want.

Use the debugger or print out your variables, including _LastError and prices and . Do you really expect us to debug your code for you?
 
William Roeder:

If there are no errors, why are you posting? Just because it compiles doesn't mean it does what you want.

Use the debugger or print out your variables, including _LastError and prices and . Do you really expect us to debug your code for you?

Why the attitude?? I been trying to debugging it for 4 hours , and i mean it compile without syntex error but obviously it have logic errors and why would i use lasterroe if there is no errors?? Please I’m not trying to be offensive for any ,if you think my post offended you in weird way don’t reply on it simple stuff peace 
 
mo798ua:

Why the attitude?? I been trying to debugging it for 4 hours , and i mean it compile without syntex error but obviously it have logic errors and why would i use lasterroe if there is no errors?? Please I’m not trying to be offensive for any ,if you think my post offended you in weird way don’t reply on it simple stuff peace 

You want MA1 to cross the 3 others on the same candle ? It's very unlikely to happen.

 
Alain Verleyen:
You want MA1 to cross the 3 others on the same candle ? It's very unlikely to happen.

no the crossover is only between ma1 and ma2 if the crossover happen ma should be>ma4 an >ma3

 
mo798ua:

no the crossover is only between ma1 and ma2 if the crossover happen ma should be>ma4 an >ma3

Ok.

Additionally you want crossup to =0 and to be true. It will never happen for sure.

if((canbuy==true)&&(crossup==0)&& (isthiscandleswingh())&&  maxswingcandle<=2)
 
mo798ua:

Hi im writing an ea to check if moving average crossed then to check if the trigger candle is the highest or lowest candle out of 8 , there is only chance for two candles max after trigger to be swing high if not the trade will be false , but my code dos not work for some reason can you please look at it i attach photo and the mql4 any help will be appreciated thanks  

Just a newbie myself, but one point might be that our comments here should reflect our code. Be precise, avoid unneccesary work and check for errors. "My code does not work.." is just asking for trouble on here ;)  And few will want to delve into dozens of lines of code to solve your problem.

Perhaps a method to test that your code is doing what it should - after each line, after each function call, if necessary - is whats needed. And experience has told me making your code more elegant and easy to read will save time later!

Looking at the very first function. As an aside, my understanding is that bars on chart is not the best way to check a new bar. Search "mql4 best new bar detection". And I'd be tempted to put the newbar code inside OnTick().


bool isthisnewcandle(){
static int barsonchart=0;
if (Bars==barsonchart)
return (false);
barsonchart=Bars;
return(true);}

// I'd find this easier to read

void OnTick(){
 static int barsonchart=0;
 if (Bars==barsonchart)return;
 barsonchart=Bars;

 // rest of code
}

 

I know this doesn't solve the problem in your code, but it's more about us finding ways to solve our own problems...

 
Alain Verleyen:

Ok.

Additionally you want crossup to =0 and to be true. It will never happen for sure.

wait, i comment out /*&&(isthiscandleswingl()) &&  minswingcandle<=2*/ and it work fine both crossup and

have different function they are no associated by any mean , please explain your point,thanks 

 
andrew:

Just a newbie myself, but one point might be that our comments here should reflect our code. Be precise, avoid unneccesary work and check for errors. "My code does not work.." is just asking for trouble on here ;)  And few will want to delve into dozens of lines of code to solve your problem.

Perhaps a method to test that your code is doing what it should - after each line, after each function call, if necessary - is whats needed. And experience has told me making your code more elegant and easy to read will save time later!

Looking at the very first function. As an aside, my understanding is that bars on chart is not the best way to check a new bar. Search "mql4 best new bar detection. I'd be tempted to put the newbar function inside OnTick().


I know this doesn't solve the problem in your code, but it's more about us finding ways to solve our own problems...

i checked every function by it own the checking for new bar function work but i will take your advise,about my ea dont work ,im not trying to be rude or untitled im front of my code 6 hours now after 4 hour of trying to figure out what is wrong i posted my issue here am i not doing enough?? am i rude for asking for help  

Reason: