Watch how to download trading robots for free
Find us on Twitter!
Join our fan page
Interesting script?
So post a link to it -
let others appraise it
You liked the script? Try it in the MetaTrader 5 terminal
Experts

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

Views:
1828
Rating:
(2)
Published:
2024.04.04 22:09
Need a robot or indicator based on this code? Order it on Freelance Go to Freelance

This code block works if you use either a Stop Loss or Not. 


Requirements 

  • You need to include "Trade.mqh " to get access to the CTrade class which allows you to work with positions and orders.

#include <Trade\Trade.mqh> // <<------------------------------------------ Include this "Trade.mqh" to access the CTrade Class 

  • You need to set an input parameter to adjust the trailing distance as you want. This is not necessary, but for convenient. 
input double Traling_Step = 3.0;
  • You need to define instance for CTrade class. the name can be any. It would be better to dine it after the OnInt event handler.

  • Then you need to create an if statement to check if there any position has been running at the moment. This statement calls for Check_TrailingStop(); function for every tick. This important because the EA should trail it sharp and smooth. Keep mind to put this statement at the top of the OnTick event handler to work properly.
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create timer
   EventSetTimer(60);

//---
   return(INIT_SUCCEEDED);
  }


CTrade trade; // <<------------------------------------------ Declare the "CTrade" calss. you can replace "trade" win any name you want
void OnTick()
  {
   
   if(PositionsTotal() > 0) // calls to trailing stop function for every tick if there is / are positions runing. 
     {
      Check_TralingStop(); 
     } 
     
       
  }

  • You need to declare a custom function called  Check_TrailingStop(); (in this case) to do the rest. You can use any name you want. 
  • The custom function loops through all the opened positions and trail them as your required distance.
void Check_TralingStop()
  {
   int totalPositions = PositionsTotal();
   for(int count =0; count < totalPositions; count++)
     {
      ulong TicketNo = PositionGetTicket(count); // Get Position Ticket number using the 'index' of the position.

      if(PositionSelectByTicket(TicketNo)) // Select a position using the ticket number (we already picked the tickt no.)
        {
         if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) // Check the position Type.
           {
            double openPrice = PositionGetDouble(POSITION_PRICE_OPEN);
            double stopLoss  = PositionGetDouble(POSITION_SL);       // <<-------------------Get Position Current Stop Loss
            double takeProfit = PositionGetDouble(POSITION_TP);
            double bidPrice  = SymbolInfoDouble(_Symbol,SYMBOL_BID);
            ulong ticket = PositionGetTicket(count);
            double trailingLevel = NormalizeDouble(bidPrice - (Traling_Step * Point()),_Digits);

            if(stopLoss < openPrice) // No stop loss is true.
              {
               if(bidPrice > openPrice && trailingLevel > openPrice) // Runs only once per position. Sets the first SL.

                  trade.PositionModify(ticket,trailingLevel,takeProfit); // Modify trailing Stop using "CTrade.trade"
              }


            if(bidPrice > openPrice && trailingLevel > stopLoss) // check trailing level is above the previos level.
              {
               trade.PositionModify(ticket,trailingLevel,takeProfit); // Modify trailing Stop using "CTrade.trade"
              }

           }
         if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
           {
            double openPrice = PositionGetDouble(POSITION_PRICE_OPEN);
            double stopLoss  = PositionGetDouble(POSITION_SL);
            double takeProfit = PositionGetDouble(POSITION_TP);
            double bidPrice  = SymbolInfoDouble(_Symbol,SYMBOL_BID);
            double askPrice  = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
            ulong ticket = PositionGetTicket(count);
            double trailingLevel = NormalizeDouble(askPrice + (Traling_Step * Point()),_Digits);

            if(stopLoss < openPrice) // No stop loss is true.
              {
               if(askPrice < openPrice && trailingLevel < openPrice) // Runs only once per position. Sets the first SL.

                  trade.PositionModify(ticket,trailingLevel,takeProfit); // Modify trailing Stop using "CTrade.trade"
              }

            if(askPrice < openPrice && trailingLevel < stopLoss) // check trailing level is above the previos level.
              {
               trade.PositionModify(ticket,trailingLevel,takeProfit); // Modify trailing Stop using "CTrade.trade"
              }
           }

        }
     }
  }










    Simple Code for Detect  A  "New Bar or New Candle " Received Simple Code for Detect A "New Bar or New Candle " Received

    This code block detects a New Bar or a New Candle when it has received.

    Logarithmic Moving Average Logarithmic Moving Average

    Logarithmic Moving Average continuously calculates the logarithmic mean of highest price and lowest price within a period.

    Dominant Candle Dominant Candle

    Dominant Candle is a a two candlestick set where the wicks intersect each other but body of the candles are either gapped up, gapped down or equal

    Counter Attack Candlestick Counter Attack Candlestick

    Counter attack candlestick pattern