
#property copyright "FOREXflash©_v3"

#include <stdlib.mqh>

//-------------------------------------------------------------------+  

extern int       Magic     =1803; 
extern bool      Long =true;     
extern bool      Short =true;   
extern int       Orders_=2;     
extern double    Lots=0.1;
extern int       TP=100;
extern int       SL=150;
extern int       Delta  =5;      
extern int       Period_MA=13;   
extern bool      AutoClose = true;
extern bool      MoneyManagement=false;
extern int       MarginPercent=2;
extern bool      UseTrailing = false;
extern int       LongProfit = 50;
extern int       ShortProfit = 60;
extern int       LONGTrailingStop = 50;
extern int       SHORTTrailingStop = 60;
extern int       LONGTrailingStep = 5;
extern int       SHORTTrailingStep = 5;
//-------------------------------------------------------------------+  

int              Slippage=3;
int              ticket;
int              m,n;
 int             X_;
int              ExpertBars;
static int       prevtime = 0;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()  
{
ExpertBars = Bars;   
return(0);  
}
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()  
{   
return(0);  
}
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
if(Time[0] == prevtime)   
return(0);
prevtime = Time[0];
//-------------------------------------------------------------------+   
if(AccountFreeMarginCheck(Symbol(),OP_BUY,Lots)>0 && AccountFreeMarginCheck(Symbol(),OP_SELL,Lots)>0 && IsTradeAllowed()==true ) 
{
//-------------------------------------------------------------------+   
if (UseTrailing) TrailPositions(); 
if (NumberOfPositions(NULL,-1,-1)< Orders_) 
{
//----------------------  Complex_Common ----------------------------+

if (Symbol()=="GBPUSD")    { m=2; n=0;}
if (Symbol()=="EURUSD")    { m=1; n=0;}
if (Symbol()=="GBPJPY")    { m=2; n=4;}
if (Symbol()=="GBPCHF")    { m=2; n=3;}
if (Symbol()=="EURJPY")    { m=1; n=4;}
if (Symbol()=="EURGBP")    { m=1; n=2;}
if (Symbol()=="USDJPY")    { m=0; n=4;}
if (Symbol()=="EURJPY")    { m=1; n=4;}
if (Symbol()=="USDCHF")    { m=0; n=3;}
if (Symbol()=="EURCHF")    { m=1; n=3;}


double Complex_1_1=iCustom(NULL,0,"Complex_Common",m,1); 
double Complex_2_1=iCustom(NULL,0,"Complex_Common",m,2); 
double Complex_1_2=iCustom(NULL,0,"Complex_Common",n,1); 
double Complex_2_2=iCustom(NULL,0,"Complex_Common",n,2); 
  
double MA_= iMA(NULL, 0, Period_MA, 0,MODE_EMA, PRICE_CLOSE, 0);
double Price_ = iClose(NULL, 0, 1);
X_ = NormalizeDouble((Complex_1_1 - Complex_1_2 ),0);
if ( Long)                  
{  
if (NumberOfPositions(NULL,OP_BUY,Magic)< 1) 
{
if (! NumberOfBarOpenLastPos(NULL,0,OP_BUY, Magic) ==0) 
{
if   
(Complex_1_1 > Complex_2_1 && Complex_1_2 < Complex_2_2 && X_ > Delta &&  X_ > 0 && MA_ < Price_ )      
{
Lots=LotsCounting(); 
ticket=OrderSend(Symbol(),0,Lots,Ask,Slippage,Bid-SL*Point,Ask+TP*Point, NULL,Magic ,0,Blue);
if(ticket < 0)          
{ 
{
Print("BUY  #",GetLastError());
return (0);
} 
Sleep(10000);
prevtime = Time[1];   
}
}
}
}
}
//-------------------------------------------------------------------+  

if (Short)                   
{     
if (NumberOfPositions(NULL,OP_SELL, Magic)< 1) 
{
if (!NumberOfBarOpenLastPos(NULL, 0,OP_SELL, Magic)  ==0) 
{
if      
(Complex_1_1 < Complex_2_1 && Complex_1_2 >  Complex_2_2 && MathAbs(X_) >  Delta &&  X_ < 0 && MA_ >Price_ )   
{
Lots=LotsCounting();        
ticket=OrderSend(Symbol(),1,Lots,Bid,Slippage,Bid+SL*Point,Bid-TP*Point,NULL,Magic ,0,Red);
if(ticket < 0)          
{ 
{
Print("SELL  #",GetLastError());
return (0);
} 
Sleep(10000);
prevtime = Time[1];   
}
}
}
}
}
}
//-------------------------------------------------------------------+  

if (AutoClose) 
{ 
for ( int v = OrdersTotal() - 1; v >= 0; v -- )             
{       
if (OrderSelect(v, SELECT_BY_POS, MODE_TRADES))            
{           
if (OrderSymbol()==Symbol())                                
{ 
//-------------------------------------------------------------------+        
if ((OrderType() == OP_BUY) && (OrderMagicNumber()==Magic))  
{ 
if  (Complex_1_1 < Complex_2_1 && Complex_1_2 >  Complex_2_2 && X_ < 0 )
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Green);
prevtime = Time[1];            
}
}  
//-------------------------------------------------------------------+
if ((OrderType() == OP_SELL)  && (OrderMagicNumber()==Magic)) {
if ( Complex_1_1 > Complex_2_1 && Complex_1_2 <  Complex_2_2 && X_ > 0 )
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Green);
prevtime = Time[1];    
}
}
}
}
}
}
}
return(0);
}
//-------------------------------------------------------------------+

void TrailPositions()
{  
int Orders = OrdersTotal();
for (int i=0; i<Orders; i++) 
{
if (!(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))) continue;
if (OrderSymbol() != Symbol() && OrderMagicNumber()==Magic) continue; 
if (OrderType() == OP_BUY) 
{
if (Bid-OrderOpenPrice() > LongProfit*Point) 
{
if (OrderStopLoss() < Bid-(LONGTrailingStop+SHORTTrailingStep-1)*Point) 
{
OrderModify(OrderTicket(), OrderOpenPrice(), Bid-LONGTrailingStop*Point,OrderTakeProfit(), 0, Blue);
}
}
}
if (OrderType() == OP_SELL) 
{
if (OrderOpenPrice()-Ask > ShortProfit*Point) 
{
if (OrderStopLoss() > Ask+(SHORTTrailingStop+SHORTTrailingStep-1)*Point || OrderStopLoss() == 0) 
{
OrderModify(OrderTicket(), OrderOpenPrice(), Ask+SHORTTrailingStop*Point,OrderTakeProfit(), 0, Blue);
}
}
}
}
}
//-------------------------------------------------------------------+

int NumberOfBarOpenLastPos(string sy="0", int tf=0, int op=-1, int mn=-1) 
{
datetime t;
int i, k=OrdersTotal();
if (sy=="" || sy=="0") sy=Symbol();
for (i=0; i<k; i++)                                    
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))      
{
if (OrderSymbol()==sy)                             
{
if (OrderType()==OP_BUY || OrderType()==OP_SELL) 
{
if (op<0 || OrderType()==op)                   
{
if (mn<0 || OrderMagicNumber()==mn)          
{
if (t<OrderOpenTime()) t=OrderOpenTime();
}
}
}
}
}
}
return(iBarShift(sy, tf, t, True));
}
//-------------------------------------------------------------------+

int NumberOfPositions(string sy="", int op=-1, int mn=-1) 
{
int i, k=OrdersTotal(), kp=0;
if (sy=="0") sy=Symbol();
for (i=0; i<k; i++)                                    
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))      
{
if (OrderSymbol()==sy || sy=="")                   
{
if (OrderType()==OP_BUY || OrderType()==OP_SELL) 
{
if (op<0 || OrderType()==op)                   
{
if (mn<0 || OrderMagicNumber()==mn) kp++;
}
}
}
}
}
return(kp);
}
//-------------------------------------------------------------------+

double LotsCounting() 
{ 
double lots=Lots;
if (MoneyManagement)      
{
double lotsize=MarketInfo(Symbol(),MODE_LOTSIZE);
double freemargin=AccountFreeMargin();
lots=0; if (lotsize>0) lots=NormalizeDouble((MarginPercent*freemargin/lotsize),1);
Comment( MarginPercent*freemargin/lotsize ); 
}
if (lots>4.9) lots=5; if (lots<0.1) lots=0.1;return (lots);   
}   
//-------------------------------------------------------------------+



