coding problem

 
Hello everyone, I am calling you because I have an error in my code, can you help me solve it please.
"' symbolIsAlreadyTrade' - function declarations are allowed on global, namespace or class scope only (ligne 5)".

Thank you.

#include <Trade\Trade.mqh>
CTrade trade ;
void OnTick(){

bool symbolIsAlreadyTrade(string symbole, uint period){
   
   if(HistorySelect(TimeCurrent() - period, TimeCurrent())){
     
      for(int i = 0; i < HistoryDealsTotal(); i += 1){
         
         ulong ticket = HistoryDealGetTicket(i);
         
         if(HistoryDealGetString(ticket, DEAL_SYMBOL) == symbole && HistoryDealGetInteger(ticket, DEAL_ENTRY) == (int)DEAL_ENTRY_IN){
            return true;
         }
      }
   }
   
   return false;
}

  
   MqlRates mrate[];
   ArraySetAsSeries(mrate,true);
   if(CopyRates(_Symbol,_Period,0,20,mrate)<0)
      return;
     if(PositionsTotal() < 20){ 
   
     {
      if(mrate[1].close>mrate[19].close)
        {
         double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
         double stoploss = ask - 400 * _Point ;
         double takeprofit = ask + 20 * _Point ;
         if(trade.Buy(0.1 , _Symbol, ask, stoploss, takeprofit))
           {
            int code = (int) trade . ResultRetcode();
            ulong ticket = trade . ResultOrder();
            Print(" Code: " + (string) code);
            Print(" Ticket: " + (string) ticket);
           }
           }
        }
      if(mrate[1].close<mrate[19].close)
        if(PositionsTotal() < 7){
        {
        
         double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
         double sellStoploss = bid + 300 * _Point ;
         double sellTakeprofit = bid - 20 * _Point ;
         if(trade.Sell(0.1, _Symbol, bid, sellStoploss, sellTakeprofit))
           {
            int code = (int) trade . ResultRetcode();
            ulong ticket = trade . ResultOrder();
            Print(" Code: " + (string) code);
            Print(" Ticket: " + (string) ticket);
           }
        }
     }
  }
  }
 
This is MQL5 saying that you are defining a function inside a function. If you just move the void OnTick () function to below the return false it will solve this problem.
 
Tim G .: Ceci est MQL5 indiquant que vous définissez une fonction à l' intérieur d'une fonction. Si vous déplacez simplement la fonction void OnTick () sous le return false, cela résoudra ce problème.

Thank you very much for your response and your promptness. I will have one last question, the problem with this code is that it opens multiple positions per second, unfortunately I can't find the code that opens a position every 15 candles.

If it's possible, can you help me please

#include <Trade\Trade.mqh>
CTrade trade ;


bool symbolIsAlreadyTrade(string symbole, uint period){
   
   if(HistorySelect(TimeCurrent() - period, TimeCurrent())){
     
      for(int i = 0; i < HistoryDealsTotal(); i += 1){
         
         ulong ticket = HistoryDealGetTicket(i);
         
         if(HistoryDealGetString(ticket, DEAL_SYMBOL) == symbole && HistoryDealGetInteger(ticket, DEAL_ENTRY) == (int)DEAL_ENTRY_IN){
            return true;
         }
      }
   }
   
   return false;
}

  void OnTick(){
   MqlRates mrate[];
   ArraySetAsSeries(mrate,true);
   if(CopyRates(_Symbol,_Period,0,10,mrate)<0)
      return;
     
   
     {
      if(mrate[1].close>mrate[9].close)
        {
         double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
         double stoploss = ask - 20000 * _Point ;
         double takeprofit = ask + 50 * _Point ;
         if(trade.Buy(0.1 , _Symbol, ask, stoploss, takeprofit))
           {
            int code = (int) trade . ResultRetcode();
            ulong ticket = trade . ResultOrder();
            Print(" Code: " + (string) code);
            Print(" Ticket: " + (string) ticket);
           }
           }
        }
      if(mrate[1].close<mrate[9].close)
        
        {
        
         double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
         double sellStoploss = bid + 20000 * _Point ;
         double sellTakeprofit = bid - 30 * _Point ;
         if(trade.Sell(0.1, _Symbol, bid, sellStoploss, sellTakeprofit))
           {
            int code = (int) trade . ResultRetcode();
            ulong ticket = trade . ResultOrder();
            Print(" Code: " + (string) code);
            Print(" Ticket: " + (string) ticket);
           }
        }
     }
  
  


?

 
39863092:

Thank you very much for your response and your promptness. I will have one last question, the problem with this code is that it opens multiple positions per second, unfortunately I can't find the code that opens a position every 15 candles.

If it's possible, can you help me please


?

//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2020, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#include <Trade\Trade.mqh>
CTrade trade ;


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool symbolIsAlreadyTrade(string symbole, uint period)
  {

   if(HistorySelect(TimeCurrent() - period, TimeCurrent()))
     {

      for(int i = 0; i < HistoryDealsTotal(); i += 1)
        {

         ulong ticket = HistoryDealGetTicket(i);

         if(HistoryDealGetString(ticket, DEAL_SYMBOL) == symbole && HistoryDealGetInteger(ticket, DEAL_ENTRY) == (int)DEAL_ENTRY_IN)
           {
            return true;
           }
        }
     }

   return false;
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
   MqlRates mrate[];
   ArraySetAsSeries(mrate,true);
   if(CopyRates(_Symbol,_Period,0,10,mrate)<0)
      return;
   datetime open_time;
   if(PositionsTotal()>0)
      if(PositionSelectByTicket(PositionGetTicket(PositionsTotal()-1)))
        {
         open_time=(datetime)PositionGetInteger(POSITION_TIME);
         if(TimeCurrent()<open_time+(15*tf_turn(_Period)))
            return;
        }
   if(mrate[1].close>mrate[9].close)
     {
      double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
      double stoploss = ask - 20000 * _Point ;
      double takeprofit = ask + 50 * _Point ;
      if(trade.Buy(0.1, _Symbol, ask, stoploss, takeprofit))
        {
         int code = (int) trade . ResultRetcode();
         ulong ticket = trade . ResultOrder();
         Print(" Code: " + (string) code);
         Print(" Ticket: " + (string) ticket);
        }
     }

   if(mrate[1].close<mrate[9].close)

     {

      double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
      double sellStoploss = bid + 20000 * _Point ;
      double sellTakeprofit = bid - 30 * _Point ;
      if(trade.Sell(0.1, _Symbol, bid, sellStoploss, sellTakeprofit))
        {
         int code = (int) trade . ResultRetcode();
         ulong ticket = trade . ResultOrder();
         Print(" Code: " + (string) code);
         Print(" Ticket: " + (string) ticket);
        }

     }
  }
//+------------------------------------------------------------------+
int tf_turn(int tf)
  {
   switch(tf)
     {
      case 0:
         return(PERIOD_CURRENT*60);
      case 1:
         return(1*60);
      case 5:
         return(5*60);
      case 15:
         return(15*60);
      case 30:
         return(30*60);
      case 60:
         return(60*60);
      case 240:
         return(240*60);
      case 1440:
         return(1440*60);
      case 10080:
         return(10080*60);
      case 43200:
         return(43200*60);
      case 2:
         return(2*60);
      case 3:
         return(3*60);
      case 4:
         return(4*60);
      case 6:
         return(6*60);
      case 10:
         return(10*60);
      case 12:
         return(12*60);
      case 16385:
         return(60*60);
      case 16386:
         return(120*60);
      case 16387:
         return(180*60);
      case 16388:
         return(240*60);
      case 16390:
         return(360*60);
      case 16392:
         return(480*60);
      case 16396:
         return(720*60);
      case 16408:
         return(1440*60);
      case 32769:
         return(10080*60);
      case 49153:
         return(43200*60);
      default:
         return(PERIOD_CURRENT*60);
     }
  }
//+------------------------------------------------------------------+

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

here you go sir

 
Code simplified
int tf_turn(int tf){ return PeriodSeconds(tf); }

 
Khuman Bakhramirad :

ici vous allez monsieur

Thank you very much ! This is exactly what I wanted, the code was really complicated 😮

 
William Roeder :
Code simplifié

Thank you very much !

 
William Roeder:
Code simplified

thank you that was amazing but actually it needs to be like this :

int tf_turn(ENUM_TIMEFRAMES tf){ return PeriodSeconds(tf); }
Reason: