Modify SL AND TP

 

I dont know how can i modify my SL and TP. I've tried allmost everything. I just can't change it to TP=50 and SL=20 pips. Anyone can help me?


//+------------------------------------------------------------------+
//|                                                EMA EMA STOCH.mq5 |
//|                                                             DKON |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "DKON"
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
// ---- Input parameters
input int SL=20;       // Stop Loss
input int TP=50;       // Take Profit
input int MAGIC=999;   // MAGIC number

MqlTradeRequest trReq;
MqlTradeResult trRez;

 double tickask;
 double tickbid;
 double SmoothedBuffer1[];
 double SmoothedBuffer2[];
 ulong EXPERT_MAGIC;
 double EmaFast = iMA
  ( 
   NULL,             // symbol name 
   PERIOD_M5,        // period 
   30,               // averaging period 
   3,                // horizontal shift 
   MODE_EMA,         // smoothing type 
   PRICE_CLOSE       // type of price or handle 
   );
 
 double EmaSlow = iMA( 
   NULL,             // symbol name 
   PERIOD_M5,        // period 
   45,               // averaging period 
   3,                // horizontal shift 
   MODE_EMA,         // smoothing type 
   PRICE_CLOSE       // type of price or handle 
   );
 
 int Stoch = iStochastic( 
   _Symbol,                // symbol name 
   PERIOD_M5,           // period 
   5,                   // K-period (number of bars for calculations) 
   3,                   // D-period (period of first smoothing) 
   3,                   // final smoothing 
   MODE_EMA,            // type of smoothing 
   STO_CLOSECLOSE       // stochastic calculation method 
   );
 int OnInit()
  {
   ZeroMemory(trReq);
   ZeroMemory(trRez);
//---- set default vaules for all new order requests
   trReq.action=TRADE_ACTION_DEAL;
   trReq.magic=MAGIC;
   trReq.symbol=Symbol();                 // Trade symbol
   trReq.volume=0.01;                      // Requested volume for a deal in lots
   trReq.deviation=1;                     // Maximal possible deviation from the requested price
   trReq.type_filling=ORDER_FILLING_FOK;  // Order execution type
   trReq.type_time=ORDER_TIME_GTC;        // Order execution time
   trReq.comment="EMA EMA STOCH";

   return(0);
  }
   
void OnTick()
 {
//------ EMA's

//---- Copy latest MA indicator values into a buffer
//---- FAST
   int copied=CopyBuffer(EmaFast,0,0,50,SmoothedBuffer1);
   int copied2=CopyBuffer(EmaSlow,0,0,50,SmoothedBuffer2);

//------ STOCH
   // We create an arral for the K-line and D-line
   double KArray[];
   double DArray[];
   
   // sort the array from the current candle downwards
   ArraySetAsSeries (KArray, true);
   ArraySetAsSeries (DArray, true);
 
   //We fill the array with price data
   CopyBuffer(Stoch,0,0,3,KArray);
   CopyBuffer(Stoch,1,0,3,DArray);
  
  //We calculate the value for the current candle
   double KValue0=KArray[0];
   double DValue0=DArray[0];
   
   //We calculate the value for the last candle
   double KValue1=KArray[1];
   double DValue1=DArray[1];
   
   //---- input parameters are ReadOnly
 
//Buy signal
   //if both values are below 20 and FastEma is higher then SlowEma = buy
   if (KValue0<20 && DValue0<20 && KValue0>DValue0 && KValue1<DValue1 && SmoothedBuffer1[1]>SmoothedBuffer2[1] && SmoothedBuffer1[2] > SmoothedBuffer2[2])
   {
        //--- declare and initialize the trade request and result of trade request
         MqlTradeRequest request={0};
         MqlTradeResult  result={0};
         //--- parameters of request
         request.action   =TRADE_ACTION_DEAL;                     // type of trade operation
         request.symbol   =Symbol();                              // symbol
         request.volume   =0.01;                                   // volume of 0.01 lot
         request.type     =ORDER_TYPE_BUY;                        // order type
         request.price    =SymbolInfoDouble(Symbol(),SYMBOL_ASK); // price for opening
         request.deviation=10;                                     // allowed deviation from the price
      if(!OrderSend(request,result))
         PrintFormat("OrderSend error %d",GetLastError());     // if unable to send the request, output the error code
         //--- information about the operation
         PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);
    }
      
     
   //Sell signal
   //if both values are above 80 and FastEma is lover then SlowEma = sell
    if (KValue0>80 && DValue0>80 && KValue0<DValue0 && KValue1>DValue1 && SmoothedBuffer1[1]<SmoothedBuffer2[1] && SmoothedBuffer1[2] < SmoothedBuffer2[2])
      {
        //--- declare and initialize the trade request and result of trade request
      MqlTradeRequest request={1};
      MqlTradeResult  result={1};
       //--- parameters of request
         request.action   =TRADE_ACTION_DEAL;                     // type of trade operation
         request.symbol   =Symbol();                              // symbol
         request.volume   =0.01;                                  // volume of 0.01 lot
         request.type     =ORDER_TYPE_SELL;                       // order type
         request.price    =SymbolInfoDouble(Symbol(),SYMBOL_BID); // price for opening
         request.deviation=10;                                    // allowed deviation from the price
         //--- send the request
         if(!OrderSend(request,result))
         PrintFormat("OrderSend error %d",GetLastError());     // if unable to send the request, output the error code
         //--- information about the operation
         PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);
      }
      //--- declare and initialize the trade request and result of trade request
   MqlTradeRequest request;
   MqlTradeResult  result;
   int total=PositionsTotal(); // number of open positions   
//--- iterate over all open positions
   for(int i=0; i<total; i++)
     {
      //--- parameters of the order
      ulong  position_ticket=PositionGetTicket(i);// ticket of the position
      string position_symbol=PositionGetString(POSITION_SYMBOL); // symbol 
      int    digits=(int)SymbolInfoInteger(position_symbol,SYMBOL_DIGITS); // number of decimal places
      ulong  magic=PositionGetInteger(POSITION_MAGIC); // MagicNumber of the position
      double volume=PositionGetDouble(POSITION_VOLUME);    // volume of the position
      double sl=PositionGetDouble(POSITION_SL);  // Stop Loss of the position
      double tp=PositionGetDouble(POSITION_TP);  // Take Profit of the position
      ENUM_POSITION_TYPE type=(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);  // type of the position
      //--- output information about the position
      PrintFormat("#%I64u %s  %s  %.2f  %s  sl: %s  tp: %s  [%I64d]",
                  position_ticket,
                  position_symbol,
                  EnumToString(type),
                  volume,
                  DoubleToString(PositionGetDouble(POSITION_PRICE_OPEN),digits),
                  DoubleToString(sl,digits),
                  DoubleToString(tp,digits),
                  magic);
      //--- if the MagicNumber matches, Stop Loss and Take Profit are not defined
      if(magic==EXPERT_MAGIC && sl==0 && tp==0)
        { 
         //--- calculate the current price levels
         double price=PositionGetDouble(POSITION_PRICE_OPEN);
         double bid=SymbolInfoDouble(position_symbol,SYMBOL_BID);
         double ask=SymbolInfoDouble(position_symbol,SYMBOL_ASK);
         int    stop_level=(int)SymbolInfoInteger(position_symbol,SYMBOL_TRADE_STOPS_LEVEL);
         double price_level;
         //--- if the minimum allowed offset distance in points from the current close price is not set
         if(stop_level<=0)
            stop_level=150; // set the offset distance of 150 points from the current close price
         else
            stop_level+=50; // set the offset distance to (SYMBOL_TRADE_STOPS_LEVEL + 50) points for reliability
            //--- calculation and rounding of the Stop Loss and Take Profit values
         price_level=stop_level*SymbolInfoDouble(position_symbol,SYMBOL_POINT);
         if(type==POSITION_TYPE_BUY)
           {
            sl=NormalizeDouble(bid-price_level,digits);
            tp=NormalizeDouble(bid+price_level,digits);
           }
         else
           {
            sl=NormalizeDouble(ask+price_level,digits);
            tp=NormalizeDouble(ask-price_level,digits);
           }
         //--- zeroing the request and result values
         ZeroMemory(request);
         ZeroMemory(result);
         //--- setting the operation parameters
         request.action  =TRADE_ACTION_SLTP; // type of trade operation
         request.position=position_ticket;   // ticket of the position
         request.symbol=position_symbol;     // symbol 
         request.sl      =sl;                // Stop Loss of the position
         request.tp      =tp;                // Take Profit of the position
         request.magic=EXPERT_MAGIC;         // MagicNumber of the position
         //--- output information about the modification
         PrintFormat("Modify #%I64d %s %s",position_ticket,position_symbol,EnumToString(type));
         //--- send the request
         if(!OrderSend(request,result))
            PrintFormat("OrderSend error %d",GetLastError());  // if unable to send the request, output the error code
         //--- information about the operation   
         PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);
        }
     }
    }


    
     
 

Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • www.mql5.com
Ask questions on technical analysis, discuss trading systems and improve your MQL5 programming skills to develop your own trading strategies. Communicate and share your experience with traders from anywhere in the world, answer questions and help beginners — MQL5.community is developing along with you. show indicators Hi I have an expert that...
 

Got to the expert advisor settings and just change the values?

 

This is mistake:

 double EmaFast = iMA
  ( 
   NULL,             // symbol name 
   PERIOD_M5,        // period 
   30,               // averaging period 
   3,                // horizontal shift 
   MODE_EMA,         // smoothing type 
   PRICE_CLOSE       // type of price or handle 
   );
 
 double EmaSlow = iMA( 
   NULL,             // symbol name 
   PERIOD_M5,        // period 
   45,               // averaging period 
   3,                // horizontal shift 
   MODE_EMA,         // smoothing type 
   PRICE_CLOSE       // type of price or handle 
   );

Read the help ( iMA ): the indicator handle has the 'int' type.

It makes no sense to look further - first you have to fix the ERROR!

 

Perhaps you should read the manual, especially the examples.
   How To Ask Questions The Smart Way. 2004
      How To Interpret Answers.
         RTFM and STFW: How To Tell You've Seriously Screwed Up.

They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick (after the indicator has updated its buffers,) you use the handle, shift and count to get the data.
          Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
          Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
          How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 2020.03.08
          How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 2020.07.05
          How to call indicators in MQL5 - MQL5 Articles 12 March 2010
Reason: