Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Nowhere without you - 6. - page 1104

 
evillive:

Initu is for initu, trade is for togo. There's OnTick for EAs, there's no need to shove everything into the init...

And for inite, there are special exit codes invented by the developers, why don't we use them?

Probably because we don't know about inite termination codes. The examples in the tutorial are different from the form in the editor. The int start() function, for example, is missing there. When I pasted the example of simple Expert Advisor from the tutorial into the inite, it somehow worked. But no matter what I wrote, it did not work. So I decided, after two weeks of experiments, to consult an expert. Thanks for the tip!

I put it all into OnTick, but nothing has changed.

 
shtr:

We don't use it, it must be because we don't know about init completion codes. The examples in the textbook differ from the form in the editor. The int start() function, for example, is missing there. When I pasted the example of simple Expert Advisor from the tutorial into the inite, it somehow worked. But no matter what I wrote, it did not work. So I decided, after two weeks of experiments, to consult an expert. Thanks for the tip!

Threw it all into OnTick, but nothing has changed.

Forget the tutorial, it's written using the old syntax, a lot has changed since then. The help from the terminal delivery is the most relevant, and kodobase, if you look, is also a source of examples in the new version of MQL.

And about not writing all code in the inite, it was before, search through the forum, there are hundreds of posts in all languages of this forum, just every time there is a new writer who is not a reader ;)

 
evillive:

Forget the tutorial, it's written according to the old syntax, a lot has changed since then. The help from the terminal delivery is the most relevant, and kodobase, if you look, is also a source of examples in the new version of MQL.

And about not writing all code in the inite, it was before, search through the forum, there are hundreds of posts in all languages of this forum, just every time there is a new writer, who is not a reader ;)

Thanks!

Old textbooks, when they lose their relevance, should be immediately snuffed out))

 
shtr:

Thank you!

Old tutorials, when they lose their relevance, should be immediately smoked))

And do not throw everything in OnTick, only what is relevant to the trade, the part of the code that runs only at startup should be left in OnInit (of course, that the names of functions should be written in Latin, just lazy to change the layout on the forum every time).

About the code itself, there are questions about the loop, it's strange, in my opinion there are not enough curly brackets...

Yes, and also for calculation of profit and loss levels, they are not correctly prescribed. The server should send the price, preferably normalized, not the value in pips.

 
evillive:

And don't put everything in OnTick, only what is relevant to trade, part of the code that triggers only at startup should be left in OnInit (of course, function names should be written in Latin, just too lazy to change the layout on the forum every time).

About the code itself, there are questions about the loop, it's strange, I think there are not enough curly brackets...

Yes, and also for calculation of profit and loss levels, they are incorrectly prescribed. The server should send the price, preferably normalized, not the value in pips.

I got it, thanks. I will check the brackets - the editor did not produce errors.

Why did the example from the book work? I took down all the functions and inserted only the example code, starting with int start. There were no errors and the trades opened.

 
shtr:

Got it, thanks. I'll check the brackets - the editor didn't give any errors.

Why did the example from the book work...? I took down all the functions and inserted only the example code, starting from int start. There were no errors and the trades opened.

Well, I had no need to trade what worked ))))

If your question is about the difference between start and ontik in new way, you should have transferred everything from start to ontik. Or rather in your case, from OnInit to OnTick.

The compiler keeps track of syntax errors, no one but a human checks the logic. In special cases, even the author of the code can get confused :)

 
evillive:

Well, what worked didn't need to be traded ))))

And Int start the old way and OnTick the new way for EAs, so you should have moved everything from start to OnTick. Or rather in your case, from OnInit to OnTick.

The compiler keeps track of syntax errors, no one but a human checks the logic. In special cases, even the author of the code can get confused :)

//+------------------------------------------------------------------+
//| 3333.mq4 |
//| Copyright 2016, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
extern int MA_1=5;
extern int MA_2=20;
extern MA_3=80;
extern inttern TP=100;
extern int SL=50;
extern double Lot=0.1;
string Symb;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
//---
return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
double MA_1_t;
double MA_2_t;
double MA_3_t;
int total;
int pos;
Symb=Symbol();
Total=0;
for( pos=1; pos<=OrdersTotal(); pos++)
pos=OrderSelect(pos-1,MODE_TRADES);
if(pos==0)
MA_1_t=iMA(NULL,0,MA_1,0,MODE_EMA,PRICE_CLOSE,0);
MA_2_t=iMA(NULL,0,MA_2,0,MODE_EMA,PRICE_CLOSE,0);
MA_3_t=iMA(NULL,0,MA_3,0,MODE_EMA,PRICE_CLOSE,0);
if(MA_2_t>MA_3_t&&MA_1_t<MA_2_t&&Open[1]<MA_1_t&&Close[1]>MA_1_t)
{
double stoploss=NormalizeDouble(Ask-SL*Point,Digits);
double takeprofit=NormalizeDouble(Ask+TP*Point,Digits);
pos=OrderSend(Symbol(),OP_BUY,Lot,Ask,3,stoploss,takeprofit, "My order",16384,0,clrGreen);
return;
}
if(MA_2_t<MA_3_t&&MA_1_t>MA_2_t&&Open[1]>MA_1_t&&Close[1]<MA_1_t)
{
double stoploss=NormalizeDouble(SL*Point-Bid,Digits);
double takeprofit=NormalizeDouble(Bid-TP*Point,Digits);
pos=OrderSend(Symbol(),OP_SELL,Lot,Bid,3,stoploss,takeprofit, "My order",16384,0,clrRed);
return;
}
return;
}
//---

//+------------------------------------------------------------------+

"Well, what worked didn't need to be peddled)))" Well, you want to scribble something yourself.)

Patient didn't come to his senses after the manipulation.

 
shtr:

"Well, you shouldn't have traded in what worked.")) Well, you want to scribble something yourself.)

The patient didn't come to his senses after the manipulation.

Well, the logic and maths is lame, it's clear that it will not work that way.

For example, why do we need a cycle in this EA? I mean, it's clear that we need to calculate how many orders for the given symbol and magician have already been placed, right? But what we see here is that the loop does not count anything and cannot count because it has not got a "counter" screwed in ))))

Total=0;                                   

   for( pos=1; pos<=OrdersTotal(); pos++)         
   pos=OrderSelect(pos-1,MODE_TRADES); И что дальше? Переменная pos всегда или 0, или 1, цикл сбивается постоянно на начало, а то и заглючить может и превратиться в бесконечный цикл.

OrderSelect() returns a boolean value, true or false, which is not appropriate for the pos variable. And the loop should better start from the oldest order, i.e., in reverse order, to avoid skipping orders.

The Total variable can be useful as a counter:

for(pos=OrdersTotal()-1;pos>=0;pos--)
{
  if(OrderSelect(pos,MODE_TRADES)) //Обязательно проверить, выбран ли ордер
  { 
    if(OrderSymbol()==Symbol()) //желательно проверить, тот ли это ордер по символу, ещё можно по магику проверить дополнительно в этой же строке
    {
     Total++; //если это наш ордер, добавляем счётчику единицу
    }
  }
}
if(Total<1)
{
  торгуем
}

I removed unnecessary things, added error output after OrderSend, for tester on non-ECN accounts the orders already open:

#property version   "1.00"
#property strict
extern int MA_1=5;
extern int MA_2=20;
extern int MA_3=80;
extern int TP=100;
extern int SL=50;
extern double Lot=0.1;

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
  
   double MA_1_t=0;
   double MA_2_t=0; 
   double MA_3_t=0;
   int Total=0;
   
   for(int pos=OrdersTotal()-1;pos>=0;pos--)
   {
     if(OrderSelect(pos,MODE_TRADES)) //Обязательно проверить, выбран ли ордер
     { 
       if(OrderSymbol()==Symbol()) //желательно проверить, тот ли это ордер по символу, ещё можно по магику проверить дополнительно в этой же строке
       {
        Total++; //если это наш ордер, добавляем счётчику единицу
       }
     }
   }
   if(Total<1)
   {
      MA_1_t=iMA(NULL,0,MA_1,0,MODE_EMA,PRICE_CLOSE,0);
      MA_2_t=iMA(NULL,0,MA_2,0,MODE_EMA,PRICE_CLOSE,0); 
      MA_3_t=iMA(NULL,0,MA_3,0,MODE_EMA,PRICE_CLOSE,0);
  
      if(MA_2_t>MA_3_t&&MA_1_t<MA_2_t&&Open[1]<MA_1_t&&Close[1]>MA_1_t)
      {
         double stoploss=NormalizeDouble(Ask-SL*Point,Digits);
         double takeprofit=NormalizeDouble(Ask+TP*Point,Digits);
         if(OrderSend(Symbol(),OP_BUY,Lot,Ask,6,stoploss,takeprofit,"My order buy",16384,0,clrGreen)<1) 
         Print("OrderSend error #",_LastError," BuySL=",stoploss," BuyTP=",takeprofit);
      }
    
      if(MA_2_t<MA_3_t&&MA_1_t>MA_2_t&&Open[1]>MA_1_t&&Close[1]<MA_1_t)
      {
         double stoploss=NormalizeDouble(Bid+SL*Point,Digits);
         double takeprofit=NormalizeDouble(Bid-TP*Point,Digits);
         if(OrderSend(Symbol(),OP_SELL,Lot,Bid,6,stoploss,takeprofit,"My order sell",16384,0,clrRed)<1)
         Print("OrderSend error #",_LastError," Sell SL=",stoploss," Sell TP=",takeprofit);
      }
   }
}

Oh, yes, also the calculation of the SL level for sales was wrong, we should be more attentive.

 
evillive:

Well, the logic and maths is lame, so it's clear that it won't work like that.

For example, why would there be a cycle in this EA? I mean, it's clear that we need to calculate how many orders for the given symbol and magic number we have, right? But what we see here is that the loop does not count anything and cannot count because it has not got a "counter" screwed in ))))

OrderSelect() returns a boolean value, true or false, which is not appropriate for the pos variable. And the loop should better start from the oldest order, i.e., in reverse order, to avoid skipping orders.

The Total variable can be useful as a counter:

I removed unnecessary things, added error output after OrderSend, for the tester on non-ECN accounts the orders already open:

Oh, yes, also the calculation of the SL level for sales was wrong, you need to be more careful.

Wow!!! Thanks!!! That requires some reflection.

Maybe there's a "self-study" on the new rules somewhere? The 4 digit page number is a bit stressful...

 
shtr:

Wow!!! Thank you! This requires some reflection.

Maybe there's a "self-study" on the new rules somewhere? The 4 digit page number is a bit stressful...

Who's easy now... There are 5 more such topics about newbies in the archive ))))

To be honest, examples from help and codobase are enough if you already have programming experience, preferably in C++. If you don't have any experience at all, you may have a look at C++ tutorial first, MQL is very similar in syntax.

Reason: