OnBarChange - page 2

 
You should first learn about the structure of a program and how the if/else and other control structures work and what the {} mean and other things before you continue poking around in the dark.
 

I tried to put return; after if(newbar() == true) like this:

if(newbar() == true)
return;

and now I have better results, basically the same number of trades as in Metastock. Still have some bugs, some signals that don't trigger an order and some orders trigered without a signal like those below. Could this happen in live trading or is just a bug?

Best regards.


 
7bit:
You should first learn about the structure of a program and how the if/else and other control structures work and what the {} mean and other things before you continue poking around in the dark.

Hi. You are totally right. But the documentation and book about MQL4 in many cases doesn't explain in a way to allow people not programmer understand in a more or less easily manner. It should have more examples. I am not a programmer and I learned some time ago to program CPLD boards, because I got some books that explained very well.

Best regards.

 

hello imrf..

that buy order opened without buy signal, because the previous green line have touch red line.. then next bar, ea open buy position and in some seconds, candlestick down greatly, and make the previous green line moving down too, so that make green line separated with red line..

as i know the previous line can moving more, if the next bar down or up greatly in first minutes..

if the candlestick at next bar down or up not big at first minutes, so the previous line will be not moving..

ok.. hehe..

 
lmrf:

Hi. You are totally right. But the documentation and book about MQL4 in many cases doesn't explain in a way to allow people not programmer understand in a more or less easily manner. It should have more examples. I am not a programmer and I learned some time ago to program CPLD boards, because I got some books that explained very well.

Best regards.


      if(newbar() == true)
         {
      if(StoCurrent>SignalCurrent && StoPrevious<SignalPrevious)    //TRADING CRITERIA
        {
         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,0,0,0,"",MAGICMA,0,Green);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
           }  
         else Print("Error opening BUY order : ",GetLastError()); 
         return(0); 
        }
      }

//or

    if(newbar() == true && StoCurrent>SignalCurrent && StoPrevious<SignalPrevious)    //TRADING CRITERIA
        {
         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,0,0,0,"",MAGICMA,0,Green);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
           }  
         else Print("Error opening BUY order : ",GetLastError()); 
         return(0); 
        }

//or

    if(newbar() && StoCurrent>SignalCurrent && StoPrevious<SignalPrevious)    //TRADING CRITERIA
        {
         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,0,0,0,"",MAGICMA,0,Green);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
           }  
         else Print("Error opening BUY order : ",GetLastError()); 
         return(0); 
        }

b.t.w. {} as nothing whit mql it's the same by all Languages

 
qjol:

b.t.w. {} as nothing whit mql it's the same by all Languages

It doesn't work that way. The only way I tried and worked was to put "if(newbar() == true) return;" before "// check for long position (BUY) possibility". This way I think if trading criteria for check for long position (BUY) possibility is false it passes control to "// check for short position (SELL) possibility".

And put again "if(newbar() == true) return;" before "if(OrderType()==OP_BUY)".

Even this way there are some problems that maybe won't be resolved with the function "newbar()".

Best regards.
total=OrdersTotal();
   if(total<1) 
     {
      // no opened orders identified
      if(AccountFreeMargin()<(1000*Lots))
        {
         Print("No sufficient funds. Free Margin = ", AccountFreeMargin());
         return(0);  
        }
      if(newbar() == true) return;
      // check for long position (BUY) possibility
      if(StoCurrent>SignalCurrent && StoPrevious<SignalPrevious)
        {
         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,0,0,0,"",MAGICMA,0,Green);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
           }  
         else Print("Error opening BUY order : ",GetLastError()); 
         return(0); 
        }
      // check for short position (SELL) possibility
      if(StoCurrent<SignalCurrent && StoPrevious>SignalPrevious)
        {
         ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,0,0,0,"",MAGICMA,0,Red);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
           }
         else Print("Error opening SELL order : ",GetLastError()); 
         return(0); 
        }
     }
// --------  
   for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&       // check for opened position 
         OrderSymbol()==Symbol() &&    // check for symbol
         OrderMagicNumber() == MAGICMA) // check for magic number
        {
         if(newbar() == true) return;
         if(OrderType()==OP_BUY)   // long position is opened
           {
            // should it be closed?
            if(StoCurrent<SignalCurrent && StoPrevious>SignalPrevious)
              {
               OrderClose(OrderTicket(),OrderLots(),Bid,0,Violet); // close position
               return(0); // exit
              }
           }
         else // go to short position
           {
            // should it be closed?
            if(StoCurrent>SignalCurrent && StoPrevious<SignalPrevious)
              {
               OrderClose(OrderTicket(),OrderLots(),Ask,0,Violet); // close position
               return(0); // exit
              }
           }
        }
     }
   return(0);
  }
// the end.
 

I think I discovered why some orders were triggered without signal. When I defined the StochasticCurrent, I chosed the current bar "0", when defined StochasticPrevious, I chosed the previous bar "1". But when I put the function Newbar() I wanted the order to trigger only on the beginning of the new bar after the system declares I have a signal to buy or sell at the end of the previous bar. But in all cases, when the new bar starts is when I have a signal to buy or sell (depending on the trading criteria, obviously) and never before the new bar start. And in many cases (not all), when the new bar ends, I don't have the signal to buy or sell because the market moved back, but the system already triggered the order at the beginning of the new bar. So I changed the "int shift" of current bar to 1 and the previous bar to 2 and the results were far better. Now when I have a Newbar, I have the confirmation that a signal ocurred after the "old new bar" closed. Not checked yet personally all the cases, but the numbers are now close of what I tested in Metastock.

I don't know if someone had the same problem, here are my 2 cents.

Best regards.

Reason: