closed orders in history

 

I would like to know if an order has just hit SL and is in ordershistory but I dont want to count the same order over and over I want to only count the new orders that were just closed can I do it like this?




for(int i = OrdersHistoryTotal()-1; i >=0; i--)

{

OrderSelect(i, SEL_BY_POS, MODE_HISTORY);

{

if(OrderProfit()<0)

{

Alert("Trade Closed in loss!");

}


is there any major faults with this code?

Thank you for the guidance to whom helps me.

 

Replace

OrderSelect(i, SEL_BY_POS, MODE_HISTORY);

to

OrderSelect(i, SELECT_BY_POS, MODE_HISTORY);

 
Roger:

Replace

OrderSelect(i, SEL_BY_POS, MODE_HISTORY);

to

OrderSelect(i, SELECT_BY_POS, MODE_HISTORY);

Thanx Roger! will do

 
23510:

Thanx Roger! will do

The code, with Roger's amendment, will work fine in terms of finding orders which were closed at a loss. 


However, if this is in an EA, the code will "count the same order over and over", which you apparently don't want to do. Every time it runs it's scanning the entire order history. It will alert about the same trades over and over again.


An alternative is to use a static variable to hold the last order number which was checked. On the first call to start(), the code checks the entire order history, from zero to whatever OrdersHistoryTotal() is. On subsequent calls to start(), the code only looks at orders which have been closed since the last call to start():


int start()
{
   static int LastHistoryCount = 0;

   // Scan the order list. Doesn't matter whether this is done from zero upwards,
   // or from OrdersHistoryTotal() downwards. 
   for(int i = LastHistoryCount ; i < OrdersHistoryTotal(); i++)
   {
      OrderSelect(i, SELECT_BY_POS, MODE_HISTORY);
      if(OrderProfit() < 0)
      {
         Alert("Trade Closed in loss!"); 
      }
   }

   LastHistoryCount = OrdersHistoryTotal();
}



However, the above code also has a problem. The "LastHistoryCount" is a variable held in memory for the EA, and is lost when the EA is shut down or reloaded. Therefore, the next time you start the EA it will start checking orders again from position zero. The only way round this is to hold the last-order in some type of external storage, such as a global variable:


int start()
{
   // Get the last order handled by the EA the last time it was used. The value
   // defaults to zero if the global variable does not yet exist.   
   string strGlobalVariableName = WindowExpertName() + "_" + Symbol() + "_LastOrderCount";
   int LastHistoryCount = GlobalVariableGet(strGlobalVariableName);

   // Scan the order list. Doesn't matter whether this is done from zero upwards,
   // or from OrdersHistoryTotal() downwards. 
   for(int i = LastHistoryCount ; i < OrdersHistoryTotal(); i++)
   {
      OrderSelect(i, SELECT_BY_POS, MODE_HISTORY);
      if(OrderProfit() < 0)
      {
         Alert("Trade Closed in loss!"); 
      }
   }

   // Store the last-order count in a global variable which persists across multiple uses of the EA
   GlobalVariableSet(strGlobalVariableName, OrdersHistoryTotal());
}


N.B. The code is deliberately designed so that it adapts the name of its global variable depending on the name of the EA and the symbol on which it's running. Therefore, you can use the same code in multiple EAs, or in the same EA running on multiple charts, without needing to change the code.

 
jjc:

The code, with Roger's amendment, will work fine in terms of finding orders which were closed at a loss.


However, if this is in an EA, the code will "count the same order over and over", which you apparently don't want to do. Every time it runs it's scanning the entire order history. It will alert about the same trades over and over again.


An alternative is to use a static variable to hold the last order number which was checked. On the first call to start(), the code checks the entire order history, from zero to whatever OrdersHistoryTotal() is. On subsequent calls to start(), the code only looks at orders which have been closed since the last call to start():


int start()
{
static int LastHistoryCount = 0;

// Scan the order list. Doesn't matter whether this is done from zero upwards,
// or from OrdersHistoryTotal() downwards.
for(int i = LastHistoryCount ; i < OrdersHistoryTotal(); i++)
{
OrderSelect(i, SELECT_BY_POS, MODE_HISTORY);
if(OrderProfit() < 0)
{
Alert("Trade Closed in loss!");
}
}

LastHistoryCount = OrdersHistoryTotal();
}



However, the above code also has a problem. The "LastHistoryCount" is a variable held in memory for the EA, and is lost when the EA is shut down or reloaded. Therefore, the next time you start the EA it will start checking orders again from position zero. The only way round this is to hold the last-order in some type of external storage, such as a global variable:


int start()
{
// Get the last order handled by the EA the last time it was used. The value
// defaults to zero if the global variable does not yet exist.
string strGlobalVariableName = WindowExpertName() + "_" + Symbol() + "_LastOrderCount";
int LastHistoryCount = GlobalVariableGet(strGlobalVariableName);

// Scan the order list. Doesn't matter whether this is done from zero upwards,
// or from OrdersHistoryTotal() downwards.
for(int i = LastHistoryCount ; i < OrdersHistoryTotal(); i++)
{
OrderSelect(i, SELECT_BY_POS, MODE_HISTORY);
if(OrderProfit() < 0)
{
Alert("Trade Closed in loss!");
}
}

// Store the last-order count in a global variable which persists across multiple uses of the EA
GlobalVariableSet(strGlobalVariableName, OrdersHistoryTotal());
}


N.B. The code is deliberately designed so that it adapts the name of its global variable depending on the name of the EA and the symbol on which it's running. Therefore, you can use the same code in multiple EAs, or in the same EA running on multiple charts, without needing to change the code.

That was a fantastic summary and excellent help! thatnk you so much! It made perfect sense and helped a LOT! i really appreciate it!

 

Fantastic description!

If you are running an EA on identical pair with several accounts, the following should be useful as the global variable is now unique for each EA, Account and Symbol.

string strGlobalVariableName = WindowExpertName() + "_" + AccountNumber() + "_" + Symbol() + "_LastOrderCount";

Reason: