I need help with the consecutive wins count MOL4

 

Hi

Using Mql4 I am trying to use the consecutive wins count to increase the lots multiplier after the winning trades starting from 1 until it reaches to the maximum lots multiplier and to reset the lots multiplier to 1 after any losing trade to start the cycle again with the consecutive win trades. 

I created this code for the lots multiplier  

  

extern bool      RTrade                = true;  

extern int       MaxLotMultiplier      = 6;

extern double    distanceMultiplier    = 1;


double LotsMultiplier()

{

   for(int Count = OrdersHistoryTotal()-1; ; Count--)

   {

      if(OrderSelect(Count,SELECT_BY_POS,MODE_HISTORY)==true)

      {if(OrderSymbol() == Symbol() && OrderMagicNumber() == magicNumber)

      {

      if(OrderProfit() > 0 ) WinCount++;

      else if(OrderProfit() < 0 ) 

      {WinCount = 0; break;}

      else break;

      }

     } 

    return WinCount; 

   }

  Count--; 

   if(RTrade == true && noHistory==false && OrderProfit() > 0) LotsMultiplier_ = WinCount;

   else if(RTrade == true && noHistory==false &&OrderProfit() < 0) LotsMultiplier_ = 1;

   if(RTrade == false || noHistory==true )  LotsMultiplier_ = 1;

    if(LotsMultiplier_ >= MaxLotMultiplier) LotsMultiplier_ = MaxLotMultiplier;

 return (LotsMultiplier_);

}  



 But when I tested it, it gives very strange results as per the following Table

No  Type Order size  Price S/L T/P Profit Balance actual Lot Multiplier required Lot Multiplier Status
1 t/p 1 0.1 1.08873 1.06873 1.08873 50 10050 1 1 Win
2 s/l 2 0.1 1.09421 1.09421 1.11421 -149.74 9900.26 1 2 Loss
3 t/p 3 0.1 1.10948 1.07948 1.10948 150.43 10050.69 1 1 Win
4 t/p 4 0.2 1.11475 1.09475 1.11475 100 10150.69 2 2 Win
5 t/p 5 0.6 1.12004 1.10004 1.12004 300 10450.69 6 3 Win
6 t/p 6 2.4 1.12532 1.10532 1.12532 1205.54 11656.24 24 4 Win
7 t/p 7 0.1 1.12963 1.14963 1.12963 49.38 11705.61 1 5 Win
8 t/p 8 0.6 1.10582 1.12582 1.10582 300 12005.61 6 6 Win Maximum Lot Multiplier
9 t/p 9 4.2 1.10049 1.12049 1.10049 2065.06 14070.67 42 6 Win
10 t/p 10 0.1 1.09969 1.11969 1.09969 48.96 14119.63 1 6 Win
11 t/p 11 0.9 1.12834 1.14834 1.12834 449.06 14568.69 9 6 Win
12 t/p 12 0.1 1.12046 1.10046 1.12046 50.1 14618.79 1 6 Win
13 t/p 13 1.1 1.09364 1.07364 1.09364 550.73 15169.52 11 6 Win
14 t/p 14 0.1 1.1052 1.0852 1.1052 50 15219.52 1 6 Win
15 t/p 15 1.3 1.11047 1.09047 1.11047 650 15869.52 13 6 Win
16 t/p 16 0.1 1.11575 1.09575 1.11575 50 15919.52 1 6 Win
17 t/p 17 1.5 1.12104 1.10104 1.12104 750 16669.52 15 6 Win
18 s/l 18 0.1 1.05907 1.05907 1.07907 -149.87 16519.65 1 6 Loss
19 s/l 19 0.1 1.04163 1.04163 1.07163 -149.84 16369.82 1 1 Loss
20 t/p 20 0.1 1.06379 1.09379 1.06379 148.86 16518.67 1 1 Win
21 t/p 21 1.7 1.05866 1.07866 1.05866 846.46 17365.14 17 2 Win
22 t/p 22 0.1 1.05995 1.03995 1.05995 50.13 17415.27 1 3 Win
23 t/p 23 1.9 1.06262 1.04262 1.06262 950.63 18365.89 19 4 Win
24 t/p 24 0.1 1.06135 1.04135 1.06135 50 18415.89 1 5 Win
25 t/p 25 2.1 1.06662 1.04662 1.06662 1050.69 19466.59 21 6 Win Maximum Lot Multiplier



Can any one help Me?

 
  1. Please use
SRC
    Play video
    Please edit your post.
    For large amounts of code, attach it.
    You pasted quoted text inside a SRC bar. That is why you lost the formatting.
  2. Don't add text inside quoted text or put text inside a SRC block, put it outside. MQL4 Forum editor problem - MQL4 forum
  3. But when I tested it, it gives very strange results

    Where in your function do you set your count to zero before counting?
  4. You are counting down (assuming history is ordered,) so the moment you find a loss, the count of consecutive wins after that loss, is what ever the count currently is. What you are returning is the first consecutive win count, not the latest.
  5. You assume history is ordered by date, it's not. Could EA Really Live By Order_History Alone? (ubzen) - MQL4 forum Your going to have to sort history to do this properly.
 

Sorry, I couldn't figure out how to do it? I hope if anyone could tell me how to correct my code

 
KhellaH:

Sorry, I couldn't figure out how to do it? I hope if anyone could tell me how to correct my code


There's not really a way to "correct" your code. Like whroeder said, you have to sort your history-orders (LIFO - Descending by Close time) programmatically before you can even begin to start counting. That's not easy in MQL if you don't know OOP concepts and the standard library. If you do, however, then it's relatively easy. First you make a class for history orders which extends CObject and then override the compare method for sorting. Next you'd want to derive a class from CArrayObj or CList to hold and sort the history deals. This is an example of how I'd do it ... 

//+------------------------------------------------------------------+
//|                                             Consecutive_wins.mq4 |
//|                                                      nicholishen |
//|                                   www.reddit.com/u/nicholishenFX |
//+------------------------------------------------------------------+
#property copyright "nicholishen"
#property link      "www.reddit.com/u/nicholishenFX"
#property version   "1.00"
#property strict
#property script_show_inputs

input int Magic = 0;

#include <Arrays\ArrayObj.mqh>
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class Deal : public CObject
{
public:
   int      ticket;
   datetime time;
   double   profit;
   // override CObject::Compare for sorting
   int Compare(const CObject *node,const int mode=0)const override
   {
      Deal *other = (Deal*)node;
      if(this.time > other.time)
         return -1;
      if(this.time < other.time)
         return 1;
      return 0;
   }
};
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class CWins : public CArrayObj
{
protected:
   int         m_magic;
public:
   CWins():m_magic(0){}
   CWins(int magic):m_magic(magic){}
   Deal* operator[](const int index)const{return (Deal*)At(index);}
   int ConsecutiveWins()
   {
      GetHistory();
      int cnt =0;
      for(int i=0;i<Total();i++)
      {
         if(this[i].profit >=0)
            cnt++;
         else
            return cnt;
      }
      return cnt;
   }
   void SortedHistoryToLog()
   {
      GetHistory();
      for(int i=0;i<Total() && i < 20;i++) //max 20 deals to log
         Print(this[i].time," - ",this[i].ticket," - $",this[i].profit);
   }
protected:
   void GetHistory()
   {
      Clear();
      for(int i=0;i<OrdersHistoryTotal();i++)
      {
         if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)       && 
           (m_magic == 0 || OrderMagicNumber() == m_magic)  &&
           (OrderType()==OP_BUY || OrderType() == OP_SELL))
         {
            Deal *deal    = new Deal;
            deal.ticket   = OrderTicket();
            deal.time     = OrderCloseTime();
            deal.profit   = OrderProfit();
            CArrayObj::Add(deal);
         }
      }
      Sort();
   }
};
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
//---
   CWins cwins(Magic);
   Print("Consecutive wins = ",cwins.ConsecutiveWins());
   cwins.SortedHistoryToLog();
}
//+------------------------------------------------------------------+