Welcome Forex EA downloads & MT4/MT5 auto-trading resources — EAs, Gold EAs, quant tools and real-world automation.
Sign In Sign Up

AK-47 Scalper EA - MetaTrader 5 Expert | Scalping EA Download - MT4/MT5 Resources

author EAcpu | 2 reads | 0 comments |

1. Enter parameters

 #Define external robot name "AK-47 EA" //Robot name
#define version "1.00"

//Import input class
#include <Trade\PositionInfo.mqh> #include <Trade\Trade.mqh> #include <Trade\SymbolInfo.mqh> #include <Trade\AccountInfo.mqh> #include <Trade\OrderInfo.mqh> //--- Introduce predefined variables to improve code readability
#define ask symbol information double ( _symbol , SYMBOL_ASK ) #define bid symbol information double ( _symbol , SYMBOL_BID ) //--- Input parameters
Input string ESettings = "——————————————————" ; //-------- --------
Input integer InpMagicNumber = 124656 ; //magic number
Input String Money Settings = "——————————————————" ; //-------- <Currency Settings> --------
Input Boolean value isVolume_Percent= True ; //Allow volume percentage
Enter double InpRisk = 3 ; //Balance risk percentage (%)
Enter String Trade Settings = "——————————————————" ; //-------- <Trade Settings> --------
Enter double Inpuser_lot = 0.01 ; //a lot
Enter double InpSL_Pips = 3.5 ; //Stop loss (in pips)
InpTP_Pips = 7 ; //TP in points (0 = no TP)
Input integer InpMax_slippage = 3 ; //Maximum slippage allow_Pips.
Enter double InpMax_spread = 5 ; //Maximum spread allowed in pips (0 = float)
Input string time setting = "——————————————————" ; //-------- <Trading time setting> --------
Input boolean InpTimeFilter = true ; //Trading time filter
Input integer InpStartHour = 2 ; //Start time
Enter an integer InpStartMinute = 30 ; //Start minute
Input integer InpEndHour = 21 ; //End time
Input integer InpEndMinute = 0 ; //End minute

2. Initialization of local variables

//--- variables
Integer Pips2Points; // Slippage 3 points 3=points 30=points
Double points 2 pairs; // Stop loss 15 points 0.015 0.0150
boolean yes order = false ; integer slip; long ac spread; string string comment = "" ;

CPositionInfo m_position; // Trading position object CTrade m_trade; // Trading object CSymbolInfo m_symbol; // Trading type information object CAccountInfo m_account; // Account information packaging COrderInfo m_order; // Pending order object

3. Main code

a/ expert initialization function

 //+------------------------------------------------------------------+
//|Expert initialization function |
//+------------------------------------------------------------------+
Integer initialization () {

//3-digit or 5-digit detection //dot and dot if ( _number % 2 == 1 ) {
      Pips2Double= _View * 10 ;
      Pips2Points = 10 ;
      Slippage = 10 * InpMax_slippage;
  }
other {
      Pips2Double= _View ;
      Pips2Points = 1 ;
      Slippage = InpMax_slippage;
  }
  
if (!m_symbol.name( symbol ())) // Set the symbol name and return ( initialization failed );
 
       Refresh rate(); //--- m_trade.SetExpertMagicNumber(InpMagicNumber);
  m_trade.SetMarginMode();
  m_trade.SetTypeFillingBySymbol(m_symbol.Name());
  m_trade.SetDeviationInPoints(slippage); //--- return ( initialization successful );
}

b/ Expert check function

//+------------------------------------------------------------------+
//|Expert check function |
//+------------------------------------------------------------------+
Blank check () {

if ( terminalinfo integer ( TERMINAL_TRADE_ALLOWED ) == false ) {
Comment ( "LazyBot\nTrading is not allowed." );
return ;
  }
  
Mql datetime structure time;
timecurrent (structTime);
  structuretime.sec = 0 ;
  
//Set the start time structTime.hour = InpStartHour;
  structTime.min = InpStartMinute;  
datetime starttime = struct to time (struct time);
  
//Set the end time structTime.hour = InpEndHour;
  structTime.min = InpEndMinute;
datetimetimeend = struct to time (struct time);
  
   ac propagation = symbolinfo integer ( _symbol , symbol_propagation );
  
  
    string comment = "\n" + external robot name + "-v" + ( string ) version;
  str comment += "\nServer time=" + String time ( time current (), TIME_DATE | TIME_SECONDS ) + "——" + DayOfWeekDescription(structTime.day_of_week);
  str comment += "\nTrading time = [" + ( thin string )InpStartHour + "h" + ( thin string )Inp start minute + "-->" + ( thin string )InpEndHour + "h" + ( thin string )InpEndMinute + "]" ;
  
   str comment += "\nCurrent spread = " + ( thin string ) acSpread + "Points" ;
  
Comment (strComment);
  
//update value update order();
  
   TrailingStop();
 
//Dieu kien giao dich theo phien Myif ( InpTimeFilter) {
if ( timecurrent () >= start time&& timecurrent () < timeend) {
if (!isOrder) OpenOrder();
      }
  }
other {
if (!isOrder) OpenOrder();
  }
  
 } //---End function

3.1 Calculating signals to send orders

//+------------------------------------------------------------------+
//|Calculate signals and send instructions |
//+------------------------------------------------------------------+
BlankOpenOrder (){
  
ENUM_ORDER_TYPE order type = order type_sell ; //-1; 
Double TP = 0 ;
double SL = 0 ;
string comment = ExtBotName;
  
//Calculate the double lot size 1 = Calculate the trading volume();
  
if (order type == order type_sell ) {
Double open price = Bid ​​- Normalized Double (InpSL_Pips/ 2 * Pips2Double, _Number );
 
           TP = Opening Price - Normalized Double (InpTP_Pips * Pips2Double, _Number );
      SL = query + normalizedDouble (InpSL_Pips/ 2 * Pips2Double, _number );
  
If (check SpreadAllow() //Check spread && Check volume value (lot1) //Check volume && CheckOrderForFREEZE_LEVEL( ORDER_TYPE_SELL_STOP , opening price) //Check the distance from openPrice to Bid && Check stop loss (open price, stop loss, take profit) //Check the distance from SL, TP to OpenPrice && CheckMoneyForTrade(m_symbol.Name(),lot1, order type_sell )) //Check balance khi lenh cho duoc Hit {
if (!m_trade.SellStop(lot1, OpenPrice, m_symbol.Name(), SL, TP, ORDER_TIME_GTC , 0 , comments))
print ( __Function__ , "--> Order sending error" , m_trade.ResultComment());
      }
  }
Else if (order type == order type purchase ) {
Double the opening price = ask price + normalized double (InpSL_Pips/ 2 * Pips2Double, _number );
      SL = Bids- NormalizedDouble (InpSL_Pips/ 2 * Pips2Double, _Number );
 
If (check SpreadAllow() //Check the spread && Check the volume value (lot1) //Check the volume && CheckOrderForFREEZE_LEVEL( ORDER_TYPE_BUY_STOP , opening price) //Check the distance from openPrice to Bid && Check stop loss (open price, stop loss, take profit) //Check the distance from SL, TP to OpenPrice && CheckMoneyForTrade(m_symbol.Name(),lot1, order type purchase )) //Check balance khi lenh cho duoc Hit {
if (!m_trade.BuyStop(lot1, OpenPrice, m_symbol.Name(), SL, TP, ORDER_TIME_GTC , 0 , comment)) // Use "ORDER_TIME_GTC" when expiration date = 0 print ( __function__ , "--> Order sending error" , m_trade.ResultComment());
      }
  }
  
 }

3.2 Calculate volume

//+------------------------------------------------------------------+
//|Calculate volume |
//+------------------------------------------------------------------+
// We define a function to calculate the position size and return the order lot size.
Double calculated volume() {

Double lot size = 0 ;

if (isVolume_Percent == false ) {
      Lot size = Inpuser_lot;
    }
other {
      Lot size = (InpRisk) * m_account.FreeMargin();
      Lot size = Lot size/ 100000 ;
Double n = MathFloor (Number of Lots/Inpuser_lot);
//Comment((string)n); Lot size = n * Inpuser_lot;
 
if (lot<Inpuser_lot)
        Lot size = Inpuser_lot;

if (LotSize > m_symbol.LotsMax()) LotSize = m_symbol.LotsMax();

if (LotSize < m_symbol.LotsMin()) LotSize = m_symbol.LotsMin();
  }
//--- return (number of lots);
}
3.3 EA has a "trailing stop loss" function, SL changes every time the price changes (falls)
 //+------------------------------------------------------------------+
//|Trailing Stop |
//+------------------------------------------------------------------+
blank trace stop() {

Double SL_in_Pip = 0 ;

for ( integer i = total number of positions ()- 1 ; i >= 0 ; i-) {
if (m_position.SelectByIndex(i)) { // Select order by index to further access its properties if ((m_position.Magic() == InpMagicNumber) && ( m_position.symbol () == m_symbol.Name())) {
// For purchase orders if (m_position.PositionType() == POSITION_TYPE_BUY ) {
//-- Calculate stop loss when price changes SL_in_Pip = normalized double (bid- m_position.StopLoss(), _number ) / Pips2Double;
if (SL_in_Pips > InpSL_Pips) {
Double's new SL = Normalized Double (Bids - InpSL_Pips * Pips2Double, _Number );
 
if (!m_trade.PositionModify(m_position.Ticket(), newSL, m_position.TakeProfit())) {
Print ( __Function__ , "--> Order modification error" , m_trade.ResultComment());
continue ; 
                   }
              }
            }

//For sell orders, if ( m_position.PositionType() == POSITION_TYPE_SELL ) {
//-- Calculate stop loss when price changes SL_in_Pip = Normalized Double (m_position.StopLoss() - bid, _number ) / Pips2Double;
if (SL_in_Pips > InpSL_Pips){
Double's new SL = Normalized Double (Bids + (InpSL_Pips) * Pips2Double, _Number );
if (!m_trade.PositionModify(m_position.Ticket(), newSL, m_position.TakeProfit())) {
Print ( __Function__ , "--> Order modification error" , m_trade.ResultComment());
//continue; }
              }
            }
        }
      }
  }
  
//--- Modify the pending order order ( integer I ="functions">Total number of orders()- 1 ; i >= 0 ; i- ) { // Return the current order quantity if (m_order.SelectByIndex(i)) { // Select a pending order by index to further access its properties if ( m_order.Symbol () == m_symbol.Name() && m_order.Magic()==InpMagicNumber) { if (m_order.OrderType() == ORDER_TYPE_BUY_STOP ) { SL_in_Pip = Normalized Double (Bid - m_order.StopLoss(), _Number ) / Pips2Double; if (SL_in_Pips < InpSL_Pips/ 2 ) { Double new operation = Normalized Double (Bid + (InpSL_Pips/ 2 ) * Pips2Double, _number ); double new TP = normalized double (newOP + InpTP_Pips * Pips2Double, _number ); double new SL = normalized double (bid - (InpSL_Pips/ 2 ) * Pips2Double, _number ); if (!m_trade.OrderModify(m_order.Ticket(), newOP, newSL, newTP, ORDER_TIME_GTC , 0 )) { print ( __function__ , "-->Modify PendingOrder Error!" , m_trade.ResultComment()); continue ; } } } else if (m_order.OrderType() == ORDER_TYPE_SELL_STOP ) { SL_in_Pip = normalized double (m_order.StopLoss() - query, _number ) / Pips2Double; if (SL_in_Pips < InpSL_Pips/ 2 ) { double new op = normalized double (ask - (InpSL_Pips/ 2 ) * Pips2Double, _number ); double new TP = normalized double (newOP - InpTP_Pips * Pips2Double, _number ); double new SL = normalized double (ask + (InpSL_Pips/ 2 ) * Pips2Double, _number ); if (!m_trade.OrderModify(m_order.Ticket(), newOP, newSL, newTP, ORDER_TIME_GTC , 0 )) { print ( __function__ , "-->Modify PendingOrder error!" , m_trade.ResultComment()); //continue; } } } } } } }


Attachment download

📎 ak-47_scalper_ea__-_mt5.mq5 (45.37 KB)

Source: MQL5 #44883

Verification code Refresh