Navigation:Home > Content >

PARA_VIRT2atr.mq4

Time: 2018-02-19 | Download file:PARA_VIRT2atr.mq4

extern double Lot = 1;
extern double DDep =0;
extern double MinLots = 0.01;						
extern double MaxLots = 1000.0;	    			
extern double TakeProfit = 170;
extern int PipCommis = 6;
extern int SL = 30;
extern int TP = 110;
extern int Level =10;//дистанция отложных ордеров
extern int Expiration =1;// время экспирации в сек.
extern double TrailingStop = 0;//обычный трейлинг
extern double TrailingStep = 1;
extern double GrafTrailingStop = 0.33;//виртуальный ,графический трейлинг. ри менее1 - в доле от профита . при более1 - как обычный трал (в пипах)
extern int GrafStopLoss = 30;
extern int U = 6;// уровен начала граф. трейлинга ( при GrafTrailingStop менее 1 )
extern int MaxSread = 10;
extern bool DelGraf =false;//откл. удалений линий для быстроты тестирования в тестере.
extern bool Clos =false;
extern bool Delete = true;
extern bool StopOrders =false;
extern bool LevelOrders =true;
extern bool ATR =false;
extern double Uatr =0.0003;
extern int Patr = 3;
extern int TFatr =0;
extern int ShiftATR =0;// бар АТР
extern string comment = "PARA";
extern int magic=333333;
extern color c1 = Lime;
extern color c2 = Red;
extern color c3 = Gold;
extern color c4 = MediumVioletRed;
double bt=0;
double st=0;
int start()
  {
   double W,W1,W2,W3,V,V1,Lots;
   double pf=0,pt=0;
   int cnt, ticket, total,b=0,s=0,bs=0,ss=0,no=0,nt=0,uo=0,z=0,zb=0,zs=0,db=0,ds=0,pb=0,ps=0,pz=0;

   if(GrafTrailingStop>0)GT();

  for(int q = OrdersTotal(); q >= 0; q--)
  {    
    if(OrderSelect(q, SELECT_BY_POS, MODE_TRADES))
    {
    if(OrderSymbol()==Symbol()&&OrderMagicNumber()==magic)
    { 
    
    if(OrderType()==OP_BUY)b=1;
    if(OrderType()==OP_SELL)s=1;
    if(OrderType()==OP_BUY&&OrderStopLoss()>OrderOpenPrice())pb=1;
    if(OrderType()==OP_SELL&&OrderStopLoss()0)  
              {                 

               if(Bid-OrderOpenPrice()>Point*TrailingStop)
                 {
                  if(OrderStopLoss()<(Bid-Point*TrailingStop)&&Bid-Point*TrailingStop>OrderOpenPrice()&&Bid-Point*TrailingStop>OrderOpenPrice()+PipCommis*Point)
                    {
                     if(Bid-Point*TrailingStop>=OrderStopLoss()+Point*TrailingStep)OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
                    }
                 }
              }
    if(OrderType()==OP_SELL&&TrailingStop>0)  
              {                 
               if(OrderOpenPrice()-Ask>Point*TrailingStop)
                 {
                  if(OrderStopLoss()>(Ask+Point*TrailingStop)&&Ask+Point*TrailingStop 0.0)
       {
         double li_140 = AccountFreeMargin() / MarketInfo(Symbol(), MODE_MARGINREQUIRED) * DDep / lotstep;
         Lots = li_140 * lotstep;
         if ( Lots > MaxLots ) Lots = MaxLots;
         if ( Lots < MinLots)Lots = MinLots;
      }
      if (DDep == 0.0)Lots = Lot;
      if ( Lots > MarketInfo(Symbol(),MODE_MAXLOT)) Lots = MarketInfo(Symbol(),MODE_MAXLOT);
      if ( Lots < MarketInfo(Symbol(),MODE_MINLOT))Lots = MarketInfo(Symbol(),MODE_MINLOT);
  
  
   
      if(StopOrders == true&&Ask-BidUatr)||ATR==false))
      {
      if(bs==0)OrderSend(Symbol(),OP_BUYSTOP,Lots,Ask+Level*Point,5,Ask+Level*Point-SL*Point-(Ask-Bid),Ask+Level*Point+TP*Point,comment,magic,0);//,Green);
      if(ss==0)OrderSend(Symbol(),OP_SELLSTOP,Lots,Bid-Level*Point,5,Bid-Level*Point+SL*Point+(Ask-Bid),Bid-Level*Point-TP*Point,comment,magic,0);//,Red);
      }
      if(LevelOrders == true)
      {
      RefreshRates();
      if( ObjectFind("BLIN")==-1)
      {
       ObjectCreate("BLIN",OBJ_HLINE,0,0,Ask+Level*Point);
       ObjectSet("BLIN",OBJPROP_COLOR,Lime);
       bt=TimeCurrent();
       
      }
      RefreshRates();
      if( ObjectFind("SLIN")==-1)
      {
       ObjectCreate("SLIN",OBJ_HLINE,0,0,Bid-Level*Point);
       ObjectSet("SLIN",OBJPROP_COLOR,Red);
       st=TimeCurrent();
      } 
      if(ObjectFind("BLIN")!=-1&&TimeCurrent()>=bt+Expiration)ObjectDelete("BLIN");
      if(ObjectFind("SLIN")!=-1&&TimeCurrent()>=st+Expiration)ObjectDelete("SLIN");
      RefreshRates();
      if(ObjectFind("BLIN")!=-1&&Ask>=ObjectGet("BLIN",1)&&Ask-BidUatr)||ATR==false))
      {
      OrderSend(Symbol(),OP_BUY,Lots,Ask,5,0,0,comment,magic,0,Green);
      ObjectDelete("BLIN");
      bt=0;
      //return(0);
      }
      RefreshRates();
      if(ObjectFind("SLIN")!=-1&&Bid<=ObjectGet("SLIN",1)&&Ask-BidUatr)||ATR==false))
      {
      OrderSend(Symbol(),OP_SELL,Lots,Bid,5,0,0,comment,magic,0,Red);
      ObjectDelete("SLIN");
      st=0;
      //return(0);
      }
      }
     return(0);
  }   
//////////////////////////////////////////////////////////////////////////////////////////////
void GT()
{
//.................................................................... 
  for(int y = OrdersTotal(); y >= 0; y--)
  {    
    if(OrderSelect(y, SELECT_BY_POS, MODE_TRADES))
    {
    if(OrderSymbol() == Symbol()) 
    {
    if(OrderType() == OP_BUY&&OrderMagicNumber() == magic)
    {
 //....................................................................   

//............................................................................................/
  if(GrafTrailingStop>0)
  {
      double SB,SS,TB,TS,sb,ss,tb,ts;
      if(ObjectFind("sb"+DoubleToStr(OrderTicket(),0))==-1)
      {
        ObjectCreate("sb"+DoubleToStr(OrderTicket(),0),OBJ_HLINE,0,0,OrderOpenPrice()-GrafStopLoss*Point-(Ask-Bid));
        ObjectSet("sb"+DoubleToStr(OrderTicket(),0),OBJPROP_COLOR,c2);
      }
      if(ObjectFind("tb"+DoubleToStr(OrderTicket(),0))==-1)
      {
        ObjectCreate("tb"+DoubleToStr(OrderTicket(),0),OBJ_HLINE,0,0,OrderOpenPrice()+TakeProfit*Point);
        ObjectSet("tb"+DoubleToStr(OrderTicket(),0),OBJPROP_COLOR,c1);
      }
      sb = ObjectGet( "sb"+DoubleToStr(OrderTicket(),0), OBJPROP_PRICE1);
      tb = ObjectGet( "tb"+DoubleToStr(OrderTicket(),0), OBJPROP_PRICE1);
      SB = NormalizeDouble(sb,Digits);
      TB = NormalizeDouble(tb,Digits);
      //if (RealStop==true&&OrderStopLoss()!=SB&&ObjectFind("sb"+OrderTicket())!=-1)
      //OrderModify(OrderTicket(),OrderOpenPrice(),SB,OrderTakeProfit(),0,CLR_NONE);
      if((Bid>=tb&&OrderProfit()>0)||Bid<=sb)OrderClose(OrderTicket(),OrderLots(),Bid,5,CLR_NONE);
      if(GrafTrailingStop >= 1)         
          {
            if(Bid - OrderOpenPrice() >= GrafTrailingStop * Point)
            {
              if(sb < (Bid - Point * GrafTrailingStop)&&sb <= (Bid - Point * TrailingStep)&&(Bid - Point * GrafTrailingStop)>=(OrderOpenPrice()+PipCommis* Point))//+pipcomis* Point
              {
                ObjectSet("sb"+DoubleToStr(OrderTicket(),0), OBJPROP_PRICE1, Bid - Point * GrafTrailingStop);
              }
            }
          }
      if(GrafTrailingStop > 0&&GrafTrailingStop < 1)         
          {
            if(Bid - OrderOpenPrice() >= U*Point)
            {
              if(sb < Bid - (Bid - OrderOpenPrice())*GrafTrailingStop&&sb <= (Bid - Point * TrailingStep)&&(Bid - Bid*GrafTrailingStop + OrderOpenPrice()*GrafTrailingStop)>=(OrderOpenPrice()+PipCommis* Point))
              {
                ObjectSet("sb"+DoubleToStr(OrderTicket(),0), OBJPROP_PRICE1, Bid - Bid*GrafTrailingStop + OrderOpenPrice()*GrafTrailingStop );
              }
            }
          }
      //if (RealStop==true&&OrderStopLoss()!=SB&&ObjectFind("sb"+OrderTicket())!=-1)
      //OrderModify(OrderTicket(),OrderOpenPrice(),SB,OrderTakeProfit(),0,CLR_NONE);
     }
//.............................................................................................              
  }
  }
  }
  }
 
//.............................................................................................  
//.............................................................................................
  for(int q = OrdersTotal(); q >= 0; q--)
  {    
    if(OrderSelect(q, SELECT_BY_POS, MODE_TRADES))
    {
    if(OrderSymbol() == Symbol()) 
    {
    if(OrderType() == OP_SELL&&OrderMagicNumber() == magic)
    { 

    //s=1;
//..............................................................................
   
  if(GrafTrailingStop>0)
  {   
      if(ObjectFind("ss"+DoubleToStr(OrderTicket(),0))==-1)
      {
        ObjectCreate("ss"+DoubleToStr(OrderTicket(),0),OBJ_HLINE,0,0,OrderOpenPrice()+GrafStopLoss*Point+(Ask-Bid));
        ObjectSet("ss"+DoubleToStr(OrderTicket(),0),OBJPROP_COLOR,c4);
      }
      if(ObjectFind("ts"+DoubleToStr(OrderTicket(),0))==-1)
      {
        ObjectCreate("ts"+DoubleToStr(OrderTicket(),0),OBJ_HLINE,0,0,OrderOpenPrice()-TakeProfit*Point);
        ObjectSet("ts"+DoubleToStr(OrderTicket(),0),OBJPROP_COLOR,c3);
      }
      ss = ObjectGet( "ss"+DoubleToStr(OrderTicket(),0), OBJPROP_PRICE1);
      ts = ObjectGet( "ts"+DoubleToStr(OrderTicket(),0), OBJPROP_PRICE1);
      SS = NormalizeDouble(ss,Digits);
      TS = NormalizeDouble(ts,Digits);

      if((Ask<=ts&&OrderProfit()>0)||Ask>=ss)OrderClose(OrderTicket(),OrderLots(),Ask,5,CLR_NONE);

        if(GrafTrailingStop>= 1)
        {
          if(OrderOpenPrice() - Ask >=  GrafTrailingStop * Point)
          {            
            if(ss> (Ask + Point* GrafTrailingStop)&&ss >= (Ask + Point* TrailingStep)&&( Ask + Point * GrafTrailingStop)<=(OrderOpenPrice()-PipCommis* Point))
            {           
              ObjectSet("ss"+DoubleToStr(OrderTicket(),0), OBJPROP_PRICE1,Ask + Point * GrafTrailingStop);
            }
          }
        }
        if(GrafTrailingStop> 0&&GrafTrailingStop< 1)
        {
          if(OrderOpenPrice() - Ask >= U*Point)
          {            
            if(ss > Ask + OrderOpenPrice()*GrafTrailingStop-Ask*GrafTrailingStop && ss >= (Ask + Point* TrailingStep)&&(Ask + OrderOpenPrice()*GrafTrailingStop-Ask*GrafTrailingStop)<=(OrderOpenPrice()-PipCommis* Point))
            {           
              ObjectSet("ss"+DoubleToStr(OrderTicket(),0), OBJPROP_PRICE1, Ask + OrderOpenPrice()*GrafTrailingStop-Ask*GrafTrailingStop);
            }
          }
        }
      //if (RealStop==true&&OrderStopLoss()!=SS&&ObjectFind("ss"+OrderTicket())!=-1)
      //OrderModify(OrderTicket(),OrderOpenPrice(),SS,OrderTakeProfit(),0,CLR_NONE);   
    }
//.............................................................................................  
  }
  }
  }
  }
//---------------------------------------------------------------------------------------------
if(!IsTesting()||DelGraf==true )
{
 for(int x=OrdersHistoryTotal();x>=0;x--){
  if(OrderSelect(x,SELECT_BY_POS,MODE_HISTORY)){
    if(OrderSymbol()==Symbol()){
      if(OrderCloseTime()!=0)
      { 
      if(ObjectFind("tb"+DoubleToStr(OrderTicket(),0))!=-1)ObjectDelete("tb"+DoubleToStr(OrderTicket(),0));
      if(ObjectFind("sb"+DoubleToStr(OrderTicket(),0))!=-1)ObjectDelete("sb"+DoubleToStr(OrderTicket(),0));
      if(ObjectFind("ts"+DoubleToStr(OrderTicket(),0))!=-1)ObjectDelete("ts"+DoubleToStr(OrderTicket(),0));
      if(ObjectFind("ss"+DoubleToStr(OrderTicket(),0))!=-1)ObjectDelete("ss"+DoubleToStr(OrderTicket(),0));
      }
      }
      }
      }
  }  
//................................................................................	
//////////////////////////////////////////////////////////////////////////////////////////////  

   return;
  }
  //----------------  Функция -----------------------------------------
int Ord_Tot_Sym()
{
int O_T=0;
      for(int rr=0;rr0)
      {
   O_T++;    
      }}
      if (O_T>0) return(1);
      else return(0);
}
//----------- CloseByALL ---------------------------
int CloseByALL()
{
int ib,is;
double lb=0,ls=0;
int   tb=0,ts=0,lbb=0;
 if  (Ord_Tot_Sym()>0) 
 {
 RefreshRates();
                  for(ib=OrdersTotal()-1;ib>=0;ib--)
                   {
                  if(OrderSelect(ib,SELECT_BY_POS,MODE_TRADES)==false) continue;
                  if (OrdersTotal()>0)
                  {
                     if (OrderType()==OP_BUY)
                     { 
                     lb=OrderLots();
                     tb=OrderTicket();
                  
                     //-- начнём поиск позиции SELL   
                        for(is=OrdersTotal()-1;is>=0;is--)
                        {
                        if(OrderSelect(is,SELECT_BY_POS,MODE_TRADES)==false) continue;
                        if (OrdersTotal()>0)
                        {
                        if (OrderType()==OP_SELL)
                        {
                        ls=OrderLots();
                        ts=OrderTicket();
                        
                        if (lb<=ls)  
                          {
                          RefreshRates();
                          OrderCloseBy(tb,ts,White);
                          break;
                          }
                        }
                        }}}
                     if (OrderType()==OP_SELL) 
                     {  
                     ls=OrderLots();
                     ts=OrderTicket();
                     
                     //-- начнём поиск позиции BUY   
                        for(lbb=OrdersTotal()-1;lbb>=0;lbb--)
                        {
                        if(OrderSelect(lbb,SELECT_BY_POS,MODE_TRADES)==false) continue;
                        if (OrdersTotal()>0)
                        {
                        if (OrderType()==OP_BUY)
                        {
                        lb=OrderLots();
                        tb=OrderTicket();
                        
                        if (ls<=lb) 
                            {
                            RefreshRates();
                            OrderCloseBy(ts,tb,White);
                            break;
                            }
                        }}}                 
                        }}}}                 
}
//-------------- CloseAll --------------------------   
int CloseALL()
{
int ic;
CloseByALL();
 while (Ord_Tot_Sym()>0) 
 {
 RefreshRates();
                  for(ic=OrdersTotal()-1;ic>=0;ic--)
                   {
                  if(OrderSelect(ic,SELECT_BY_POS,MODE_TRADES)==false) continue;
                  if (OrdersTotal()>0)
                  {
                  if (OrderType()==OP_BUY)
                  {
                  //RefreshRates();
                  OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),0,CLR_NONE);
                  if (IsTradeAllowed()) continue;
                  else Sleep(1000);
                  }
                                    
                  if (OrderType()==OP_SELL)
                  {
                  //RefreshRates();
                  OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),0,CLR_NONE);
                  if (IsTradeAllowed()) continue;
                  else Sleep(1000);
                  }}}
//--------- Закрыли все позиции теперь закроем все ордера ------------------------                    
                  for(ic=OrdersTotal()-1;ic>=0;ic--)
                   {
                  if(OrderSelect(ic,SELECT_BY_POS,MODE_TRADES)==false) continue;
                  if (OrdersTotal()>0)
                  {
                  if (OrderType()==OP_BUYSTOP || OrderType()==OP_SELLSTOP ||
                     OrderType()==OP_BUYLIMIT || OrderType()==OP_SELLLIMIT)
                  OrderDelete(OrderTicket());
                  }}
      //PlaySound("news.wav" );
      }
   return(0);
  }

Recommend