Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1115

 
I figured it out, it's just a broken file, but the executable is intact, so it works...
 
xxz:

......
EA works, but when I try to open it in editor

......

xxz:

it's not an executable, it's an mq5 file...

this is my code...

So form your phrases correctly.

On topic. I usually open such problematic ones in Notepad++. It helps. But not always )))

 
Сергей Таболин:

So form your phrases correctly.

On topic. I usually open such problematic ones in Notepad++. It helps. But not always ))))

I also decided to look through it with Notepad++ and it was almost all zeros, although the file was the normal size as it should be...

 
Comments not related to this topic have been moved to "Questions from MQL4 MT4 MetaTrader 4 beginners".
 

I have written an EA. The person I wrote it to, on the first run, opens two positions one after the other. I cannot reproduce this problem with myself. I would like to ask you for help.

The OnTick() function

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick() {
   
   GetData();
   CheckForOpen();
   CheckForCloseCP();
   CheckForCloseAll();

}
//+------------------------------------------------------------------+

Only the CheckForOpen() function is responsible for opening positions:

enum opp_mode {
   DoClose,       // Close and reverse with initial lot
   DoReverse,     // Close and reverse (continue Martingale last lot)
   DoHedge,       // No closing reversal (Hedge mode)
};

// Trade & MM Section
input opp_mode Mode_opp    = DoHedge;     // Trading Mode

//+------------------------------------------------------------------+
//| Check for Open function                                          |
//+------------------------------------------------------------------+
void CheckForOpen() {

   if (!IsBarNew) return;

double lot=0;
positions=Positions();

   if (Signal==OP_BUY) {
      if (longs>0) return;
      if (shorts==0) {     // First Order Placing
         Print("   *** MSG ***   "+msg);
         PlaceMarketOrder(OP_BUY, Lot, _ask, _SL, _TP);
      }
      if (shorts>0) {
         if (Mode_opp==DoClose) {      // Closing by Opposite Signal
            Print("   *** MSG ***   "+msg);
            Print("   ***   Close mode SELL by opposite signal."); 
            ClosePositions(OP_SELL);
            PlaceMarketOrder(OP_BUY, Lot, _ask, _SL, _TP);
         }
         if (Mode_opp==DoReverse) {    // Reverse by Opposite Signal
            Print("Reversing SELL by opposite signal.");
            lot=GetMaxLot(ORDER_TYPE_SELL);
            if (IsMartingale) lot=lot*LotMult;
            ClosePositions(OP_SELL);
            PlaceMarketOrder(OP_BUY, lot, _ask, _SL, _TP);
         }
         if (Mode_opp==DoHedge) {      // Hedging by Opposite Signal
            Print("Hedging SELL by opposite signal."); 
            MartinCounterB=50; MartinCounterS=50;
            PlaceMarketOrder(OP_BUY, GetMaxLot(ORDER_TYPE_SELL)*LotMult, _ask, _SL, _TP);
         }
      }
   }
   if (Signal==OP_SELL) {
      if (shorts>0) return;
      if (longs==0) {     // First Order Placing
         Print("   *** MSG ***   "+msg);
         PlaceMarketOrder(OP_SELL, Lot, _bid, _SL, _TP);
      }
      if (longs>0) {
         if (Mode_opp==DoClose) {      // Closing by Opposite Signal
            Print("   *** MSG ***   "+msg);
            Print("   ***   Close mode BUY by opposite signal."); 
            ClosePositions(OP_BUY);
            PlaceMarketOrder(OP_SELL, Lot, _bid, _SL, _TP);
         }
         if (Mode_opp==DoReverse) {    // Reverse by Opposite Signal
            Print("Reversing BUY by opposite signal."); 
            lot=GetMaxLot(ORDER_TYPE_BUY);
            if (IsMartingale) lot=lot*LotMult;
            ClosePositions(OP_BUY);
            PlaceMarketOrder(OP_SELL, lot, _bid, _SL, _TP);
         }
         if (Mode_opp==DoHedge) {      // Hedging by Opposite Signal
            Print("Hedging BUY by opposite signal."); 
            MartinCounterB=50; MartinCounterS=50;
            PlaceMarketOrder(OP_SELL, GetMaxLot(ORDER_TYPE_BUY)*LotMult, _bid, _SL, _TP);
         }
      }
   }
}
//+------------------------------------------------------------------+

The function that sends a request to open a position from the market. I added a delayed replay in it when the server crashes because I often get requotes/offquotes when testing with MetaQuotes MT5 demo.

//+------------------------------------------------------------------+
//| Place Market Order function                                      |
//+------------------------------------------------------------------+
bool PlaceMarketOrder(int oper, double lot, double oop, double sl, double tp) {   //  mn - Magic Number

double d_sl=0, d_tp=0;

   MqlTick stTick;
   if (SymbolInfoTick(_Symbol,stTick)==false) {
      Print("SymbolInfoTick function returned FALSE. Error=", GetLastError());
   }

   Print("Placing market order. Type="+(string)oper+", OrderOpenPrice="+DoubleToString(oop,_Digits)+
         ", Bid="+DoubleToString(_bid,_Digits)+", Ask="+DoubleToString(_ask,_Digits));

   if (MathAbs(lot-Lot)<1 e-5) {MartinCounterB=0; MartinCounterS=0;}
     
   if (oper==OP_BUY)  {
      for (int i=0; i<5; i++) {
         if (SymbolInfoTick(_Symbol,stTick)==false) {
            Print("SymbolInfoTick function returned FALSE. Error=", GetLastError());
         }
         _ask=stTick.ask;
         d_sl=ND(_ask-sl*pips); if (MathAbs(sl)<1 e-5) d_sl=ND(0.0);
         d_tp=ND(_ask+tp*pips); if (MathAbs(tp)<1 e-5) d_tp=ND(0.0);
         if (m_trade.Buy(NormalizeLot(lot),NULL,_ask,d_sl,d_tp,Comm)) {PlaySound("expert.wav"); return(true);}
         else {PlaySound("disconnect.wav"); Sleep(5000);}
      }
   }
   if (oper==OP_SELL) {
      for (int i=0; i<5; i++) {
         if (SymbolInfoTick(_Symbol,stTick)==false) {
            Print("SymbolInfoTick function returned FALSE. Error=", GetLastError());
         }
         _bid=stTick.bid;
         d_sl=ND(_bid+sl*pips); if (MathAbs(sl)<1 e-5) d_sl=ND(0.0);
         d_tp=ND(_bid-tp*pips); if (MathAbs(tp)<1 e-5) d_tp=ND(0.0);
         if (m_trade.Sell(NormalizeLot(lot),NULL,_bid,d_sl,d_tp,Comm)) {PlaySound("expert.wav"); return(true);}
         else {PlaySound("disconnect.wav"); Sleep(5000);}
      }
   }
     
   return(false);
     
}
//+------------------------------------------------------------------+

The function of controlling the opening of a new bar:

//+------------------------------------------------------------------+
//| Global Variables                                                 |
//+------------------------------------------------------------------+
bool   IsBarNew=false;


//+------------------------------------------------------------------+
//| Bar refreshing function                                          |
//| true - new bar opened, false - not opened                        |
//+------------------------------------------------------------------+
bool IsNewBar() {

static datetime SavedTime=iTime(NULL,PERIOD_CURRENT,0);
       datetime curTime  =iTime(NULL,PERIOD_CURRENT,0);

   if (curTime>SavedTime) {
         SavedTime=curTime;
         return(true);
   }
   else return(false);
}
//+------------------------------------------------------------------+

The second position opens immediately after the first one, in the same second, the ticks differ by one. I initially thought there was a second copy of the EA on the second chart. I asked to close all of them and leave one chart and run the EA, but according to the customer and judging by the screenshot, that is not the reason. Subsequent positions are opened one at a time, there are no more duplicates. I cannot reproduce it, I have already wracked my brains. I have tested it on my MT5 demo ICMarkets hedge as well as the orderer. Screenshot of the doublet.


The whole EA.

Files:
 
Grigori.S.B:

I have written an EA. The person I wrote it to, on the first run, opens two positions one after the other. I cannot reproduce this problem with myself. I would like to ask you for help.

The OnTick() function

Only the CheckForOpen() function is responsible for opening positions:

The function that sends a request to open a position from the market. I added a delayed replay in it when the server crashes because I often get requotes/offquotes when testing with MetaQuotes MT5 demo.

The function of controlling the opening of a new bar:

The second position opens immediately after the first one, in the same second, the ticks differ by one. I initially thought there was a second copy of the EA on the second chart. I asked to close all of them and leave one chart and run the EA, but according to the customer and judging by the screenshot, that is not the reason. Subsequent positions are opened one at a time, there are no more duplicates. I cannot reproduce it, I have already wracked my brains. I have tested it on my MT5 demo ICMarkets hedge as well as the orderer. Screenshot of the doublet.


The whole EA.

I use this function to control new bar - sometimes there are errors when copying bar date.

//+------------------------------------------------------------------------------------------------------------------+
//| Возвращает TRUE, если появился новый бар на текущем ТФ
//+------------------------------------------------------------------------------------------------------------------+
bool isNewBar()
  {
   datetime tm[];
   static datetime prevBarTime=0;

   if(CopyTime(_Symbol,0,0,1,tm)<0)
     {
      Print("%s CopyTime error = %d",__FUNCTION__,GetLastError());
     }
   else
     {
      if(prevBarTime!=tm[0])
        {
         prevBarTime=tm[0];
         return true;
        }
      return false;
     }
   return true;
  }
 
Aleksey Vyazmikin:

I use such a function to control a new bar - there are times when there are errors when copying the bar date.

Thank you. I will try to replace it. Although so far the function I've used has worked fine as well. I have an idea to display bar open time and price in the prints; then it would be clearly seen that the error is there.

However, I am confused by the fact that duplots appear only at opening of the first position while subsequent positions are opened correctly and the EA works for hours generating dozens of them. And the function of bar opening control works every time. And according to the conditions the Expert Advisor opens the first position when there is none and the next ones open only when the price has moved far enough after the first one was opened.

 
Grigori.S.B:

I have written an EA. The person I wrote it to, on the first run, opens two positions one after the other. I cannot reproduce this problem with myself. I would like to ask you for help.

The OnTick() function

Only the CheckForOpen() function is responsible for opening positions:

The function that sends a request to open a position from the market. I added a delayed replay in it when the server crashes because I often get requotes/offquotes when testing with MetaQuotes MT5 demo.

The function of controlling the opening of a new bar:

The second position opens immediately after the first one, in the same second, the ticks differ by one. I initially thought there was a second copy of the EA on the second chart. I asked to close all of them and leave one chart and run the EA, but according to the customer and judging by the screenshot, that is not the reason. Subsequent positions are opened one at a time, there are no more duplicates. I cannot reproduce it, I have already wracked my brains. I have tested it on my MT5 demo ICMarkets hedge as well as the orderer. Screenshot of the doublet.


The whole EA.

And how do you check that a position has not already been opened on this bar?

 
Artyom Trishkin:

And how do you check that no position has opened on this bar yet?

No, there is no such check.

There are such checks:

  • If a new bar has opened, with its first tick a position can be opened,
  • If there are no positions, the first position can be opened.
I.e. for some reason two positions open at once on the first tick of a new bar. And only the first positions are opened. Further this phenomenon is not observed.
 
Grigori.S.B:

No, there is no such check.

There are such checks:

  • If a new bar has opened, with its first tick a position can be opened,
  • If there are no positions, the first position can be opened.
I.e. for some reason two positions open at once on the first tick of a new bar. And only the first positions are opened. Further this phenomenon is not observed.

I do not have time to analyze what is going on there. But try to check the number of open positions on the new bar. If there are none - then open.

Reason: