Creating a robot - page 4

 
Yes, I wrote, I wrote, I thought about it, I didn't say it accurately. I always have trouble articulating my thoughts. On the tenth change, the words seem to make sense
 
Роман Жилин:

Oooh, thank you very much, with so much information you can get so much done...

I'm just about to leave on a business trip, so I'm thinking of going deeper into the materials I've been given, but the coding... I could do it on a sheet of paper too, it would be a good training tool...


Regards, Roman

This is a tiny fraction of what you need to know, a grain of sand in the sea of program code. But it's not enough to know what to use, where to use it and when to use it!

If we proceed from the name of the subject"Robot Creation", then you need to have a break-even (profitable, or whatever you want to call it) trading strategy, and only then study the MQL5 programming language.

By the way, the MetaEditor of the MT5 terminal has the MQL5 Wizard, with the help of which you can easily get the code of the ready-made Expert Advisor using the modules of trading signals, which were created on the basis of popular indicators, without any knowledge of the programming language. With the help of the MQL5 Wizard, you can quickly build an Expert Advisor and test your strategy, if it is based on indicators only. Here is the link to the article about building a trading robot using MQL5 Wizard: https://www.mql5.com/ru/articles/171.

Sincerely, Vladimir.

Мастер MQL5: Создание эксперта без программирования
Мастер MQL5: Создание эксперта без программирования
  • www.mql5.com
При создании автоматических торговых систем возникает необходимость написания алгоритмов анализа рыночной ситуации и генерации торговых сигналов, алгоритмов сопровождения открытых позиций, систем управления капиталом и контроля риска торговли. После того как код модулей написан самой сложной задачей является компоновка всех частей и отладка...
 
MrBrooklin:

57 and a bit. And the answer to your question about the way is already known, and I quote:

"Roman Zhilin:

No, there is no process in freelancing, which you can develop on your own as you need. And the only one to blame for my mistakes will be myself, not a third-party programmer. So, you will have to learn, learn, code, stumble, improve your strategies, and learn again".

Sincerely, Vladimir.

A good selection, thank you.

Reminds me of Lenin's will :) But it's right, it's never too late to learn.

In order to understand what kind of Expert Advisor you need, you should begin to work on it in the first place.

 

Added two more buttons to close a position

//+------------------------------------------------------------------+
//|                                                         0002.mq5 |
//|                        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"
#define    InpMagic  182979245
//---
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
//---
CPositionInfo  m_position; // trade position object
CTrade         m_trade;    // trading object
CSymbolInfo    m_symbol;   // symbol info object
//---
input double InpLots          =0.01; // Lots
//---
double m_adjusted_point;   // point value adjusted for 3 or 5 points
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   if(!m_symbol.Name(Symbol())) // sets symbol name
      return(INIT_FAILED);;
//---
   m_trade.SetExpertMagicNumber(InpMagic);
   m_trade.SetMarginMode();
   m_trade.SetTypeFillingBySymbol(m_symbol.Name());
//--- tuning for 3 or 5 digits
   int digits_adjust=1;
   if(m_symbol.Digits()==3 || m_symbol.Digits()==5)
      digits_adjust=10;
   m_adjusted_point=m_symbol.Point()*digits_adjust;
//---
   m_trade.SetDeviationInPoints(3*digits_adjust);
   if(!m_position.Select(Symbol()))
     {
      CheckObject();
      CheckObjectClose();
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   if(ObjectFind(0,"BUY")==0)
     {
      ObjectDelete(0,"BUY");
     }
   if(ObjectFind(0,"SELL")==0)
     {
      ObjectDelete(0,"SELL");
     }
//---
   if(ObjectFind(0,"Close BUY")==0)
     {
      ObjectDelete(0,"Close BUY");
     }
   if(ObjectFind(0,"Close SELL")==0)
     {
      ObjectDelete(0,"Close SELL");
     }
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   CheckButon();
   CheckButonClose();
  }
//+------------------------------------------------------------------+
//| Check for long position closing                                  |
//+------------------------------------------------------------------+
bool CheckButon(void)
  {
//---
   bool res=false;
     {
      if(ObjectGetInteger(0,"BUY",OBJPROP_STATE)!=0)
        {
         ObjectSetInteger(0,"BUY",OBJPROP_STATE,0);
         double price=m_symbol.Ask();
           {
            //--- open position
            if(m_trade.PositionOpen(m_symbol.Name(),ORDER_TYPE_BUY,InpLots,price,0.0,0.0))
               printf("Position by %s to be opened",m_symbol.Name());
            else
              {
               printf("Error opening BUY position by %s : '%s'",m_symbol.Name(),m_trade.ResultComment());
               printf("Open parameters : price=%f,TP=%f",price,0.0);
              }
            PlaySound("ok.wav");
           }
        }
      if(ObjectGetInteger(0,"SELL",OBJPROP_STATE)!=0)
        {
         ObjectSetInteger(0,"SELL",OBJPROP_STATE,0);
         double price0=m_symbol.Bid();
           {
            if(m_trade.PositionOpen(m_symbol.Name(),ORDER_TYPE_SELL,InpLots,price0,0.0,0.0))
               printf("Position by %s to be opened",m_symbol.Name());
            else
              {
               printf("Error opening SELL position by %s : '%s'",m_symbol.Name(),m_trade.ResultComment());
               printf("Open parameters : price=%f,TP=%f",price0,0.0);
              }
            PlaySound("ok.wav");
           }
        }
      res=true;
     }
//--- result
   return(res);
  }
//+------------------------------------------------------------------+
//| Check for long position closing                                  |
//+------------------------------------------------------------------+
bool CheckObject(void)
  {
//---
   bool res=false;
     {
      ObjectCreate(0,"BUY",OBJ_BUTTON,0,0,0);
      ObjectSetInteger(0,"BUY",OBJPROP_XDISTANCE,ChartGetInteger(0,CHART_WIDTH_IN_PIXELS)-102);
      ObjectSetInteger(0,"BUY",OBJPROP_YDISTANCE,37);
      ObjectSetString(0,"BUY",OBJPROP_TEXT,"BUY");
      ObjectSetInteger(0,"BUY",OBJPROP_BGCOLOR,clrMediumSeaGreen);
      ObjectCreate(0,"SELL",OBJ_BUTTON,0,0,0);
      ObjectSetInteger(0,"SELL",OBJPROP_XDISTANCE,ChartGetInteger(0,CHART_WIDTH_IN_PIXELS)-50);
      ObjectSetInteger(0,"SELL",OBJPROP_YDISTANCE,37);
      ObjectSetString(0,"SELL",OBJPROP_TEXT,"SELL");
      ObjectSetInteger(0,"SELL",OBJPROP_BGCOLOR,clrDarkOrange);
      res=true;
     }
//--- result
   return(res);
  }
//+------------------------------------------------------------------+
//| Check for long position closing                                  |
//+------------------------------------------------------------------+
bool CheckButonClose(void)
  {
//---
   bool res=false;
   double level;
     {
      if(ObjectGetInteger(0,"Close BUY",OBJPROP_STATE)!=0)
        {
         ObjectSetInteger(0,"Close BUY",OBJPROP_STATE,0);
         if(FreezeStopsLevels(level))
            ClosePositions(POSITION_TYPE_BUY,level);
         PlaySound("ok.wav");
        }
      if(ObjectGetInteger(0,"Close SELL",OBJPROP_STATE)!=0)
        {
         ObjectSetInteger(0,"Close SELL",OBJPROP_STATE,0);
         if(FreezeStopsLevels(level))
            ClosePositions(POSITION_TYPE_SELL,level);
         PlaySound("ok.wav");
        }
      res=true;
     }
//--- result
   return(res);
  }
//+------------------------------------------------------------------+
//| Check for long position closing                                  |
//+------------------------------------------------------------------+
bool CheckObjectClose(void)
  {
//---
   bool res=false;
     {
      ObjectCreate(0,"Close BUY",OBJ_BUTTON,0,0,0);
      ObjectSetInteger(0,"Close BUY",OBJPROP_XDISTANCE,ChartGetInteger(0,CHART_WIDTH_IN_PIXELS)-102);
      ObjectSetInteger(0,"Close BUY",OBJPROP_YDISTANCE,57);
      ObjectSetString(0,"Close BUY",OBJPROP_TEXT,"Close BUY");
      ObjectSetInteger(0,"Close BUY",OBJPROP_BGCOLOR,clrTomato);
      ObjectCreate(0,"Close SELL",OBJ_BUTTON,0,0,0);
      ObjectSetInteger(0,"Close SELL",OBJPROP_XDISTANCE,ChartGetInteger(0,CHART_WIDTH_IN_PIXELS)-50);
      ObjectSetInteger(0,"Close SELL",OBJPROP_YDISTANCE,57);
      ObjectSetString(0,"Close SELL",OBJPROP_TEXT,"Close SELL");
      ObjectSetInteger(0,"Close SELL",OBJPROP_BGCOLOR,clrFireBrick);
      res=true;
     }
//--- result
   return(res);
  }
//+------------------------------------------------------------------+
//| Check Freeze and Stops levels                                    |
//+------------------------------------------------------------------+
bool FreezeStopsLevels(double &level)
  {
//--- check Freeze and Stops levels
   if(!RefreshRates() || !m_symbol.Refresh())
      return(false);
//--- FreezeLevel -> for pending order and modification
   double freeze_level=m_symbol.FreezeLevel()*m_symbol.Point();
   if(freeze_level==0.0)
      freeze_level=(m_symbol.Ask()-m_symbol.Bid())*3.0;
//--- StopsLevel -> for TakeProfit and StopLoss
   double stop_level=m_symbol.StopsLevel()*m_symbol.Point();
   if(stop_level==0.0)
      stop_level=(m_symbol.Ask()-m_symbol.Bid())*3.0;
   if(freeze_level<=0.0 || stop_level<=0.0)
      return(false);
   level=(freeze_level>stop_level)?freeze_level:stop_level;
   double spread=m_symbol.Spread()*m_adjusted_point;
   level=(level>spread)?level:spread;
//---
   return(true);
  }
//+------------------------------------------------------------------+
//| Close positions                                                  |
//+------------------------------------------------------------------+
void ClosePositions(const ENUM_POSITION_TYPE pos_type,const double level)
  {
   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.Symbol()==m_symbol.Name()/* && m_position.Magic()==InpMagic*/)
            if(m_position.PositionType()==pos_type)
              {
               if(m_position.PositionType()==POSITION_TYPE_BUY)
                 {
                  bool take_profit_level=(m_position.TakeProfit()!=0.0 && m_position.TakeProfit()-m_position.PriceCurrent()>=level) || m_position.TakeProfit()==0.0;
                  bool stop_loss_level=(m_position.StopLoss()!=0.0 && m_position.PriceCurrent()-m_position.StopLoss()>=level) || m_position.StopLoss()==0.0;
                  if(take_profit_level && stop_loss_level)
                     m_trade.PositionClose(m_position.Ticket()); // close a position by the specified symbol
                 }
               if(m_position.PositionType()==POSITION_TYPE_SELL)
                 {
                  bool take_profit_level=(m_position.TakeProfit()!=0.0 && m_position.PriceCurrent()-m_position.TakeProfit()>=level) || m_position.TakeProfit()==0.0;
                  bool stop_loss_level=(m_position.StopLoss()!=0.0 && m_position.StopLoss()-m_position.PriceCurrent()>=level) || m_position.StopLoss()==0.0;
                  if(take_profit_level && stop_loss_level)
                     m_trade.PositionClose(m_position.Ticket()); // close a position by the specified symbol
                 }
               PlaySound("ok.wav");
              }
  }
//+------------------------------------------------------------------+
//| Refreshes the symbol quotes data                                 |
//+------------------------------------------------------------------+
bool RefreshRates()
  {
//--- refresh rates
   if(!m_symbol.RefreshRates())
     {
      Print(__FILE__," ",__FUNCTION__,", ERROR: ","RefreshRates error");
      return(false);
     }
//--- protection against the return value of "zero"
   if(m_symbol.Ask()==0 || m_symbol.Bid()==0)
     {
      Print(__FILE__," ",__FUNCTION__,", ERROR: ","Ask == 0.0 OR Bid == 0.0");
      return(false);
     }
//---
   return(true);
  }
//+------------------------------------------------------------------+
Files:
0002.mq5  11 kb
 
MrBrooklin:

... I still don't understand the meaning of the constant phrase that begins with the word"Returns".

Who returns, who does it return to, where does it return to, why does it return it? I still can't figure it out...

Perhaps I can explain.

Suppose that you have a symbol (symbol, e.g. EUR/USD) that is oscillating on the screen and a program/advisor/robot is running in the terminal. The robot is executing the code that you have filled into it. And this code has these strings:

           ... 
 
           if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;

           double OpenPrice = OrderOpenPrice(); 

	   ...
"orderSelect" is a trade function, it selects an already open order for further work with it.
//In this example, if order selection fails (...==false), further execution of " if " function is interrupted by "break" command.

Next. We have selected the order using the OrderSelect trade function. Now we work with it, with a specific order. For simplicity, we will take a condition that we have only two orders open.

Next, we enter a variable OpenPrice [double type] and assign to it the value of price at which the order we have selected was opened (code section OpenPrice=OrderOpenPrice(); )

HERE is an explanation for you what the RETURN of a parameter means. The OrderOpenPrice function returns the value of the current instrument price. That is, after the program requested the current price from the server, it returned the value of that price to you and assigned that value to a variable.

 

Added MACD Indicator

 //+------------------------------------------------------------------+
//|                                                         0003.mq5 |
//|                        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"
#define   InpMagic   182979245
//---
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
//---
CPositionInfo  m_position; // trade position object
CTrade         m_trade;     // trading object
CSymbolInfo    m_symbol;   // symbol info object
//---
input double InpLots= 0.01 ; // Lots
//---
double    m_adjusted_point; // point value adjusted for 3 or 5 points
int       handle_iCustom;   // variable for storing the handle of the iStochastic indicator
datetime ExtPrevBars= 0 ;     // "0" -> D'1970.01.01 00:00';
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit ()
  {
//---
   if (!m_symbol.Name( Symbol ())) // sets symbol name
       return ( INIT_FAILED );;
//---
   m_trade.SetExpertMagicNumber(InpMagic);
   m_trade.SetMarginMode();
   m_trade.SetTypeFillingBySymbol(m_symbol.Name());
//--- tuning for 3 or 5 digits
   int digits_adjust= 1 ;
   if (m_symbol. Digits ()== 3 || m_symbol. Digits ()== 5 )
      digits_adjust= 10 ;
   m_adjusted_point=m_symbol. Point ()*digits_adjust;
//---
   m_trade.SetDeviationInPoints( 3 *digits_adjust);
   if (!m_position.Select( Symbol ()))
     {
      CheckObject();
      CheckObjectClose();
     }
   handle_iCustom= iMACD (m_symbol.Name(), Period (), 12 , 26 , 9 , PRICE_CLOSE );
//--- if the handle is not created
   if (handle_iCustom== INVALID_HANDLE )
     {
       //--- tell about the failure and output the error code
       PrintFormat ( "Failed to create handle of the handle_iCustom indicator for the symbol %s/%s, error code %d" ,
                  m_symbol.Name(),
                   EnumToString ( Period ()),
                   GetLastError ());
       //--- the indicator is stopped early
       return ( INIT_FAILED );
     }
//---
   return ( INIT_SUCCEEDED );
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit ( const int reason)
  {
//---
   if ( ObjectFind ( 0 , "BUY" )== 0 )
     {
       ObjectDelete ( 0 , "BUY" );
     }
   if ( ObjectFind ( 0 , "SELL" )== 0 )
     {
       ObjectDelete ( 0 , "SELL" );
     }
//---
   if ( ObjectFind ( 0 , "Close BUY" )== 0 )
     {
       ObjectDelete ( 0 , "Close BUY" );
     }
   if ( ObjectFind ( 0 , "Close SELL" )== 0 )
     {
       ObjectDelete ( 0 , "Close SELL" );
     }
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick ()
  {
//---
   CheckButon();
   CheckButonClose();
   if (SearchTradingSignals())
     {
       return ;
     }
  }
//+------------------------------------------------------------------+
//| Check for long position closing                                  |
//+------------------------------------------------------------------+
bool CheckButon( void )
  {
//---
   bool res= false ;
     {
       if ( ObjectGetInteger ( 0 , "BUY" , OBJPROP_STATE )!= 0 )
        {
         ObjectSetInteger ( 0 , "BUY" , OBJPROP_STATE , 0 );
         double price=m_symbol.Ask();
           {
             //--- open position
             if (m_trade.PositionOpen(m_symbol.Name(), ORDER_TYPE_BUY ,InpLots,price, 0.0 , 0.0 ))
               printf ( "Position by %s to be opened" ,m_symbol.Name());
             else
              {
               printf ( "Error opening BUY position by %s : '%s'" ,m_symbol.Name(),m_trade.ResultComment());
               printf ( "Open parameters : price=%f,TP=%f" ,price, 0.0 );
              }
             PlaySound ( "ok.wav" );
           }
        }
       if ( ObjectGetInteger ( 0 , "SELL" , OBJPROP_STATE )!= 0 )
        {
         ObjectSetInteger ( 0 , "SELL" , OBJPROP_STATE , 0 );
         double price0=m_symbol.Bid();
           {
             if (m_trade.PositionOpen(m_symbol.Name(), ORDER_TYPE_SELL ,InpLots,price0, 0.0 , 0.0 ))
               printf ( "Position by %s to be opened" ,m_symbol.Name());
             else
              {
               printf ( "Error opening SELL position by %s : '%s'" ,m_symbol.Name(),m_trade.ResultComment());
               printf ( "Open parameters : price=%f,TP=%f" ,price0, 0.0 );
              }
             PlaySound ( "ok.wav" );
           }
        }
      res= true ;
     }
//--- result
   return (res);
  }
//+------------------------------------------------------------------+
//| Check for long position closing                                  |
//+------------------------------------------------------------------+
bool CheckObject( void )
  {
//---
   bool res= false ;
     {
       ObjectCreate ( 0 , "BUY" , OBJ_BUTTON , 0 , 0 , 0 );
       ObjectSetInteger ( 0 , "BUY" , OBJPROP_XDISTANCE , ChartGetInteger ( 0 , CHART_WIDTH_IN_PIXELS )- 102 );
       ObjectSetInteger ( 0 , "BUY" , OBJPROP_YDISTANCE , 37 );
       ObjectSetString ( 0 , "BUY" , OBJPROP_TEXT , "BUY" );
       ObjectSetInteger ( 0 , "BUY" , OBJPROP_BGCOLOR , clrMediumSeaGreen );
       ObjectCreate ( 0 , "SELL" , OBJ_BUTTON , 0 , 0 , 0 );
       ObjectSetInteger ( 0 , "SELL" , OBJPROP_XDISTANCE , ChartGetInteger ( 0 , CHART_WIDTH_IN_PIXELS )- 50 );
       ObjectSetInteger ( 0 , "SELL" , OBJPROP_YDISTANCE , 37 );
       ObjectSetString ( 0 , "SELL" , OBJPROP_TEXT , "SELL" );
       ObjectSetInteger ( 0 , "SELL" , OBJPROP_BGCOLOR , clrDarkOrange );
      res= true ;
     }
//--- result
   return (res);
  }
//+------------------------------------------------------------------+
//| Check for long position closing                                  |
//+------------------------------------------------------------------+
bool CheckButonClose( void )
  {
//---
   bool res= false ;
   double level;
     {
       if ( ObjectGetInteger ( 0 , "Close BUY" , OBJPROP_STATE )!= 0 )
        {
         ObjectSetInteger ( 0 , "Close BUY" , OBJPROP_STATE , 0 );
         if (FreezeStopsLevels(level))
            ClosePositions( POSITION_TYPE_BUY ,level);
         PlaySound ( "ok.wav" );
        }
       if ( ObjectGetInteger ( 0 , "Close SELL" , OBJPROP_STATE )!= 0 )
        {
         ObjectSetInteger ( 0 , "Close SELL" , OBJPROP_STATE , 0 );
         if (FreezeStopsLevels(level))
            ClosePositions( POSITION_TYPE_SELL ,level);
         PlaySound ( "ok.wav" );
        }
      res= true ;
     }
//--- result
   return (res);
  }
//+------------------------------------------------------------------+
//| Check for long position closing                                  |
//+------------------------------------------------------------------+
bool CheckObjectClose( void )
  {
//---
   bool res= false ;
     {
       ObjectCreate ( 0 , "Close BUY" , OBJ_BUTTON , 0 , 0 , 0 );
       ObjectSetInteger ( 0 , "Close BUY" , OBJPROP_XDISTANCE , ChartGetInteger ( 0 , CHART_WIDTH_IN_PIXELS )- 102 );
       ObjectSetInteger ( 0 , "Close BUY" , OBJPROP_YDISTANCE , 57 );
       ObjectSetString ( 0 , "Close BUY" , OBJPROP_TEXT , "Close BUY" );
       ObjectSetInteger ( 0 , "Close BUY" , OBJPROP_BGCOLOR , clrTomato );
       ObjectCreate ( 0 , "Close SELL" , OBJ_BUTTON , 0 , 0 , 0 );
       ObjectSetInteger ( 0 , "Close SELL" , OBJPROP_XDISTANCE , ChartGetInteger ( 0 , CHART_WIDTH_IN_PIXELS )- 50 );
       ObjectSetInteger ( 0 , "Close SELL" , OBJPROP_YDISTANCE , 57 );
       ObjectSetString ( 0 , "Close SELL" , OBJPROP_TEXT , "Close SELL" );
       ObjectSetInteger ( 0 , "Close SELL" , OBJPROP_BGCOLOR , clrFireBrick );
      res= true ;
     }
//--- result
   return (res);
  }
//+------------------------------------------------------------------+
//| Check Freeze and Stops levels                                    |
//+------------------------------------------------------------------+
bool FreezeStopsLevels( double &level)
  {
//--- check Freeze and Stops levels
   if (!RefreshRates() || !m_symbol.Refresh())
       return ( false );
//--- FreezeLevel -> for pending order and modification
   double freeze_level=m_symbol.FreezeLevel()*m_symbol. Point ();
   if (freeze_level== 0.0 )
      freeze_level=(m_symbol.Ask()-m_symbol.Bid())* 3.0 ;
//--- StopsLevel -> for TakeProfit and StopLoss
   double stop_level=m_symbol.StopsLevel()*m_symbol. Point ();
   if (stop_level== 0.0 )
      stop_level=(m_symbol.Ask()-m_symbol.Bid())* 3.0 ;
   if (freeze_level<= 0.0 || stop_level<= 0.0 )
       return ( false );
   level=(freeze_level>stop_level)?freeze_level:stop_level;
   double spread=m_symbol.Spread()*m_adjusted_point;
   level=(level>spread)?level:spread;
//---
   return ( true );
  }
//+------------------------------------------------------------------+
//| Close positions                                                  |
//+------------------------------------------------------------------+
void ClosePositions( const ENUM_POSITION_TYPE pos_type, const double level)
  {
   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. Symbol ()==m_symbol.Name() /* && m_position.Magic()==InpMagic*/ )
             if (m_position.PositionType()==pos_type)
              {
               if (m_position.PositionType()== POSITION_TYPE_BUY )
                 {
                   bool take_profit_level=(m_position.TakeProfit()!= 0.0 && m_position.TakeProfit()-m_position.PriceCurrent()>=level) || m_position.TakeProfit()== 0.0 ;
                   bool stop_loss_level=(m_position.StopLoss()!= 0.0 && m_position.PriceCurrent()-m_position.StopLoss()>=level) || m_position.StopLoss()== 0.0 ;
                   if (take_profit_level && stop_loss_level)
                     m_trade.PositionClose(m_position.Ticket()); // close a position by the specified symbol
                 }
               if (m_position.PositionType()== POSITION_TYPE_SELL )
                 {
                   bool take_profit_level=(m_position.TakeProfit()!= 0.0 && m_position.PriceCurrent()-m_position.TakeProfit()>=level) || m_position.TakeProfit()== 0.0 ;
                   bool stop_loss_level=(m_position.StopLoss()!= 0.0 && m_position.StopLoss()-m_position.PriceCurrent()>=level) || m_position.StopLoss()== 0.0 ;
                   if (take_profit_level && stop_loss_level)
                     m_trade.PositionClose(m_position.Ticket()); // close a position by the specified symbol
                 }
               PlaySound ( "ok.wav" );
              }
  }
//+------------------------------------------------------------------+
//| Refreshes the symbol quotes data                                 |
//+------------------------------------------------------------------+
bool RefreshRates()
  {
//--- refresh rates
   if (!m_symbol.RefreshRates())
     {
       Print ( __FILE__ , " " , __FUNCTION__ , ", ERROR: " , "RefreshRates error" );
       return ( false );
     }
//--- protection against the return value of "zero"
   if (m_symbol.Ask()== 0 || m_symbol.Bid()== 0 )
     {
       Print ( __FILE__ , " " , __FUNCTION__ , ", ERROR: " , "Ask == 0.0 OR Bid == 0.0" );
       return ( false );
     }
//---
   return ( true );
  }
//+------------------------------------------------------------------+
//| Search trading signals                                           |
//+------------------------------------------------------------------+
bool SearchTradingSignals( void )
  {
//--- we work only at the time of the birth of new bar
   datetime time_0= iTime (m_symbol.Name(), Period (), 0 );
   if (time_0==ExtPrevBars)
       return ( false );
   ExtPrevBars=time_0;
   if (!RefreshRates())
     {
      ExtPrevBars= 0 ;
       return ( false );
     }
//---
   double level;
   double main[],signal[];
   ArraySetAsSeries (main, true );
   ArraySetAsSeries (signal, true );
   int start_pos= 0 ,count= 3 ;
   if (!iGetArray(handle_iCustom, MAIN_LINE ,start_pos,count,main) ||
      !iGetArray(handle_iCustom, SIGNAL_LINE ,start_pos,count,signal))
     {
      ExtPrevBars= 0 ;
       return ( false );
     }
//--- check for long position (BUY) possibility
   if (main[ 0 ]< 0 )
       if (main[ 0 ]>signal[ 0 ] && main[ 1 ]<signal[ 1 ])
        {
         double price=m_symbol.Ask();
           {
             //--- open position
             if (m_trade.PositionOpen(m_symbol.Name(), ORDER_TYPE_BUY ,InpLots,price, 0.0 , 0.0 ))
               printf ( "Position by %s to be opened" ,m_symbol.Name());
             else
              {
               printf ( "Error opening BUY position by %s : '%s'" ,m_symbol.Name(),m_trade.ResultComment());
               printf ( "Open parameters : price=%f,TP=%f" ,price, 0.0 );
              }
             PlaySound ( "ok.wav" );
           }
         if (FreezeStopsLevels(level))
            ClosePositions( POSITION_TYPE_SELL ,level);
        }
//--- check for short position (SELL) possibility
   if (main[ 0 ]> 0 )
       if (main[ 0 ]<signal[ 0 ] && main[ 1 ]>signal[ 1 ])
        {
         double price0=m_symbol.Bid();
           {
             if (m_trade.PositionOpen(m_symbol.Name(), ORDER_TYPE_SELL ,InpLots,price0, 0.0 , 0.0 ))
               printf ( "Position by %s to be opened" ,m_symbol.Name());
             else
              {
               printf ( "Error opening SELL position by %s : '%s'" ,m_symbol.Name(),m_trade.ResultComment());
               printf ( "Open parameters : price=%f,TP=%f" ,price0, 0.0 );
              }
             PlaySound ( "ok.wav" );
           }
         if (FreezeStopsLevels(level))
            ClosePositions( POSITION_TYPE_BUY ,level);
        }
//---
   return ( true );
  }
//+------------------------------------------------------------------+
//| Get value of buffers                                             |
//+------------------------------------------------------------------+
double iGetArray( const int handle, const int buffer, const int start_pos, const int count, double &arr_buffer[])
  {
   bool result= true ;
   if (! ArrayIsDynamic (arr_buffer))
     {
       Print ( "This a no dynamic array!" );
       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 ( "Failed to copy data from the indicator, error code %d" , GetLastError ());
       //--- quit with zero result - it means that the indicator is considered as not calculated
       return ( false );
     }
   return (result);
  }
//+------------------------------------------------------------------+
Files:
0003.PNG  107 kb
0003.mq5  15 kb
 
SanAlex:

Added MACD indicator

The basics are there - now it's all up to you

 
4elovechishe:

I might be able to explain.

Let's say that you currently have a symbol (e.g. EUR/USD) fluctuating on your screen and a program/advisor/robot running in the terminal. The robot is executing the code that you have filled into it. And this code has these strings:

"orderSelect" is a trade function, it selects an already open order for further work with it.
//In this example, if order selection fails (...==false), further execution of " if " function is interrupted by "break" command.

Next. We have selected the order using the OrderSelect trade function. Now we work with it, with a specific order. For simplicity, we will take a condition that we have only two orders open.

Next, we enter a variable OpenPrice [double type] and assign to it the value of price at which the order we have selected was opened (code section OpenPrice=OrderOpenPrice(); )

HERE is an explanation for you what the RETURN of a parameter means. The OrderOpenPrice function returns the value of the current instrument price. That is, after the program has requested the current price from the server, it has returned the value of that price to you and assigned that value to a variable.

Thank you for your clarification. I hope it will also help Roman in mastering the programming language.

Regards, Vladimir.

 

Hello! Well, maybe someone can help me...

Currently dealing with order opening/closing mechanisms and have run into a problem with closing open positions.

The code is simple. The idea of the algorithm is to draw the MA (moving average) with a period of 100 on the chart. If the previous candle [1] opened above the MA, and closed below the MA, then the next candle [0] opens a SELL orderto sell.

//(The conditions to buy are reversed. I am not explaining them)

For closing of the order the following conditions - the current price has passed from the price of opening of the order the set value of points, for example 40.

Example: A lot is opened at Bid= 1.20045, it should close at Ask= 1.20005.

The code of opening and closing is packed into 2 corresponding functions which in their turn are called with OnTick() function. In fact, with every tick the closing condition should be checked, but in fact the price may fall below the specified level (closing level) but the order won't close. Screenshots and code are attached.

 
4elovechishe:

Hello! Well, maybe someone can help me too...

Currently dealing with order opening/closing mechanisms and have run into a problem with closing open positions.

The code is simple. The idea of the algorithm is to draw the MA (moving average) with a period of 100 on the chart. If the previous candle [1] opened above the MA, and closed below the MA, then the next candle [0] opens a SELL orderto sell.

//(The conditions to buy are reversed. I am not explaining them)

For closing of the order the following conditions - the current price has passed from the price of opening of the order the set value of points, for example 40.

Example: A lot is opened at Bid= 1.20045, it should close at Ask= 1.20005.

The code of opening and closing is packed into 2 corresponding functions which in their turn are called with OnTick() function. In fact, with every tick the closing condition should be checked, but in fact the price may fall below the specified level (closing level) but the order won't close. I am attaching the screenshots and the code.

There is a forum thread at https://www.mql5.com/ru/forum/160683/page767#comment_10725713

You might get help there faster.

Sincerely, Vladimir.

Любые вопросы новичков по MQL4 и MQL5, помощь и обсуждение по алгоритмам и кодам
Любые вопросы новичков по MQL4 и MQL5, помощь и обсуждение по алгоритмам и кодам
  • 2019.02.21
  • www.mql5.com
В этой ветке я хочу начать свою помощь тем, кто действительно хочет разобраться и научиться программированию на новом MQL4 и желает легко перейти н...