Hi, experienced coders!
I've experienced a strange problem on arrays in MT4. My EA is dealing with multi-pairs. So I need to use arrays.
as the attached code shows, i declare some variables in array form before "init()". later, in a procedure to summarize number of trades, number of lots, and floating profit/loss, the calculation to put these values into these declared array variables won't work.
the 4 alerts() i put in the code all return 0 for the corresponding values (NO.of trades, lots, and P/L), while in fact they are supposed to reflect values like 2,3,0.6, or 120 (for example).
one example: this line:
Alert("<2>: this " + Pairs[i] + " has PL of " + DoubleToStr(PairNetPL[i],1) );
should give me:
"<2>: this USDJPY has PL of 110.5"
but actually gives me:
"<2>: this USDJPY has PL of 0"
the problem is that I am sure the "OrderProfit()" returns 110.5 too. seemingly, somehow, the values simply cannot be conveyed to the array even my code writes so.
anyone can shed a light for me here?
many thanks.
I've experienced a strange problem on arrays in MT4. My EA is dealing with multi-pairs. So I need to use arrays.
as the attached code shows, i declare some variables in array form before "init()". later, in a procedure to summarize number of trades, number of lots, and floating profit/loss, the calculation to put these values into these declared array variables won't work.
the 4 alerts() i put in the code all return 0 for the corresponding values (NO.of trades, lots, and P/L), while in fact they are supposed to reflect values like 2,3,0.6, or 120 (for example).
one example: this line:
Alert("<2>: this " + Pairs[i] + " has PL of " + DoubleToStr(PairNetPL[i],1) );
should give me:
"<2>: this USDJPY has PL of 110.5"
but actually gives me:
"<2>: this USDJPY has PL of 0"
the problem is that I am sure the "OrderProfit()" returns 110.5 too. seemingly, somehow, the values simply cannot be conveyed to the array even my code writes so.
anyone can shed a light for me here?
many thanks.
Inserted Code
double PairBuys[],PairSells[],PairNetPos[],PairNetLot[],PairNetPL[]; // before the init() void Count_PL_Trades_Lots() { TotalTrades = 0; EAPL = 0.0; ArrayInitialize(PairBuys,0); ArrayInitialize(PairSells,0); ArrayInitialize(PairNetPos,0); ArrayInitialize(PairNetLot,0); ArrayInitialize(PairNetPL,0); for (int k = 0; k < OrdersTotal(); k++) { OrderSelect(k,SELECT_BY_POS,MODE_TRADES); if (OrderMagicNumber() == MagicNumber && OrderLots() == Lots) { for (int i = 0; i < ArraySize(Pairs); i++) { if (OrderSymbol() == Pairs[i]) { Alert("<1>: pair is " + Pairs[i] + " with type of " + OrderType() + ", and PL " + OrderProfit()); PairNetPL[i] = PairNetPL[i] + OrderProfit(); Alert("<2>: this " + Pairs[i] + " has PL of " + DoubleToStr(PairNetPL[i],1) ); EAPL = EAPL + PairNetPL[i]; if (OrderType() == OP_BUY) { PairBuys[i] = PairBuys[i] + 1; Alert("<3>: " + DoubleToStr(PairBuys[i],1)); } if (OrderType() == OP_SELL) PairSells[i] = PairSells[i] + 1; Alert(WindowExpertName() + Pairs[i] + " has " + DoubleToStr(PairBuys[i],0) + " buys; and " +DoubleToStr(PairSells[i],0) + " sells."); } } } } for (i = 0; i < ArraySize(Pairs); i++) { if (PairBuys[i] > PairSells[i]) PairNetPos[i] = PairBuys[i] - PairSells[i]; else PairNetPos[i] = -PairBuys[i] + PairSells[i]; PairNetLot[i] = PairNetPos[i] * Lots; TotalTrades = TotalTrades + PairNetPos[i]; } //Alert(WindowExpertName() +"- TotalTrades:" + DoubleToStr(TotalTrades,0)); return(0); }