거래 로봇을 무료로 다운로드 하는 법을 시청해보세요
당사를 Facebook에서 찾아주십시오!
당사 팬 페이지에 가입하십시오
스크립트가 흥미로우신가요?
그렇다면 링크 to it -
하셔서 다른 이들이 평가할 수 있도록 해보세요
스크립트가 마음에 드시나요? MetaTrader 5 터미널에서 시도해보십시오
Experts

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

조회수:
1896
평가:
(2)
게시됨:
2024.04.04 22:09
이 코드를 기반으로 한 로봇이나 지표가 필요하신가요? 프리랜스로 주문하세요 프리랜스로 이동

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