Questions from Beginners MQL5 MT5 MetaTrader 5 - page 641

 
Hi all! Can I ask for some help? I have reset values of global variables for some reason. I have written a simple code which shows everything.
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#include <Trade\Trade.mqh>
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   GlobalVariableSet("Mflag",0);
//---
   return(INIT_SUCCEEDED);
  }

void OnTick()
  {
//---

 Print("до ",GlobalVariableGet("Mflag"));
 GlobalVariableSet("Mflag",1);
 Print("после ", GlobalVariableGet("Mflag"));
     
     
  }

The result is:

before 0.0

after 1.0

before 0.0

after 1.0

to 0.0

It turns into 0 with every tick, although it sets 0 only when calling OnInit, which, as you know, is called once at the start of the EA.

What is the problem, tell me!

 
paylesss:
Hi all! Can I get some help here? I'm having global variable values reset for some reason. I've written a simple code that shows everything.

The result is:

before 0.0

after 1.0

before 0.0

after 1.0

to 0.0

It turns into 0 with every tick, although it sets 0 only when calling OnInit, which, as you know, is called once at the start of the EA.

What is the problem, tell me!

I checked it on the latest build:

2016.10.12 12:15:07.743 Terminal        MetaTrader 5 x64 build 1445 started (MetaQuotes Software Corp.)
2016.10.12 12:15:07.761 Terminal        Windows 10 Pro (x64 based PC), IE 11.00, UAC, Intel Core i3-3120 M  @ 2.50 GHz, RAM: 4737 / 8077 Mb, HDD: 40749 / 233310 Mb, GMT+02:00
2016.10.12 12:15:07.761 Terminal        C:\Users\KVN\AppData\Roaming\MetaQuotes\Terminal\D0E8209F77C8CF37AD8BF550E51FF075

Expert Advisor code:

//+------------------------------------------------------------------+
//|                                                 ProjectName.mql5 |
//|                                      Copyright 2012, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#include <Trade\Trade.mqh>
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   GlobalVariableSet("Mflag",0);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   Print("до ",GlobalVariableGet("Mflag"));
   GlobalVariableSet("Mflag",1);
   Print("после ",GlobalVariableGet("Mflag"));
  }
//+------------------------------------------------------------------+

Result:

2016.10.12 12:29:50.934 Tests (GBPUSD,M5)       до 0.0
2016.10.12 12:29:50.934 Tests (GBPUSD,M5)       после 1.0
2016.10.12 12:29:51.400 Tests (GBPUSD,M5)       до 1.0
2016.10.12 12:29:51.400 Tests (GBPUSD,M5)       после 1.0
2016.10.12 12:29:51.819 Tests (GBPUSD,M5)       до 1.0
2016.10.12 12:29:51.819 Tests (GBPUSD,M5)       после 1.0
2016.10.12 12:29:53.199 Tests (GBPUSD,M5)       до 1.0
2016.10.12 12:29:53.199 Tests (GBPUSD,M5)       после 1.0
2016.10.12 12:29:53.251 Tests (GBPUSD,M5)       до 1.0
2016.10.12 12:29:53.251 Tests (GBPUSD,M5)       после 1.0
2016.10.12 12:29:53.341 Tests (GBPUSD,M5)       до 1.0
2016.10.12 12:29:53.341 Tests (GBPUSD,M5)       после 1.0

No errors detected, code works correctly.

Files:
Tests.mq5  3 kb
 

Thanks! Restarting the computer helped! It's a shame, I've been sitting here all morning))

 
Hello, could you tell me if it is possible to implement a formula in the code for a tester?
If(mql5 cloud network is switched off and testing takes more than 2-5 hours (for example)) then switch on mql5 cloud network
 
newnek:
Hello, could you tell me if it is possible to implement a formula in the code for a tester?
If(mql5 cloud network is disabled and testing takes more than 2-5 hours (for example)) then enable mql5 cloud network
I think MQL5 has no control functions for enabling/disabling MQL5 Cloud Network yet. If you immediately start testing from the command line, you can connect the network.
 
Karputov Vladimir:
It seems that MQL5 does not yet have functions to control connecting/disconnecting the MQL5 Cloud Network. If you start testing immediately from the command line, you can connect the network.
Yes, I've seen that, I just want to turn on the cloud after some time, not to spend money on payment, but when I am sure that everything has been done in my Expert Advisor and I haven't stopped testing it
 
newnek:
Yes, I've seen that, I just want to turn on the cloud after some time, not to spend money to pay, but when I'm sure that I've made all the arrangements in the EA and haven't stopped testing it
Then WinAPI is the only way to help.
 
Vladislav Andruschenko:
Thank you.
 
Karputov Vladimir:
Then only WinAPI will help.
I haven't gotten this far yet
 

wrote an owl, but for some reason the trailing stop is not working... What could be the reason?

//+------------------------------------------------------------------+
//|                                                         DDos.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
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
// Переменные, изменяемые программно
int Corr;                    // Автоматический корректор котировок, 10 для 5-значных котировок и 1 для 4-значных
int TwoDigCorr;              // Корректор для работы с двухзначными котировками
bool New_Bar=false;          // Флаг образования нового бара
int Oppoz;                   // Флаг выставления противоположной отложки при СЛ
int TradeFlag;               // Флаг возможности открытия сделок
//--- input parameter
input double InpPips=20;
extern double Lot=0.01;
extern int TrailingStop = 30;    // размер трейлинг стопа
extern int TrailingStep = 10;   // шаг трейлинга
extern int Magic = 0;        // номер ордера
extern int Slippage = 5;       // проскальзывание
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
 // Проверка необходимых для работы условий
  if (IsTradeAllowed())
   Alert("Работа советника  по символу ",Symbol()," начата.");
  else
  {
  Alert("Торговля советниками не разрешена! Советник не будет работать. Проверьте настройки Вашего терминала.");
  }
  // Проверка пользовательских объемов
    double Mlot=MarketInfo(Symbol(),MODE_MINLOT);
    if(Lot<Mlot)
     {
      Alert("Недопустимо маленькое значение объема сделок. Объем установлен на допустимый минимум!");
      Print("Недопустимо маленькое значение объема сделок. Объем установлен на допустимый минимум!");
      Lot=Mlot;
     }
  // Инициализация первичных данных
   if (Point==0.0001) Corr=1;
     else Corr=10;
     if(Digits==2) TwoDigCorr=10;
    else TwoDigCorr=1;
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   static double prev_price=0;
   double price=0;
//---
   MqlTick last_tick;
//--- 
   if(SymbolInfoTick(Symbol(),last_tick))
     {
      prev_price=last_tick.bid;

      double temp=prev_price-price;
      double rezult=temp*MathPow(10,Digits());
      prev_price=price;
      Comment("    rezult = ",rezult,"    price = ",price,"    temp = ",temp,"    Pips = ",InpPips*Point);
      if(rezult>InpPips)
        {
         OrderSend(Symbol(),OP_BUY,Lot,Ask,5,Bid-100*Point,Bid+1500*Point,0,0,5,clrBlue);// открываем бай
         OrderSend(Symbol(),OP_SELL,Lot,Bid,5,Ask+100*Point,Ask-1500*Point,0,0,10,clrRed); // открываем селл
         
        }
        for (int i=1; i<=OrdersTotal(); i++)       //Цикл по всем ордерам,..
     {                                        //отражённым в терминале
      if(OrderSelect(i-1,SELECT_BY_POS)==true)//Если есть следующий
        {                                     
         OrderSelect(0,SELECT_BY_TICKET);
         OrderSelect(0,SELECT_BY_TICKET);
        
        }
     }                                        //Конец тела цикла
    
     }
    
   else Print("SymbolInfoTick() failed, error = ",GetLastError());
  }
  
  
  
  //+------------------------------------------------------------------+
  
  void Trailing()
  {
  for(int i=OrdersTotal() -1; i>0; i--)
  {
  if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
  {
  if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
  {
  if(OrderType()== OP_BUY)
  {
  if(Bid - OrderOpenPrice() > TrailingStop*Point || OrderStopLoss() == 0 )
  {
  if (OrderStopLoss() < Bid-(TrailingStep+TrailingStep)*Point || OrderStopLoss() == 0)
  {
 if  (!OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid-TrailingStop*Point,Digits),0,0 ));
  Print("Ошибка модификации ордера на покупку!");
  }
  }
  }
 if (OrderType() == OP_SELL)
 {
 if (OrderOpenPrice()- Ask > TrailingStop*Point || OrderStopLoss() == 0 )
{
if (OrderStopLoss() > Ask +(TrailingStep+TrailingStep)*Point || OrderStopLoss() == 0)
{
if (!OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Ask + TrailingStop*Point, Digits),0,0));
Print("Ошибка модификации ордера на продажу!");
}
}
 }

  }
  }
  }
  }