Code
//+------------------------------------------------------------------+
//| SCALPING PERFECT.mq4 |
//| Copyright 2019, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
extern string Name_EA = "SCALPING PERFECT";
extern int TakeProfit = 80;
int StopLoss = 0;
extern int ecartask=6;
extern int ecartbid=6;
int MagicNumber =03122019;
extern int LotFactor=180;//150 <---> 300
int Slippage=0;
extern int Start_Time = 0; // Time to allow trading to start ( hours of 24 hr clock ) 0 for both disables
extern int Finish_Time = 0; // Time to stop trading ( hours of 24 hr clock ) 0 for both disables
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
if(((TimeHour(TimeCurrent())>=Start_Time)) && ((TimeHour(TimeCurrent())<=Finish_Time )))return ;
int total=OrdersTotal();
int i;
double OpenLongOrders=0,OpenShortOrders=0,PendLongs=0,PendShorts=0;
if(total==0 && OpenLongOrders==0 && OpenShortOrders==0 && PendLongs==0 && PendShorts==0)
{
openbuy();
sellstop();
opensell();
buystop();
}
for(i=0;i<total;i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
{
int type=OrderType();
if(type == OP_BUY ) {OpenLongOrders=OpenLongOrders+1;}
if(type == OP_SELL ) {OpenShortOrders=OpenShortOrders+1;}
if(type == OP_BUYSTOP ) {PendLongs=PendLongs+1;}
if(type == OP_SELLSTOP ) {PendShorts=PendShorts+1;}
if(total==1 && OpenLongOrders==0 && OpenShortOrders==0 && (PendLongs==1 || PendShorts==1))
{
deleteallpendingorders();
}
}
}
}
//+------------------------------------------------------------------+
//| opensell |
//+------------------------------------------------------------------+
void opensell()
{
int ticket=OrderSend(Symbol(),OP_SELL,(AccountBalance() * 0.01 /LotFactor)*1,Bid,5,Bid+(StopLoss*Point),Bid-(TakeProfit*Point),"SCALPING PERFECT",MagicNumber,0,Red);
if (GetLastError()!=0) Print(" BUY Order Error "+(string)ticket);
}
//+------------------------------------------------------------------+
//| buystop |
//+------------------------------------------------------------------+
void buystop1()
{
int ticket= OrderSend(Symbol(),OP_BUYSTOP, (AccountBalance() * 0.01 /LotFactor)*3,Ask+ecartask*Point,5,(Ask+ecartask*Point)-StopLoss*Point,(Ask+ecartask*Point)+TakeProfit*Point,"SCALPING PERFECT",MagicNumber,0,clrBlue);
if (GetLastError()!=0) Print(" BUY Order Error "+(string)ticket);
}
//+------------------------------------------------------------------+
//| sellstop |
//+------------------------------------------------------------------+
void sellstop1()
{
int ticket =OrderSend(Symbol(),OP_SELLSTOP, (AccountBalance() * 0.01 /LotFactor)*5,Bid-ecartbid*Point,5,(Bid-ecartbid*Point)+StopLoss*Point,(Bid-ecartbid*Point)-TakeProfit*Point,"SCALPING PERFECT",MagicNumber,0,clrBlue);
if (GetLastError()!=0) Print(" SELL Order Error "+(string)ticket);
}
//+------------------------------------------------------------------+
//| openbuy |
//+------------------------------------------------------------------+
void openbuy()
{
int ticket= OrderSend(Symbol(),OP_BUY,(AccountBalance() * 0.01 /LotFactor)*1,Ask,5,Ask-StopLoss*Point,Ask+TakeProfit*Point,"SCALPING PERFECT",MagicNumber,0,clrBlue);
if (GetLastError()!=0) Print(" BUY Order Error "+(string)ticket);
}
//+------------------------------------------------------------------+
//| sellstop |
//+------------------------------------------------------------------+
void sellstop()
{
int ticket =OrderSend(Symbol(),OP_SELLSTOP, (AccountBalance() * 0.01 /LotFactor)*3,Bid-ecartbid*Point,5,(Bid-ecartbid*Point)+StopLoss*Point,(Bid-ecartbid*Point)-TakeProfit*Point,"SCALPING PERFECT",MagicNumber,0,clrBlue);
if (GetLastError()!=0) Print(" SELL Order Error "+(string)ticket);
}
//+------------------------------------------------------------------+
//| buystop |
//+------------------------------------------------------------------+
void buystop()
{
int ticket= OrderSend(Symbol(),OP_BUYSTOP, (AccountBalance() * 0.01 /LotFactor)*5,Ask+ecartask*Point,5,(Ask+ecartask*Point)-StopLoss*Point,(Ask+ecartask*Point)+TakeProfit*Point,"SCALPING PERFECT",MagicNumber,0,clrBlue);
if (GetLastError()!=0) Print(" BUY Order Error "+(string)ticket);
}
//+------------------------------------------------------------------+
//| deleteallpendingorders |
//+------------------------------------------------------------------+
void deleteallpendingorders()
{
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber && ((OrderType()==OP_BUY) || (OrderType()==OP_SELL) ||(OrderType()==OP_BUYSTOP) || (OrderType()==OP_SELLSTOP) || (OrderType()==OP_BUYLIMIT) || (OrderType()==OP_SELLLIMIT)))
{
bool modify= OrderDelete(OrderTicket());
}
}
}
//+------------------------------------------------------------------+