Discussion of article "Step-by-Step Guide to Writing an Expert Advisor in MQL5 for Beginners" - page 12

 

Please forgive me, I didn't understand how to use this button at the time, now I think I do.

extern int tp = 1000;
extern int sl = 1000;
extern double Lots = 0.2;
int ticket;
void OnStart()
{
datetime date1=D'2014.10.28.13.22.13';
 if(OrdersTotal()==0)
 {
 ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,0,Bid-sl*Point,Bid+tp*Point,"",123,0,Red);
 }
 if (OrdersTotal()==1 && Hour( )==13 && Minute( )==23 && Seconds ( )>=00)
 {
 bool select1=OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES);
 bool close1=OrderClose(ticket,Lots,Bid,20,Green);
 }
 return(0);
}
[Deleted]  
Grenjohn:

Please forgive me, I didn't understand then how to use this button, now I think I do.

It's all about the condition:

if( OrdersTotal() == 0 )

It lacks a check that the date you specified date1 has occurred. Now you should open an order immediately with the first tick, if there are no open orders! I.e. you need to do the same as here,

if (OrdersTotal()==1 && Hour( )==13 && Minute( )==23 && Seconds ( )>=00)

but specify the correct day, hour, minute, second (a few seconds).

Yes and one more thing. Prices sl and tp should be normalised, otherwise there may be errors.

 

Something it has stopped joining to the chart and please tell me what it means to normalise sl and tp prices?

extern int tp = 1000;
extern int sl = 1000;
extern double Lots = 0.3;
int ticket;
void OnStart()
{
datetime date1=D'2014.10.28.15.08.30';
 if(OrdersTotal()==0 && Hour( )==15 && Minute( )==08 && Seconds( )>=30)
 {
 ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,0,Bid-sl*Point,Bid+tp*Point,"",123,0,Red);
 }
 if (OrdersTotal()==1 && Hour( )==15 && Minute( )==09 && Seconds( )>=00)
 {
 bool select1=OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES);
 bool close1=OrderClose(ticket,Lots,Bid,20,Green);
 }
 return(0);
}
 
Grenjohn:

Please forgive me, I didn't understand how to use this button, now I think I understand it.

extern datetime date1=D'2014.10.28 13:22:13';
extern datetime date2=D'2014.10.29 13:22:13';
extern int tp = 1000;
extern int sl = 1000;
extern double Lots = 0.2;
int ticket = 0;

void OnStart()
{
 static int flag = 0;

 switch ( flag )
 {
   // 0 - no position
   case 0:
   {
     // When date1 time comes, open a position
     if ( TimeCurrent() >= date1 )
     {
       if ( ticket <= 0 )
       {
         ticket = OrderSend( ... );
       }
       if ( ticket > 0 )
       {
         flag = 1;
       }
     }
     break;
   }
   // 1 was a position
   case 1:
   {
     // Close the position when the date2 time arrives
     if ( TimeCurrent() >= date2 )
     {
        if ( ticket > 0 && OrderSelect( ticket, SELECT_BY_TICKET ) && OrderCloseTime() <= 0 )
        {
           if ( OrderClose( ticket, ... ) == true )
           {
              ticket = 0;
              flag = 2;
           }
        }
     }
     break;
   }
 }
}

When date1 occurs we open a position, when date2 occurs we close it, after that we do nothing until restart.

[Deleted]  
Grenjohn:

Something it has stopped joining to the chart and please tell me what it means to normalise sl and tp prices?

https://www.mql5.com/en/docs/convert/normalizedouble
Документация по MQL5: Преобразование данных / NormalizeDouble
Документация по MQL5: Преобразование данных / NormalizeDouble
  • www.mql5.com
Преобразование данных / NormalizeDouble - справочник по языку алгоритмического/автоматического трейдинга для MetaTrader 5
 

Thank you all very much for your help and for your patience, but so far, unfortunately, it does not work!!!?

I'm trying to add an Expert Advisor, but it's not added!?!?

extern datetime date1=D'2014.10.29 22:29:10';
extern datetime date2=D'2014.10.29 21:30:00';
extern int tp = 1000;
extern int sl = 1000;
extern double Lots = 0.30;
int ticket = 0;
void OnStart()
{
 static int flag = 0;

 switch ( flag )
 {
   // 0 - no position
   case 0:
   {
     // When date1 time comes, open a position
     if ( TimeCurrent() >= date1 )
     {
       if ( ticket <= 0 )
       {
         ticket = OrderSend(Symbol(),OP_BUY,Lots,Ask,0,Bid-sl*Point,Bid+tp*Point,"",123,0,Red);
       }
       if ( ticket > 0 )
       {
         flag = 1;
       }
     }
     break;
   }
   // 1 was a position
   case 1:
   {
     // Close the position when the date2 time arrives
     if ( TimeCurrent() >= date2 )
     {
        if ( ticket > 0 && OrderSelect( ticket, SELECT_BY_TICKET ) && OrderCloseTime() <= 0 )
        {
           if ( OrderClose( ticket,Lots,Bid,20,Green) == true )
           {
              ticket = 0;
              flag = 2;
           }
        }
     }
     break;
   }
 }
}


 
Grenjohn:

Thank you all very much for your help and for your patience, but so far, unfortunately, it does not work!!!?

I'm trying to add an Expert Advisor, but it's not added!?!?


It means that you have not created an Expert Advisor, but an indicator.

Create a new EA through the wizard and replace only the code of the OnStart function and all global variables/external parameters.

[Deleted]  
komposter:

It means that you have not created an Expert Advisor, but an indicator.

Create a new EA through the wizard and replace only the code of the OnStart function and all global variables/external parameters.

Most likely, he created an Expert Advisor, but with the OnStart() function, which is only for scripts.
[Deleted]  
Grenjohn:

Thank you all very much for your help and for your patience, but so far, unfortunately, it does not work!!!?

I'm trying to add an Expert Advisor, but it's not added!?!?


Create a new Expert Advisor in MQL4, copy the existing code there and replace the line

void OnStart()

by

void OnTick()

This is in case you want to create an Expert Advisor. If you want to create a script, create a new script and completely copy the existing code there without changes (although there is no sense in a script in case of time-based trading).

 
From the article:
Новый бар характеризуется величиной тикового объема, равной 1, если он больше 1, то выполнение функции OnTick завершается.

Where is the code that satisfies this description? I don't understand something, or there is no such thing in the code.