Navigation:Home > Content >

A_XM3Limit.mq4

Time: 2013-09-03 | Download file:A_XM3Limit.mq4

#include 
#include 
extern double  POINT =1000;//50...100...
extern double  TP       =  0;
extern double  StopLoss       =  30;  //начальный стоплосс 
extern bool  modif = false;
extern bool  OrderModif = true;
extern int Volumes          =50;
extern int Expiration       = 5;
extern int ExpirationOrders    = 5;
extern double  TrSt           =  10; //Трал
extern double  TrStep         =1;
extern double MinProfit       =8;//мин. профит модификации стоплоса
extern int TF2 =5;
extern int Dist = 15;//дистанция выставления ордера
extern int DistOrders = 3;
extern int DistModif =0;
extern double Lot = 0.01;
extern double MaxLot = 30;
extern bool  FaktorProfit = false;
extern int NormalizeLot =2;//нормализация порядка знаков после зпт. в значении лота 1 - 0.1 ....2 - 0.01
extern bool   LotOptimized = false;//включение лототрона в доле от свободных средств
extern double    Risk = 0.2;//размер доли свободных средств
extern int    max_spread =15;//макс. значение разрешённого спреда в пип
extern bool  Sound = false;

extern int Magic = 64;
extern string com = "Limit";

//int            cnt,i;
double         lots=0,p;

int ptrend=0;
double TU =0;
double TD =0;

double TK=0;
double TK1=0;
double TK2=0;
double tb=0,ts=0,ask=0,bid=0;

int init()
{
   

   return(0);
}

int deinit()
{
ObjectDelete("U");
ObjectDelete("U1");
   return(0);
}



int start()
{   
int bo=0,so=0,cb=0,cs=0,no=0,zb=0,zs=0,sell=0,buy=0,db=0,ds=0,tiket =0;
double sl=0,tp =0;
double tik = (Ask+Bid)/2;
if(modif==true)
{
if(ObjectFind("s1")!=-1&&ObjectGet("s1",1)Ask+Dist*Point)ObjectSet("b1",1,Ask+Dist*Point); 
}
   int kp=0,kl=0,dob=0,dos=0;
   double pr,l;
   if(!IsTesting())
   {
   for(int e=OrdersHistoryTotal();e>=0;e--)
   {
   if(OrderSelect(e,SELECT_BY_POS,MODE_HISTORY))
   {
   if(OrderSymbol()==Symbol()&&OrderMagicNumber()==Magic)
   {
   if(OrderCloseTime()>0)
   {

   if(OrderProfit()>0){pr+=OrderProfit()+OrderCommission();kp++;}
   if(OrderProfit()<0){l+=OrderProfit()+OrderCommission();kl++;}
   }}}}   
   }
   for (int q = OrdersTotal(); q >=0; q--)   
   {
      OrderSelect(q, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol()&&OrderMagicNumber()==Magic)
      {
       if(OrderType()==OP_BUYLIMIT)zb=1;
       if(OrderType()==OP_SELLLIMIT)zs=1;  
       if(OrderType()==OP_BUY&&(FaktorProfit==false||(FaktorProfit==true&&OrderProfit()<0)))zb=1;
       if(OrderType()==OP_SELL&&(FaktorProfit==false||(FaktorProfit==true&&OrderProfit()<0)))zs=1;  
       if(OrderType()==OP_BUY&&OrderStopLoss()>OrderOpenPrice())cs=1;
       if(OrderType()==OP_SELL&&OrderStopLoss()DistOrders*Point+DistModif*Point&&OrderComment()!="dop")OrderModify(OrderTicket(),Bid-DistOrders*Point,0,0,0,CLR_NONE);
       if(OrderModif ==true&&OrderType()==OP_SELLLIMIT&&OrderOpenPrice()-Ask>DistOrders*Point+DistModif*Point&&OrderComment()!="dop")OrderModify(OrderTicket(),Ask+DistOrders*Point,0,0,0,CLR_NONE);       
                    
       if(OrderType()==OP_SELLLIMIT||OrderType()==OP_BUYLIMIT&&TimeCurrent()-OrderOpenTime()>ExpirationOrders)OrderDelete(OrderTicket());
       //if(OrderType()==OP_SELLLIMIT&&tik0&&TK1>0&&TK2>0)OrderDelete(OrderTicket());
       //if(OrderType()==OP_BUYLIMIT&&tik>TK&&TK>TK1&&TK1>TK2&&TK>0&&TK1>0&&TK2>0)OrderDelete(OrderTicket());
      }
   }
//----

lots=NormalizeDouble(GetLotSize(),NormalizeLot);
if(lots>MaxLot)lots=MaxLot;
       
if(tb>0&&TimeCurrent()-tb>Expiration)ObjectDelete("b1");
if(ts>0&&TimeCurrent()-ts>Expiration)ObjectDelete("s1");

if(zb==0&&ask>0&&ObjectFind("b1")!=-1&&ask=ObjectGet("b1",1))
 {
 tiket=OrderSend(Symbol(),OP_BUYLIMIT,lots,Bid-DistOrders*Point,0,0,0,com, Magic, 0, Green);
 ObjectDelete("b1");
 tb=0;
 if (Sound&&tiket>0)PlaySound("stops.wav" ); 
 }     
 if(zs==0&&bid>0&&ObjectFind("s1")!=-1&&bid>ObjectGet("s1",1)&&Bid<=ObjectGet("s1",1))
 {
 tiket=OrderSend(Symbol(),OP_SELLLIMIT,lots,Ask+DistOrders*Point,0,0,0,com, Magic, 0, Red);
 ObjectDelete("s1");
 ts=0;
 if (Sound&&tiket>0)PlaySound("stops.wav" ); 
 }
       
       
        if(MathAbs(Close[0]-Open[0])Open[1]+20*Point)&&Volume[1]>Volumes)
        {           
            
                   
            if(Close[0]Open[0])
            {
            {ObjectCreate("s1",1,0,0,Bid-Dist*Point);ObjectSet("s1",6,Yellow);ObjectSet("s1",7,1);ts=TimeCurrent();} 
            }
            if(Close[0]>iOpen(Symbol(),TF2,0)&&Close[0]0)PlaySound("stops.wav" );
            
            
         }     
 
    bid=Bid;
    ask=Ask;  
//---------------------------------------------------------------------------------------------------
        for (int n = OrdersTotal(); n >=0; n--)   
   {
      OrderSelect(n, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol()&&OrderMagicNumber()==Magic)
      {
       if(OrderType()==OP_BUYSTOP&&cb==1)OrderDelete(OrderTicket());
       if(OrderType()==OP_SELLSTOP&&cs==1)OrderDelete(OrderTicket());

      }
    } 
     
      for (int m = OrdersTotal(); m >=0; m--)   
      {
      OrderSelect(m, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol()&&OrderMagicNumber()==Magic)
      {    

      
      if (OrderType() == OP_BUY)
      {
         if(StopLoss>0&&Ask<=OrderOpenPrice()-StopLoss*Point)OrderClose(OrderTicket(),OrderLots(),Bid,0,CLR_NONE);
         if(StopLoss>0&&OrderStopLoss()==0)
         {
           OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid - StopLoss*Point,Digits),OrderTakeProfit(), 0, CLR_NONE);
         }
         if(TP>0&&OrderTakeProfit()==0)
         {
            OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),Ask+TP*Point, 0, CLR_NONE);
         }
         if (TrSt>0)//&&TimeCurrent()-OrderOpenTime()>30)
         {
            if (OrderStopLoss()==0||OrderStopLoss()=OrderOpenPrice()+MinProfit*Point&&Bid - TrSt*Point>=OrderStopLoss()+TrStep*Point||OrderStopLoss()==0)
            {
              OrderModify(OrderTicket(),OrderOpenPrice(),Bid - TrSt*Point,OrderTakeProfit(), 0, CLR_NONE);
            }
         }        
      }
      if (OrderType() == OP_SELL)//&&(OrderStopLoss()==0||OrderStopLoss()>OrderOpenPrice()||Volume[0]<3)
      {   
         if(StopLoss>0&&Bid>=OrderOpenPrice()+StopLoss*Point)OrderClose(OrderTicket(),OrderLots(),Ask,0,CLR_NONE);
         if(StopLoss>0&&OrderStopLoss()==0)
         {
           OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Ask + StopLoss*Point,Digits),OrderTakeProfit(), 0, CLR_NONE);
         }  
         if(TP>0&&OrderTakeProfit()==0)
         {
            OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),Bid-TP*Point, 0, CLR_NONE);
         }   
         if (TrSt>0)//&&TimeCurrent()-OrderOpenTime()>30)
         {
           if (OrderStopLoss()==0||OrderStopLoss() > Ask +(TrSt)*Point&&Ask + TrSt*Point<=OrderOpenPrice()-MinProfit*Point&&Ask + TrSt*Point<=OrderStopLoss()-TrStep*Point||OrderStopLoss()==0) 
           {
              OrderModify(OrderTicket(),OrderOpenPrice(),Ask + TrSt*Point,OrderTakeProfit(), 0, CLR_NONE);
           }
         }            
      }
   }
   }
     
    ObjectCreate("U1", OBJ_LABEL, 0, 0, 0, 0, 0);
    ObjectSet("U1", OBJPROP_CORNER, 2);
    ObjectSet("U1", OBJPROP_YDISTANCE, 45);
    ObjectSet("U1", OBJPROP_XDISTANCE, 10);
    color col = Yellow;
    if(pr+l>0)col=Lime;
    if(pr+l<0)col=Red; 
    ObjectSetText("U1", "Profit " + DoubleToStr(kp,0)+"/"+DoubleToStr(pr,2)+"  Loss " + DoubleToStr(kl,0)+"/"+DoubleToStr(l,2)+"  All " + DoubleToStr(kp+kl,0)+"/" +DoubleToStr(pr+l,2),12, "Tahoma",col ); 
    
    ObjectCreate("U", OBJ_LABEL, 0, 0, 0, 0, 0);
    ObjectSet("U", OBJPROP_CORNER, 2);
    ObjectSet("U", OBJPROP_YDISTANCE, 65);
    ObjectSet("U", OBJPROP_XDISTANCE, 10);
    color col2 = Yellow;
    if((Ask-Bid)/Pointmax_spread)col2=Red;
    ObjectSetText("U", "Spread " + DoubleToStr((Ask-Bid)/Point,0)+"  Magic "+ DoubleToStr(Magic,0)+"  V "+ DoubleToStr(Volume[1],0),12, "Tahoma",col2 ); 
   TK2=TK1;
   TK1=TK;
   TK=(Ask+Bid)/2;
  
   return(0);
}
double GetLotSize()
{
if(LotOptimized == TRUE){
double Loty;
//........................................................................
        int m;
        double MG;
        if(Lot>0)
        {
        if(AccountFreeMargin()>AccountMargin())MG=AccountFreeMargin();       
        double Min_Lot = MarketInfo(Symbol(), MODE_LOTSTEP);             
        if(Risk>0)
        {
          m=MG/MarketInfo (Symbol(), MODE_MARGINREQUIRED)*Risk/Min_Lot;
          Loty = m*Min_Lot;
        if(Loty < MarketInfo(Symbol(), MODE_MINLOT))
        Loty =MarketInfo(Symbol(), MODE_MINLOT);            
        if(Loty > MarketInfo (Symbol(), MODE_MAXLOT))
        Loty = MarketInfo (Symbol(), MODE_MAXLOT);                  
        }
        }
        if(Risk==0)Loty= Lot;

//......................................................................................, 
}
else
{
Loty=Lot;
}
   return (Loty);
}


Recommend