Download MetaTrader 5

Newbie need help

To add comments, please log in or register
Kiril Spiridonov
114
Kiril Spiridonov  

Hello! I really need help with my EA.

I show great results on backtesting, but unfortunately, I cannot publish it, because of errors.

I am not a professional programmer and I really need help.

I've copied and pasted some part of the help and I think it helped to solve some errors.

I get error 130 and 4051 and sometimes error 1


#property copyright "KirilSp"
#property link ""
#property version "1.0"
#property strict






extern double LotSize = 1;
extern double StopLoss = 50;
extern double TakeProfit = 0;
extern double TrailingStopLimit = 10;
extern double TrailingStopStop = 10;
extern int MagicNumber = 23310;
double volume;

bool CheckMoneyForTrade(string symb, double lots,int type)
  {
   double free_margin=AccountFreeMarginCheck(symb,type, lots);
   //-- if there is not enough money
   if(free_margin<0)
     {
      string oper=(type==OP_BUY)? "Buy":"Sell";
      Print("Not enough money for ", oper," ",lots, " ", symb, " Error code=",GetLastError());
      return(false);
     }
   //--- checking successful
   return(true);
  }
  
  bool IsNewOrderAllowed()
  {
//--- get the number of pending orders allowed on the account
   int max_allowed_orders=(int)AccountInfoInteger(ACCOUNT_LIMIT_ORDERS);

//--- if there is no limitation, return true; you can send an order
   if(max_allowed_orders==0) return(true);

//--- if we passed to this line, then there is a limitation; find out how many orders are already placed
   int orders=OrdersTotal();

//--- return the result of comparing
   return(orders<max_allowed_orders);
  }
  
  
bool OrderModifyPreChecked(int iTicket, double dPrice, double dSL, double dTP, datetime tExpire, color cColor=CLR_NONE) {
  if(OrderType()<=OP_SELL && ((NormalizeDouble(OrderStopLoss()-dSL,8)!=0) || (NormalizeDouble(OrderTakeProfit()-dTP,8)!=0))) return(true);
  else if(OrderType()>OP_SELL && ((NormalizeDouble(OrderStopLoss()-dSL,8)!=0) || (NormalizeDouble(OrderTakeProfit()-dTP,8)!=0) || (NormalizeDouble(OrderOpenPrice()-dPrice,8)!=0) || (tExpire!=OrderExpiration()))) return(true);
  return(false); }



bool OrderModifyCheck(int ticket,double price,double sl,double tp)
  {
//--- select order by ticket
   if(OrderSelect(ticket,SELECT_BY_TICKET))
     {
      //--- point size and name of the symbol, for which a pending order was placed
      string symbol=OrderSymbol();
      double point=SymbolInfoDouble(symbol,SYMBOL_POINT);
      //--- check if there are changes in the Open price
      bool PriceOpenChanged=true;
      int type=OrderType();
      if(!(type==OP_BUY || type==OP_SELL))
        {
         PriceOpenChanged=(MathAbs(OrderOpenPrice()-price)>point);
        }
      //--- check if there are changes in the StopLoss level
      bool StopLossChanged=(MathAbs(OrderStopLoss()-sl)>point);
      //--- check if there are changes in the Takeprofit level
      bool TakeProfitChanged=(MathAbs(OrderTakeProfit()-tp)>point);
      //--- if there are any changes in levels
      if(PriceOpenChanged || StopLossChanged || TakeProfitChanged)
         return(true);  // order can be modified      
      //--- there are no changes in the Open, StopLoss and Takeprofit levels
      else
      //--- notify about the error
         PrintFormat("Order #%d already has levels of Open=%.5f SL=%.5f TP=%.5f",
                     ticket,OrderOpenPrice(),OrderStopLoss(),OrderTakeProfit());
     }
//--- came to the end, no changes for the order
   return(false);       // no point in modifying 
  }
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+






int LongTicket;
int ShortTicket;
double RealPoint;

bool CheckVolumeValue(double volume,string &description)
  {
//--- minimal allowed volume for trade operations
   double min_volume=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN);
   if(volume<min_volume)
     {
      description=StringFormat("Volume is less than the minimal allowed SYMBOL_VOLUME_MIN=%.2f",min_volume);
      return(false);
     }

//--- maximal allowed volume of trade operations
   double max_volume=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MAX);
   if(volume>max_volume)
     {
      description=StringFormat("Volume is greater than the maximal allowed SYMBOL_VOLUME_MAX=%.2f",max_volume);
      return(false);
     }

//--- get minimal step of volume changing
   double volume_step=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_STEP);
   
   

   int ratio=(int)MathRound(volume/volume_step);
   if(MathAbs(ratio*volume_step-volume)>0.0000001)
     {
      description=StringFormat("Volume is not a multiple of the minimal step SYMBOL_VOLUME_STEP=%.2f, the closest correct volume is %.2f",
                               volume_step,ratio*volume_step);
      return(false);
     }
   description="Correct volume value";
   return(true);
  }



int init()
        {
           Print(MarketInfo(Symbol(), MODE_LOTSIZE));
      Print(MarketInfo(Symbol(), MODE_MINLOT));
      Print(MarketInfo(Symbol(), MODE_LOTSTEP));
      Print(MarketInfo(Symbol(), MODE_MAXLOT));
      RealPoint = RealPipPoint(Symbol());
      if (RealPoint == 0) return (true);
      else return (false);
     
        }



int start()
        {
   
   //--- price levels for orders and positions
   double priceopen,stoploss,takeprofit;
//--- ticket of the current order 
   int orderticket;
/*
   ... get the order ticket and new StopLoss/Takeprofit/PriceOpen levels
*/
//--- check the levels before modifying the order   
   if(OrderModifyCheck(orderticket,priceopen,stoploss,takeprofit))
     {
      //--- checking successful
      OrderModify(orderticket,priceopen,stoploss,takeprofit,OrderExpiration());
     }
                
                
                
        
                
                OrderSelect(LongTicket,SELECT_BY_TICKET);
                
                
                if(OrderCloseTime() != 0 || LongTicket == 0)
                {
                
                    
bool buy_condition_1 = iMA(NULL, 0, 12, 0, MODE_SMA, PRICE_CLOSE, 0)  > Ask + 0.0005 ;

double LongStopLoss;
double LongTakeProfit;


                                 
                if( buy_condition_1   )
                        {
                                
                                OrderSelect(ShortTicket,SELECT_BY_TICKET);
                                
                                if(OrderCloseTime() == 0 && ShortTicket > 0)
                                        {
                                                                                        
                                                bool Closed = OrderClose(ShortTicket,OrderLots(),Ask,0,Red);
                                        }                               

                                
                                
                                
                                
                                LongTicket = OrderSend(Symbol(),OP_BUY,LotSize,Ask,0,0,0,"Buy Order",MagicNumber,0,Green);
                                
                                
                                OrderSelect(LongTicket,SELECT_BY_TICKET); 
                                double OpenPrice = OrderOpenPrice();
                                
            
             
            if(StopLoss > 0) double LongStopLoss = OpenPrice - (StopLoss * RealPoint);
            if(TakeProfit > 0) double LongTakeProfit = OpenPrice + (TakeProfit * RealPoint);
            
            
                                if(LongStopLoss > 0 || LongTakeProfit > 0) 
                                {
               bool LongMod = OrderModify(LongTicket,OpenPrice,LongStopLoss, LongTakeProfit,0);
               if(!LongMod)
               Print("Error in OrderModify. Error code=",GetLastError());
               else
              
                 
                  OrderModify(LongTicket,0 ,Bid - 2*LongStopLoss*Point, Ask + 2*LongTakeProfit*Point,0);
                  Print( GetLastError());
                  
              
               Print("Order modified successfully.");
                                }
                                
                                                        
                                ShortTicket = 0;
                        }
                
                }               
                
                
                
              
               
       
                                
if (TrailingStopLimit > 0 && TrailingStopStop > 0)
         {
                     double TrailingStopLimitReal = TrailingStopLimit * RealPoint;
                     double TrailingStopStopReal = TrailingStopStop * RealPoint;
                     
                     OrderSelect(LongTicket, SELECT_BY_TICKET);             
                     if (OrderType() == OP_BUY && OrderCloseTime() == 0)
                        {
                           
                           if(Bid - OrderOpenPrice() > TrailingStopLimitReal) 
                              {

                                 if(OrderStopLoss() < Bid - (TrailingStopLimitReal * 2)) 
                                    {
                                       double LongTrailingStopLoss = OrderStopLoss() + TrailingStopStopReal;
                                       
                                     
                                       
                                      bool res = OrderModify(OrderTicket(), OrderOpenPrice(), LongTrailingStopLoss, OrderTakeProfit(), 0, MediumSeaGreen);      
                                      if(!res)
                                      Print("Error in OrderModify. Error code=",GetLastError());
                                      else
                                      
                                    
                                        
                                      OrderModify(OrderTicket(), 0, Bid - 2*LongTrailingStopLoss*Point, Ask + 2*OrderTakeProfit()*Point, 0, MediumSeaGreen);
                                      Print( GetLastError());
                                   
                                      Print("Order modified successfully.");
                                    }
                              }
                        }
                        
                     OrderSelect(ShortTicket, SELECT_BY_TICKET);   
                     if (OrderType() == OP_SELL && OrderCloseTime() == 0)
                         {
                           
                           if(OrderOpenPrice()- Ask > TrailingStopLimitReal) 
                              {
                              

                                 if(OrderStopLoss() > Ask + (TrailingStopLimitReal * 2)) 
                                    {
                                       double ShortTrailingStopLoss = OrderStopLoss() - TrailingStopStopReal;
                                      bool res1 = OrderModify(OrderTicket(), OrderOpenPrice(), ShortTrailingStopLoss, OrderTakeProfit(), 0, MediumSeaGreen);
                                      if(!res1)
                                       Print("Error in OrderModify. Error code=",GetLastError());
                                       else
                                       
                                       
                                       OrderModify(OrderTicket(), 0, Bid + 2*ShortTrailingStopLoss*Point, Ask - 2*OrderTakeProfit()*Point, 0, MediumSeaGreen);
                                       Print( GetLastError());
                                       
                                        
                                        
                                       Print("Order modified successfully.");   
                                    }
                              }
                          }     
                        
                
                }               
                                return(0);

        }


double CalcPoint;
double RealPipPoint(string Currency)
        {
                int CalcDigits = MarketInfo(Currency,MODE_DIGITS);
                if(CalcDigits == 2 || CalcDigits == 3) double CalcPoint = 0.01;
                else if(CalcDigits == 4 || CalcDigits == 5) CalcPoint = 0.0001;
                return(CalcPoint);
        }    


Ian Tavener
1347
Ian Tavener  

Just, at a glance, it seems you are using a lot of undeclared variables.  Like int orderticket;

I think you mean here, you want to select by orderticket. If that's the case, maybe you want to use this to get order ticket:

int tradeticket;
for(int i=0;i<OrdersTotal();i++)
{
   if(!OrderSelect(i,SELECT_BY_POS))
       continue;
   tradeticket=OrderTicket();
}
To add comments, please log in or register