Can someone please help me fix my code for the new version of mql4?

 

Hello my code is not decompiling correctly for mt4 build 1090 now after I try to run some back test Please help me clean up my code thank you. My code is included in attachments and I will paste it below:

#include <WinUser32.mqh>
#include <stderror.mqh>
#include <stdlib.mqh>
 
extern int    Magic=1111;

extern double MaxSpread=1.0;//0.6;
extern double Lot=0.1;
extern double SL=7.0;//3.5;//3.5;//5;
extern double TP=0.7;//0.4;
extern bool   invertJump=true;//false;
extern bool   invertBuySell=true;//false;
extern bool   TradeBuy=true;
extern bool   TradeSell=true;
extern bool   UseMM=true;
extern double MM=5;//9.5;//9.5;



extern bool   useMoving=false;
extern int    MovingPeriod=10;//if price under moving so can buy only

extern bool   noMoreTrades=false;

double pAsk=0;
double pBid=0;
double point;

bool canBuy=true;
bool canSell=true;

bool imaBuy=true;
bool imaSell=true;

double ArrPrice[10000];
int    ArrTimer[10000];

int t=0;
double l=0;
int nd;
int check;

void deinit()
{
}
 
void init()
{
  Comment("");
  if(MarketInfo(Symbol(),MODE_MINLOT)<0.1)
  {
    nd=2;
  }
  else if(MarketInfo(Symbol(),MODE_MINLOT)>0.1)
  {
    nd=0;
  }
  else
  {
    nd=1;
  }
 
  
   if(Digits==2 || Digits==3)point=0.01;
   if(Digits==4 || Digits==5)point=0.0001;
  
   ArrayInitialize(ArrPrice,0);
   ArrayInitialize(ArrTimer,0);
  
  pAsk=MarketInfo(Symbol(),MODE_ASK);
  pBid=MarketInfo(Symbol(),MODE_BID);
}
 
int start()
{
    int x;
    pAsk=MarketInfo(Symbol(),MODE_ASK);
    pBid=MarketInfo(Symbol(),MODE_BID);
   
    for(x=0;x<30;x++){if(TryClose())break;Sleep(100);}
    CalcLot();
   
    checkMoving();
    checkJump();
    for(x=0;x<30;x++){if(TryTrade())break;Sleep(100);}

       

}





void CalcLot()
{
  if(UseMM==true)
  {
    l=NormalizeDouble(AccountEquity()*MM/10000,nd);
    if(l<MarketInfo(Symbol(),MODE_MINLOT)) l=MarketInfo(Symbol(),MODE_MINLOT);
    if(l>MarketInfo(Symbol(),MODE_MAXLOT)) l=MarketInfo(Symbol(),MODE_MAXLOT);
  }
  else
  {
    l=Lot;
  }
}


 
bool TryTrade(){
   int total=OrdersTotal();

  //if(canBuy)Alert("buy"+(pAsk-pBid)/point);
  //if(canSell)Alert("Sell"+(pAsk-pBid)/point);
 
  if(TradeBuy==true && canBuy==true && imaBuy && !noMoreTrades && (pAsk-pBid)/point<=MaxSpread){
      if(!invertBuySell && MyOrdersBuyTotal (Magic)<1){OrderSend(Symbol(),OP_BUY,l,MarketInfo(Symbol(),MODE_ASK),0,0,0,"",Magic,0,Green);total++;}
      if( invertBuySell && MyOrdersSellTotal(Magic)<1){OrderSend(Symbol(),OP_SELL,l,MarketInfo(Symbol(),MODE_BID),0,0,0,"",Magic,0,Red);total++;}//inv
      check=GetLastError();if(check!=ERR_NO_ERROR) Print("error: ",ErrorDescription(check));
  }
 
  if(TradeSell==true && canSell==true && imaSell && !noMoreTrades && (pAsk-pBid)/point<=MaxSpread){
      if(!invertBuySell && MyOrdersSellTotal(Magic)<1){OrderSend(Symbol(),OP_SELL,l,MarketInfo(Symbol(),MODE_BID),0,0,0,"",Magic,0,Red);total++;}
      if( invertBuySell && MyOrdersBuyTotal (Magic)<1){OrderSend(Symbol(),OP_BUY,l,MarketInfo(Symbol(),MODE_ASK),0,0,0,"",Magic,0,Green);total++;}//inv
      check=GetLastError();if(check!=ERR_NO_ERROR) Print("error: ",ErrorDescription(check));
  }

   if(total==OrdersTotal()){return(true);}else{return(false);}
}




 
bool TryClose(){
   double p,moneyProfit,minProfit,maxLose;
   int total=OrdersTotal();
  
   for(int i=0;i<OrdersTotal();i++){
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
     
      //close profit
      if(OrderMagicNumber()==Magic && OrderSymbol()==Symbol() && OrderType()==OP_SELL){
         p=OrderOpenPrice()-MarketInfo(Symbol(),MODE_ASK);
         moneyProfit=pipsProfit(OrderLots(),p);
         minProfit  =pipsProfit(OrderLots(),TP*point);
         maxLose    =pipsProfit(OrderLots(),-SL*point);
         if(moneyProfit+OrderCommission()>=minProfit){OrderClose(OrderTicket(),OrderLots(),MarketInfo(Symbol(),MODE_ASK),0,Blue);total--;}
         if(moneyProfit+OrderCommission()<=maxLose  ){OrderClose(OrderTicket(),OrderLots(),MarketInfo(Symbol(),MODE_ASK),0,Blue);total--;}
      }
   
      //close profit
      if(OrderMagicNumber()==Magic && OrderSymbol()==Symbol() && OrderType()==OP_BUY){
         p=MarketInfo(Symbol(),MODE_BID)-OrderOpenPrice();
         moneyProfit=pipsProfit(OrderLots(),p);
         minProfit  =pipsProfit(OrderLots(),TP*point);
         maxLose    =pipsProfit(OrderLots(),-SL*point);
         if(moneyProfit+OrderCommission()>=minProfit){OrderClose(OrderTicket(),OrderLots(),MarketInfo(Symbol(),MODE_BID),0,Red);total--;}
         if(moneyProfit+OrderCommission()<=maxLose  ){OrderClose(OrderTicket(),OrderLots(),MarketInfo(Symbol(),MODE_ASK),0,Blue);total--;}
      }
   }
  
   //return (true);
   if(total==OrdersTotal()){return(true);}else{return(false);}
}
 
int MyOrdersBuyTotal(int Magic)
{
  int c=0;
  int total  = OrdersTotal();

  for (int cnt = 0 ; cnt < total ; cnt++)
  {
    OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
    if (OrderMagicNumber() == Magic && OrderSymbol()==Symbol() && OrderType()==OP_BUY)
    {
      c++;
    }
  }
  return(c);
}
 
int MyOrdersSellTotal(int Magic)
{
  int c=0;
  int total  = OrdersTotal();
  for (int cnt = 0 ; cnt < total ; cnt++)
  {
    OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
    if (OrderMagicNumber() == Magic && OrderSymbol()==Symbol() && OrderType()==OP_SELL)
    {
      c++;
    }
  }
  return(c);
}


//////////////////REMON//////////////////////////////
double calc_iMA(int iPeriod){
   double d=0;
   for(int i=0;i<iPeriod;i++){
      d+=Close[i];
   }
   d/=iPeriod;
   return(d);
}
/////////////////////////////////////////
double pipsProfit(double lots,double pips){
   //Alert(MarketInfo(Symbol(),MODE_TICKVALUE));
   //return(lots*pips/point*10);
   return(MarketInfo(Symbol(),MODE_TICKVALUE)*lots*pips/point*10);
}
//////////////////////////////////////////
void checkJump(){
    //move all step to down
    for(int i=ArraySize(ArrPrice)-1;i>0;i--){
        ArrPrice[i]=ArrPrice[i-1];
        ArrTimer[i]=ArrTimer[i-1];
    }
   
    ArrPrice[0]=Bid;
    ArrTimer[0]=GetTickCount();
   
    int max=ArraySize(ArrTimer)-1;
   
    for(i=0;i<ArraySize(ArrTimer);i++){
        if(ArrTimer[0]-ArrTimer[i]>0.5*1000 && ArrPrice[i]!=0){max=i;break;}
    }

    for(i=0;i<max;i++){
        double p=(Bid-ArrPrice[i])/point;
        //Alert(p + " , " +(pAsk-pBid)/point);
        if(!invertJump){
            if(p>= 0.5){canSell=true;}else{canSell=false;}//jump up go sell
            if(p<=-0.5 ){canBuy =true;}else{canBuy =false;}//jump down go buy
        }else{
            if(p>= 0.5){canBuy =true;}else{canBuy =false;}//jump up go buy
            if(p<=-0.5){canSell=true;}else{canSell=false;}//jump down go sell
        }
    }

}

void checkMoving(){
    double v=calc_iMA(MovingPeriod);
    if(Close[0]>v){imaBuy =true;}else{imaBuy =false;}
    if(Close[0]<v){imaSell=true;}else{imaSell=false;}
}


Files:
 

Please use the </> button to insert your code above.


 
jeb2k18:

Hello my code is not decompiling correctly for mt4 build 1090 now after I try to run some back test Please help me clean up my code thank you. My code is included in attachments and I will paste it below:

You probably mean that your code not be compiled correctly. Here it is (I've only fixed errors and warnings. I haven't checked the logic).

Files:
 
  1. When you post code please use the CODE button (Alt-S)! (For large amounts of code, attach it.) Please edit your (original) post.
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  2. Why did you post your MT4 question in the Root / MT5 EA section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  3. l=NormalizeDouble(AccountEquity()*MM/10000,nd);
    Lots size is not a function of equity. Risk depends on your initial stop loss, lot size, and the value of the pair.
    1. You place the stop where it needs to be - where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
    2. Account Balance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the SPREAD, and DeltaPerLot is usually around $10/pip but it takes account of the exchange rates of the pair vs. your account currency.)
    3. Do NOT use TickValue by itself - DeltaPerLot and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or whether it is returning a value in the instrument's base currency.
                MODE_TICKVALUE is not reliable on non-fx instruments with many brokers.
    4. You must normalize lots properly and check against min and max.
    5. You must also check FreeMargin to avoid stop out
    Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5=0.1 Lots maximum.

 

Thank you Petr for your help. Yes that is what I meant I couldn't compile it correctly with the new syntax for higher builds been a while since I touched code with later builds and I am a bit rusty. Maybe need to read some updated schema for new build versions coming out. Thanks again for all your help.

Sorry whroeder1. Forgot about posting rules here. Will remember for next time. Just wanted someone to see my code as soon as possible and thought posting root it would get seen and not ignored. And thanks for the logic revision I will look into adding it into the code once I find out how to apply this new logic. Can you please make an example revision .mq4 with your corrections so I know where to start in the code, so I can see if the strategy is still stable with your recommendations? Thank you whroder1.


High Regards to you both.

Thanks and Cheers

Happy Pips.

 

Hi Jeb2k/Petr,


How to make this EA to work on OANDA broker ? Please suggest

 
Kasam Shaikh: How to make this EA to work on OANDA broker ?

Why do you think it doesn't?

 
whroeder1:

Why do you think it doesn't?

This EA works well with following broker.  I have personally checked  FXCH live account result with bonus amount but due to bad broker review afraid to deposit amount. Same EA dont work with Tickmill , XM , OANDA or Exness broker .


1) www.forex-swiss.com   (  FXCH  )

2) 2pipsforex.com  (  GCG  )

 
Kasam Shaikh: EA dont work with Tickmill , XM , OANDA or Exness broker .
  1. "Doesn't work" is meaningless - just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires - meaningless.

  2. Not surprising since it never checks return codes. You would know why, if you Check your return codes for errors, and report them including GLE/LE. Don't look at it unless you have an error. Don't just silence the compiler, it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    Only those functions that return a value (e.g. iClose, MarketInfo, etc.) must you call ResetLastError before in order to check after.

 
William Roeder:
  1. "Doesn't work" is meaningless - just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires - meaningless.

  2. Not surprising since it never checks return codes. You would know why, if you Check your return codes for errors, and report them including GLE/LE. Don't look at it unless you have an error. Don't just silence the compiler, it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    Only those functions that return a value (e.g. iClose, MarketInfo, etc.) must you call ResetLastError before in order to check after.

Thanks i will have look .
Reason: