Me
Me
Me
Log in
MetaTrader 5 / Experts
Lazy Bot MT5 (Daily Breakout EA) - expert for MetaTrader 5
Nguyen Quoc Hung
7099 (18)
Input parameters
//Import External class
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
#include <Trade\AccountInfo.mqh>
#include <Trade\OrderInfo.mqh>
//Local parameters
datetime last;
int totalBars;
int Pips2Points; // slippage 3 pips 3=points 30=points
double Pips2Double; // Stoploss 15 pips 0.015 0.0150
double slippage;
double acSpread;
string strComment = "";
CPositionInfo m_position; // trade position object
CTrade m_trade; // trading object
CSymbolInfo m_symbol; // symbol info object
CAccountInfo m_account; // account info wrapper
COrderInfo m_order; // pending orders object
3. Main Code
Explaining the Strategy, Everyday Bot will delete All the old Orders and find
Highest and Lowest value of the previous daily Bar and Send to two Pending orders
"BUY_STOP", "SELL_STOP". (No TakeProfit).
int OnInit()
{
//---
//3 or 5 digits detection
//Pip and point
if(_Digits % 2 == 1)
{
Pips2Double = _Point*10;
Pips2Points = 10;
slippage = 10* Inpuser_SLippage;
}
else
{
Pips2Double = _Point;
Pips2Points = 1;
slippage = Inpuser_SLippage;
}
void OnTick()
{
if(TerminalInfoInteger(TERMINAL_TRADE_ALLOWED) == false)
{
Comment("LazyBot\nTrade not allowed.");
return;
}
TimeLocal(timeLocal);
TimeCurrent(timeServer);
Comment(strComment);
//Check Trailing
TrailingSL();
//---
if(last != iTime(m_symbol.Name(), PERIOD_D1, 0))// && hourCurrent >
InpStartHour)
{
//Check Trading time
if(isTradingTime)
{
if(hourCurrent >= InpStartHour) // && hourCurrent < InpEndHour){
{
DeleteOldOrds();
//Send Order BUY_STOP va SELL_STOP
OpenOrder();
//+------------------------------------------------------------------+
//| CALCULATE SIGNAL AND SEND ORDER |
//+------------------------------------------------------------------+
void OpenOrder()
{
double TP_Buy = 0, TP_Sell = 0;
double SL_Buy = 0, SL_Sell = 0;
//Check Maximum Spread
if(InpMax_spread != 0){
if(acSpread > InpMax_spread){
Print(__FUNCTION__," > current Spread is greater than user
Spread!...");
return;
}
}
//Calculate Lots
double lot1 = CalculateVolume();
if(CheckVolumeValue(lot1)
&& CheckOrderForFREEZE_LEVEL(ORDER_TYPE_BUY_STOP, OpenPrice)
&& CheckMoneyForTrade(m_symbol.Name(),lot1, ORDER_TYPE_BUY)
&& CheckStopLoss(OpenPrice, SL_Buy))
{
if(!m_trade.BuyStop(lot1, OpenPrice, m_symbol.Name(), SL_Buy,
TP_Buy, ORDER_TIME_GTC, 0, comment))// use "ORDER_TIME_GTC" when expiration date =
0
Print(__FUNCTION__, "--> Buy Error");
}
if(CheckVolumeValue(lot1)
&& CheckOrderForFREEZE_LEVEL(ORDER_TYPE_SELL_STOP, OpenPrice)
&& CheckMoneyForTrade(m_symbol.Name(),lot1, ORDER_TYPE_SELL)
&& CheckStopLoss(OpenPrice, SL_Sell))
{
if(!m_trade.SellStop(lot1, OpenPrice, m_symbol.Name(), SL_Sell,
TP_Sell, ORDER_TIME_GTC, 0, comment))
Print(__FUNCTION__, "--> Sell Error");
}
}
3.2 New day, delete all old orders
//+------------------------------------------------------------------+
//| Delele Old Orders |
//+------------------------------------------------------------------+
void DeleteOldOrds()
{
string sep=";"; // A separator as a character
ushort u_sep; // The code of the separator character
string result[]; // An array to get strings
if(k > 2)
{
string sym = m_symbol.Name();
if((m_order.Magic() == InpMagicNumber) && (sym == result[1]))
{
m_trade.OrderDelete(m_order.Ticket());
}
}
}
}
}
3.3 EA has function "trailing StopLoss", SL will change every time price change and
make profit increase
//+------------------------------------------------------------------+
//| TRAILING STOPLOSS |
//+------------------------------------------------------------------+
void TrailingSL()
{
double SL_in_Pip = 0;
m_trade.PositionModify(m_position.Ticket(),
order_stoploss1, m_position.TakeProfit());
}
}
m_trade.PositionModify(m_position.Ticket(),
order_stoploss1, m_position.TakeProfit());
}
}
}
}
}
}
3.4 Check Volume value
//+------------------------------------------------------------------+
//| Check the correctness of the order volume |
//+------------------------------------------------------------------+
bool CheckVolumeValue(double volume)
{
//--- minimal allowed volume for trade operations
double min_volume = m_symbol.LotsMin();
return(true);
}
3.5 Check FreeLevel
//+------------------------------------------------------------------+
//| CHECK FREEZE LEVEL |
//+------------------------------------------------------------------+
bool CheckOrderForFREEZE_LEVEL(ENUM_ORDER_TYPE type, double price)//change name of
this function
{
int freeze_level = (int)SymbolInfoInteger(_Symbol, SYMBOL_TRADE_FREEZE_LEVEL);
//+------------------------------------------------------------------+
//|
|
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
Video MQL5: