Really Dumb Newbie Question

Back to topics list To post a new topic, please log in or register
avatar
3
Maudise 2016.08.29 23:38
 

Hi All,

Hoping this is a really quick fix.  I've been teaching myself the MQL4 trading language and having followed a couple of examples successfully, I decided to start building from scratch.  I've written a much longer code than the one below, but the problem I've had is that none of the trades will execute on a back test.  I have compiled it and it only gives warnings (Key one being 'not all control paths return a value).

I've written a much smaller code just to test my knowledge of executing a simple trade, for the purposes of trying to debug my larger code, however this 'simple' one isn't working either.  The code was simply built so that it would look at the RSI (iRSI), and if it was below 50, then execute a buy trade of 1 lot with a TP of 50 and SL of 30.

//+------------------------------------------------------------------+
//|                                               SimpleExecutor.mq4 |
//|                                                               MB |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "MB"
#property link      "http://www.mql5.com"
#property version   "1.00"
//#property strict

//Simple testing environment to build to see if I can execute the trade
extern double Buy_Zone = 50; //What buy zone for the RSI
extern double Sell_Zone = 70;  //What is your sell zone for the RSI?

int start()
   {
   int ticket; 
   double RSI_Reading;
   
   RSI_Reading = iRSI(_Symbol,0,14,PRICE_CLOSE,0);
   
      if(RSI_Reading < Buy_Zone)
      {
      //buy
      ticket = OrderSend(Symbol(),OP_BUY,1,Ask,3,Ask - 30*Point,Ask + 50*Point,"My Order",16384,0,Green);
      return(0);
      }
   
   }
   


Can anyone give me a quick pointer as to why this isn't working? For reference I'm backtesting on GBPUSD15m From 18th to 29th August 2016.

Other things which are baffling me;

    - Why does the code generate the 'Not all control paths have values' warning

    - why does OrderSend need Symbol() rather than _Symbol or are they interchangeable?

    - Does the MAGIC number actually do anything other than identify it within the trading history?


Much obliged for any assistance

Maudise


avatar
Moderator
3416
GumRai 2016.08.29 23:51 #
 
int start()
   {
   int ticket; 
   double RSI_Reading;
   
   RSI_Reading = iRSI(_Symbol,0,14,PRICE_CLOSE,0);
   
      if(RSI_Reading < Buy_Zone)
      {
      //buy
      ticket = OrderSend(Symbol(),OP_BUY,1,Ask,3,30,50,"My Order",16384,0,Green);
      return(0);   //Remove this
      }
    return(0);     //and put here
   }

A stop Loss of 30.00000 will be way above the GBPUSD price, and a tp of 50.0000 will be unreachable.

You have to calculate the SL and TP properly as you mean that the SL should be Ask - 0.0030

avatar
3
Maudise 2016.08.30 00:04 #
 
GumRai:

A stop Loss of 30.00000 will be way above the GBPUSD price, and a tp of 50.0000 will be unreachable.

You have to calculate the SL and TP properly as you mean that the SL should be Ask - 0.0030


Thanks for the quick response.  I've just amended the code (I had done an edit, apologies for not annotating the body with the edit).  It's cleared the warning which is definitely forward motion but still no dice :S.

The code is below again with updated version.  I had also looked at doing as Ask + 50*Point but have seen this both done within the OrderSend function, but also having it outside using a NormalizeDouble(Ask + 50*Points,Digits).  Is it a style thing, with the benefits being the latter will be less prone to errors with odd numbers?


//+------------------------------------------------------------------+
//|                                               SimpleExecutor.mq4 |
//|                                                               MB |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "MB"
#property link      "http://www.mql5.com"
#property version   "1.00"
#property strict

//Simple testing environment to build to see if I can execute the trade
extern double Buy_Zone = 50; //What buy zone for the RSI
extern double Sell_Zone = 70;  //What is your sell zone for the RSI?

int start()
   {
   int ticket; 
   double RSI_Reading;
   
   RSI_Reading = iRSI(_Symbol,0,14,PRICE_CLOSE,0);
   
      if(RSI_Reading < Buy_Zone)
      {
      //buy
      ticket = OrderSend(Symbol(),OP_BUY,1,Ask,3,Ask - 0.0030,Ask + 0.0050,"My Order",16384,0,Green);
      }
   return(0);
   }


avatar
1211
FMIC 2016.08.30 00:38 #
 
Maudise: Thanks for the quick response.  I've just amended the code (I had done an edit, apologies for not annotating the body with the edit).  It's cleared the warning which is definitely forward motion but still no dice :S.

The code is below again with updated version.  I had also looked at doing as Ask + 50*Point but have seen this both done within the OrderSend function, but also having it outside using a NormalizeDouble(Ask + 50*Points,Digits).  Is it a style thing, with the benefits being the latter will be less prone to errors with odd numbers?

  1. First of all, learn to add verification code to your EA in order to debug what is happening. By this, I mean adding a few "Print()" statements so that you can look into the Experts or Journal logs to see debug information. In this case Print out the Ticket that is being returned by OrderSend.
  2. Since, OrderSend is not working, that means that the Ticket number is probably -1, signaling an Error, so Print out the Error by using the GetLastError() function or by using the newer _LastError variable. The Error number will help identify the cause (see documentation on OrderSend).
  3. After fixing the error, your code will undoubtedly start to spit out hundreds of orders until your get an account stop-out (see link below by WHRoeder about stop-out and FreeMargin), because you don't track when an order has been opened and thus prevent a new one from being placed on the next tick.
  4. Don't use the old style of programing in MQL4. If you are starting to learn, then you might as well start correctly applying the new style which is more compatible with MQL5. So, instead of "start()" use "OnTick()".
    See the following link for a rundown of the changes: https://docs.mql4.com/mql4changes
  5. So, while you at it with the new style, use the strict format so as to improve conditions and catch mistakes early during the compile phase. (i.e. use the "#property strict" as mentioned in the previous link of MQL4 changes).
  6. Also, as friendly advice (because some users don't agree on this), don't ever use "NormalizeDouble". Rather do things correctly, and for that I am just going to quote another user here who usually provides these links (with a few extra points that you should consider as your EA progresses):
    WHRoeder:
    Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is always wrong
avatar
3
Maudise 2016.08.30 00:46 #
 

Thanks, FMIC.  I'll give those pointers a run through.  It's a challenge on all new languages if the syntax etc changes then it can be difficult to spot which is the 'new' version versus which is the 'old' version.

I've done some reading around and had picked up the NormalizeDouble, I guess another pitfall of reading how-to's and intro guides which can quickly become dated.

RE: Point 3 - Totally agree, the one of the things I intend to tinker with is the checking how many trades are open and to only place new trades on new bars etc.

Highly appreciative of the help so far, and hopefully will be back with some more intriguing queries in the fullness of time!

Maudise

Back to topics list  

To add comments, please log in or register