Whats wrong with this code, help please.

 

Hi all,

Both order buy and sell are on the same basics, buy ordermodify works well but sell ordermodify only works once(where it should modify every bar with new price untill filled). no errors showing. Ive checked stoplvls , slippage, bid/ask..all i could think of.  

 

 

#property copyright "
#property link      "ww

#include <FX001b.mqh>

//--- input parameters
extern bool DynamicLotSize = true;
extern double EquityPercent = 2;
extern double FixedLotSize = 0.1;

extern double    LotSize=0.1;
extern double    StopLoss=30.0;
extern double    TakeProfit=60.0;
extern int       Slippage=5;
extern int       MagicNumber=123;
extern int       FastMAPeriod=21;
extern int       SlowMAPeriod=89;

//global variables
int BuyTicket;
int SellTicket;
double UsePoint;
int UseSlippage;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   UsePoint = PipPoint(Symbol());
   UseSlippage = GetSlippage(Symbol(),Slippage);
   
   
  }


//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
   //MovingAverages
   double FastMA = iMA(NULL,0,FastMAPeriod,0,1,0,0);
   double SlowMA = iMA(NULL,0,SlowMAPeriod,0,0,0,0);
   
   //Buy Order
   if(FastMA>SlowMA && BuyTicket == 0)
      {
       OrderSelect(SellTicket,SELECT_BY_TICKET);
       if(OrderCloseTime() == 0 && SellTicket>0 && OrderType() == OP_SELL)
          {
           double CloseLots = OrderLots();
           double ClosePrice = Ask;
    
           bool Closed = OrderClose(SellTicket,CloseLots,ClosePrice,UseSlippage,Red);
           if(Closed == true) SellTicket = 0;
          }
       //DELETE any pending Orders   
       else if(OrderCloseTime() == 0 && SellTicket > 0 && OrderType() == OP_SELLLIMIT)
          {
           bool Deleted = OrderDelete(SellTicket,Red);
           if(Deleted == true) SellTicket = 0;
          }
       //---------------------------------------------------------------------------------    
       //calclotsize
       double LotSize = CalcLotSize(DynamicLotSize,EquityPercent,StopLoss,FixedLotSize);
       LotSize = VerifyLotSize(LotSize);
       //Calculate stop loss and take profit
       double PendingPrice = NormalizeDouble(SlowMA,Digits);
       //open buy Order
       BuyTicket = OrderSend(Symbol(),OP_BUYLIMIT,LotSize,PendingPrice,UseSlippage,0,0,"Buy limit Order",MagicNumber,0,Green);
       
       SellTicket=0;
      }
      double NewPendingPrice = NormalizeDouble(SlowMA,Digits);
      if(BuyTicket>0 && OrderOpenPrice() != NewPendingPrice)
        {
         
         for (int i = OrdersTotal()-1; i >= 0; i--) 
            {
               OrderSelect(BuyTicket, SELECT_BY_TICKET);
               NewPendingPrice = NormalizeDouble(SlowMA,Digits);
               double BuyStopLoss = CalcBuyStopLoss(Symbol(),StopLoss,NewPendingPrice);
               if(BuyStopLoss > 0)
                  {
                   BuyStopLoss =  AdjustAboveStopLevel(Symbol(),BuyStopLoss,5);
                  }
               double BuyTakeProfit = CalcBuyTakeProfit(Symbol(),TakeProfit,NewPendingPrice);
               if(BuyTakeProfit > 0)
                  {
                   BuyTakeProfit =  AdjustBelowStopLevel(Symbol(),BuyTakeProfit,5);
                  }

               AddStopProfit(BuyTicket,BuyStopLoss,BuyTakeProfit);  
                  
               
                  
             }
        }
   //Sell Order
   if(FastMA<SlowMA && SellTicket == 0)
      {
       
       OrderSelect(BuyTicket,SELECT_BY_TICKET);
       if(OrderCloseTime() == 0 && BuyTicket>0 && OrderType() == OP_BUY)
          {
           CloseLots = OrderLots();
           ClosePrice = Bid;
    
           Closed = OrderClose(BuyTicket,CloseLots,ClosePrice,UseSlippage,Red);
          }
       //DELETE any pending Orders   
       else if(OrderCloseTime() == 0 && BuyTicket > 0 && OrderType() == OP_BUYLIMIT)
          {
           Deleted = OrderDelete(BuyTicket,Red);
           if(Deleted == true) BuyTicket = 0;
          }   
       //-------------------------------------------------------------------------------------------------------------
       PendingPrice = NormalizeDouble(SlowMA,Digits);
       //calclotsize
       LotSize = CalcLotSize(DynamicLotSize,EquityPercent,StopLoss,FixedLotSize);
       LotSize = VerifyLotSize(LotSize);
       //open sell Order
       SellTicket = OrderSend(Symbol(),OP_SELLLIMIT,LotSize,PendingPrice,UseSlippage,0,0,"Sell limit Order",MagicNumber,0,Red);
       
       BuyTicket=0;
      
       if(SellTicket>0)
          {
            for (i = OrdersTotal()-1; i >= 0; i--) 
              { 
               OrderSelect(SellTicket, SELECT_BY_TICKET);
               NewPendingPrice = NormalizeDouble(SlowMA,Digits);
               double SellStopLoss = CalcSellStopLoss(Symbol(),StopLoss,NewPendingPrice);
               if(SellStopLoss > 0)
                  {
                   SellStopLoss =  AdjustBelowStopLevel(Symbol(),SellStopLoss,5);
                  }
               double SellTakeProfit = CalcSellTakeProfit(Symbol(),TakeProfit,NewPendingPrice);
               if( SellTakeProfit > 0)
                  {
                    SellTakeProfit =  AdjustAboveStopLevel(Symbol(), SellTakeProfit,5);
                  }
               AddStopProfit(SellTicket,SellStopLoss,SellTakeProfit);  
              } 
          }
                  
     }   
   return(0);
  }

I cant attach include file as it not exist in folder, it exists once i open Editor...whats wrong?

thanks 

 
Sirru82: I cant attach include file as it not exist in folder, it exists once i open Editor...whats wrong?
look in the experst\include directory not experts.
 
WHRoeder:
look in the experst\include directory not experts.

Sorry I was not too specific. File is not in experts-include, I saved it. EA works /compile with this include file. File only shows in Editor.
 

What can be wrong with my editor/system. I searched for .mqh files and win found it but cannot open/find directory. After playing with settings ,now whole include file is gone. Only visible in Editor. And in editor i get ,,lock,, sign on files. Seems like there is limited access to this files, locked for me.

I tried to copy content on another MT4 editor and save/create new inlude file...but its the same. What can be wrong? Maybe reinstalling MT4 would help?

I now tried on laptop to create/save new mqh include, and still the same. If there is error or wrong parameters in include file will it not save at all?
 
Sirru82:

What can be wrong with my editor/system. I searched for .mqh files and win found it but cannot open/find directory. After playing with settings ,now whole include file is gone. Only visible in Editor. And in editor i get ,,lock,, sign on files. Seems like there is limited access to this files, locked for me.

I tried to copy content on another MT4 editor and save/create new inlude file...but its the same. What can be wrong? Maybe reinstalling MT4 would help?

I now tried on laptop to create/save new mqh include, and still the same. If there is error or wrong parameters in include file will it not save at all?
Perhaps it's a UAC issue,  are you using Vista/Windows 7 and have MT4 installed in Program Files ?  try installing in a directory other than Program Files, for example C:\MT4Installs\
 
RaptorUK:
Perhaps it's a UAC issue,  are you using Vista/Windows 7 and have MT4 installed in Program Files ?  try installing in a directory other than Program Files, for example C:\MT4Installs\


Windows7, all MT4's are in Program Files, yes. I'll try your way now. Windows lounched some updates recently, maybe thats the issue.

With new MT4 in C:\\ directory , all works fine. Now I have to move all data there and uninstall all remaining versions.  

Thank you. 

 

Going back to my question. Please review code and include file attached now. Problem has been shown above. Same parameters for buy and sell order placement and ordermodifications - hower sell orders dont work. Only modify once, whether it should modify on every bar after cross.

I checked stopvls - adding above/below, slippage, wrong bid/ask used - no errors showing(i have error instructions with everything for debugging)

At the moment  below statement can be responsible for some errors, as MA dont change fast enough. 

if(BuyTicket>0 && OrderOpenPrice() != NewPendingPrice)

 I added func to verify stoplvl for sell orders  so new code looks like this. But still no errors comming up. Whats wrong with SellOrderModifcations? Maybe loop is wrong?

 Please help, maybe some tips ?

#property copyright "Copyright 2012, Pipsomaniak"
#property link      "http://www.pipsomania.eu"

#include <FX001b.mqh>

//--- input parameters
extern bool DynamicLotSize = true;
extern double EquityPercent = 2;
extern double FixedLotSize = 0.1;

extern double    LotSize=0.1;
extern double    StopLoss=30.0;
extern double    TakeProfit=60.0;
extern int       Slippage=5;
extern int       MagicNumber=123;
extern int       FastMAPeriod=21;
extern int       SlowMAPeriod=89;

//global variables
int BuyTicket;
int SellTicket;
double UsePoint;
int UseSlippage;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   UsePoint = PipPoint(Symbol());
   UseSlippage = GetSlippage(Symbol(),Slippage);
   
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
   //MovingAverages
   double FastMA = iMA(NULL,0,FastMAPeriod,0,1,0,0);
   double SlowMA = iMA(NULL,0,SlowMAPeriod,0,0,0,0);
   
   //Buy Order
   if(FastMA>SlowMA && BuyTicket == 0)
      {
       OrderSelect(SellTicket,SELECT_BY_TICKET);
       if(OrderCloseTime() == 0 && SellTicket>0 && OrderType() == OP_SELL)
          {
           double CloseLots = OrderLots();
           double ClosePrice = Ask;
    
           bool Closed = OrderClose(SellTicket,CloseLots,ClosePrice,UseSlippage,Red);
           if(Closed == true) SellTicket = 0;
          }
       //DELETE any pending Orders   
       else if(OrderCloseTime() == 0 && SellTicket > 0 && OrderType() == OP_SELLLIMIT)
          {
           bool Deleted = OrderDelete(SellTicket,Red);
           if(Deleted == true) SellTicket = 0;
          }
       //---------------------------------------------------------------------------------    
       //calclotsize
       double LotSize = CalcLotSize(DynamicLotSize,EquityPercent,StopLoss,FixedLotSize);
       LotSize = VerifyLotSize(LotSize);
       //Calculate stop loss and take profit
       double PendingPrice = NormalizeDouble(SlowMA,Digits);
       //open buy Order
       BuyTicket = OrderSend(Symbol(),OP_BUYLIMIT,LotSize,PendingPrice,UseSlippage,0,0,"Buy limit Order",MagicNumber,0,Green);
       
       SellTicket=0;
      }
      double NewPendingPrice = NormalizeDouble(SlowMA,Digits);
      if(BuyTicket>0 && OrderOpenPrice() != NewPendingPrice)
        {
         for (int i = OrdersTotal()-1; i >= 0; i--) 
            {
             OrderSelect(BuyTicket, SELECT_BY_TICKET);
             NewPendingPrice = NormalizeDouble(SlowMA,Digits);
             double BuyStopLoss = CalcBuyStopLoss(Symbol(),StopLoss,NewPendingPrice);
             if(BuyStopLoss > 0)
                {
                 BuyStopLoss =  AdjustAboveStopLevel(Symbol(),BuyStopLoss,5);
                }
             double BuyTakeProfit = CalcBuyTakeProfit(Symbol(),TakeProfit,NewPendingPrice);
             if(BuyTakeProfit > 0)
                {
                 BuyTakeProfit =  AdjustBelowStopLevel(Symbol(),BuyTakeProfit,5);
                }
             AddStopProfit(BuyTicket,BuyStopLoss,BuyTakeProfit); 
             
            }
        }
   //Sell Order
   if(FastMA<SlowMA && SellTicket == 0)
      {
       OrderSelect(BuyTicket,SELECT_BY_TICKET);
       if(OrderCloseTime() == 0 && BuyTicket>0 && OrderType() == OP_BUY)
          {
           CloseLots = OrderLots();
           ClosePrice = Bid;
    
           Closed = OrderClose(BuyTicket,CloseLots,ClosePrice,UseSlippage,Red);
          }
       //DELETE any pending Orders   
       else if(OrderCloseTime() == 0 && BuyTicket > 0 && OrderType() == OP_BUYLIMIT)
          {
           Deleted = OrderDelete(BuyTicket,Red);
           if(Deleted == true) BuyTicket = 0;
          }   
       //-------------------------------------------------------------------------------------------------------------
       PendingPrice = NormalizeDouble(SlowMA,Digits);
       //calclotsize
       LotSize = CalcLotSize(DynamicLotSize,EquityPercent,StopLoss,FixedLotSize);
       LotSize = VerifyLotSize(LotSize);
       //open sell Order
       SellTicket = OrderSend(Symbol(),OP_SELLLIMIT,LotSize,PendingPrice,UseSlippage,0,0,"Sell limit Order",MagicNumber,0,Red);
       
       BuyTicket=0;
      
       if(SellTicket>0 && OrderOpenPrice() != NewPendingPrice)
          {
            for (i = OrdersTotal()-1; i >= 0; i--) 
              { 
               OrderSelect(SellTicket, SELECT_BY_TICKET);
               NewPendingPrice = NormalizeDouble(SlowMA,Digits);
               double SellStopLoss = CalcSellStopLoss(Symbol(),StopLoss,NewPendingPrice);
               if(SellStopLoss > 0)
                  {
                   SellStopLoss =  AdjustBelowStopLevel(Symbol(),SellStopLoss,5);
                  }
               double SellTakeProfit = CalcSellTakeProfit(Symbol(),TakeProfit,NewPendingPrice);
               if( SellTakeProfit > 0)
                  {
                    SellTakeProfit =  AdjustAboveStopLevel(Symbol(), SellTakeProfit,5);
                  }
               bool VerifiedU = VerifyUpperStopLevel(Symbol(),SellStopLoss);
               if(VerifiedU == false) Alert("Sell stop loss is invalid!");
               bool VerifiedB = VerifyBelowStopLevel(Symbol(),SellTakeProfit);
               if(VerifiedB == false) Alert("Sell take profit is invalid!");        
               AddStopProfit(SellTicket,SellStopLoss,SellTakeProfit);
              } 
          }
     }   
   return(0);
  }
Files:
fx001b.mqh  24 kb
 
Sirru82:

Going back to my question. Please review code and include file attached now. Problem has been shown above. Same parameters for buy and sell order placement and ordermodifications - hower sell orders dont work. Only modify once, whether it should modify on every bar after cross.

I checked stopvls - adding above/below, slippage, wrong bid/ask used - no errors showing(i have error instructions with everything for debugging)

At the moment  below statement can be responsible for some errors, as MA dont change fast enough. 

 I added func to verify stoplvl for sell orders  so new code looks like this. But still no errors comming up. Whats wrong with SellOrderModifcations? Maybe loop is wrong?

 Please help, maybe some tips ?

I don't know which code you wrote but I strongly suspect you didn't write it all . . .

The code in the include file checks the return values from the functions it uses,  then if a return value suggests that there was a problem the code reports the error back to the journal/experts tab and log.  \

For example:

    //Modify Order
    bool TicketMod = OrderModify(argTicket,OpenPrice,argStopLoss,argTakeProfit,0,Green);
    //Error handling
    if(TicketMod == false)
      {
       int ErrorCode = GetLastError();
       
    
       string ErrAlert = StringConcatenate("Add Stop/Profit - Error",ErrorCode);
       Alert(ErrAlert);
       string ErrLog = StringConcatenate("Bid: ",MarketInfo(OrderSymbol(),MODE_BID),"Ask: ",MarketInfo(OrderSymbol(),MODE_ASK),"Ticket: ",argTicket,"Stop: ",argStopLoss,
                       "Profit:   ",argTakeProfit); 
       Print(ErrLog);
      }



The code you have posted here does not do that  . . .  why ?   don't you want to know if your OrderSend has failed ?  don't you want to know why it has failed ?  don't you want to know what the value of Bid, Ask, PendingPrice were when your OrderSend failed ?  Pleas, please, please . . . do yourself a big favour,  add the return value test,  report all the information back to the log . . .  then when the OrderSend fails you will be able to find out why.

 

This will help:  What are Function return values ? How do I use them ? 

 

I am learning , copying and applying all I do from book ,,EA Programming,, Andrew R.Young...thats why I dont understand some of this coding yet. It is not created by me -yes. But modified, as I learn this way.

thank you for tips, I will continue learning from that.

Best regards 

 

haha ...

so am i,  im learning from that book too.

page 172. 

 
what is the include file for ??
Reason: