Martingale EA calculation problems

175

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);

profit [0] = OrderProfit ();}

OrderSelect(total1-2, SELECT_BY_POS, MODE_HISTORY);

profit [1] = OrderProfit ();}

OrderSelect(total1-3, SELECT_BY_POS, MODE_HISTORY);

profit [2] = OrderProfit ();}

OrderSelect(total1-4, SELECT_BY_POS, MODE_HISTORY);

profit [3] = OrderProfit ();}

OrderSelect(total1-5, SELECT_BY_POS, MODE_HISTORY);

profit [4] = OrderProfit ();}

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

pnl = pnl + profit[2];

pnl = pnl + profit[3];

pnl = pnl + profit[4];

if (pnl < 0);
loss = true;

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

11

Hello,

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(OrderSelect(G,SELECT_BY_POS,MODE_HISTORY))
if(OrderMagicNumber()== MagicNumber)
if(OrderOpenTime() >= (TimeCurrent()-(Hour()*60*60+Minute()*60)))
Sum_PnL = Sum_PnL + OrderProfit();
}```
15331

1.  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 today = DateOfDay(when == 0 ? TimeCurrent() : when);
int      iD1   = iBarShift(NULL, PERIOD_D1,  today - 1);
return iTime(NULL, PERIOD_D1, iD1); }