Dear sirs,
I urge you to include these 2 functions (actually they are events) to the start()
, init() and deinit() functions:
int OnTakeProfit ()
returns the order ticket number which just closed by hitting the TakeProfit level.
int OnStopLoss ()
returns the order ticket number which just closed by hitting the StopLoss level.
We need those events too much to monitor the trades.
Currently when we need to monitor a trade we have one of 2 options:
1- Go thru all the History to find the last trade and do a lot of work to know if it was opened by our EA and
was it hit the TakeProfit or the StopLoss.
2- We don't set initial stoploss or takeprofit to the order we want to monitor and make the EA to close it at the level we want
but this is very dangerous because of in the case of we lost the connection we
can loss a lot of money.
Hope to hear the programmers' comments!
CG
OnStopLoss
OrdersHistoryTotal > PrevOrdersHistoryTotal && LastOrderClosePrice == LastOrderStopLoss
OnTakeProfit
OrdersHistoryTotal > PrevOrdersHistoryTotal && LastOrderClosePrice == LastOrderTakeProfit
//+------------------------------------------------------------------+ //| MyOrders.mq4 | //| Copyright © 2007, MetaQuotes Software Corp. | //| https://www.metaquotes.net/ru/ | //+------------------------------------------------------------------+ #property copyright "Copyright © 2007, MetaQuotes Software Corp." #property link "https://www.metaquotes.net/ru/" #property show_inputs extern int ExpertMagicNumber=123456; //+------------------------------------------------------------------+ //| Calculate all type orders by my MagicNumber | //+------------------------------------------------------------------+ int MyOrdersTotal(int & buyNumber, int & sellNumber,int & buyLimitNumber ,int & sellLimitNumber,int & buyStopNumber, int & sellStopNumber, int MagicNumber=0 ) { int res,orderType; //---- buyNumber=0; sellNumber=0; buyLimitNumber=0; sellLimitNumber=0; buyStopNumber=0; sellStopNumber=0; for (int i=OrdersTotal()-1;i>=0;i--) if (OrderSelect(i,SELECT_BY_POS)) { if (MagicNumber==0 || (OrderMagicNumber()==MagicNumber && MagicNumber!=0)) { switch(OrderType()) { case OP_BUY: buyNumber++;break; case OP_SELL: sellNumber++;break; case OP_BUYLIMIT: buyLimitNumber++;break; case OP_SELLLIMIT: sellLimitNumber++;break; case OP_BUYSTOP: buyStopNumber++;break; case OP_SELLSTOP: sellStopNumber++;break; } } } //---- res=buyNumber+sellNumber+buyLimitNumber+sellLimitNumber+buyStopNumber+sellStopNumber; return(res); } //+------------------------------------------------------------------+ //| script program start function | //+------------------------------------------------------------------+ int start() { //---- int buys,sells,buyLs,sellLs,buySts,sellSts; if (MyOrdersTotal(buys,sells,buyLs,sellLs,buySts,sellSts,ExpertMagicNumber)>0) Print("Long=",buys," Short=",sells," Buy limit=",buyLs," Sell Limit=",sellLs," Buy Stop=",buySts," Sell Stop=",sellSts); //---- return(0); } //+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| OrdersAlerts.mq4 |
//| Copyright © 2007, MetaQuotes Software Corp. |
//| https://www.metaquotes.net/ru/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, MetaQuotes Software Corp."
#property link "https://www.metaquotes.net/ru/"
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CheckSummChanged(int &ControlSumm)
{
bool res;
int currSumm;
//----
for(int i=0;i<OrdersTotal();i++)
{
if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) currSumm+=(OrderType()+1)*OrderTicket();
}
//----
if (currSumm!=ControlSumm)
{
ControlSumm=currSumm;
res=true;
}
return(res);
}
//+------------------------------------------------------------------+
//| script program start function |
//+------------------------------------------------------------------+
int start()
{
int prevcheckSumm;
//----
while (!IsStopped())
{
if (CheckSummChanged(prevcheckSumm)) Alert("Изменения в ордерах!");
Sleep(1000);
}
//----
return(0);
}
//+------------------------------------------------------------------+
Dear sirs,
I urge you to include these 2 functions (actually they are events) to the start() , init() and deinit() functions:
int OnTakeProfit ()
returns the order ticket number which just closed by hitting the TakeProfit level.
int OnStopLoss ()
returns the order ticket number which just closed by hitting the StopLoss level.
We need those events too much to monitor the trades.
Currently when we need to monitor a trade we have one of 2 options:
1- Go thru all the History to find the last trade and do a lot of work to know if it was opened by our EA and was it hit the TakeProfit or the StopLoss.
2- We don't set initial stoploss or takeprofit to the order we want to monitor and make the EA to close it at the level we want but this is very dangerous because of in the case of we lost the connection we can loss a lot of money.
Hope to hear the programmers' comments!
CG
But for now how about a structure below to solve your requests? A call to function OrderEvents() at the beginning of start() to generate the necessary events.
start() { OrderEvents(MAGIC); ... } void OrderEvents( int magic ) { static datetime lastCheck; for( int o = 0; o < OrdersHistoryTotal(); o++ ) { if( OrderSelect(o, SELECT_BY_POS, MODE_HISTORY) && OrderMagicNumber() == magic && && OrderCloseTime() > lastCheck ) if( OrderClosePrice() == OrderStopLoss() ) OnStopLoss(OrderTicket()); else if( OrderClosePrice() == OrderTakeProfit() ) OnTakeProfit(OrderTicket()); } lastCheck = TimeCurrent(); } void OnStopLoss( int ticket ) { ...<do something with this stoploss trade> } void OnTakeProfit( int ticket ) { ...<do something with this takeprofit trade> }
Dear sirs,
I urge you to include these 2 functions (actually they are events) to the start()
, init() and deinit() functions:
int OnTakeProfit ()
returns the order ticket number which just closed by hitting the TakeProfit level.
int OnStopLoss ()
returns the order ticket number which just closed by hitting the StopLoss level.
We need those events too much to monitor the trades.
Currently when we need to monitor a trade we have one of 2 options:
1- Go thru all the History to find the last trade and do a lot of work to know if it was opened by our EA and
was it hit the TakeProfit or the StopLoss.
2- We don't set initial stoploss or takeprofit to the order we want to monitor and make the EA to close it at the level we want
but this is very dangerous because of in the case of we lost the connection we
can loss a lot of money.
Hope to hear the programmers' comments!
CG
But for now how about a structure below to solve your requests? A call to function OrderEvents() at the beginning of start() to generate the necessary events.
start() { OrderEvents(MAGIC); ... } void OrderEvents( int magic ) { static datetime lastCheck; for( int o = 0; o < OrdersHistoryTotal(); o++ ) { if( OrderSelect(o, SELECT_BY_POS, MODE_HISTORY) && OrderMagicNumber() == magic && && OrderCloseTime() > lastCheck ) if( OrderClosePrice() == OrderStopLoss() ) OnStopLoss(OrderTicket()); else if( OrderClosePrice() == OrderTakeProfit() ) OnTakeProfit(OrderTicket()); } lastCheck = TimeCurrent(); } void OnStopLoss( int ticket ) { ...<do something with this stoploss trade> } void OnTakeProfit( int ticket ) { ...<do something with this takeprofit trade> }Thank you very much!
I wasn't asking for a problem solving code. However the code you generosity present is very proficient and tidy.
I just prefer the decrement loop:
for( int o = OrdersHistoryTotal() ; o >= OrdersHistoryTotal(); o-- )
Even with this great code one problem left:
What if the order we are searching for is not listed in the History Tab that's because the MetaTrader's user didn't select to show "All History" – Figure 1.
In general I'm not asking the MetaQutes' Team for a problem solution but I'm requesting some of new EVENTs to be added to the MQL4 language to make it Even Driven Language like the most of modern languages.
Your welcome CG.
Your decrement loop seems odd. The initial value and check value are the same?
If what you're looking is only event generation for recent sl/tp events, there is no need for full history, only recent ones should be sufficient (as recent as few ticks ago).
for( int o = OrdersHistoryTotal() ; o >= 0; o-- )

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Dear sirs,
I urge you to include these 2 functions (actually they are events) to the start() , init() and deinit() functions:
int OnTakeProfit ()
returns the order ticket number which just closed by hitting the TakeProfit level.
int OnStopLoss ()
returns the order ticket number which just closed by hitting the StopLoss level.
We need those events too much to monitor the trades.
Currently when we need to monitor a trade we have one of 2 options:
1- Go thru all the History to find the last trade and do a lot of work to know if it was opened by our EA and was it hit the TakeProfit or the StopLoss.
2- We don't set initial stoploss or takeprofit to the order we want to monitor and make the EA to close it at the level we want but this is very dangerous because of in the case of we lost the connection we can loss a lot of money.
Hope to hear the programmers' comments!
CG