Trailing Stop for my custome EA (MT4)

 

Hi, i need a help.... This is my first EA and i am a beginner...

can anyone help me to put Trailing stop for my EA ... i tried many ways but trailing stop is not activating when i am putting it on strategy tester... please help...


//+------------------------------------------------------------------+
//|                                                    	 demo 1 .mq4 |
//|                             Copyright 2020, Kevola Forex Academy |
//|                                                    t.me/kevolafx |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, Kevola Forex Academy."
#property link      "t.me/kevolafx"
#property version   "1.00"
#property strict
datetime dt;

input int magic = 123;
input double lot_size = 0.10;

bool check;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   dt = Time[0];
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   if(isnewbar()){
      //you can replace the period here
      double prev_ma200 = iMA(_Symbol, PERIOD_CURRENT, 50, 0, MODE_EMA, PRICE_CLOSE, 2);
      double prev_ma5 = iMA(_Symbol, PERIOD_CURRENT, 5, 0, MODE_EMA, PRICE_CLOSE, 2);
      double ma200 = iMA(_Symbol, PERIOD_CURRENT, 50, 0, MODE_EMA, PRICE_CLOSE, 1);
      double ma5 = iMA(_Symbol, PERIOD_CURRENT, 5, 0, MODE_EMA, PRICE_CLOSE, 1);
      
      if(prev_ma200>prev_ma5 && ma200<ma5){
         if(OrdersTotal()!=0) closeexisting();
         //send the order
         check = OrderSend(_Symbol, OP_BUY, lot_size, Ask, 0, 0,0,NULL, magic);
      }
      else if( prev_ma200 < prev_ma5 && ma200>ma5){
         if(OrdersTotal()!=0) closeexisting();
         
         check = OrderSend(_Symbol, OP_SELL, lot_size, Bid, 0, 0, 0, NULL, magic);
      }
      
   }
  }


//close all existing position
void closeexisting(){
   for(int i = OrdersTotal()-1; i>=0; i--){
      check = OrderSelect(i, SELECT_BY_POS);
      if(check && OrderMagicNumber() == magic){
         check = OrderClose(OrderTicket(), OrderLots(), MarketInfo(_Symbol, MODE_BID+OrderType()), 10);
      }
   }
}


//Check if new bar
bool isnewbar(){
   if(Time[0] != dt){
      dt = Time[0];
      return true;
   }
   return false;
}
//+------------------------------------------------------------------+
The Fundamentals of Testing in MetaTrader 5
The Fundamentals of Testing in MetaTrader 5
  • www.mql5.com
The idea of ​​automated trading is appealing by the fact that the trading robot can work non-stop for 24 hours a day, seven days a week. The robot does not get tired, doubtful or scared, it's is totally free from any psychological problems. It is sufficient enough to clearly formalize the trading rules and implement them in the algorithms, and...
 
  1. Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum?
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. Kevola: an anyone help me to put Trailing stop for my EA ... i tried many ways but trailing stop is not activating when i am putting it on strategy tester... please help...

    Don't lie to us. Your posted code doesn't have a trailing stop.

    Help you with what? You haven't stated a problem, you stated a want. Show us your attempt (using the CODE button) and state the nature of your problem.
              No free help 2017.04.21

    Or pay someone. Top of every page is the link Freelance.
              Hiring to write script - General - MQL5 programming forum 2018.05.12