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

 
Aleksei Stepanenko:

From two points on a line, you can find the price of an arbitrary third point on that line, also in the future (and vice versa).

Thank you! I will try it.

P.S. The only thing. I do not understand at a glance. Will it work in the Expert Advisor, in MT4?

 
Hello, fellow experts. I need your help in correcting the indicator. The essence of the indicator is as follows. Calculate the value of the price increase relative to the previous bar. For zero takes a star bar. That is, the opening price is equal to the closing price. When compiling no errors, but when testing an error on the line 80 20 characters. The signal line is also incorrectly drawn. But I think this is the reason for incorrect calculation of the main buffer. Please help me to fix it.
//+------------------------------------------------------------------+
//|                                                         MSBB.mq4 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

#include <MovingAverages.mqh>

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1  clrGreen
#property indicator_color2  clrRed
#property  indicator_width1  1
input int            InpMSBBPeriod=3;        // Period
input ENUM_MA_METHOD InpMSBBMethod=MODE_SMA;  // Method
//--- indicator buffers
double         ExtMSBBBuffer[];
double         ExtTempBuffer[];
double         ExtPriceBuffer[];
double         ExtSignalBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit(void)
  {
//--- indicator buffers mapping
   IndicatorDigits(Digits-2);
//--- drawing settings
   IndicatorBuffers(4);
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(0,ExtMSBBBuffer);
   SetIndexBuffer(1,ExtSignalBuffer);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(2,ExtTempBuffer);
   SetIndexBuffer(2,ExtPriceBuffer);
   SetIndexDrawBegin(1,InpMSBBPeriod);
//--- name for DataWindow and indicator subwindow label
   IndicatorShortName("MSBB("+IntegerToString(InpMSBBPeriod)+")");
   SetIndexLabel(0,"MSBB");
   SetIndexLabel(1,"Signal");
//---
   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[])
  {
   int    i;//limit;
//------
   if(rates_total<=InpMSBBPeriod || InpMSBBPeriod<=2)
      return(0);
   /*//--- counting from 0 to rates_total
      ArraySetAsSeries(ExtMSBBBuffer,false);
      ArraySetAsSeries(ExtSignalBuffer,false);
      ArraySetAsSeries(open,false);
      ArraySetAsSeries(high,false);
      ArraySetAsSeries(low,false);
      ArraySetAsSeries(close,false);*/
//---
  // limit=rates_total-prev_calculated;
   //if(prev_calculated>0)
     // limit++;
//--- typical price and its moving average
   for(i=0; i<rates_total; i++)
     {
      ExtTempBuffer[i] = NormalizeDouble((close[i]-open[i])/Point(),2);
      ExtPriceBuffer[i] = NormalizeDouble((close[i+1]-open[i+1])/Point(),2);
      //ExtMSBBBuffer[i]=price_open+ExtTempBuffer[i];
      //Print("ExtPriceBuffer[i] = ", ExtPriceBuffer[i]);
      if(ExtTempBuffer[i]==0)
         ExtMSBBBuffer[i]=0.0;
      if(ExtPriceBuffer[i]>0 && ExtTempBuffer[i]>0)
        {
         double price_open = NormalizeDouble((open[i]-open[i+1])/Point(),2);
         double price_close = NormalizeDouble((close[i]-close[i+1])/Point(),2);
         if((price_open<0 && price_close>0)||(price_open>0 && price_close<0))
            ExtMSBBBuffer[i] = 0.0;
         if((price_open<0 && price_close<0)||(price_open>0 && price_close>0))
            ExtMSBBBuffer[i]=ExtTempBuffer[i]+price_open;
        }
      if(ExtPriceBuffer[i]>0 && ExtTempBuffer[i]<0)
        {
         double price_open = NormalizeDouble((open[i]-close[i+1])/Point(),2);
         double price_close = NormalizeDouble((close[i]-open[i+1])/Point(),2);
         if((price_open<0 && price_close>0)||(price_open>0 && price_close<0))
            ExtMSBBBuffer[i] = 0.0;
         if((price_open>0 && price_close>0)||(price_open<0 && price_close<0))
            ExtMSBBBuffer[i]=ExtTempBuffer[i]+price_open;
        }
      if(ExtPriceBuffer[i]<0 && ExtTempBuffer[i]<0)
        {
         double price_open = NormalizeDouble((open[i]-open[i+1])/Point(),2);
         double price_close = NormalizeDouble((close[i]-close[i+1])/Point(),2);
         if((price_open<0 && price_close>0)||(price_open>0 && price_close<0))
            ExtMSBBBuffer[i]=0.0;
         if((price_open<0 && price_close<0)||(price_open>0 && price_close>0))
            ExtMSBBBuffer[i]=ExtTempBuffer[i]+price_open;
        }
      if(ExtPriceBuffer[i]<0 && ExtTempBuffer[i]>0)
        {
         double price_open = NormalizeDouble((open[i]-close[i+1])/Point(),2);
         double price_close = NormalizeDouble((close[i]-open[i+1])/Point(),2);
         if((price_open>0 && price_close<0)||(price_open<0 && price_close>0))
            ExtMSBBBuffer[i]=0.0;
         if((price_open>0 && price_close>0)||(price_open<0 && price_close<0))
            ExtMSBBBuffer[i]=ExtTempBuffer[i]+price_open;
        }
      //--- signal line counted in the 2-nd buffer
      //ExtSignalBuffer[i]=iMAOnArray(ExtMSBBBuffer,0,InpMSBBPeriod,0,InpMSBBMethod,0);
      SimpleMAOnBuffer(rates_total,prev_calculated,1,InpMSBBPeriod+2,ExtMSBBBuffer,ExtSignalBuffer);
      Print ("ExtSignalBuffer = ", ExtSignalBuffer[i]);
      //--- done
     }
   /* if(ExtPriceBuffer[i]>0||ExtPriceBuffer[i]<0)
     {
      ExtMSBBBuffer[i] = ExtPriceBuffer[i]+ExtTempBuffer[i];
      Print("ExtMSBBBuffer[i] = ", ExtMSBBBuffer[i]);
     }
   if(ExtPriceBuffer[i]==0)
     {
      ExtMSBBBuffer[i] = 0.0;
      Print("ExtMSBBBuffer[i] = ", ExtMSBBBuffer[i]);
     }
   }*/
//---
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
 
How do I find out the number of the local agent on which the single test is taking place?
 

Good afternoon!

Can you please help with an EA?

It makes trades on RSI signals from 30 and 70 levels in the appropriate direction, creates a grid.

I have a kind of stop loss % in it, but from time to time orders are hanging and will not close until I close them manually or until I sell the deposit.

I.e. such orders are opened, the price has already gone away for 5000 pips and beyond, but they are still hanging in the red.

You need to find the error. If this is not possible, we should insert a separate Stop Loss in pips into our EA.

I tried combining 2 EAs into one, but it didn't work with my skills.

Files:
 

Hello. Can you give me a hint? I need to get the number of points passed in the last tick. But I can't get it.

#property indicator_chart_window
#property indicator_buffers 1
double ExtMapBuffer[];
double dOldPriceEURUSD, dNewPriceEURUSD;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   IndicatorDigits(5);
   SetIndexBuffer(0, ExtMapBuffer);
   SetIndexEmptyValue(0,0.0);        
   dOldPriceEURUSD=iClose("EURUSD",0,0);
   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[])
  {
   dNewPriceEURUSD=iClose("EURUSD",0,0);
   double delta=NormalizeDouble(dOldPriceEURUSD-dNewPriceEURUSD,5);
   ExtMapBuffer[0] = delta;
   Alert(delta);     
   dOldPriceEURUSD=dNewPriceEURUSD;
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
Forallf:

Hello. Can you give me a hint? I need to get the number of points passed in the last tick. But it's not working.

Try this.

//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2018, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 1
double ExtMapBuffer[];
double dOldPriceEURUSD, dNewPriceEURUSD;
double delta;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   IndicatorDigits(5);
   SetIndexBuffer(0, ExtMapBuffer);
   SetIndexEmptyValue(0, 0.0);
   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[])
  {
   dNewPriceEURUSD = NormalizeDouble(Close[0],Digits);
   delta = dOldPriceEURUSD - dNewPriceEURUSD;
   Comment(" delta = ", DoubleToStr(delta ,5));
   dOldPriceEURUSD = dNewPriceEURUSD;
   ExtMapBuffer[0] = delta;
 Alert(" delta = ", DoubleToStr(delta ,5));
   return(rates_total);
  }
//+------------------------------------------------------------------+
Domain Registration Services
  • www.registryrocket.com
Get a unique domain name plus our FREE value-added services to help you get the most from it. Call it a "dot-com name," a URL, or a domain. Whatever you call it, it's the cornerstone of your online presence, and we make getting one easy. Simply enter the name you want in the search field above, and we'll tell you instantly whether the name is...
 
Александр:

Try it this way.

Thank you!
 

Hello again.
Please pay attention to a newbie's question.
I need to point out errors in the code, because in the tester, the Expert Advisor does not open orders...
The compiler does not show any errors or warnings, the same journal shows no errors...

extern double Lot=0.1;            
extern int Slippage = 3;
extern int TakeProfit = 30;
extern int StopLoss   = 30;
extern int MA_Smoth_S = 60;
extern int MA_Smoth_B = 12;
extern int MA_Simpl_S = 3;
extern int MA_Simpl_B = 1;
int start()
         {
          //___________________

          double SL, TP;
          int MA_Simpl_S_Cl,      //
              MA_Simpl_S_Op,      //
              MA_Simpl_B_Cl,      //
              MA_Simpl_B_Op;      //
         
          //________________

          //------------

          SL=NormalizeDouble(Bid-StopLoss*Point,Digits);      // 
          TP=NormalizeDouble(Bid+TakeProfit*Point,Digits);    //
          SL = StopLoss;                        
          TP = TakeProfit;
          if(_Digits==5 || _Digits==3)
            {
             SL = SL*10;
             TP = TP*10;
             return(0);
            }
            
          //_______________

          MA_Smoth_S = iMA(NULL,0,60,0,MODE_SMMA,PRICE_CLOSE,1);
          MA_Smoth_B = iMA(NULL,0,12,0,MODE_SMMA,PRICE_CLOSE,1);
          MA_Simpl_S = iMA(NULL,0,3,0,MODE_SMA,PRICE_CLOSE,1);
          MA_Simpl_B = iMA(NULL,0,1,0,MODE_SMA,PRICE_CLOSE,1);
          MA_Simpl_S_Cl = iMA(NULL,0,3,0,MODE_SMA,PRICE_CLOSE,1);
          MA_Simpl_S_Op = iMA(NULL,0,3,0,MODE_SMA,PRICE_CLOSE,2);
          MA_Simpl_B_Cl = iMA(NULL,0,1,0,MODE_SMA,PRICE_CLOSE,1);
          MA_Simpl_B_Op = iMA(NULL,0,1,0,MODE_SMA,PRICE_CLOSE,2);
          
          //______________________

          while(MA_Smoth_B > MA_Smoth_S)
               {
                if(MA_Simpl_B_Op < MA_Simpl_S_Op && MA_Simpl_B_Cl > MA_Simpl_S_Cl)
                  {
                   bool check = OrderSend(Symbol(),OP_BUY,Lot,NormalizeDouble(Ask, Digits),Slippage,SL,TP,"Buy",0,0,clrGreen);
                   return(0);
                  }
               }
               
          //_____________________

          while(MA_Smoth_S > MA_Smoth_B)
               {
                if(MA_Simpl_B_Op > MA_Simpl_S_Op && MA_Simpl_B_Cl < MA_Simpl_S_Cl)
                  {
                   check = OrderSend(Symbol(),OP_SELL,Lot,NormalizeDouble(Ask, Digits),Slippage,SL,TP,"Sell",0,0,clrRed);
                   return(0);
                  }   
               }     
          return(0);
         } 
 

Good day to all!

I am trying to mql4 to mql5.

Question: Why is mql5 calculating and displaying some unknown for me expression like 2.99999999 - (minus) 05 instead of the difference between current price and value of Hay variable, which should be <1 (like in mql4)?

How should I make mql5 correctly calculate the difference between these values? I normalize all values using NormalizeDouble(), but the above values

values are displayed unchanged. This is strange to me as both values are of doble type

Thank you all for your help.

#include <Trade\Trade.mqh>                                        
int tm, s1 ;                                    
double P=SymbolInfoDouble(Symbol(),SYMBOL_BID),S=P+0.0030,T=P-0.0010,Lou,Hay,DL=0.0030; 
CTrade            m_Trade;                 //структура для выполнения торговых операций
//=============================================================================================================
void OnTick()
  {
Print("===============",SymbolInfoDouble(Symbol(),SYMBOL_BID) - Hay,"===Hay====",Hay,"===SymbolInfoDouble()====",SymbolInfoDouble(Symbol(),SYMBOL_BID)); 

m_Trade.Sell(0.1,Symbol(),P,S,T);
Hay=SymbolInfoDouble(Symbol(),SYMBOL_BID);

   }


 
MrBrooklin:

Hello Ivan, no one scolds newbies here, on the contrary, they try to help. I am a beginner myself. Now, regarding your question. Several positions are opened because the check to open a position was performed but the check was forgotten to stop. The operator return returns control to the calling program (taken from the MQL5 Reference).

We must add return to the Expert Advisor code (highlighted in yellow):

In addition, to prevent the compiler from generating warnings, one more condition should be added in the Buy and Sell position opening conditions to check OrderSend(mrequest,mresult). This condition is defined by the if operator and should look like this:

One more thing should be taken into account. Sometimes, when moving from one trading day to another at 23:59:59, an opened position closes and then, at 00:00:00, a new position opens. This is so-called rollover close and rollover open, which depends on the particular forex dealer and its trading conditions. Search the forum, I've got some information about it somewhere.

Yours sincerely, Vladimir.


Hello.

Thank you very much for your reply! But I do not understand why do I need the return operator? There are two conditions in this code and the check should stop when one of them is fulfilled.

//--- есть ли открытые позиции?
   bool Buy_opened=false;  // переменные, в которых будет храниться информация 
   bool Sell_opened=false; // о наличии соответствующих открытых позиций

   if(PositionSelect(_Symbol)==true) // есть открытая позиция
     {
      if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)  // если истина, выполняем условие 1
        {
         Buy_opened=true;  //это длинная позиция
        }
      else if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)  // иначе выполняем условие 2
        {
         Sell_opened=true; // это короткая позиция
        }
     }
Or is it not?
Reason: