Help error !

 
//+------------------------------------------------------------------+
//|                                                     Morphius.mq4 |
//|                        Copyright 2022, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
input double TakeProfit    =400;
input double StopLoss      =170; 
input double Lots          =0.75;


void OnTick()
  {
int    ticket,total;  
 
double CloseAllTrades()
{
for (int i=OrdersTotal(); i>=0; i--)
{
if(OrderSelect(i,SELECT_BY_POS)==true)

if (OrderSymbol() ==Symbol())
  {
   OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),5,Red);  
  }
}
}
   
   if(TakeProfit<10)
     {
      Print("TakeProfit less than 10");
      return;
     }
double Ma20m1 = iMA( 
          _Symbol,           // symbol 
             PERIOD_M1,        // timeframe 
             20,        // MA averaging period 
             0,         // MA shift 
             MODE_EMA,        // averaging method 
             PRICE_CLOSE,    // applied price 
             0             // shift 
   );     

double Ma5m1 = iMA( 
          _Symbol,           // symbol 
             PERIOD_M30,        // timeframe 
             5,        // MA averaging period 
             0,         // MA shift 
             MODE_EMA,        // averaging method 
             PRICE_CLOSE,    // applied price 
             0             // shift 
   );
   
double Ma5m1l = iMA( 
          _Symbol,           // symbol 
             PERIOD_M30,        // timeframe 
             5,        // MA averaging period 
             0,         // MA shift 
             MODE_EMA,        // averaging method 
             PRICE_CLOSE,    // applied price 
             1             // shift 
   );   
     
   total=OrdersTotal();
   if(total<1)
     {
      //--- no opened orders identified
      if(AccountFreeMargin()<(1000*Lots))
        {
         Print("We have no money. Free Margin = ",AccountFreeMargin());
         return;
        }
      //--- check for long position (BUY) possibility
      if(
      Ma5m1  > Ma5m1l
       
       )//
        {
         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Bid-StopLoss*Point,Ask+TakeProfit*Point,"macd sample",16384,0,Green);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
               Print("BUY order opened : ",OrderOpenPrice());
           }
         else
            Print("Error opening BUY order : ",GetLastError());
            if(
            Ma5m1 < Ma5m1l
            )
            CloseAllTrades();
            
         return;
        }
      //--- check for short position (SELL) possibility
      if(
      Ma5m1 < Ma5m1l
      
       )// 
        {
         ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Ask+StopLoss*Point,Bid-TakeProfit*Point,"macd sample",16384,0,Red);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
               Print("SELL order opened : ",OrderOpenPrice());
           }
         else
            Print("Error opening SELL order : ",GetLastError());
            if(
            Ma5m1 > Ma5m1l
            )
            CloseAllTrades();
            
        }
      //--- exit from the "no opened orders" block
      return;
     }
   
  }
//+------------------------------------------------------------------+



I get an error ['CloseAllTrades' - function declarations are allowed on global, namespace or class scope only Morphius.mq4 37 8]

what is that ?

Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 

problem resolved now i get another error


double CloseAllTrades()

{

for (int i=OrdersTotal(); i>=0; i--)

{

if(OrderSelect(i,SELECT_BY_POS)==true)



if (OrderSymbol() ==Symbol())

  {

   OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),5,Red);  

  }

}

}


the error is '}' - not all control paths return a value Morphius.mq4 43 1


 
Use the </> button to insert your code.
 
    >
  1. Ofir Nissim #: the error is '}' - not all control paths return a value Morphius.mq4 43 1

    You declared your function returns a double. What are you returning before the last brace?

  2. OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),5,Red);  

    You assume the order is a buy; code fails on sell orders. Use OrderClosePrice() and be direction independent.

 
Ofir Nissim #:

problem resolved now i get another error



the error is '}' - not all control paths return a value Morphius.mq4 43 1


Your double CloseAllTrades() should be a void… or at least a bool…. OrderClose() is a Boolean function… you can return the result of that… bool result = OrderClose(………).. and before last } return(result)