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

 
Vinin:

Or maybe just make a list of active instruments with the help of an EA. The approach is kind of complicated. It could be solved in a simpler way
We could simply specify a number in the input parameters, but the thing is that during trading, situations may occur, when trading is suspended on some currencies. Then EAs on other currencies should recalculate their parameters.
 
forexnew:
You could simply specify a number in the input parameters, but the point is that in the process of trading there may be situations where trading is suspended on some currencies. Then EAs on other currencies have to recalculate their parameters.

Why specify. The Expert Advisor can get it by itself. And at the same time, it can manage other Expert Advisors. Everything depends on the task
 

Figar0:

В самой функции ошибок нет, наверно ошибки возникают при попытке ее использовать, но это вы нам не показываете. Выкладывайте то что не компилится прямо файлом и гадать будет не надо.

//+------------------------------------------------------------------+
//|                                                  MACD Sample.mq4 |
//|                      Copyright © 2005, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+

extern double TakeProfit = 50;
extern double TrailingStop = 30;
extern double MACDOpenLevel=3;
extern double MACDCloseLevel=2;
extern double MATrendPeriod=26;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   double MacdCurrent, MacdPrevious, SignalCurrent;
   double SignalPrevious, MaCurrent, MaPrevious;
   int cnt, ticket, total;
// initial data checks
// it is important to make sure that the expert works with a normal
// chart and the user did not make any mistakes setting external 
// variables (Lots, StopLoss, TakeProfit, 
// TrailingStop) in our case, we check TakeProfit
// on a chart of less than 100 bars
   if(Bars<100)
     {
      Print("bars less than 100");
      return(0);  
     }
   if(TakeProfit<10)
     {
      Print("TakeProfit less than 10");
      return(0);  // check TakeProfit
     }
double Lots()      // Расчет используемого лота
  {
   double Lots;
   Lots=AccountFreeMargin()/10000*5;
   Lots=MathMin(15,MathMax(0.1,Lots));
   if(Lots<0.1) 
     Lots=NormalizeDouble(Lots,2);
   else
     {
     if(Lots<1) Lots=NormalizeDouble(Lots,1);
     else       Lots=NormalizeDouble(Lots,0);
     }
     return(Lots);
  }
//+------------------------------------------------------------------+  

// to simplify the coding and speed up access
// data are put into internal variables
   MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
   MacdPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);
   SignalCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0);
   SignalPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);
   MaCurrent=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,0);
   MaPrevious=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,1);

   total=OrdersTotal();
   if(total<1) 
     {
      // no opened orders identified
      if(AccountFreeMargin()<(1000*Lots))
        {
         Print("We have no money. Free Margin = ", AccountFreeMargin());
         return(0);  
        }
      // check for long position (BUY) possibility
      if(MacdCurrent<0 && MacdCurrent>SignalCurrent && MacdPrevious<SignalPrevious &&
         MathAbs(MacdCurrent)>(MACDOpenLevel*Point) && MaCurrent>MaPrevious)
        {
         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,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()); 
         return(0); 
        }
      // check for short position (SELL) possibility
      if(MacdCurrent>0 && MacdCurrent<SignalCurrent && MacdPrevious>SignalPrevious && 
         MacdCurrent>(MACDOpenLevel*Point) && MaCurrent<MaPrevious)
        {
         ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,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()); 
         return(0); 
        }
      return(0);
     }
   // it is important to enter the market correctly, 
   // but it is more important to exit it correctly...   
   for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&   // check for opened position 
         OrderSymbol()==Symbol())  // check for symbol
        {
         if(OrderType()==OP_BUY)   // long position is opened
           {
            // should it be closed?
            if(MacdCurrent>0 && MacdCurrent<SignalCurrent && MacdPrevious>SignalPrevious &&
               MacdCurrent>(MACDCloseLevel*Point))
                {
                 OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
                 return(0); // exit
                }
            // check for trailing stop
            if(TrailingStop>0)  
              {                 
               if(Bid-OrderOpenPrice()>Point*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-Point*TrailingStop)
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
                     return(0);
                    }
                 }
              }
           }
         else // go to short position
           {
            // should it be closed?
            if(MacdCurrent<0 && MacdCurrent>SignalCurrent &&
               MacdPrevious<SignalPrevious && MathAbs(MacdCurrent)>(MACDCloseLevel*Point))
              {
               OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
               return(0); // exit
              }
            // check for trailing stop
            if(TrailingStop>0)  
              {                 
               if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
                 {
                  if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
                     return(0);
                    }
                 }
              }
           }
        }
     }
   return(0);
  }
// the end.
 
Vinin:

Why would you specify this? The Expert Advisor can obtain it by itself. And at the same time, it can manage other Expert Advisors. It all depends on the task.

I don't know about managing other EAs. The EA is the same on all currency pairs, and the pattern is this:

if 2 currency pairs: on the first one it shows 2, on the second one it shows 1

If 3 currency pairs: the first one shows 3, the second one 2, the third one 1

if it is 4 currency pairs: the first one shows 4, the second one shows 3, the third one shows 2, the fourth one shows 1.

I have a feeling that what is calculated on the first currency is not taken into account by the Expert Advisor on the other currency pair, or it somehow depends on the number of open windows (currency pairs)!

 
Vinin:

Why specify. The councillor can get it himself. And at the same time he can manage other advisers. It all depends on the task.
Thank you. I have already figured it out myself. I just need to remove the line and count kp from 1
if(OrderSymbol()==Symbol())   break;
 
skyjet:

For starters, your lot calculation function is declared inside the start function. Take it out. Then there will be some errors with undeclared variable, but I think you can handle them on your own.
 
skyjet:
Why do I need NormalizeDlouble() in lot calculation?

In order for there to be two digits after the comma. Otherwise a position with 0.009 lot will not be opened, because it will not be a multiple of the minimum lot step.

   double Lots;
   Lots=AccountFreeMargin()/10000*5;
   Lots=MathMin(15,MathMax(0.1,Lots));
   if(Lots<0.1) 
     Lots=NormalizeDouble(Lots,2);
   else
     {
     if(Lots<1) Lots=NormalizeDouble(Lots,1);// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!
     else       Lots=NormalizeDouble(Lots,0);// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!
     } 

 

Guys!

how do i make every 40th order a larger lot? :: market and pending

how about this?

int Nom = OrderTicket();                        // Номер ордера

a= Nom%40;                                      // к-во делить на 40 без остатка
if a = 0
lot = 2;  

 
 
Figar0:

For starters, your lot calculation function is declared inside the start function. Take it out. Then there will be some errors with undeclared variable, but I think you can handle them on your own.
Thanks for the help.
 
DDFedor:
skyjet:
what is NormalizeDlouble() for in the lot calculation?

In order for there to be two decimal places after the comma. Otherwise, a position with a lot of 0.009 will not be opened, as it will not be a multiple of the minimum lot step.

Thank you for the clarification.
Reason: