Loop for

To add comments, please log in or register
northedan
175
northedan  

What's wrong with this?

EA crasches in this section.

int WinCount;
int LossCount;

for (int Count = OrdersHistoryTotal()-1; ; Count--)
{
   OrderSelect(Count,SELECT_BY_POS, MODE_HISTORY);
   if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
   {
      if(OrderProfit() > 0 && LossCount == 0) WinCount++;
      else if (OrderProfit() < 0 && WinCount == 0) LossCount++;
      else break;
   }
}
northedan
175
northedan  

Full code.

#property copyright "Tarcísio Allyson"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict


// Extern Variable

extern double LotSize = 0.01;
extern double TakeProfit = 5;
extern double StopLoss = 15;

extern int Slippage = 5;
extern int MagicNumber = 123;

extern int MAFastPeriodo = 21;
extern int MAMainPeriodo = 42;
extern int MASlowPeriodo = 66;

// Global Variable

int BuyTicket;
int SellTicket;
double UsePoint;
int UseSlippage;

double BuyStopLoss;
double BuyTakeProfit;

double SellStopLoss;
double SellTakeProfit;


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   Comment("EA Started");
   UsePoint = PipPoint(Symbol());
   UseSlippage = GetSlippage(Symbol(), Slippage);

//---
   return(0);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---

   double FastMAValue = iMA(NULL,0,MAFastPeriodo,0,MODE_EMA,PRICE_CLOSE,0);
   double MainMAValue = iMA(NULL,0,MAMainPeriodo,0,MODE_EMA,PRICE_CLOSE,0);
   double SlowMAValue = iMA(NULL,0,MASlowPeriodo,0,MODE_EMA,PRICE_CLOSE,0);
   
   //Buy Order
   if (FastMAValue > MainMAValue && MainMAValue > SlowMAValue && Bid > SlowMAValue && Bid < MainMAValue && BuyTicket == 0)
      {
         OrderSelect(SellTicket,SELECT_BY_TICKET);
         //Close order
         if(OrderCloseTime() == 0 && SellTicket > 0)
            {
               double CloseLots = OrderLots();
               double ClosePrice = Ask;
               
               bool Closed = OrderClose(SellTicket,CloseLots,ClosePrice,UseSlippage,Red);
            }
         
         double OpenPrice = Ask;
         // Calculate stop loss and take profit
                                if(StopLoss > 0) BuyStopLoss = OpenPrice - (StopLoss * UsePoint);
                                if(TakeProfit > 0) BuyTakeProfit = OpenPrice + (TakeProfit * UsePoint);
                                
         // Open buy order
                        BuyTicket = OrderSend(Symbol(),OP_BUY,LotSize,OpenPrice,UseSlippage,BuyStopLoss,BuyTakeProfit,"Buy Order",MagicNumber,0,Green);
      }
      
   //Sell Order
   if (FastMAValue < MainMAValue && MainMAValue < SlowMAValue && Bid < SlowMAValue && Bid > MainMAValue && SellTicket == 0)
      {
         OrderSelect(BuyTicket,SELECT_BY_TICKET);
         //Close order
         if(OrderCloseTime() == 0 && BuyTicket > 0)
            {
               double CloseLots = OrderLots();
               double ClosePrice = Bid;
               
               bool Closed = OrderClose(BuyTicket,CloseLots,ClosePrice,UseSlippage,Red);
            }
         
         double OpenPrice = Bid;
         // Calculate stop loss and take profit
                                if(StopLoss > 0) SellStopLoss = OpenPrice + (StopLoss * UsePoint);
                                if(TakeProfit > 0) SellTakeProfit = OpenPrice - (TakeProfit * UsePoint);
                                
         // Open sell order
                        SellTicket = OrderSend(Symbol(),OP_SELL,LotSize,OpenPrice,UseSlippage,SellStopLoss,SellTakeProfit,"Sell Order",MagicNumber,0,Red);
      }
      
    if (Ask < SellTakeProfit || Ask > SellStopLoss)
      {
         SellTicket = 0;
      }
    if (Bid > BuyTakeProfit || Bid < BuyStopLoss)
      {
         BuyTicket = 0;
      }
      
    //+--------------------------------------------------
    //MARTINGALE
    int WinCount;
    int LossCount;
    
    for (int Count = OrdersHistoryTotal()-1; ; Count--)
      {
         OrderSelect(Count,SELECT_BY_POS, MODE_HISTORY);
         if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
         {
            if(OrderProfit() > 0 && LossCount == 0) WinCount++;
            else if (OrderProfit() < 0 && WinCount == 0) LossCount++;
            else break;
         }
      }
    
  }
//+------------------------------------------------------------------+


// Pip Point Fuction
double PipPoint(string Currency)
   {
      double CalcPoint;
      int CalcDigits = MarketInfo(Currency, MODE_DIGITS);
      if (CalcDigits == 2 || CalcDigits == 3) CalcPoint = 0.01;
      else if (CalcDigits == 4 || CalcDigits == 5) CalcPoint = 0.0001;
      return(CalcPoint);
   }

// Get Slippage Function
int GetSlippage(string Currency, int SlippagePips)
   {
      double CalcSlippage;
      int CalcDigits = MarketInfo(Currency,MODE_DIGITS);
                if(CalcDigits == 2 || CalcDigits == 4) CalcSlippage = SlippagePips;
                else if(CalcDigits == 3 || CalcDigits == 5) CalcSlippage = SlippagePips * 10;
                return(CalcSlippage);
Descubra novos recursos para o MetaTrader 5 com a comunidade e os serviços MQL5
Descubra novos recursos para o MetaTrader 5 com a comunidade e os serviços MQL5
  • www.mql5.com
Tire dúvidas sobre análise técnica, fale com outros traders sobre sistemas de negociação e melhore suas habilidades de programação de estratégias em linguagem MQL5. Participe do fórum, compartilhe ideias com traders de todo o mundo e ajude com feedback os novatos — nossa comunidade está crescendo junto com você. METATRADER 4 - CORRETORAS NO...
Mohamad Zulhairi Baba
16412
Mohamad Zulhairi Baba  
northedan:

What's wrong with this?
EA crasches in this section.

try this

int WinCount = 0;
int LossCount = 0;

for (int Count = OrdersHistoryTotal()-1; Count >= 0; order--) {
   if(OrderSelect(Count,SELECT_BY_POS, MODE_HISTORY)) {
      if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) {
         if(OrderProfit() > 0 && LossCount == 0) WinCount++;
         else if (OrderProfit() < 0 && WinCount == 0) LossCount++;
         else break;
      }
   }
}
William Roeder
20210
William Roeder  
Mohamad Zulhairi Baba: try this
Agreed, mostly :).
  1. There is no variable order. Mohamad meant count--. (Always prefer --count. Important with classes. Slight optimization with int types.)
  2. And once you subtract one, it is no longer a count but a position. Code what you mean, either A) rename the variable, or B) loop count>0 and select count-1.
  3. The break is wrong. If it is not a trade of the current symbol/magic number, ignore it. The break means other trades (on other charts) and manual trading breaks the code.
  4. No need for nested if's.
    for (int Position = OrdersHistoryTotal()-1; Position >= 0; --Position) if(
       OrderSelect(Position,SELECT_BY_POS, MODE_HISTORY)
    && OrderSymbol() == Symbol() 
    && OrderMagicNumber() == MagicNumber) {
             if(OrderProfit() > 0 && LossCount == 0) WinCount++;
             else if (OrderProfit() < 0 && WinCount == 0) LossCount++;
    }
    alternatively
    #define COUNT uint //  One based.
    #define INDEX uint // Zero based.
    for (COUNT count = OrdersHistoryTotal(); count > 0;){ --count; // count is now an INDEX
    if(OrderSelect(count,SELECT_BY_POS, MODE_HISTORY) ...
  5. In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading,) while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing:
    1. For non-FIFO (US brokers,) (or the EA only opens one order per symbol,) you can simply count down in a position loop, and you won't miss orders. Get in the habit of always counting down.
                Loops and Closing or Deleting Orders - MQL4 and MetaTrader 4 - MQL4 programming forum
      For FIFO (US brokers,) and you (potentially) process multiple orders per symbol, you must count up and on a successful operation, reprocess all positions (set index to -1 before continuing.)
    2. and check OrderSelect in case earlier positions were deleted.
                What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
                Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    3. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use, on the next order / server call, the Predefined Variables (Bid/Ask) or (be direction independent and use) OrderClosePrice().
Mohamad Zulhairi Baba
16412
Mohamad Zulhairi Baba  
whroeder1:
  1. There is no variable order. Mohamad meant count--. (Always prefer --count. Important with classes. Slight optimization with int types.

  1. order variable is typo error, when copying form my code. thank you for that.
  2. Increment/decrement operators, and side effects.
  3. notice you post less now, on this forum :)
3.3 — Increment/decrement operators, and side effects
3.3 — Increment/decrement operators, and side effects
  • www.learncpp.com
Incrementing (adding 1 to) and decrementing (subtracting 1 from) a variable are so common that they have their own operators in C. There are actually two versions of each operator — a prefix …
nicholi shen
2392
nicholi shen  
FWIW, inc/dec operator can go either before or after the int iterator in a MQL for loop and there's no difference or performance advantage (*in MQL, last time I profiled)-- so it boils down to personal style. That said, I've personally grown fond of 
for(int i=x-1; i>=0; --i)
//and
for(int i=0; i<x; i++)

because it makes the indexing direction instantly stand out, thus making the code easier to mentally parse.  I mostly agree with whoeder with the exception of a few points. *These are notes for the OP

Point 2: Agree. Variables should be named to explicitly identify it. By convention, the index iterator is usually named i (short for index or iterator) followed by the remaining (lower-case) ascii-alphabet for nested loops. 

for(int i=0; i<x-1; i++)
   for(int j=i+1; j<x; j++)

Point 3: Is incorrect. The code will not break on a different symbol's position because the only way this block is entered is on a matching symbol&magic condition. That said, this should be refactored to use booleans instead of integers since it's checking for truth not quantity. 

Point 4.1: Agreed. Although you should never begin a nested statement on the same line as the parent statement. This is a "code-smell" that will burn you in the debugging process and will generally make other programmers cringe in horror if they were to see your src. If you go by most c++ style guidelines then your code would look like something like this. 

for(int i=OrdersHistoryTotal()-1; i>=0; --i) {
   if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)
      && OrderSymbol() == Symbol() 
      && OrderMagicNumber() == MAGIC_NUMBER
   ){
      ...
   }
}

Point 4.2: There's no reason to go redefining types. This is otherwise known as code obfuscation (contrived complexity) and the only one who will be confused is yourself when you go to re-read your code a month later... You'll have to bounce around the page referencing the actual type so just use the actual type from the start. Your variable names (which should start with a lowercase letter, BTW) should describe what the variable is not a macro definition. 

Point 5.1: Agree on all points except recursion is generally a better pattern instead of resetting the index var because of the potential to get stuck in an infinite loop on failing orders. 

bool close_all() 
{
   RefreshRates();
   for(int i=0; i<OrdersTotal(); i++) {
      if(OrderSelect(i, SELECT_BY_POS)
         && OrderSymbol() == Symbol() 
         && OrderMagicNumber() == MAGIC_NUMBER
      ){
         if(OrderClose(...))
            return close_all();
         return false;
      }
   }
   return true;
}
northedan
175
northedan  
I'm just follow the book Expert-Advisor-Programming-Creating-Automated-Trading-System-in-MQL-for-Metatrader-4. I'm learning to code. I'll test all recommedation. Thanks for help.
Mike Tanton
533
Mike Tanton  

Jeez Northeden (Norbert).... do you even comprehend what you have asked....?

Try this.... "Focus"..... Ask one question.... focus.... 

Maybe take the "Break" out...

Also set the "Variables" to 0...

...take the "Else If"... out... make it a straight "if".....

...."Else If's" are not good.... in my opinion

To add comments, please log in or register