How to start with MQL5 - page 2

 
Vladimir Karputov:

Nobody did a double post. I did a post transfer from one topic to another:


Double post was at https://www.mql5.com/en/forum/262046

Now it is in the deleted folder.

I will code & automate your strategy for free
I will code & automate your strategy for free
  • 2018.06.30
  • www.mql5.com
I am a software developer with 10 years of experience, 3 years of trading experience and I will code & automate your strategy for free...
 
Hi Vladimir,

Thank you for the response given. but the point I mean is not like that and the program you sent the value in 10 arrays formed from the new candle is still the same as the previous candle.


I mean, every tick is recorded in an array and can be called up.

Implementation Example:

if (tick [10]> closeCandlePrev)

{

   Buy

}

like This Video :

[broken youtube link was deleted by moderator]


 
Keith Watford:

Double post was at https://www.mql5.com/en/forum/262046

Now it is in the deleted folder.

Hi Keith Watford,

Sorry, I'm new to this forum, so I haven't explored much so I don't know if my question is reposted, but I need information and input and don't know where to go I want to ask my problem.

 
Anggono Utomo :


I was a little wrong: I need to set "start_time" to "0":

//+------------------------------------------------------------------+
//|                                                         Temp.mq5 |
//|                              Copyright © 2019, Vladimir Karputov |
//|                                           http://wmua.ru/slesar/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2019, Vladimir Karputov"
#property link      "http://wmua.ru/slesar/"
#property version   "1.001"
//+------------------------------------------------------------------+
//| Enum Copy Ticks Flags                                            |
//+------------------------------------------------------------------+
enum ENUM_COPY_TICKS_FLAGS
  {
   info=0,     // COPY_TICKS_INFO
   trade=1,    // COPY_TICKS_TRADE
   all=2,      // COPY_TICKS_ALL
  };
//--- input parameters
input ENUM_COPY_TICKS_FLAGS   InpFlags = trade; // The Copy Ticks Flags: 
input uint                    InpCount = 10;    // The number of ticks that you want to receive 
//---
datetime ExtPrevBars=0;        // "0" -> D'1970.01.01 00:00';
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//--- we work only at the time of the birth of new bar
   datetime time_0=iTime(Symbol(),Period(),0);
   if(time_0==ExtPrevBars)
      return;
   ExtPrevBars=time_0;
//---
   MqlTick tick_array[];   // Tick receiving array 
   //ArraySetAsSeries(tick_array,false);
//--- Measuring start time before receiving the ticks 
   uint start=GetTickCount();
//--- Requesting the tick history since 1970.01.01 00:00.001 (parameter from=1 ms) 
   int received=CopyTicks(Symbol(),tick_array,InpFlags,0,InpCount);
   if(received!=-1)
     {
      //--- Showing information about the number of ticks and spent time 
      PrintFormat("%s: received %d ticks in %d ms",Symbol(),received,GetTickCount()-start);
      //--- If the tick history is synchronized, the error code is equal to zero 
      if(GetLastError()==0)
        {
         int limit=(received>=10)?10:received;
         for(int i=0;i<limit;i++)
            Print("Time : ",tick_array[i].time," Array ","Bid : ",tick_array[i].bid," Ask : ",tick_array[i].ask," Array ",i);
        }
      else
         PrintFormat("%s: Ticks are not synchronized yet, %d ticks received for %d ms. Error=%d",
                     _Symbol,received,GetTickCount()-start,_LastError);
     }
  }
//+------------------------------------------------------------------+


2019.08.18 10:29:43.378 2019.03.11 00:00:00   EURUSD: received 0 ticks in 0 ms
2019.08.18 10:29:44.177 2019.03.11 01:00:00   EURUSD: received 10 ticks in 0 ms
2019.08.18 10:29:44.177 2019.03.11 01:00:00   Time : 2019.03.11 00:58:07 Array Bid : 1.12341 Ask : 1.12345 Array 0
2019.08.18 10:29:44.177 2019.03.11 01:00:00   Time : 2019.03.11 00:58:15 Array Bid : 1.12342 Ask : 1.12346 Array 1
2019.08.18 10:29:44.177 2019.03.11 01:00:00   Time : 2019.03.11 00:58:22 Array Bid : 1.12339 Ask : 1.12343 Array 2
2019.08.18 10:29:44.177 2019.03.11 01:00:00   Time : 2019.03.11 00:58:30 Array Bid : 1.1234 Ask : 1.12344 Array 3
2019.08.18 10:29:44.177 2019.03.11 01:00:00   Time : 2019.03.11 00:58:37 Array Bid : 1.12335 Ask : 1.12339 Array 4
2019.08.18 10:29:44.177 2019.03.11 01:00:00   Time : 2019.03.11 00:58:59 Array Bid : 1.12339 Ask : 1.12343 Array 5
2019.08.18 10:29:44.177 2019.03.11 01:00:00   Time : 2019.03.11 00:59:12 Array Bid : 1.12341 Ask : 1.12345 Array 6
2019.08.18 10:29:44.177 2019.03.11 01:00:00   Time : 2019.03.11 00:59:24 Array Bid : 1.12343 Ask : 1.12347 Array 7
2019.08.18 10:29:44.177 2019.03.11 01:00:00   Time : 2019.03.11 00:59:59 Array Bid : 1.12339 Ask : 1.12343 Array 8
2019.08.18 10:29:44.177 2019.03.11 01:00:00   Time : 2019.03.11 01:00:00 Array Bid : 1.12339 Ask : 1.12347 Array 9
2019.08.18 10:29:44.964 2019.03.11 02:00:00   EURUSD: received 10 ticks in 0 ms
2019.08.18 10:29:44.964 2019.03.11 02:00:00   Time : 2019.03.11 01:59:00 Array Bid : 1.12316 Ask : 1.12324 Array 0
2019.08.18 10:29:44.964 2019.03.11 02:00:00   Time : 2019.03.11 01:59:05 Array Bid : 1.12315 Ask : 1.12323 Array 1
2019.08.18 10:29:44.964 2019.03.11 02:00:00   Time : 2019.03.11 01:59:10 Array Bid : 1.12317 Ask : 1.12325 Array 2
2019.08.18 10:29:44.964 2019.03.11 02:00:00   Time : 2019.03.11 01:59:16 Array Bid : 1.12315 Ask : 1.12323 Array 3
2019.08.18 10:29:44.964 2019.03.11 02:00:00   Time : 2019.03.11 01:59:21 Array Bid : 1.12316 Ask : 1.12324 Array 4
2019.08.18 10:29:44.964 2019.03.11 02:00:00   Time : 2019.03.11 01:59:27 Array Bid : 1.12314 Ask : 1.12322 Array 5
2019.08.18 10:29:44.964 2019.03.11 02:00:00   Time : 2019.03.11 01:59:32 Array Bid : 1.12315 Ask : 1.12323 Array 6
2019.08.18 10:29:44.964 2019.03.11 02:00:00   Time : 2019.03.11 01:59:38 Array Bid : 1.12313 Ask : 1.12321 Array 7
2019.08.18 10:29:44.964 2019.03.11 02:00:00   Time : 2019.03.11 01:59:43 Array Bid : 1.12314 Ask : 1.12322 Array 8
2019.08.18 10:29:44.964 2019.03.11 02:00:00   Time : 2019.03.11 01:59:59 Array Bid : 1.12312 Ask : 1.1232 Array 9
Files:
Temp.mq5  7 kb
 

cool ...

Thanks a lot vladimir, I hope our discussion in the future will be much better for developing EA.

May Allah bestow lots of fortune on you.

 

Simple script: it opens a BUY position. In this case, you can set Stop loss and Take profit.

Code: 'Open Positions SL TP.mq5'

Please note - Stop loss and Take profit must be normalized using the method CSymbolInfo::NormalizePrice

//+------------------------------------------------------------------+
//|                                         Open Positions SL TP.mq5 |
//|                              Copyright © 2019, Vladimir Karputov |
//|                                           http://wmua.ru/slesar/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2019, Vladimir Karputov"
#property link      "http://wmua.ru/slesar/"
#property version   "1.00"
#property script_show_inputs
//---
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
//---
CTrade         m_trade;                      // object of CTrade class
CSymbolInfo    m_symbol;                     // object of CSymbolInfo class
//--- input parameters
input ushort   InpStopLoss          = 15;          // Stop Loss, in pips (1.00045-1.00055=1 pips)
input ushort   InpTakeProfit        = 46;          // Take Profit, in pips (1.00045-1.00055=1 pips)
input ulong    InpDeviation         = 10;          // Deviation, in points (1.00045-1.00055=10 points)
//---
input ulong    InpMagic             = 397752952;   // Magic number
//---
double   m_stop_loss                = 0.0;         // Stop Loss      -> double
double   m_take_profit              = 0.0;         // Take Profit    -> double
double   m_adjusted_point;                         // point value adjusted for 3 or 5 points
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   if(!m_symbol.Name(Symbol())) // sets symbol name
     {
      Print(__FILE__," ",__FUNCTION__,", ERROR: CSymbolInfo.Name");
      return;
     }
   RefreshRates();
//---
   m_trade.SetExpertMagicNumber(InpMagic);
   m_trade.SetMarginMode();
   m_trade.SetTypeFillingBySymbol(m_symbol.Name());
   m_trade.SetDeviationInPoints(InpDeviation);
//--- 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_stop_loss             = InpStopLoss              * m_adjusted_point;
   m_take_profit           = InpTakeProfit            * m_adjusted_point;
//--- open buy
   double sl=(m_stop_loss==0.0)?0.0:m_symbol.Ask()-m_stop_loss;
   double tp=(m_take_profit==0.0)?0.0:m_symbol.Ask()+m_take_profit;
   m_trade.Buy(m_symbol.LotsMin(),
               m_symbol.Name(),
               m_symbol.Ask(),
               m_symbol.NormalizePrice(sl),
               m_symbol.NormalizePrice(tp));
  }
//+------------------------------------------------------------------+
//| 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:
 

Last deal - catch the last deal in OnTradeTransaction ()

//+------------------------------------------------------------------+
//|                                                    Last deal.mq5 |
//|                              Copyright © 2019, Vladimir Karputov |
//|                                           http://wmua.ru/slesar/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2019, Vladimir Karputov"
#property link      "http://wmua.ru/slesar/"
#property version   "1.00"
//--- input parameters
input int      Input1=9;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---

  }
//+------------------------------------------------------------------+
//| TradeTransaction function                                        |
//+------------------------------------------------------------------+
void OnTradeTransaction(const MqlTradeTransaction &trans,
                        const MqlTradeRequest &request,
                        const MqlTradeResult &result)
  {
//--- get transaction type as enumeration value
   ENUM_TRADE_TRANSACTION_TYPE type=trans.type;
//--- if transaction is result of addition of the transaction in history
   if(type==TRADE_TRANSACTION_DEAL_ADD)
     {
      long     deal_ticket       =0;
      long     deal_order        =0;
      long     deal_time         =0;
      long     deal_time_msc     =0;
      long     deal_type         =-1;
      long     deal_entry        =-1;
      long     deal_magic        =0;
      long     deal_reason       =-1;
      long     deal_position_id  =0;
      double   deal_volume       =0.0;
      double   deal_price        =0.0;
      double   deal_commission   =0.0;
      double   deal_swap         =0.0;
      double   deal_profit       =0.0;
      string   deal_symbol       ="";
      string   deal_comment      ="";
      string   deal_external_id  ="";
      if(HistoryDealSelect(trans.deal))
        {
         deal_ticket       =HistoryDealGetInteger(trans.deal,DEAL_TICKET);
         deal_order        =HistoryDealGetInteger(trans.deal,DEAL_ORDER);
         deal_time         =HistoryDealGetInteger(trans.deal,DEAL_TIME);
         deal_time_msc     =HistoryDealGetInteger(trans.deal,DEAL_TIME_MSC);
         deal_type         =HistoryDealGetInteger(trans.deal,DEAL_TYPE);
         deal_entry        =HistoryDealGetInteger(trans.deal,DEAL_ENTRY);
         deal_magic        =HistoryDealGetInteger(trans.deal,DEAL_MAGIC);
         deal_reason       =HistoryDealGetInteger(trans.deal,DEAL_REASON);
         deal_position_id  =HistoryDealGetInteger(trans.deal,DEAL_POSITION_ID);

         deal_volume       =HistoryDealGetDouble(trans.deal,DEAL_VOLUME);
         deal_price        =HistoryDealGetDouble(trans.deal,DEAL_PRICE);
         deal_commission   =HistoryDealGetDouble(trans.deal,DEAL_COMMISSION);
         deal_swap         =HistoryDealGetDouble(trans.deal,DEAL_SWAP);
         deal_profit       =HistoryDealGetDouble(trans.deal,DEAL_PROFIT);

         deal_symbol       =HistoryDealGetString(trans.deal,DEAL_SYMBOL);
         deal_comment      =HistoryDealGetString(trans.deal,DEAL_COMMENT);
         deal_external_id  =HistoryDealGetString(trans.deal,DEAL_EXTERNAL_ID);
        }
      else
         return;
      ENUM_DEAL_ENTRY enum_deal_entry=(ENUM_DEAL_ENTRY)deal_entry;
      if(deal_type==DEAL_TYPE_BUY || deal_type==DEAL_TYPE_SELL)
        {
         Print(TimeToString(deal_time,TIME_DATE|TIME_MINUTES)," | ",
               deal_symbol," | ",
               EnumToString((ENUM_DEAL_TYPE)deal_type)," | ",
               EnumToString((ENUM_DEAL_ENTRY)deal_entry));
        }
     }
  }
//+------------------------------------------------------------------+
Files:
Last_deal.mq5  9 kb
 

Example: Birth of new bar

//+------------------------------------------------------------------+
//|                                             Birth of new bar.mq5 |
//|                              Copyright © 2019, Vladimir Karputov |
//|                                           http://wmua.ru/slesar/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2019, Vladimir Karputov"
#property link      "http://wmua.ru/slesar/"
#property version   "1.000"
#property description "Example: Birth of new bar"
/*
   barabashkakvn Trading engine 3.105
*/
//---
datetime m_prev_bars             = 0;        // "0" -> D'1970.01.01 00:00';
string   m_symbol_period         = "";       //
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   m_symbol_period=Symbol()+","+StringSubstr(EnumToString(Period()),7);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//--- we work only at the time of the birth of new bar
   datetime time_0=iTime(Symbol(),Period(),0);
   if(time_0==m_prev_bars)
      return;
   m_prev_bars=time_0;
   Alert("There is a new bar on the "+m_symbol_period+" symbol!");
  }
//+------------------------------------------------------------------+
Files:
 

One deal in N bars

//+------------------------------------------------------------------+
//|                                           One deal in N bars.mq5 |
//|                              Copyright © 2019, Vladimir Karputov |
//|                                           http://wmua.ru/slesar/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2019, Vladimir Karputov"
#property link      "http://wmua.ru/slesar/"
#property version   "1.00"
//--- input parameters
input uchar    InpBars              = 4;           // Deal in N bars
input ulong    InpDeviation         = 10;          // Deviation, in points (1.00045-1.00055=10 points)
input ulong    InpMagic             = 200;         // Magic number
//---
datetime m_last_deal_IN = 0;                       // "0" -> D'1970.01.01 00:00';
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//--- signal
   if(PositionsTotal()==0)
     {
      datetime time[];
      ArraySetAsSeries(time,true);
      int start_pos=0;        // start position
      int count=InpBars+1;    // data count to copy
      if(CopyTime(Symbol(),Period(),start_pos,count,time)!=count)
         return;
      //--- check
      if(time[InpBars]>m_last_deal_IN)
        {
         //--- open position
        }
     }
  }
//+------------------------------------------------------------------+
//| TradeTransaction function                                        |
//+------------------------------------------------------------------+
void OnTradeTransaction(const MqlTradeTransaction &trans,
                        const MqlTradeRequest &request,
                        const MqlTradeResult &result)
  {
//--- get transaction type as enumeration value
   ENUM_TRADE_TRANSACTION_TYPE type=trans.type;
//--- if transaction is result of addition of the transaction in history
   if(type==TRADE_TRANSACTION_DEAL_ADD)
     {
      long     deal_ticket       =0;
      long     deal_order        =0;
      long     deal_time         =0;
      long     deal_time_msc     =0;
      long     deal_type         =-1;
      long     deal_entry        =-1;
      long     deal_magic        =0;
      long     deal_reason       =-1;
      long     deal_position_id  =0;
      double   deal_volume       =0.0;
      double   deal_price        =0.0;
      double   deal_commission   =0.0;
      double   deal_swap         =0.0;
      double   deal_profit       =0.0;
      string   deal_symbol       ="";
      string   deal_comment      ="";
      string   deal_external_id  ="";
      if(HistoryDealSelect(trans.deal))
        {
         deal_ticket       =HistoryDealGetInteger(trans.deal,DEAL_TICKET);
         deal_order        =HistoryDealGetInteger(trans.deal,DEAL_ORDER);
         deal_time         =HistoryDealGetInteger(trans.deal,DEAL_TIME);
         deal_time_msc     =HistoryDealGetInteger(trans.deal,DEAL_TIME_MSC);
         deal_type         =HistoryDealGetInteger(trans.deal,DEAL_TYPE);
         deal_entry        =HistoryDealGetInteger(trans.deal,DEAL_ENTRY);
         deal_magic        =HistoryDealGetInteger(trans.deal,DEAL_MAGIC);
         deal_reason       =HistoryDealGetInteger(trans.deal,DEAL_REASON);
         deal_position_id  =HistoryDealGetInteger(trans.deal,DEAL_POSITION_ID);

         deal_volume       =HistoryDealGetDouble(trans.deal,DEAL_VOLUME);
         deal_price        =HistoryDealGetDouble(trans.deal,DEAL_PRICE);
         deal_commission   =HistoryDealGetDouble(trans.deal,DEAL_COMMISSION);
         deal_swap         =HistoryDealGetDouble(trans.deal,DEAL_SWAP);
         deal_profit       =HistoryDealGetDouble(trans.deal,DEAL_PROFIT);

         deal_symbol       =HistoryDealGetString(trans.deal,DEAL_SYMBOL);
         deal_comment      =HistoryDealGetString(trans.deal,DEAL_COMMENT);
         deal_external_id  =HistoryDealGetString(trans.deal,DEAL_EXTERNAL_ID);
        }
      else
         return;
      ENUM_DEAL_ENTRY enum_deal_entry=(ENUM_DEAL_ENTRY)deal_entry;
      if(deal_symbol==Symbol() && deal_magic==InpMagic)
        {
         if(deal_type==DEAL_TYPE_BUY || deal_type==DEAL_TYPE_SELL)
           {
            if(deal_entry==DEAL_ENTRY_IN)
              {
               m_last_deal_IN=iTime(Symbol(),Period(),0);
              }
           }
        }
     }
  }
//+------------------------------------------------------------------+
Files:
 

Example: the number of all positions for current symbol and for which the Magic number matches the specified parameter 'magic number':

//+------------------------------------------------------------------+
//|                                        CalculateAllPositions.mq5 |
//+------------------------------------------------------------------+
#property version   "1.001"
#property description "Calculate all positions"
#include <Trade\PositionInfo.mqh>
CPositionInfo  m_position;                   // trade position object
//--- input parameters
input ulong    m_magic=15489;                // magic number
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   Comment("");
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   int total_position=CalculateAllPositions();
   Comment("All position: ",IntegerToString(total_position));
  }
//+------------------------------------------------------------------+
//| TradeTransaction function                                        |
//+------------------------------------------------------------------+
void OnTradeTransaction(const MqlTradeTransaction &trans,
                        const MqlTradeRequest &request,
                        const MqlTradeResult &result)
  {
//---

  }
//+------------------------------------------------------------------+
//| Calculate all positions                                          |
//+------------------------------------------------------------------+
int CalculateAllPositions()
  {
   int total=0;

   for(int i=PositionsTotal()-1; i>=0; i--)
      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
         if(m_position.Symbol()==Symbol() && m_position.Magic()==m_magic)
            total++;
//---
   return(total);
  }
//+------------------------------------------------------------------+
Reason: