In MQL4
, I know how to set stopLoss
and takeProfit
.
However, I would like to do something else when such events actually take place.
Is there any event listener associated with such?
In MQL4
, I know how to set stopLoss
and takeProfit
.
However, I would like to do something else when such events actually take place.
Is there any event listener associated with such?
Unfortunately, there are no trade-events in MQL4
.
However, it can be simulated as such ( logic-only-code, may not compile ):
#property copyright "No copyright, can be used freely, Joseph Lee"
#property link "https://www.facebook.com/joseph.fhlee"
int vaiTicketList[];
int start() {
int viIndex;
// -----------------------------------------------------------
// EVENT CHECK SECTION:
// Check vaiTicketList (populated in the previous cycle) to see if
// each of the (previously) open ticket is still currently open.
// -----------------------------------------------------------
for( viIndex=0; viIndex<ArrayRange(vaiTicketList,0); viIndex++) {
// Check if Ticket which was previously opened in the last
// cycle is no longer open now.
if(!OrderSelect( vaiTicketList[viIndex], SELECT_BY_TICKET ) ) {
// -----------------------------------
// EVENT CATEGORIZATION:
// -----------------------------------
// Handle possible events here:
// -- Close event: (OrderSelect( ticket, SELECT_BY_TICKET, MODE_HISTORY) == true)
if( OrderSelect(vaiTicketList[viIndex], SELECT_BY_TICKET, MODE_HISTORY) )
eventTrade_Closed( vaiTicketList[viIndex] );
// -- StopLoss ( Buy: When OrderClosePrice() <= OrderStopLoss(),
// Sell: When OrderClosePrice() >= OrderStopLoss() )
// -- TakeProfit (Buy: When OrderClosePrice() >= OrderTakeProfit(),
// Sell: When OrderClosePrice() <= OrderTakeProfit() )
// -- Expiration, Cancel, etc, etc
}
}
// -----------------------------------------------------------
// Store a list of all currently OPEN trade tickets into array.
// This is used to be compared in the next tick.
// -----------------------------------------------------------
ArrayResize( vaiTicketList, OrdersTotal() );
for ( viIndex=0; viIndex<OrdersTotal(); viIndex++) {
if(OrderSelect(viIndex, SELECT_BY_POS, MODE_TRADES)) {
vaiTicketList[viIndex] = OrderTicket();
}
}
// -----------------------------------------------------------
};
// ---------------------------------------
// This is the Trade Close event handler
// ---------------------------------------
bool eventTrade_Closed( int pviTicket ) {
bool vbIsEventBubble = true;
// Do something here to handle the event.
// FEATURE: vbIsEventBubble TRUE will allow event bubbles.
return( vbIsEventBubble);
}
bool eventTrade_otherPossibleEvents1() {};
bool eventTrade_otherPossibleEvents2() {};
bool eventTrade_otherPossibleEvents3() {};
bool eventTrade_otherPossibleEventsN() {};
Something along this line. Hope it helps.
you can use OrdersHistoryTotal() with a static variable to recognize this event. if this value is increased means that a position has closed.
we may create one such and test it's activation on an OnTick()
event-bound handler basis.
void OnTick(){ // MQL4 system-initiated event-handler
// ---
myOnTickStealthTP_EventMONITOR(); // my Event Monitor
myOnTickStealthSL_EventMONITOR(); // my Event Monitor
// ---
// other code
}
Extending, upon not2qubit's conjecture ( irrespective how on-topic, weak or wrong one might consider that ):
You just posted an artificial non-existing function. What good is that? It would have been far more helpful if you could have provided as partially working code snippet for what you suggest. Recalling that most users of MQL4 are not programmers. – not2qubit 47 mins ago
void myOnTickStealthTP_EventMONITOR(){ // HERE put everything,
// TP_Event // what the "something else"
// ( when such events
// actually take place
// )
// meant - that's fair, isn't it ?
...
}
void myOnTickStealthSL_EventMONITOR(){ // HERE put everything,
// SL_Event // what the "something else"
// ( when such events
// actually take place
// )
// meant - that's fair, isn't it ?
...
}