Download MetaTrader 5
To add comments, please log in or register
Added support for the Spanish language. Come and check it out!
huatboyz 2015.03.09 07:57 

Hi All MQL experts,

I have been trying to code an Martingale EA based on past history P&L, however, my programming skills is just not up to mark, I am lacking an auto computation algorithm for the past few days, on a daily basis. It's difficult to segregate/differentiate orders and then calculate p&l based on each trading day, because I could have 3 trade losses on one day and 5 trade losses on another day, (especially month end differentiation would be another problem). 

Basically the idea is this, if the past trading day is a loss, I would need to double up on my lots on the next trading day, if the previous 2 days was losses, means I would need to double up twice, so on and so forth. If the third day is a win, we would revert back to original lots.

How to calculate P&L for each trading day? Because I attached my EA for 12 pairs, I could have maximum 12 trades a day, I would need to calculate P&L for each trading day, for e.g. my total losses (1 loss on AUDUSD and 1 loss on NZDUSD) would be both added up together for ONE trading day.

I have managed to program for last 5 trades (based on trade open date) for previous trading day and if losses occur, we will double up, if profit, we will stick to the original lots.


Would really appreciate if anyone could help me out. Please and God bless you.

I know using "FOR" loops would be useful, however, I tried, and it doesn't seem to call out the correct values. I am ignoring open trades though, so don't have to calculate p&l for open trades, just closed out trades.

My code are as below:-

double lotsize;
double lots = 0.1;

int total1; int cnt1; int tradedate [5];
double profit[5];

total1 = OrdersHistoryTotal ();

OrderSelect(total1-1, SELECT_BY_POS, MODE_HISTORY);

{tradedate [0] = TimeDay(OrderOpenTime());
profit [0] = OrderProfit ();}

OrderSelect(total1-2, SELECT_BY_POS, MODE_HISTORY);

{tradedate [1] = TimeDay(OrderOpenTime());
profit [1] = OrderProfit ();}

OrderSelect(total1-3, SELECT_BY_POS, MODE_HISTORY);

{tradedate [2] = TimeDay(OrderOpenTime());
profit [2] = OrderProfit ();}

OrderSelect(total1-4, SELECT_BY_POS, MODE_HISTORY);

{tradedate [3] = TimeDay(OrderOpenTime());
profit [3] = OrderProfit ();}

OrderSelect(total1-5, SELECT_BY_POS, MODE_HISTORY);

{tradedate [4] = TimeDay(OrderOpenTime());
profit [4] = OrderProfit ();}

/* Check PNL for prev day */
double pnl;
if (tradedate[0] == tradedate[1])
pnl = profit[0] + profit[1];

if (tradedate[0] == tradedate[2])
pnl = pnl + profit[2];

if (tradedate[0] == tradedate[3])
pnl = pnl + profit[3];

if (tradedate[0] == tradedate[4])
pnl = pnl + profit[4];

if (pnl < 0);
loss = true;

if (loss = true) lots = lots *2;

Dirk Muehlich
Dirk Muehlich 2015.03.09 10:42  


if i understand it right, this could be a solution for PnL per day

double Sum_PnL;

for(int G=0; G<=OrdersHistoryTotal()-1; G++)
    if(OrderMagicNumber()== MagicNumber)
      if(OrderOpenTime() >= (TimeCurrent()-(Hour()*60*60+Minute()*60)))
      Sum_PnL = Sum_PnL + OrderProfit();
whroeder1 2015.03.09 13:15  
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. Martingale will blow your account. Why do you want to loose all your money? If you want to double your risk but are trading 12 pairs, shouldn't you increase each by 2/12?
  3. History list is not sorted. Make a sorted list Could EA Really Live By Order_History Alone? - MQL4 forum
  4. Then you can loop through the list and make your sums easily
    Just typed, Not compiled.
        int     tickets[],      nTickets = GetHistoryOrderByCloseTime(tickets);
        #define MAX_DAY 5
        double PnL[MAX_DAY]; int iDay=-1; datetime when=0;
        for(int iTicket = 0; iTicket < nTickets; iTicket++) if (
            OrderSelect(tickets[iTicket], SELECT_BY_TICKET) 
        &&  OrderType() <= OP_SELL // ignore deleted pending/transfers.
           datetime oct = OrderCloseTime();
           datetime day = DateOfDay(oct);
           if(when != day){ when = day; if(++iDay == MAX_DAY) break;
           double profit = OrderProfit() + OrderSwap() + OrderCommission();
           PnL[iDay] += profit;
    static int HR2400 = PERIOD_D1 * 60; // 86400 = 24 * 3600
    int      TimeOfDay(datetime when=0){
       return (when == 0 ? TimeCurrent() : when) % HR2400 );            }
    datetime DateOfDay(datetime when=0){
       return (when == 0 ? TimeCurrent() : when) - TimeOfDay(when) );   }
    datetime Tomorrow( datetime when=0){
       return DateOfDay(when == 0 ? TimeCurrent() : when) + HR2400);    }
    datetime Yesterday(datetime when=0){   DownloadHistory(PERIOD_D1);
       datetime today = DateOfDay(when == 0 ? TimeCurrent() : when);
       int      iD1   = iBarShift(NULL, PERIOD_D1,  today - 1);
       return iTime(NULL, PERIOD_D1, iD1); }
    void  DownloadHistory(ENUM_TIMEFRAMES aPeriod){
       ResetLastError(); (void) iOpen(_Symbol,aPeriod,0);
       if(_LastError != 0){
          if(_LastError != ERR_HISTORY_WILL_UPDATED){ Print(_LastError); return;
          Sleep(15000); RefreshRates();
    }  }
    Just typed, Not compiled.
  5. Check your return codes (OrderSelect) What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  6. Filter by magic number.
huatboyz 2015.03.10 08:11  
Thanks for all the replies ! Greatly appreciated. I will take a look and let you guys know. Cheers.
To add comments, please log in or register