Welcome Forex EA downloads & MT4/MT5 auto-trading resources — EAs, Gold EAs, quant tools and real-world automation.
Sign In Sign Up

Code Block for "Trailing Stop" based on current market price. (Ask / Bid) - ex

author EAcpu | 4 reads | 0 comments |

Code Block for "Trailing Stop" based on current market price. (Ask / Bid) - expert for MetaTrader 5

This code block works regardless of whether you use a stop loss or not.

Require

 #include <Trade\Trade.mqh> // <<--------------------------------------------------------------------- Include this "Trade.mqh" to access the CTrade class
Enter double tracking step size = 3.0 ;
  • You need to define an instance tradenet class. The name can be anything. It's better to have an OnInt event handler after the meal.

  • Then you need to create an if statement to check if a position is currently running. This statement calls the Check_TrailingStop(); function for each tick . This is important because the EA should track it sharp and smooth . Remember to put this declaration at the top of the OnTick event handler for it to work properly.
 //+------------------------------------------------------------------+
//|Expert initialization function |
//+------------------------------------------------------------------+
Integer initialization ()
  { //--- Create a timer event to set the timer ( 60 ); //--- Return ( initialization successful );
  }

CTrade Trade; // <<------------------------------------------------------------- Declare the "CTrade" class. You can replace "trade" with any name you want
Blank check ()
  {
  
if ( Total Positions () > 0 ) // If there are positions running, the trailing stop function is called for each price tick.    {
      Check_TralingStop(); 
    } 
  
  
          }
 BlankCheck_TralingStop ()
  {
Integer total positions = total positions ();
for ( integer count = 0 ; count < total positions; count++)
    {
Oolong ticket number = position to get tickets (count); // Use the "index" of the position to get the position order number. if ( position selected by ticket (ticket number)) // Use the ticket number to select the position (we have already selected the ticket number) {
if ( Position GetInteger ( PositionType ) == POSITION_TYPE_BUY ) // Check the position type.          {
Double the opening price = double the position ( POSITION_PRICE_OPEN );
Double the stop loss = double the position ( Position_SL ); // <<--------------------Get the current stop loss of the position and double the profit = double the position ( POITION_TP );
double bid price = symbolinfodouble ( _symbol , SYMBOL_BID );
Oolong ticket = location acquisition ticket (count);
Double's TrailingLevel = normalized double (bid - (Traling_Step * view ()), _number );

If (stop loss < opening price) // no stop loss is correct.              {
if (bid > open price && trailing level > open price) // Runs only once per position. Sets the first SL. trade.PositionModify(ticket, trailing level, take profit); // Modify trailing stop using "CTrade.trade" }

if (bidPrice > openPrice && trailingLevel > stopLoss) // check trailing level is above the previos level. {
              trade.PositionModify(ticket, trailing level, take profit); // Use "CTrade.trade" to modify trailing stop loss }

          }
if ( position getinteger ( positionType ) == POSITION_TYPE_SELL )
          {
Double the opening price = double the position ( POSITION_PRICE_OPEN );
Double stop loss = position gained double ( position_SL );
Double profit = double position ( POSITION_TP );
double bid price = symbolinfodouble ( _symbol , SYMBOL_BID );
double askPrice = symbolinfodouble ( _symbol , SYMBOL_ASK );
Oolong ticket = location acquisition ticket (count);
Double's TrailingLevel = Normalized Double (Ask+(Traling_Step * View ()), _Number );

If (stop loss < opening price) // no stop loss is correct.              {
if (askPrice < openPrice && TrailingLevel < openPrice) // Only run once per location. Set up the first SL.                  trade.PositionModify(ticket, trailing level, take profit); // Use "CTrade.trade" to modify trailing stop loss }

if (askPrice < openPrice && TrailingLevel < stopLoss) // Check if the trailing level is higher than the previous level.              {
              trade.PositionModify(ticket, trailing level, take profit); // Use "CTrade.trade" to modifyChange trailing stop} } } } }



Attachment download

📎 traling_stop_vwhen_sl_used_and_noth.mq5 (5.87 KB)

Source: MQL5 #49021

Code Block for "Trailing Stop" based on current market price. (Ask / Bid) - ex Usage and filtering suggestions

Code Block for "Trailing Stop" based on current market price. (Ask / Bid) - ex is a resource related to foreign exchange trading tools. Before downloading or referencing, it is recommended to confirm the platform version, transaction type, time period, whether DLL is required, whether source code is included, parameter description and historical update records.

Both automated trading tools and foreign exchange indicators should first undergo backtesting, simulated account observation, and small capital verification. Pay special attention to spreads, slippages, maximum drawdowns, position opening frequency, stop loss methods, trading periods and broker rules, and avoid direct real-time execution based on titles or short-term profit descriptions.

Code Trailing Stop MQL5 CodeBase Block
Verification code Refresh