Calculating weekly profits in pips (Solved)

800

Good afternoon everyone,

I am trying to calculate the net profit/net loss of an expert advisor for the current week. For some reason, this function always returns 0 during backtesting.

```double GetWeekProfits()
{
int type, netprofit = 0;
datetime starttime = iTime(Symbol(), PERIOD_W1, 0);
for(int i = 0; i < OrdersHistoryTotal(); i++)
{
if(!OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
{
type = OrderType();
if(OrderOpenTime() >= starttime && OrderMagicNumber() == MagicNumber)
{
if(type == OP_BUY)
{
netprofit += (OrderClosePrice() - OrderOpenPrice()) / DecimalPip;
} else if(type == OP_SELL) {
netprofit += (OrderOpenPrice() - OrderClosePrice()) / DecimalPip;
}
}
}
}
return(netprofit);
}```
I'll be most grateful if anyone could spot where the error is :-) Thanks in advance!
17205

If the OrderSelect() fails you then try to use OrderType() . . .  how exactly does that make logical sense ?  if the OrderSelect()  works you do noting . . .  just return netprofit which you haven't changed from it's initial value of 0.0.
800

RaptorUK:
If the OrderSelect() fails you then try to use OrderType() . . .  how exactly does that make logical sense ?  if the OrderSelect()  works you do noting . . .  just return netprofit which you haven't changed from it's initial value of 0.0.

OMG you are right! Stupid mistake =P Thanks a lot! This is the final function if someone is interested:

```double GetWeekProfits()
{
int type, netprofit = 0;
datetime starttime = iTime(Symbol(), PERIOD_W1, 0);
for(int i = 0; i < OrdersHistoryTotal(); i++)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
{
type = OrderType();
if(OrderOpenTime() >= starttime && OrderMagicNumber() == MagicNumber)
{
if(type == OP_BUY)
{
netprofit += (OrderClosePrice() - OrderOpenPrice()) / DecimalPip;
} else if(type == OP_SELL) {
netprofit += (OrderOpenPrice() - OrderClosePrice()) / DecimalPip;
}
}
}
}
return(netprofit);
}```

Thanks a lot raptor!

6626

See also the three indicators here https://www.mql5.com/en/code/10387  calculating profits of EA's
112

Point Zero:

OMG you are right! Stupid mistake =P Thanks a lot! This is the final function if someone is interested:

Thanks a lot raptor!

Would you be so kind to help me out? I want to display in COMMENT(); the following:

Trade Statistics

------------------------

Today's Trading       :   <TT_number_of_trades_made> / <TT_profit_or_loss> - <TT_gain_in_percentage> %

This Month's Trade   :  <TM_number_of_trades_made> / <TM_profit_or_loss> - <TM_gain_in_percentage> %

Last Month's Trade   :  <LM_number_of_trades_made> / <LM_profit_or_loss> - <LM_gain_in_percentage> %

------------------------

Any ideas on how to borrow your code above to achieve it? Thanks!

981

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
// 33058 ALL HISTORY // 33054 GRAFİĞİ KAYDET
// 33058 - All History, 33057 - Last 3 Months, 33063 - Last Month
//#define MT4_WMCMD_ALL_HISTORY 33058
//#define MT4_WMCMD_3_MONTHS 33057
//#define MT4_WMCMD_LAST_MONTH 33063
#include <WinUser32.mqh>
#import "user32.dll"
int    GetAncestor(int, int);
#import
#define GA_ROOT 2
// https://forum.mql4.com/ru/14463/page5#401551
#define MT4_WMCMD_ALL_HISTORY 33058
void OnStart()
{
//---

int      main = GetAncestor(WindowHandle(Symbol(), Period()), GA_ROOT);
PostMessageA(main, WM_COMMAND, MT4_WMCMD_ALL_HISTORY, 0);

}
research по теме кодов к PostMessageA
• 2010.10.22
• www.mql5.com
Уважаемые все, кому это собственно интересно. Провел тут небольшое исследование на тему кодов к функции PostMessageA, результат выкладываю ниже...
To add comments, please log in or register