Download MetaTrader 5

return value of 'OrderSend' should be checked?????

To add comments, please log in or register
Reilly Gailey
428
Reilly Gailey 2016.11.21 09:33 

Hey so I wrote up my first ea, but im getting a couple warnings I dont know how to fix, can someone please help?

 

here is the whole ea

 

//+------------------------------------------------------------------+
//|                                          Jim Dandy Course EA.mq4 |
//|                                                           Reilly |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Reilly"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

//////////////////////////////////////////////////////////////////////
//INPUT//
//////////////////////////////////////////////////////////////////////
extern int takeprofit=50;
extern double stoploss=25;
extern int fastma=5;
extern int fastmashift=0;
//^ ma shift is offset of linegraph on chart
extern int fastmamethod=0;
//^ma method is simple/exponential moving average etc
extern int fastmaappliedto=0;
//^applied to is basing the moving average on the closing price of the bar or the opening price etc
extern int slowma=21;
extern int slowmashift=0;
extern int slowmamethod=0;
extern int slowmaappliedto=0;
extern double lotsize=0.01;
extern int magicnumber = 1337;
double pips;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   double ticksize = MarketInfo(Symbol(),MODE_TICKSIZE);
   if (ticksize = 0.00001 || 0.001)
   double pips = (ticksize*10);
   else pips = ticksize;
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---

double previousfast = iMA(NULL,0,fastma,fastmashift,fastmamethod,fastmaappliedto,1);
double currentfast = iMA(NULL,0,fastma,fastmashift,fastmamethod,fastmaappliedto,1);
//^(NULL=moving average on the current currency pair. 0=the current timeframe. fastma= the moving average period.
double previousslow = iMA(NULL,0,slowma,slowmashift,slowmamethod,slowmaappliedto,1);
double currentslow = iMA(NULL,0,slowma,slowmashift,slowmamethod,slowmaappliedto,1);

if(previousfast<previousslow && currentfast>currentslow)
//^sell
   if(OrdersTotal()==0)
   OrderSend(Symbol(),OP_BUY,lotsize,Ask,3,Ask-(stoploss*pips),Ask+(takeprofit*pips),"EA Jim Dandy MA Trade",magicnumber,0,Green);
if(previousfast>previousslow && currentfast<currentslow)
//^buy
   if(OrdersTotal()==0)
    OrderSend(Symbol(),OP_SELL,lotsize,Bid,3,Bid+(stoploss*pips),Bid-(takeprofit*pips),"EA Jim Dandy MA Trade",magicnumber,0,Red);
  
  }
//+------------------------------------------------------------------+


 

agu2a
68
agu2a 2016.11.26 04:57  

When opening orders you should check them like

bool Buy = OrderSend(.... 

Keith Watford
Moderator
9639
Keith Watford 2016.11.26 05:09  
agu2a:

When opening orders you should check them like

bool Buy = OrderSend(.... 

No, OrderSend returns an integer, the ticket number or -1 if it fails.

OP, check the return value and if it is -1, then you know the order failed, so take appropiate action, check for last error.

Biantoro Kunarto
15259
Biantoro Kunarto 2016.11.26 06:07  

You can use this :

void OnTick()
  {
//---

double previousfast = iMA(NULL,0,fastma,fastmashift,fastmamethod,fastmaappliedto,1);
double currentfast = iMA(NULL,0,fastma,fastmashift,fastmamethod,fastmaappliedto,1);
//^(NULL=moving average on the current currency pair. 0=the current timeframe. fastma= the moving average period.
double previousslow = iMA(NULL,0,slowma,slowmashift,slowmamethod,slowmaappliedto,1);
double currentslow = iMA(NULL,0,slowma,slowmashift,slowmamethod,slowmaappliedto,1);

int ticket = 0;
if(previousfast<previousslow && currentfast>currentslow)
//^sell
   if(OrdersTotal()==0)
      ticket = OrderSend(Symbol(),OP_BUY,lotsize,Ask,3,Ask-(stoploss*pips),Ask+(takeprofit*pips),"EA Jim Dandy MA Trade",magicnumber,0,Green);
if(previousfast>previousslow && currentfast<currentslow)
//^buy
   if(OrdersTotal()==0)
      ticket = OrderSend(Symbol(),OP_SELL,lotsize,Bid,3,Bid+(stoploss*pips),Bid-(takeprofit*pips),"EA Jim Dandy MA Trade",magicnumber,0,Red);
  
  }
whroeder1
14742
whroeder1 2016.11.26 15:41  

ReillyFox: getting a couple warnings I dont know how to fix

   if(OrdersTotal()==0)
      ticket = OrderSend(...);
  1. What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  2. Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.) Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 forum
To add comments, please log in or register