avatar
Untitled

dungnguyen552020 16 12th May, 2020

MARKUP 16.75 KB
                                           
                         //+------------------------------------------------------------------+
//|                                                       baiso1.mq4 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
// NOI KHAI BAO BIEN


       double khoiLuong;

        string Com   ;
      color maucualenh = clrRed;
       int loaiLenh = OP_BUY;
      double giavaolenh=0;
      bool chophepGD = true;
      int  Magic =999;
       double balance;// acc


     extern double stoploss = 20;
     extern  double takeprofit = 50;

     extern double stoplossEquity =  0;
     extern double takeprofitEquity = 0;

     extern double khoangcachBANDtoida = 30;

     extern double startLOT= 0.01;// lot bat dau
     extern double risk = 5;
     //buy
     int dembuy;double lowbuy,lastbuylot;
     // sell
     int demsell;double hightsell , lastselllot;

    // extern double khoangcach = 10;
  string pair[] ;
 string sym;
 int thutu;
 bool  active = false;
 datetime thoigiangiaodich;

//+------------------------------------------------------------------+
void OnInit()
  {
      sym=Symbol();
      active = true;
  }
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
  }
//+------------------------------------------------------------------+
void OnTick()
  {
      if(active==false){return;}
         double lotsize;
   //========== SL TP EQUITY
  /* if(stoplossEquity >0 && AccountEquity() < stoplossEquity )
   {dongLenh_ToanBo();active=false;return;}

   if(takeprofitEquity >0 && AccountEquity() > takeprofitEquity )
   {dongLenh_ToanBo();active=false;return;}*/

   if
   (
         (stoplossEquity >0 && AccountEquity() < stoplossEquity )
      || (takeprofitEquity >0 && AccountEquity() > takeprofitEquity )
   )
   {dongLenh_ToanBo();active=false;return;}

   /*if(A){ 123}
   if(B){ 123}
   if(A || B ){123}
   if((A1 && A2 ) || B ){123}*/
   //===========================================
   checkOnline();// --> dembuy ,demsell
  // checkTP_SL();
   //TrailingSL();

   double will1 = iWPR(Symbol(),0,14,1);
   double will2 = iWPR(Symbol(),0,14,2);
   double bandtren = iBands(Symbol(),0,20,2,0,PRICE_CLOSE,MODE_UPPER,1);
   double bandduoi = iBands(Symbol(),0,20,2,0,PRICE_CLOSE,MODE_LOWER,1);
   double kcband = MathAbs(bandduoi - bandtren );

      Comment(AccountEquity());

   if(will2 < -90 && will1 > -90 && kcband <khoangcachBANDtoida*10*Point && dembuy==0)
   {
    if(risk ==0 ){lotsize = startLOT;}
    if(risk > 0 ){lotsize = get_lot_from_money_point(Symbol(),AccountEquity(),risk,stoploss*10*Point) ; }
      //Comment(stoploss*10*Point + "/"+ lotsize + "    "+risk );
     vaoLenh(Symbol(),OP_BUY,lotsize,0,stoploss,takeprofit,Magic,Com);
   }

   if(will2 > -5 && will1 < -5 && kcband <khoangcachBANDtoida*10*Point && demsell==0)
   {
    if(risk ==0 ){lotsize = startLOT;}
    if(risk > 0 ){lotsize = get_lot_from_money_point(Symbol(),AccountEquity(),risk,stoploss*10*Point) ; }

     vaoLenh(Symbol(),OP_SELL,lotsize,0,stoploss,takeprofit,Magic,Com);
   }

  }// end ontick
  //===================================================
void   checkOnline()
   {
    lowbuy = 1000000;
    hightsell = 0;
  //ArrayInitialize(dem,0);
  dembuy=0;demsell =0;

     for(int i = OrdersTotal()-1 ; i>=0; i--)
         {
             if(OrderSelect(i, SELECT_BY_POS,MODE_TRADES)==False)  {continue;}
             if(OrderMagicNumber() != Magic){continue;}
             if(OrderSymbol()!= sym){continue;}
           //  Print(i+"/"+OrderTicket());
             //=============== dem so lenh buy sell
             if(OrderType()==OP_BUY)
             {dembuy++;
              if(OrderOpenPrice() < lowbuy )
                {
                      lowbuy  = OrderOpenPrice();
                     lastbuylot= OrderLots();
                }

             }//0,1 // dem++; <==> dem = dem+1;
             if(OrderType()==OP_SELL)
             {demsell++;
               if(OrderOpenPrice() > hightsell )
                {
                      hightsell = OrderOpenPrice();
                     lastselllot = OrderLots();
                }

             }//0,1 // dem++; <==> dem = dem+1;

          }  //end for


   }
   //========================================
double get_lot_from_money_point(string symm,double balancee,double risk,double slPoint)
  {
      double lotsize ;
      double   sotien = MathAbs(risk/100*balancee);  // 10000 * 5 /100
      double tickval = MarketInfo(symm , MODE_TICKVALUE);
      double ticksize = MarketInfo(symm , MODE_TICKSIZE);
      //========cong thuc chinh
    if(slPoint!=0 && ticksize !=0 && tickval !=0 )
    {
     lotsize = sotien /(slPoint*tickval/ticksize);
    }
    //Comment(sotien+ "/"+lotsize + "/"+balance );
      // =========== 0.2568746655
    double lotstep = MarketInfo(symm,MODE_LOTSTEP);//Comment(lotstep);
    int step;
    if(lotstep==1){step = 0;}
    if(lotstep == 0.01){step =2;}
    if(lotstep == 0.001){step =3;}
    if(lotstep == 0.1 ){step =1;}
    lotsize = NormalizeDouble(lotsize,step);
    if (lotsize < MarketInfo(symm, MODE_MINLOT))            lotsize = MarketInfo(symm, MODE_MINLOT);
    if (lotsize > MarketInfo(symm, MODE_MAXLOT))            lotsize = MarketInfo(symm, MODE_MAXLOT);

      return(lotsize);
  }
//-------------
bool Check_inside(double gia1,double line1,double line2)
  {
   if((line1<=gia1 && gia1<=line2) || (line2<=gia1 && gia1<=line1)){return(true);}
   else {return(false);}
  }
//=====================
void dongLenh_ToanBo()
  {
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) {continue;}
       if (OrderSymbol()!=sym)                       {continue;}
       if (OrderMagicNumber()!=Magic)                {continue;}
      if(OrderType()<2)  {dongLenh_Ticket(OrderTicket(), OrderLots());}
      if(OrderType()>=2) {int rec = OrderDelete(OrderTicket(),CLR_NONE);}
     }
  }
//+------------------------------------------------------------------+
void dongLenh_Ticket(int ticket,double lot)
  {
   int Slippage=3;
   if(!OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) {return;}
   double price; RefreshRates();
   int dem;
   while(OrderCloseTime()==0 && dem<100)// chua dong dc lenh
     { dem++;
    // OrderType()%2==0
   // OrderType()==OP_BUY
      if(MathMod(OrderType(),2)==0) {price=MarketInfo(OrderSymbol(),MODE_BID);}//buy
      else {price=MarketInfo(OrderSymbol(),MODE_ASK);}// sell
     // if(true){.....}
      if(OrderClose(ticket,lot,price,Slippage,Gold)) {break;}// thoat vong lap}
      if(!OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) {return;}
      Sleep(100); // Tam dung 0.1 s
     }
   return;
  }
//+------------------------------------------------------------------+
//-----------
void dongLenh_theo_danhdau(string  danhdauc)// "lenh gap thep"
  { //string tex = IntegerToString(num);
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) {continue;}
      // if (OrderSymbol()!=sym)                       {continue;}
      //  if (OrderMagicNumber()==Ma) {    if (OrderType()>=2) {int rec = OrderDelete(OrderTicket(),CLR_NONE);} continue;      }
  //    group=StringToInteger(StringSubstr(OrderMagicNumber(),StringLen(OrderMagicNumber())-1,1));//   Print("magicnumber   "+OrderMagicNumber());    //   Print("group "+ group);
      if(StringFind(OrderComment(),danhdauc,0)>=0 && OrderType()<2) {dongLenh_Ticket(OrderTicket(),OrderLots());}
     }

  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void dongLenh_theo_type(int op1,int op2)// OP_BUY , OP_BUYSTOP
  {
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) {continue;}
      if(OrderSymbol()!=sym) {continue;}
      if (OrderMagicNumber()!=Magic)                     {continue;}
      if(OrderType()==op1)  {dongLenh_Ticket(OrderTicket(), OrderLots());}
      if(OrderType()==op2)  { int res =OrderDelete(OrderTicket(),CLR_NONE);}
     }
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
/*
double lenhcuoicung(string symm)
{  double lastprofit;       datetime lasttime ;

   for(int i =0; i <= OrdersHistoryTotal()-1; i++)
   {
    if(OrderSelect(i, SELECT_BY_POS,MODE_HISTORY)==False)  {continue;}
    if(OrderSymbol() != symm){continue;}
    if(OrderMagicNumber()!= Magic){continue;}
    if(OrderType()>1 ){continue;}

    if(OrderCloseTime()> lasttime  )
         {
         lasttime = OrderCloseTime(); // 6h
         lastprofit = OrderProfit();
         lastLotsize = OrderLots();
         lasttype = OrderType();
         }
   }

return(lastprofit);
}*/
//======================================================================
void   vaoLenh(string symm, int typee, double lott, double pricee, double slpip,double tppip,int magicc, string comm )
{
   if(lott ==0){return;}
   int normallotunit  ;
   if(MarketInfo(symm, MODE_MINLOT)== 0.01){normallotunit = 2;}
   if(MarketInfo(symm, MODE_MINLOT)== 0.1){normallotunit = 1;}
   if(MarketInfo(symm, MODE_MINLOT)== 0.001){normallotunit = 3;}
   lott = NormalizeDouble(lott, normallotunit );
   //---------------------------
   double slprice, tpprice; color mau;
   if(typee== OP_BUY)
       {
         pricee = MarketInfo(symm,MODE_ASK);
         slprice = pricee - slpip*10*MarketInfo(symm ,MODE_POINT);
         tpprice = pricee + tppip*10*MarketInfo(symm ,MODE_POINT);
         mau = clrBlue;
       }

   if(typee== OP_SELL)
       {
         pricee = MarketInfo(symm,MODE_BID);
         slprice = pricee + slpip*10*MarketInfo(symm ,MODE_POINT);
         tpprice = pricee - tppip*10*MarketInfo(symm ,MODE_POINT);
         mau = clrRed;
       }
   pricee = NormalizeDouble(pricee,MarketInfo(symm , MODE_DIGITS));
   slprice = NormalizeDouble(slprice,MarketInfo(symm , MODE_DIGITS));
   tpprice = NormalizeDouble(tpprice,MarketInfo(symm , MODE_DIGITS));
  //-----gui lenh
   double thanhcong = OrderSend(symm,typee,lott,pricee,20,0,0,comm,magicc,0,mau);

  // ----- CHINH SL TP
  bool sucess =false; int dem;
  if(thanhcong >0 && slprice !=0 && tpprice!=0 )
      {
         while ( sucess == false && dem<20)
         {  sucess =  OrderModify(thanhcong,pricee,slprice,tpprice,0,clrNONE);
          dem++; Sleep(50);
         }
      int error = GetLastError();
      if(error !=0 && error !=1){ Print("bi loi: "+ error);}
      }
  /*
  ECN  - LIEN NGAN HANG
  STP - LIEN NGAN HANG
  1 , GUI LENH DI
  2 , CHINH SUA SL TP

  OM LENH - OM LENH */

}
//====================================================================
/*
void laygiatrinen()
{

  // tim  mo hinh enguffing

  for(int n =500 ; n >=0; n--)
  {
      double highttruoc, lowtruoc , hightsau, lowsau ;
     highttruoc = iHigh(Symbol(),0,n+1);
     lowtruoc  = iLow(Symbol(),0,n+1);

     hightsau = iHigh(Symbol(),0,n);
     lowsau  = iLow(Symbol(),0,n);

     if(highttruoc<hightsau && lowtruoc > lowsau)
     {
         Comment(n);// gan nhat cay nen so 0
         //break;
     }

  }

  */

  /*int caynencaonhat,caynenthapnhat;
  caynencaonhat= iHighest(Symbol(),0,MODE_HIGH,100,0);// tra ve cay nen cao nhat
  caynenthapnhat = iLowest(Symbol(),0,MODE_LOW,100,0);// tra ve cay nen thap nhat

  double giacaonhat = iHigh(Symbol(),0,caynencaonhat);//  lay gias hight cua ....
  double giathapnhat = iLow(Symbol(),0,caynenthapnhat);// lay low ....

  // Comment(caynencaonhat +" / "+ caynenthapnhat);

  Comment(giacaonhat +" / "+ giathapnhat);*/





//+==================================================================+
int  Order_Open(int ordType,string sym_,double lots,double price,double sl,double tp,int mag,string com,double bidask)
  {// int ticket;
  if(lots==0){return(0);}
   color col; double Stoploss,TakeProfit;
   double unit=1;   if(bidask!=0){unit= bidask;}
   if(ordType==OP_BUY) {price=MarketInfo(sym_,MODE_ASK);Stoploss=price-sl*unit; TakeProfit=price+tp*unit; col=Blue;}
   if(ordType==OP_SELL) {price=MarketInfo(sym_,MODE_BID);Stoploss=price+sl*unit; TakeProfit=price-tp*unit; col=Red;}
   price=NormalizeDouble(price,MarketInfo(sym_,MODE_DIGITS));
   int NormalizeLot;   if(MarketInfo(sym_,MODE_MINLOT)==0.1) {NormalizeLot=1;} else {NormalizeLot=2;}
   lots=NormalizeDouble(lots,NormalizeLot);
   int sucess=-1; int ross=0;int demm;
   sucess=OrderSend(sym_,ordType,lots,price,3,0,0,com,mag,0,col);Sleep(100);
   if(sucess>0 && (sl!=0 || tp!=0))
     {
      while(ross<=0 && demm<20){  ross=OrderModify(sucess,price,Stoploss,TakeProfit,0,clrNONE); demm++;Sleep(100);    }
     }
     int loi = GetLastError();
   if(loi!=0 && loi !=1 ){ Print("eror"+loi);          Print(sym_+ "/price "+ price+ " /op "+ordType+"/lot "+lots);
}
   return(sucess);
  }
//+------------------------------------------
//======================================================
void checkLisicen()
{
  /*datetime x =  iTime(Symbol(),PERIOD_D1,0);// thoi gian dau ngay
  // x=  TimeCurrent();// thoi gian hien tai
   //TimeHour(x);
  // TimeMinute()
 // dau ngay + 3 hour()?
  x= x +3*60*60;
   Comment(x);
   */


  if(
        TimeYear( TimeCurrent())<=2020
     && TimeMonth( TimeCurrent())<=4
    ){active= true;}
  // --> file ex4 */

  //ham chuyen doi
  /*datetime x = StrToTime(giobatdau);
  string a = "06";
  string b = "30";
  string c= a+ ":"+b;

 datetime d =  StrToTime(c)+60;
  Comment(d);
  StrToInteger*/
/*  string commentt = "| 5 (6)lenhdautien";
  int vitri=  StringFind(commentt,"(",0);
  string cat = StringSubstr(commentt,vitri+1,1);
  Comment(cat);*/
  //Str
}
/*
//==========================================
int demsolenh(string captiencandem)
{
   int dem;
   for(int i = OrdersTotal()-1 ; i>=0; i--)
   {
    if(OrderSelect(i, SELECT_BY_POS,MODE_TRADES)==False)  {continue;}
    if(OrderSymbol() != captiencandem){continue;}
    if(OrderMagicNumber()!= Magic){continue;}
        dem ++;// dem dc 1 lenh
   }

return(dem);
}*/
//====================================
//====================================
double dinhdangLot(double khoiLuong)
{
   if(khoiLuong==0){ khoiLuong = MarketInfo(Symbol(),MODE_MINLOT)   ; }
   if(khoiLuong> MarketInfo(Symbol(),MODE_MAXLOT)){ khoiLuong = MarketInfo(Symbol(),MODE_MAXLOT)   ; }

    khoiLuong = NormalizeDouble(khoiLuong,2);

     return(khoiLuong);// tra ve khoi da duoc dinh dang

}
//+==================================================================+
void napchuoiPair(string chuoiPair1 , string phancach) // "/"  ","
{
   //extern string chuoiPair = "/EURUSD/GBPUSD/USDJPY/EURJPY/";
  if(StringLen(chuoiPair1)<6){ArrayResize(pair,1);pair[0]=Symbol();return;}
 string ktra;

 int sta; int sto;string text ;
 chuoiPair1  = phancach+chuoiPair1+phancach;
 for(int i=0;i < 1000 ;i++)
   {
      sta = StringFind(chuoiPair1,phancach,sto);//1 //6
      sto = StringFind(chuoiPair1,phancach,sta+1);//6  // 12
      if(sta+1 >= StringLen(chuoiPair1)){break;}

   text = StringSubstr(chuoiPair1 ,sta +1,sto - sta -1);
    ArrayResize(pair,i+1,0);
   pair[i] = text;
   ktra  += text + ",";
   }
Print( "cac cap tien la: " +ktra);
}
                      
                                       
To share this paste please copy this url and send to your friends
RAW Paste Data

Comments

Authentication required

You must log in to post a comment.

Log in
    There are no comments yet.