How to start with MQL5 - page 27

 
Vladimir Karputov:

Possible idea: apply Fractals. Probably with Fractals the picture will become clearer ...

This will definately help me, but i dont understand how i would check to see if the fractal is above the bollinger band. 

 
Ahmad861 :

This will definately help me, but i dont understand how i would check to see if the fractal is above the bollinger band. 

Show the picture, please.

 
Vladimir Karputov:

Show the picture, please.

As you can see the left vertical line closes with a fractal under the bollinger band and the right vertical line has the close of candle 1 under bollinger band, the code i used earlier would check close prices under bb band but now integrating fractals, i can't seem to figure out how, as some fractalvalues have some random numbers or are empty

 
Vladimir Karputov:

Show the picture, please.

I managed to figure it out, by using !=EMPTY_VALUE to check if the array containing the fractal values are empty or not

 
Ahmad861 :

As you can see the left vertical line closes with a fractal under the bollinger band and the right vertical line has the close of candle 1 under bollinger band, the code i used earlier would check close prices under bb band but now integrating fractals, i can't seem to figure out how, as some fractalvalues have some random numbers or are empty

Yes, Fractals can contain '0.0' or 'EMPTY_VALUE'. Example:

Code: 'iFractals.mq5'

//+------------------------------------------------------------------+
//|                                                    iFractals.mq5 |
//|                              Copyright © 2021, Vladimir Karputov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2021, Vladimir Karputov"
#property version   "1.000"
//--- input parameters
input int         InputBars=9;
//---
int      handle_iFractals;             // variable for storing the handle of the iFractals indicator
bool     m_init_error      = false;    // error on InInit
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- forced initialization of variables
   m_init_error            = false;    // error on InInit
//--- create handle of the indicator iFractals
   handle_iFractals=iFractals(Symbol(),Period());
//--- if the handle is not created
   if(handle_iFractals==INVALID_HANDLE)
     {
      //--- tell about the failure and output the error code
      PrintFormat("Failed to create handle of the iFractals indicator for the symbol %s/%s, error code %d",
                  Symbol(),
                  EnumToString(Period()),
                  GetLastError());
      //--- the indicator is stopped early
      m_init_error=true;
      return(INIT_SUCCEEDED);
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   if(m_init_error)
      return;
//---
   double upper[],lower[];
   ArraySetAsSeries(upper,true);
   ArraySetAsSeries(lower,true);
   int start_pos=0,count=6;
   if(!iGetArray(handle_iFractals,UPPER_LINE,start_pos,count,upper) || !iGetArray(handle_iFractals,LOWER_LINE,start_pos,count,lower))
      return;
//---
   string text="";
   for(int i=0; i<count; i++)
     {
      //---
      string text_upper="";
      if(upper[i]==0.0 || upper[i]==EMPTY_VALUE)
         text_upper="";
      else
         text_upper=DoubleToString(upper[i],Digits());
      //---
      string text_lower="";
      if(lower[i]==0.0 || lower[i]==EMPTY_VALUE)
         text_lower="";
      else
         text_lower=DoubleToString(lower[i],Digits());
      //---
      text=text+IntegerToString(i)+"#: "+"Upper "+text_upper+", Lower "+text_lower+"\n";
     }
   Comment(text);
  }
//+------------------------------------------------------------------+
//| Get value of buffers                                             |
//+------------------------------------------------------------------+
bool iGetArray(const int handle,const int buffer,const int start_pos,
               const int count,double &arr_buffer[])
  {
   bool result=true;
   if(!ArrayIsDynamic(arr_buffer))
     {
      PrintFormat("ERROR! EA: %s, FUNCTION: %s, this a no dynamic array!",__FILE__,__FUNCTION__);
      return(false);
     }
   ArrayFree(arr_buffer);
//--- reset error code
   ResetLastError();
//--- fill a part of the iBands array with values from the indicator buffer
   int copied=CopyBuffer(handle,buffer,start_pos,count,arr_buffer);
   if(copied!=count)
     {
      //--- if the copying fails, tell the error code
      PrintFormat("ERROR! EA: %s, FUNCTION: %s, amount to copy: %d, copied: %d, error code %d",
                  __FILE__,__FUNCTION__,count,copied,GetLastError());
      //--- quit with zero result - it means that the indicator is considered as not calculated
      return(false);
     }
   return(result);
  }
//+------------------------------------------------------------------+


Result:

iFractals

Files:
iFractals.mq5  9 kb
 

How to close a position of a certain type

The code 'Close Positions Type.mq5'

The code is executed in the form of a script (ATTENTION! This is not an advisor !!!). The code closes all positions of the selected type BY ANY SYMBOL and ANY Magic Number.

//+------------------------------------------------------------------+
//|                                         Close Positions Type.mq5 |
//|                              Copyright © 2021, Vladimir Karputov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2021, Vladimir Karputov"
#property version   "1.000"
//---
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
CPositionInfo  m_position;                   // trade position object
CTrade         m_trade;                      // trading object
#property script_show_inputs
input ENUM_POSITION_TYPE InpPositionType  = POSITION_TYPE_BUY; // Close all position ...
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   ClosePositions(InpPositionType);
  }
//+------------------------------------------------------------------+
//| Close positions                                                  |
//+------------------------------------------------------------------+
void ClosePositions(const ENUM_POSITION_TYPE pos_type)
  {
   for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of current positions
      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
         if(m_position.PositionType()==pos_type)
           {
            if(m_position.PositionType()==POSITION_TYPE_BUY)
              {
               if(!m_trade.PositionClose(m_position.Ticket())) // close a position by the specified m_symbol
                  Print(__FILE__," ",__FUNCTION__,", ERROR: ","BUY PositionClose ",m_position.Ticket(),", ",m_trade.ResultRetcodeDescription());
              }
            if(m_position.PositionType()==POSITION_TYPE_SELL)
              {
               if(!m_trade.PositionClose(m_position.Ticket())) // close a position by the specified m_symbol
                  Print(__FILE__," ",__FUNCTION__,", ERROR: ","SELL PositionClose ",m_position.Ticket(),", ",m_trade.ResultRetcodeDescription());
              }
           }
  }
//+------------------------------------------------------------------+
Files:
 
Vladimir Karputov:

Yes, Fractals can contain '0.0' or 'EMPTY_VALUE'. Example:

Code: 'iFractals.mq5'


Result:


I got to know how to use fractals and it has helped me in coding my EA but coming back to my initial problem of having the chart form a V shape while the candles aren't necessarily forming a V.


 

Maximum price of the indicator in the visible window

Code: ChartGetDouble.mq5

Task: in the subwindow number '1' find out the maximum value of the window

//+------------------------------------------------------------------+
//|                                               ChartGetDouble.mq5 |
//|                              Copyright © 2021, Vladimir Karputov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2021, Vladimir Karputov"
#property version   "1.00"
#property script_show_inputs
//--- input parameters
input int   InpSubWindow   = 1;        // Subwindow number
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   Print(ChartGetDouble(ChartID(),CHART_PRICE_MAX,InpSubWindow));
  }
//+------------------------------------------------------------------+


Result:

2021.06.10 05:33:34.362  ChartGetDouble (USDCAD,M15)    57.3267
Files:
 
Vladimir Karputov:

Start with a small problem: describe the "level" algorithm. Start embodying your idea in MQL5 code. Then I will help.

This is what i had meant by levels, how can i automate the procedure of checking whether price in the past had been acting as a support or resistance level

 
Ahmad861 :

This is what i had meant by levels, how can i automate the procedure of checking whether price in the past had been acting as a support or resistance level

Problem # 1: Determine to what depth you need to scan the market.

Reason: