Download MetaTrader 5

FREE EA: The Forbidden Fruit EA - page 2

To add comments, please log in or register
Cuong Truong
4956
Cuong Truong  

Here's the function i'm working on to automatically close all trades if price hit the following prices that are input by user in the settings box.

What's wrong with it, and why isn't it working? 

 

//=== Global variables


extern bool ActivateAutoCloseBreakOutOfRange = True;
extern double HowManyPipsToAutoClose = 10;
double pips;
extern double MustBeLower  = 1.10; //Price must be lower than this value to sell
extern double MustBeHigher = 1.06; //Price must be higher than this value to sell

extern double StartingDeposit = 1000;
extern double MoneyAboveBalance=10; //how much profit above the StartingDeposit before EA can close trades


//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
        double ticksize = MarketInfo(Symbol(), MODE_TICKSIZE);
        if (ticksize == 0.00001 || ticksize == 0.001)
           pips = ticksize*10;
           else pips =ticksize;
          
//---
   return(INIT_SUCCEEDED);
  }


//======I want the function below to automatically close all trades, if price move outside of price range
//====== also, before it close all trades, EA checks to see if current equity is higher than balance input in global setting.

if(ActivateAutoCloseBreakOutOfRange == True)
{
if( (Bid > (MustBeHigher + (HowManyPipsToAutoClose*pips))) && (AccountEquity() > (StartingDeposit + MoneyAboveBalance) ) ) CloseAllOrders();
if( (Bid < (MustBeLower  - (HowManyPipsToAutoClose*pips))) && (AccountEquity() > (StartingDeposit + MoneyAboveBalance) ) ) CloseAllOrders();
};


//+------------------------------------------------------------------+

void CloseAllOrders() {
   for (int trade = OrdersTotal() - 1; trade >= 0; trade--) {
      if(!OrderSelect(trade, SELECT_BY_POS, MODE_TRADES))continue;
      if (OrderSymbol() == Symbol()) {
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) {
            if ( (OrderType() == OP_BUY) || (OrderType()== OP_SELL) ) 
            if(!OrderClose(OrderTicket(), OrderLots(), Bid, slip, Blue)) GetLastError();
         }
         Sleep(1000);
      }
   }
}
Trinh Dat
40296
Trinh Dat  
 if(!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), slip, Blue)) GetLastError();
        
Arnold Bobrinskiy
14544
Arnold Bobrinskiy  
Cuong Truong:
you don't put it anywhere, you just need to load it from the settings menu.
Do you use Stoploss = 5000? It's the same if you don't use it at all.
Cuong Truong
4956
Cuong Truong  
Trinh Dat:

why do I get the error: if expression is not allow in global scope.

How do I fix that? 

Stuart Browne
6978
Stuart Browne  
Cuong Truong:

why do I get the error: if expression is not allow in global scope.

How do I fix that? 

Most likely, you have accidentally closed a function out by a misplaced } so your IF expression falls out into a global scope rather than the local function
Abdullah Alrai
30155
Abdullah Alrai  
//=== Global variables


extern bool ActivateAutoCloseBreakOutOfRange = True;
extern double HowManyPipsToAutoClose = 10;
double pips;
extern double MustBeLower  = 1.10; //Price must be lower than this value to sell
extern double MustBeHigher = 1.06; //Price must be higher than this value to sell
extern int MagicNumber=1378;
extern int slip = 3;
extern double StartingDeposit = 1000;
extern double MoneyAboveBalance=10; //how much profit above the StartingDeposit before EA can close trades


//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
        double ticksize = MarketInfo(Symbol(), MODE_TICKSIZE);
        if (ticksize == 0.00001 || ticksize == 0.001)
           pips = ticksize*10;
           else pips =ticksize;
          
//---
   return(INIT_SUCCEEDED);
  }

int start(){
//======I want the function below to automatically close all trades, if price move outside of price range
//====== also, before it close all trades, EA checks to see if current equity is higher than balance input in global setting.

if(ActivateAutoCloseBreakOutOfRange == True)
{
if( (Bid > (MustBeHigher + (HowManyPipsToAutoClose*pips))) && (AccountEquity() > (StartingDeposit + MoneyAboveBalance) ) ) CloseAllOrders();
if( (Bid < (MustBeLower  - (HowManyPipsToAutoClose*pips))) && (AccountEquity() > (StartingDeposit + MoneyAboveBalance) ) ) CloseAllOrders();
};
return(0);
}

//+------------------------------------------------------------------+

void CloseAllOrders() {
   for (int trade = OrdersTotal() - 1; trade >= 0; trade--) {
      if(!OrderSelect(trade, SELECT_BY_POS, MODE_TRADES))continue;
      if (OrderSymbol() == Symbol()) {
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) {
            if ( (OrderType() == OP_BUY) || (OrderType()== OP_SELL) ) 
            if(!OrderClose(OrderTicket(), OrderLots(), Bid, slip, Blue)) GetLastError();
         }
         Sleep(1000);
      }
   }
}
i think it is correct now you lost your start func.... and i repair it
Cuong Truong
4956
Cuong Truong  
Arnold Bobrinskiy:
Do you use Stoploss = 5000? It's the same if you don't use it at all.
Let's say if you want the EA to trade inside a range of 200 pips and you want the EA to stop trading if price exceed 200 pips. You can set the stop loss to 201 or 202 pips.
Siti Latifah
12440
Siti Latifah  

hello,

I can't find your mql4 (ea) , can you send me ?

Cuong Truong
4956
Cuong Truong  
Siti Latifah:

hello,

I can't find your mql4 (ea) , can you send me ?

sent
Langat Naftali
1426
Langat Naftali  
Cuong Truong:
Let's say if you want the EA to trade inside a range of 200 pips and you want the EA to stop trading if price exceed 200 pips. You can set the stop loss to 201 or 202 pips.
If you need to close exactly at 200 pips, then use OrderOpenPrice() +/-200 points else if you need trailing when in profit of more than 200 pips ,then will be : 200+or- stoploss allowed by broker.
To add comments, please log in or register