Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 588

 
lil_lil:

I am closing 4 positions at once, how can I count them as one in a row in the loss-counting function?

They may close, because of slippage, not at the same price and the time may be different

Trial:

If the broker allows counter closing, we should open a counter order with a volume equal to the volume of cumulatively closed orders to fix the closing price. Then the closing price is fixed and you can leisurely execute 4 trades to close the opposite orders.

 

Can you tell me how to determine how the margin is calculated?

Some brokers charge margin on each position. And some brokers, only on the difference in volumes on buy and sell. (I.e. if the account is full lock, then the margin is zero)


How do I determine the method of margin calculation programmatically?

 

Hello. I am looking for a fractal above the MA.

Looking for a fractal above the MA, I memorize a bar, then I look for a fractal from this bar and if it is less than the first one I find, then true

Constantly false in the comments

Comment(f_ma(Symbol(),0,i));
//+----------------------------------------------------------------------------+

bool f_ma(string sy="0",int tf=0,int nf=0) 
  {
   if(sy=="" || sy=="0") sy=Symbol();
   double f=0,MA=0,fr1,fr2;
   int    d=MarketInfo(sy,MODE_DIGITS),s;
   if(d==0) if(StringFind(sy,"JPY")<0) d=4; else d=2;
   int    i,k=iBars(sy,tf),kf,num_bar;

   for(i=nf+2; i<k; i++) 
     {
      f=iFractals(sy,tf,MODE_UPPER,i);
      MA=iMA(NULL,0,35,0,MODE_EMA,PRICE_CLOSE,i);
      if(f!=0) 
        {
        // kf++;
         if(f>MA) {num_bar=i; fr1=NormalizeDouble(f, d);continue;}
         
        }
     }
     //
      for(i=nf+ num_bar; i<k; i++) 
     {
      f=iFractals(sy,tf,MODE_UPPER,i);
      if(f!=0) 
        {
        fr2=NormalizeDouble(f, d);
        if(fr2<fr1) return(true);
        }
     }
     
     
   return(false);
  }
 
Ghabo:

Hello. I am looking for a fractal above the MA.

Looking for a fractal above the MA, I memorize a bar, then I look for a fractal from this bar and if it is lower than the first one I find, then true

Constantly false in the comments

The problem is in this line

         if(f>MA) {num_bar=i; fr1=NormalizeDouble(f, d);continue;}


If a fractal is found and it is above the MA, we should interrupt the cycle instead of continuing it. After the first fractal is found there will be another one and a third one and so on to ...

 
Alexey Viktorov:

The problem is in this line


If a fractal is found and it is above the MA, we should interrupt the cycle instead of continuing it. After the first fractal is found there will be another one and a third one and so on to ...

Thank you. Replaced by break;Now it is always true. but the conditionif(fr2<fr1) is not met.
 
Ghabo:
Thank you. Replaced by break;Now it is always true. but the conditionif(fr2<fr1) is not met.

So we have to add this condition before displaying the function's result. What is so hard about it?

 
Alexey Viktorov:

So you have to add this condition before the function outputs the result. What's so hard about it?

Shouldn't the function return false if the condition is not met?

#property strict
#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
if(rates_total<100) return(0);
int limit=rates_total-prev_calculated;
if(limit>200)limit=200;
   for(int i=limit; i>0; i--)
     {
     
     
     Comment(f_ma(Symbol(),0,i));
     }
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
  //-------------------
  bool f_ma(string sy="0",int tf=0,int nf=0) 
  {
   if(sy=="" || sy=="0") sy=Symbol();
   double f=0,MA=0,fr1,fr2;
   int    d=MarketInfo(sy,MODE_DIGITS),s;
   if(d==0) if(StringFind(sy,"JPY")<0) d=4; else d=2;
   int    i,k=iBars(sy,tf),kf,num_bar;

   for(i=nf+2; i<k; i++) 
     {
      f=iFractals(sy,tf,MODE_UPPER,i);
      MA=iMA(NULL,0,35,0,MODE_EMA,PRICE_CLOSE,i);
      if(f!=0) 
        {
        // kf++;
         if(f>MA) {num_bar=i; fr1=NormalizeDouble(f, d);break;}
         
        }
     }
     //
      for(i=nf+ num_bar; i<k; i++) 
     {
      f=iFractals(sy,tf,MODE_UPPER,i);
      if(f!=0) 
        {
        fr2=NormalizeDouble(f, d);
        if(fr2<fr1) return(true);
        }
     }
     
     
   return(false);
  }
  
 
Ghabo:

Shouldn't the function return false if the condition is not met?

Check that iFractals returns zero or an empty value.

 
Alexey Viktorov:

Check that iFractals returns zero or an empty value.

There is an empty value in the data window. Replacedif(f!=0) with if(f!=EMPTY_VALUE), no change in result.
 
Ghabo:
There is an empty value in the data window. Replacedif(f!=0) with if(f!=EMPTY_VALUE), the result is unchanged.

1. I said check, not replace. iFractals returns 0 if there is no fractal. I don't use it regularly and naturally I don't clog up the undried remnants of memory with such trivia.

2. The function, though terribly written, still works fine.

3. The last value is displayed in Comment. Replace it with

     Print("************ ", f_ma(Symbol(), 0, 0));
And see how many times it prints
2018.07.30 21:02:43.656 00 EURUSD.e,H1: ************ true