Want to close trades on achieving weekly profit of $ 50

 
double AccountWeekProfit()

{
   int netprofit = 0;
   datetime starttime = iTime(Symbol(), PERIOD_W1, 0);
   for(int i = 0; i < OrdersHistoryTotal(); i++)
   {
      if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
      {
         
         if(OrderCloseTime() >= starttime )
         {
               netprofit += (OrderProfit() - OrderCommission());
              
         }
      }
   }
   return(netprofit);

}


This function returns $ 4.00 where as my last week profit is (-502.06 Loss ), 

Could not figure out what is going wrong. Guys kindly help

 

Forum on trading, automated trading systems and testing trading strategies


When you post code please use the CODE button (Alt-S)!

Use the CODE button

 

When you use OrderHistoryTotal() remeber that the history is loaded from the MT4 platfrom, so if you are looking for last week history but in MT4 history you have set "Last day", in OrdersHistoryTotal() there will be only trades closed on last day.

Do it, to make su to load all history.

PS. You are not couting OrderSwap() in your function. And remember that, due to the fact that OrderSwap() and OrderCommision() are negative value you need to add them, not subtract them.

 
Fabio Cavalloni:

When you use OrderHistoryTotal() remeber that the history is loaded from the MT4 platfrom, so if you are looking for last week history but in MT4 history you have set "Last day", in OrdersHistoryTotal() there will be only trades closed on last day.

Do it, to make su to load all history.


I did that, even then the weekly result from function is $ -92

Where as the real from history is $ 574.51

What I am doing wrong?


 

You are not couting OrderSwap() in your function. Remember that, due to the fact that OrderSwap() and OrderCommision() are negative value you need to add them, not subtract them.

And remeber to exclude OrdeType()>1 (pending closed, deposits and withdrawals)

 
Fabio Cavalloni:

You are not couting OrderSwap() in your function. Remember that, due to the fact that OrderSwap() and OrderCommision() are negative value you need to add them, not subtract them.

And remeber to exclude OrdeType()>1 (pending closed, deposits and withdrawals)

double AccountWeekProfit()


{

   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(type == OP_BUY || type == OP_SELL)

         if(OrderCloseTime() >= starttime )

         {

               netprofit += (OrderProfit() +OrderSwap()+ OrderCommission());

         }

      }

   }

   return(netprofit);

}


After this also the  week profit is -78

where as last week profit is -565.20


dont know where is the mistake

 
Vijay Akash T P:

double AccountWeekProfit()


{

   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(type == OP_BUY || type == OP_SELL)

         if(OrderCloseTime() >= starttime )

         {

               netprofit += (OrderProfit() +OrderSwap()+ OrderCommission());

         }

      }

   }

   return(netprofit);

}


After this also the  week profit is -78

where as last week profit is -565.20


dont know where is the mistake

Firstly, the mistake is that you refuse to follow moderators suggestion, like posting the code in the right way.

You are using an INT variable to store a double value.

{
   double netprofit = 0;
   datetime starttime = iTime(Symbol(), PERIOD_W1, 0);

   for(int i = 0; i < OrdersHistoryTotal(); i++) {
      if( !OrderSelect(i, SELECT_BY_POS, MODE_HISTORY) || OrderType()>1 || OrderCloseTime()<starttime) continue;
      netprofit += OrderProfit() +OrderSwap()+ OrderCommission();
   }
   return(netprofit);
}
 
Fabio Cavalloni:

Firstly, the mistake is that you refuse to follow moderators suggestion, like posting the code in the right way.

You are using an INT variable to store a double value.

Perfect. Good Work. Thanks

 
Vijay Akash T P:

Perfect. Good Work. Thanks

<Deleted>

 
Vijay Akash T P:

<Deleted>

How many times have you been told to use the code button when pasting code?


When you post code please use the CODE button (Alt-S)!

Use the CODE button

 
Keith Watford:

How many times have you been told to use the code button when pasting code?


When you post code please use the CODE button (Alt-S)!

Ok. I will follow
Reason: