3

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?

user3666197
  • 1
  • 6
  • 50
  • 92
user1819047
  • 667
  • 9
  • 18

3 Answers3

0

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.

user3666197
  • 1
  • 6
  • 50
  • 92
jlee88my
  • 2,935
  • 21
  • 28
  • `if(!OrderSelect()` will only execute upon failure, your code is unusable. It's quite likely you meant to write `if(!OrderSelect()) continue;`. In addition, nested `OrderSelect()`'s will ensure that all your orders are getting totally mixed up! – not2qubit Dec 04 '19 at 13:00
  • @not2qubit: Perhaps you will want to rethink the logic. It is correct that the if(!OrderSelect()) will only execute if the order is no longer available. This is intentional and is done as a "test" to see if the previously opened order is now closed. If it is closed, then do the inside section. So, the code is correct. – jlee88my Dec 08 '19 at 13:30
  • Aha, ok, I guess that's also a way to do it. It just a shame that in order to do it in the *correct* way in MQL4, often seem completely the reverse of other languages. Which is why detailed descriptions and explanations are essential for mq4 code. Either way, it seem that you're nesting the *`OrderSelect()'s`* in the *for()* loop, which is possible, but generally considered a big no-no, as the items are not returned in any order, and may even return garbage if pointer has died before operation is complete. – not2qubit Dec 08 '19 at 20:10
0

you can use OrdersHistoryTotal() with a static variable to recognize this event. if this value is increased means that a position has closed.

-1

No, there is no such direct event listener.


But:

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 ?
     ...
}
user3666197
  • 1
  • 6
  • 50
  • 92
  • 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 Dec 03 '19 at 18:50
  • **Negative**, @not2qubit.This Answer **does provide a solution to the O/P defined question** ( *"Is there any event listener associated with such?"* in context with *"I know how to set SL and TP"* & **an explicit will expressed as:** *"However, I would like to do something else when such events actually take place"* ). At the same time it also has a maximum achievable content, as far as one can help **in the O/P posted context** (where s/he did not define any detail, **what** the ***"something else** (when such events actually take place)"* actually is,which s/he does know, doesn't s/he? G/L! – user3666197 Dec 03 '19 at 19:37