[ARCHIVE] Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Nowhere without you - 3. - page 198

 
mmmmm.... well personally i think it's written correctly - the only problem is the placement of the code snippets.... could you place it correctly? it's not clear to me how to do that.
 
splxgf:
Honestly, I don't know if you can describe one function within another, I've never come across it.
Don't confuse us darkies. At the level of this branch, it is enough to state that all user functions must be located outside the Start function.
 
CLAIN:
mmmmm.... well personally, i think it's all written correctly - the only problem is the placement of the code snippets.... could you place it correctly? it's not clear to me how to do that

Well I won't post it exactly, but as has already been written all the custom functions are beyond start.

At concept level

Start (){}

MACD{}

AnalyzeSignal(){}

Remove the description of custom functions to a separate file, make sure the rest of the program compiles without error bracketing messages and dock each function to the very end of the resulting code.

 

Thank you, it worked =) only the deals don't open, but that's a small thing =) couldn't have done it without you )

//+------------------------------------------------------------------+
//|                                                     MACD ^^^.mq4 |
//|                      Copyright © 2011, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

extern int TP = 100; 
extern int TS = 100;
extern double lots = 0.1;
int slip = 3;
int Magic = 1;

int total;
int ticket = 0; //объявил тотал и тикет



//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----


int signal = signal_MACD(); 
AnalyzeSignal(signal);




//----
   return(0);
  }
//+------------------------------------------------------------------+






int signal_MACD()
{
   int MACD[8] = {0,1,2,3,4,5,6,7};
MACD[0] = iMACD(Symbol(),1,5,34,5,PRICE_CLOSE,MODE_MAIN,7);
MACD[1] = iMACD(Symbol(),1,5,34,5,PRICE_CLOSE,MODE_MAIN,6);
MACD[2] = iMACD(Symbol(),1,5,34,5,PRICE_CLOSE,MODE_MAIN,5);
MACD[3] = iMACD(Symbol(),1,5,34,5,PRICE_CLOSE,MODE_MAIN,4);
MACD[4] = iMACD(Symbol(),1,5,34,5,PRICE_CLOSE,MODE_MAIN,3);
MACD[5] = iMACD(Symbol(),1,5,34,5,PRICE_CLOSE,MODE_MAIN,2);
MACD[6] = iMACD(Symbol(),1,5,34,5,PRICE_CLOSE,MODE_MAIN,1);
MACD[7] = iMACD(Symbol(),1,5,34,5,PRICE_CLOSE,MODE_MAIN,0);
   total = OrdersTotal();
   if(total<1)
   {
      if(MACD[0]<0)
      {
         if(MACD[1]>0)
         {
            if(MACD[2]>0)
            {
               if(MACD[3]>0)
               {
                  if(MACD[4]>0)
                  {
                     if(MACD[5]>0)
                     {
                        if(MACD[6]>0)
                        {
                           if(MACD[7]<0)
                           {
                              return(-100);
                           }
                        }
                     }
                  }
               }
            }
         }
      }
      if(MACD[0]>0)
      {
         if(MACD[1]<0)
         {
            if(MACD[2]<0)
            {
               if(MACD[3]<0)
               {
                  if(MACD[4]<0)
                  {
                     if(MACD[5]<0)
                     {
                        if(MACD[6]<0)
                        {
                           if(MACD[7]>0)
                           {
                              return(100);
                           }
                        }
                     }
                  }
               }
            }
         }
      }
   }
}

void AnalyzeSignal(int signal)
{
   if(signal == 100)
   {
      ticket = OrderSend(Symbol(),OP_BUY,lots,Ask,slip,Bid-TS*Point,Bid+TP*Point,"покупаем",Magic,0,Green);
      if(ticket>0)
      {
         OrderSelect(ticket,SELECT_BY_TICKET);
         Print("открылись на покупку по цене:", OrderOpenPrice());
      }
      else
      {
         Print("открыться не удалось по причине:", GetLastError());
         return(0);
      }
   }
   if(signal == -100)
   {
      ticket = OrderSend(Symbol(),OP_SELL,lots,Bid,slip,Ask+TS*Point,Ask-TP*Point,"Продаем",Magic,0,Green);
      if(ticket>0)
      {
         OrderSelect(ticket,SELECT_BY_TICKET);
         Print("открылись на продажу по цене:", OrderOpenPrice());
      }
      else
      {
         Print("открыться не удалось по причине:", GetLastError());
         return(0);
      }
   }
}

 

seek advice from a programmer


avatar
1
Metamql 20.09.2011 14:31
Dear Professionals, please give some advice. There are two moving averages with periods of 5 and 60. When crossing the fast MA over the slow one from the bottom upwards + filters, it opens a Buy trade. The problem is, how to make a trade open only once and when it closes, not open a new trade, even if there are suitable conditions to open, until there is a reverse crossover?
 

Hello.

Please help - please respond to my post on page 196. (question about int MAGIC;)

Thank you in advance.

 
nemo811:

Hello.

Please help - please respond to my post on page 196. (question about int MAGIC;)

Thank you in advance.


Have you tried to make functions with parameters? So as not to use external global variables (everyone can call them by their own name)
 
sergeev:

seek advice from a programmer


1
Metamql 20.09.2011 14:31
Dear Professionals, please give some advice. There are two moving averages with periods of 5 and 60. When crossing a fast MA over a slow MA from the bottom up + filters, opens a trade on Buy. The problem is, how to make a trade open only once and when it closes, not to open a new trade, even if there are suitable conditions to open, until a reverse crossover occurs?

set a flag to prohibit further opening and remove the flag when crossing back, a couple of bars can be delayed to prevent bouncing
 
Vinin:

Have you tried making functions with parameters? So as not to use external global variables (everyone can call them their own way)

I mean, like:

int OrdTicket_b(int MAGIC)
 {
  string SMB=Symbol();
  int OrdTicket;
  int i;
  for(i=0;i<OrdersTotal();i++)
   {
    if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == true)
     {
      if(OrderSymbol()==SMB)
       {
        if(OrderMagicNumber()==MAGIC || OrderMagicNumber()==777)
         {
          if(OrderType()==OP_BUY || OrderType()==OP_SELL)
           {
            OrdTicket=OrderTicket();
           }  
         }  
       }          
     }
   }
  return(OrdTicket);
 }

???

...and the condition

    if(OrderMagicNumber()==MAGIC || OrderMagicNumber()==777)

will be tracked correctly?

 
nemo811:

I mean, like:

???

...and the condition

will be tracked correctly?


int OrdTicket_b(int lMAGIC)
 {
  string SMB=Symbol();
  int OrdTicket=0;
  int i;
  for(i=0;i<OrdersTotal();i++)
   {
    if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == true)
     {
      if(OrderSymbol()==SMB)
       {
        if(OrderMagicNumber()==lMAGIC)
         {
          if(OrderType()==OP_BUY || OrderType()==OP_SELL)
           {
            OrdTicket=OrderTicket();
           }  
         }  
       }          
     }
   }
  return(OrdTicket);
 }
Like this, the variable OrdTicket should be assigned some default value. Otherwise there are no open positions and the function will return something
Reason: