Trailing stop loss and Tp when current candle close below or above MA

 

Good day

i'm new to mql5 and have been struggling for a week now to get it right but still no luck. i'm willing to pay someone to just edit my EA so it has a trailing stop loss and a Tp

if anyone is willing to help me please send me a massage or a email  nielotto5@gmail.com 

Here is the EA code


#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
// Global inputs
#include <Trade/Trade.mqh>
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
//Global scope/level
input double Tp = 800;
input double Sl = 600; 
int lmahandle;
int hmahandle;
int mahandle; 
double lmavalues[];
double hmavalues[];        // store eunique handel of indicator
double mavalues[];    // store moveing avrg price value
MqlRates bar[];
MqlRates hbar[];
MqlRates lbar[];      // previos bar data , hig,low,open,close
datetime lastbar;    // only when a new bar has been created
double Lots = 0.01;
datetime openTimeBuy = 0;
datetime openTimeSell = 0;
// Global C class
CTrade trade;
int OnInit(){
  
//ima is moving avrg
  
  lmahandle = iMA(_Symbol,PERIOD_M30,1,1,MODE_SMA,PRICE_LOW);
  ArraySetAsSeries(lmavalues,true);
  ArraySetAsSeries(lbar,true); 
  
  hmahandle = iMA(_Symbol,PERIOD_M30,1,1,MODE_SMA,PRICE_HIGH);
  ArraySetAsSeries(hmavalues,true);
  ArraySetAsSeries(hbar,true);
  
  mahandle = iMA(_Symbol,PERIOD_M30,50,0,MODE_SMMA,PRICE_OPEN);   
  ArraySetAsSeries(mavalues,true);
  ArraySetAsSeries(bar,true);
  lastbar = 0;
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason){
//---
   
}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
   double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
   CopyBuffer(hmahandle,0,0,10,hmavalues);
   CopyRates(_Symbol,PERIOD_M30,0,10,hbar);
   CopyBuffer(lmahandle,0,0,10,lmavalues);
   CopyRates(_Symbol,PERIOD_M30,0,10,lbar); 
   CopyBuffer(mahandle,0,0,10,mavalues);
   CopyRates(_Symbol,PERIOD_M30,0,10,bar);
   
   long hdigits = SymbolInfoInteger(_Symbol,SYMBOL_DIGITS);
   double currenthmavalue = NormalizeDouble(hmavalues[1],(int)hdigits);
   
   long ldigits = SymbolInfoInteger(_Symbol,SYMBOL_DIGITS);
   double currentlmavalue = NormalizeDouble(lmavalues[1],(int)ldigits); 
   
   long digits = SymbolInfoInteger(_Symbol,SYMBOL_DIGITS);
   double currentmavalue = NormalizeDouble(mavalues[1],(int)digits);
   
   if(bar[0].time > lastbar){
   lastbar = bar[0].time;
   
// check if price move above Moving Avrage
   
    if(bar[1].open < mavalues[1] && bar[0].close > mavalues[1]){
    Alert("price above MA50 _ 30M ",bar[1].time, "|",currentmavalue);
    openTimeBuy = iTime(_Symbol,PERIOD_CURRENT,0);
    double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
    double sl = ask - Sl * SymbolInfoDouble(_Symbol,SYMBOL_POINT);
    double tp = bar[0].close < mavalues[1];
     
    trade.PositionOpen(_Symbol,ORDER_TYPE_BUY,Lots,ask,sl,tp);
    
}
    
//  Buy if price move below MA50 and close above MA50
 
     if(bar[1].open > mavalues[1] && lmavalues[0] < mavalues[1] && bar[0].close > mavalues[1]){
     Alert("price pullback above MA50 _ 30M ",bar[0].high,bar[1].time, "|",currentmavalue);
      openTimeBuy = iTime(_Symbol,PERIOD_CURRENT,0);
      double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
      double sl = ask - Sl * SymbolInfoDouble(_Symbol,SYMBOL_POINT);
      double tp = bar[0].close < mavalues[1];
    
      trade.PositionOpen(_Symbol,ORDER_TYPE_BUY,Lots,ask,sl,tp);  
   
}
// check if price move below Moving Avrage
   
    if(bar[1].open > mavalues[1] && bar[0].close < mavalues[1]){
    Alert("price below MA50 _ 30M ",bar[1].time,"|",currentmavalue);
    openTimeSell = iTime(_Symbol,PERIOD_CURRENT,0);
    double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
    double sl = bid + Sl * SymbolInfoDouble(_Symbol,SYMBOL_POINT);
    double tp = bar[1].open < mavalues[1] && bar[0].close > mavalues[1];
    trade.PositionOpen(_Symbol,ORDER_TYPE_SELL,Lots,bid,sl,tp); 
}     
// Sell if price move above MA50 and close belowe MA50
 
     if(bar[1].open < mavalues[1] && hmavalues[0] > mavalues[1] && bar[0].close < mavalues[1]){
     Alert("price pullback below MA50 _ 30M ",bar[0].high,bar[1].time, "|",currentmavalue);
     openTimeSell = iTime(_Symbol,PERIOD_CURRENT,0);
     double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
     double sl = bid + Sl * SymbolInfoDouble(_Symbol,SYMBOL_POINT);
     double tp = bid - Tp * SymbolInfoDouble(_Symbol,SYMBOL_POINT);
     trade.PositionOpen(_Symbol,ORDER_TYPE_SELL,Lots,bid,sl,tp);
}      
  
      openTimeSell = iTime(_Symbol,PERIOD_CURRENT,0);
     double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
     double sl = bid + Sl * SymbolInfoDouble(_Symbol,SYMBOL_POINT);
     double tp = bar[1].open < mavalues[1] && bar[0].close > mavalues[1];
     trade.PositionOpen(_Symbol,ORDER_TYPE_SELL,Lots,bid,sl,tp); 
  
} 
   
}
   
//+------------------------------------------------------------------+
Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • 2024.11.17
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 
Please don't post code with so much white-space. I have edited your post to remove all the extraneous empty lines in your code.
 
Thank you Fernando. Do you write EA and will you be able to help me please 
 
Thanks in advance Ahmad

I just want to add a trailing stop loss with input values so that you can adjust the distance from the current price to the trailing SL and my take profit must be wen the current candle close above the MA line (sell) and below the MA(buy) 
 
Niel05otto #:
Thanks in advance Ahmad

I just want to add a trailing stop loss with input values so that you can adjust the distance from the current price to the trailing SL and my take profit must be wen the current candle close above the MA line (sell) and below the MA(buy) 

I hope you are successful

Check the file below

Files:
expert.mq5  9 kb
 
Thank you Ahmad. 

But it still doesn't have a trailing stop loss and the tp must be when it cross the moving average line
 
Niel05otto #:
ممنون احمد

اما هنوز استاپ ضرر عقبی ندارد و tp باید زمانی باشد که از خط میانگین متحرک عبور کند

I'm using a dictionary for your language, so I don't understand exactly what your problem is. Please explain more fully and with examples

 
Hi Ahmad. I took a screenshot of part of the coding for the trailing stop loss and also attached two pictures of when to buy/sell and the take profit position 
 
Niel05otto #: Can someone please give me feedback on the results.

Your previous two posting attempts were removed. Please read your system messages (private messages).

If you want to discuss the EA results, then you will need to provide the full source code for the EA, otherwise it will be considered self-promotion.

EDIT: And for screenshots, please use your computer's own "Prints Screen" method instead of photos from your phone — 8 ways to take a screenshot on Windows 10 and Windows 1