Download MetaTrader 5
To add comments, please log in or register
Do you have questions to the application's developer - ask them in Comments section
Mccallum28 2016.04.15 21:53 

I have the following EA  that I would like to open a long position when the indicator plots a blue x and short when it plots an amber x. Can someone point out the error in the code please.


//|                                                Raz1pairs.mq4 |
//|                      Copyright © 2011, MetaQuotes Software Corp. |
//|                               |
// This EA is for study purpose only. it is not profitable in anyway.

#property copyright "Copyright © 2011, MetaQuotes Software Corp." // #property copyright  is used for the ownership of the EA
#property link      ""                   // #property ownership is used for the owners website link.

extern string Symbol_1 = "EURUSD"; // in here i have specified what Symbol i will let the Ea trade in. in my example it is EURUSD.
                                   // extern means that when i add the EA on chart at "inputs". it will be possible to change 
                                   // its value when i add the Ea on chart. so whenever you use extern in your code you will see it
                                   // at input screen. 
                                   // string is used , because to indetify which pair i want to trade. and we can only indentify
                                   // pairs by text.

//| expert initialization function                                   |

//| expert deinitialization function                                 |
int deinit()
//| expert start function                                            |

datetime newbar; // the part datetime newbar; so that everytime an arrow is given it wont open a new order every tick.

int start() // this starts the engine of the code.


if(newbar==Time[0])return(0); // 
else newbar=Time[0];

// below is the code to import a custom indicator into an EA. 
// double ArrowUp is used to give the indicator input a name called ArrowUp
// iCustom is used to import the indicator.
// (Null indicates we are using the current chart symbol ( in our case EURUSD)
// 0 is the current chart period
// "supersignals" is the name of the indicator we import in the EA
// 0 is the buffer Value of the indicator.
// 1 is the last in the sentence is the shift parameter. this checks the closing value of the last bar.

double iprofiteerUp = iCustom(NULL,0,"iprofiteer(13)",1,1); 

double iprofiteerDown = iCustom(NULL,0,"iprofiteer(13)",2,1);

if (iprofiteerUp != 1 ) // when a Blue arrow is given on the chart it will give a signal to open a buy order.

// ordersend is used to open a buy or sell order. in our case a buy Order.
// Symbol_1 = the symbol that was added in extern string Symbol_1 = "EURUSD"; 
// OP_BUY = to give a buy order when a blue arrow is given.
// OP_SELL = to give a sell order when a red arrow is given.
// 0.1 = lotsize 
// MarketInfo(Symbol_1,MODE_ASK) = gives a signal to server to ask for last incoming price.
// 2 = slippage
// NULL = Stoploss. since i have not indentified it in this Ea it is not used
// NULL = TakeProfit. since i have not indentified it in this Ea it is not used
// "Rapidfire" a bunch of text i can give in the order.
// magic = the magic number i can give every order. 
// NULL = datetime expiration for pending orders. since i dont open any pending orders in this EA i have it on NULL.
// LimeGreen = the color of the arrow that you see on a chart when a buy is done.
    OrderSend(Symbol_1,OP_BUY,2, MarketInfo(Symbol_1,MODE_ASK), 2, NULL, NULL, "RapidFire", NULL, LimeGreen); 
if (iprofiteerDwn) != 2 ) // when a red arrow is given on the chart it will give a signal to open a sell order.
  OrderSend(Symbol_1,OP_SELL,0.1, MarketInfo(Symbol_1,MODE_BID), 2, NULL, NULL, "RapidFire", NULL, FireBrick);   
return(0); // returns back to the start function of the program.
Keith Watford
Keith Watford 2016.04.15 22:21  

These seem a bit unlikely

if (iprofiteerUp != 1 )

if (iprofiteerDwn) != 2 )

a more likely check would be

if (iprofiteerUp != 0 &&  iprofiteerUp != EMPTY_VALUE)
whroeder1 2016.04.16 14:16  
  1. Check your return codes and find out why. 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. And had you you would know that NULL is not a valid price (SL/TP) nor a magic number, and that LimeGreen is not a valid datetime (expiration.) OrderSend - Trade Functions - MQL4 Reference
    OrderSend(Symbol_1,OP_BUY,2, MarketInfo(Symbol_1,MODE_ASK), 2, NULL, NULL, "RapidFire", NULL, LimeGreen); 
  3. Do not trade multiple currencies in one EA
    • You can't use any predefined variables, can't use the tester, must poll (not OnTick,) and usually other problems.
    • Code it to trade the chart pair only. Look at the others if you must.
    • Then put it on other charts to trade the other pairs. Done.
To add comments, please log in or register