Navigation´╝ÜHome > Content >

EA_EmaLevel_v1.0.mq4

Time: 2018-12-06 | Download file:EA_EmaLevel_v1.0.mq4

#property copyright "micscal"
#property link      ""

extern string Name_Expert = "EA EmaLevel";
int MaxTrade=1;
extern bool SendEmail=true;
extern double StartLot=0.01;
extern double IncreaseLot=0.01;
extern int Slippage = 3;
extern string text1="Ma Level";
extern int MAPeriod=200;
extern double LevelSell1=380;
extern double LevelSell2=500;
extern double LevelSell3=618;
extern double LevelSell4=786;
extern double LevelSell5=1000;
extern double LevelSell6=1270;
extern double LevelSell7=1610;
extern double LevelSell8=2000;
extern double LevelBuy1=-380;
extern double LevelBuy2=-500;
extern double LevelBuy3=-618;
extern double LevelBuy4=-786;
extern double LevelBuy5=-1000;
extern double LevelBuy6=-1270;
extern double LevelBuy7=-1610;
extern double LevelBuy8=-2000;

int Magic;
int stopbuy1=1,stopbuy2=1,stopbuy3=1,stopbuy4=1,stopbuy5=1,stopbuy6=1,stopbuy7=1,stopbuy8=1;
int stopsell1=1,stopsell2=1,stopsell3=1,stopsell4=1,stopsell5=1,stopsell6=1,stopsell7=1,stopsell8=1;
double oldprice;
double Lots;

int init() {
return(0);}

int deinit() {
return(0);}

int start()
  {
   // Calcola MagicNumber
   Magic = MagicFromSymbol();

// Controllo lo stato degli ordini, trailing stop e breakeven
   double pips=MarketInfo(Symbol(),MODE_DIGITS);
   double diviso;
   if (pips==5 || pips==4) diviso=100000;
   if (pips==3) diviso=1000;
   if (pips==2) diviso=100;

// Lettura Indicatori
   string strategy;
   double price=iClose(Symbol(),0,0);
   double oprice=iOpen(Symbol(),0,0);

   double level0=iMA(Symbol(),0,MAPeriod,0,MODE_SMMA,PRICE_CLOSE,0);
   double levelb1,levelb2,levelb3,levelb4,levelb5,levelb6,levelb7,levelb8;
   double levels1,levels2,levels3,levels4,levels5,levels6,levels7,levels8;
   if (LevelSell1!=0) levels1=level0+(LevelSell1/diviso);if (LevelBuy1!=0) levelb1=level0+(LevelBuy1/diviso);
   if (LevelSell2!=0) levels2=level0+(LevelSell2/diviso);if (LevelBuy2!=0) levelb2=level0+(LevelBuy2/diviso);
   if (LevelSell3!=0) levels3=level0+(LevelSell3/diviso);if (LevelBuy3!=0) levelb3=level0+(LevelBuy3/diviso);
   if (LevelSell4!=0) levels4=level0+(LevelSell4/diviso);if (LevelBuy4!=0) levelb4=level0+(LevelBuy4/diviso);
   if (LevelSell5!=0) levels5=level0+(LevelSell5/diviso);if (LevelBuy5!=0) levelb5=level0+(LevelBuy5/diviso);
   if (LevelSell6!=0) levels6=level0+(LevelSell6/diviso);if (LevelBuy6!=0) levelb6=level0+(LevelBuy6/diviso);
   if (LevelSell7!=0) levels7=level0+(LevelSell7/diviso);if (LevelBuy7!=0) levelb7=level0+(LevelBuy7/diviso);
   if (LevelSell8!=0) levels8=level0+(LevelSell8/diviso);if (LevelBuy8!=0) levelb8=level0+(LevelBuy8/diviso);
   
   int onlb1,onlb2,onlb3,onlb4,onlb5,onlb6,onlb7,onlb8;
   int onls1,onls2,onls3,onls4,onls5,onls6,onls7,onls8;
   if (LevelSell1!=0 && oldpricelevels1) onls1=1; else onls1=0;
   if (LevelSell2!=0 && oldpricelevels2) onls2=1; else onls2=0;
   if (LevelSell3!=0 && oldpricelevels3) onls3=1; else onls3=0;
   if (LevelSell4!=0 && oldpricelevels4) onls4=1; else onls4=0;
   if (LevelSell5!=0 && oldpricelevels5) onls5=1; else onls5=0;
   if (LevelSell6!=0 && oldpricelevels6) onls6=1; else onls6=0;
   if (LevelSell7!=0 && oldpricelevels7) onls7=1; else onls7=0;
   if (LevelSell8!=0 && oldpricelevels8) onls8=1; else onls8=0;

   if (LevelBuy1!=0 && oldprice>levelb1 && pricelevelb2 && pricelevelb3 && pricelevelb4 && pricelevelb5 && pricelevelb6 && pricelevelb7 && pricelevelb8 && price>levelb8) onlb8=1; else onlb8=0;
   

if (oldprice!=price) oldprice=price;
// Ingresso nel mercato ordine
   int ticket;   
   RefreshRates();
   
   // buy
      if (onlb1==1 && stopbuy1==0)
         {Lots=StartLot;stopbuy1=1;ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,Name_Expert + " " +Symbol(),Magic,0,Green);}
      if (stopbuy1==1 && onlb2==1 && stopbuy2==0)
         {Lots=Lots+IncreaseLot;stopbuy2=1;ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,Name_Expert + " " +Symbol(),Magic,0,Green);}
      if (stopbuy2==1 && onlb3==1 && stopbuy3==0)
         {Lots=Lots+IncreaseLot;stopbuy3=1;ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,Name_Expert + " " +Symbol(),Magic,0,Green);}
      if (stopbuy3==1 && onlb4==1 && stopbuy4==0)
         {Lots=Lots+IncreaseLot;stopbuy4=1;ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,Name_Expert + " " +Symbol(),Magic,0,Green);}
      if (stopbuy4==1 && onlb5==1 && stopbuy5==0)
         {Lots=Lots+IncreaseLot;stopbuy5=1;ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,Name_Expert + " " +Symbol(),Magic,0,Green);}
      if (stopbuy5==1 && onlb6==1 && stopbuy6==0)
         {Lots=Lots+IncreaseLot;stopbuy6=1;ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,Name_Expert + " " +Symbol(),Magic,0,Green);}
      if (stopbuy6==1 && onlb7==1 && stopbuy7==0)
         {Lots=Lots+IncreaseLot;stopbuy7=1;ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,Name_Expert + " " +Symbol(),Magic,0,Green);}
      if (stopbuy7==1 && onlb8==1 && stopbuy8==0)
         {Lots=Lots+IncreaseLot;stopbuy8=1;ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,Name_Expert + " " +Symbol(),Magic,0,Green);}
   // sell
      if (onls1==1 && stopsell1==0)
         {Lots=StartLot;stopsell1=1;ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,Name_Expert+" " +Symbol(),Magic,0,Orange);}
      if (stopsell1==1 && onls2==1 && stopsell2==0)
         {Lots=Lots+IncreaseLot;stopsell2=1;ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,Name_Expert+" " +Symbol(),Magic,0,Orange);}
      if (stopsell2==1 && onls3==1 && stopsell3==0)
         {Lots=Lots+IncreaseLot;stopsell3=1;ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,Name_Expert+" " +Symbol(),Magic,0,Orange);}
      if (stopsell3==1 && onls4==1 && stopsell4==0)
         {Lots=Lots+IncreaseLot;stopsell4=1;ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,Name_Expert+" " +Symbol(),Magic,0,Orange);}
      if (stopsell4==1 && onls5==1 && stopsell5==0)
         {Lots=Lots+IncreaseLot;stopsell5=1;ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,Name_Expert+" " +Symbol(),Magic,0,Orange);}
      if (stopsell5==1 && onls6==1 && stopsell6==0)
         {Lots=Lots+IncreaseLot;stopsell6=1;ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,Name_Expert+" " +Symbol(),Magic,0,Orange);}
      if (stopsell6==1 && onls7==1 && stopsell7==0)
         {Lots=Lots+IncreaseLot;stopsell7=1;ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,Name_Expert+" " +Symbol(),Magic,0,Orange);}
      if (stopsell7==1 && onls8==1 && stopsell8==0)
         {Lots=Lots+IncreaseLot;stopsell8=1;ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,Name_Expert+" " +Symbol(),Magic,0,Orange);}

//
if (openPositions(Magic)==1 && stopbuy1==1 && price>level0) CloseAll();
if (openPositions(Magic)==2 && stopbuy1==1 && price>levelb1) CloseAll();
if (openPositions(Magic)==3 && stopbuy1==1 && price>levelb2) CloseAll();
if (openPositions(Magic)==4 && stopbuy1==1 && price>levelb3) CloseAll();
if (openPositions(Magic)==5 && stopbuy1==1 && price>levelb4) CloseAll();
if (openPositions(Magic)==6 && stopbuy1==1 && price>levelb5) CloseAll();
if (openPositions(Magic)==7 && stopbuy1==1 && price>levelb6) CloseAll();
if (openPositions(Magic)==8 && stopbuy1==1 && price= 0; l_pos_12--) {
      OrderSelect(l_pos_12, SELECT_BY_POS, MODE_TRADES);
      if (a_magic_0 == OrderMagicNumber()) l_count_8++;
   }
   return (l_count_8);
}
int MagicFromSymbol() 
{ // included by Renato 
   int MagicNumber=0;  
   for (int i=0; i<5; i++) 
   { 
     MagicNumber=MagicNumber*3+StringGetChar(Symbol(),i)+StringGetChar(Name_Expert,i);  
   }  
   MagicNumber=MagicNumber*2+Period(); 
   return(MagicNumber);  
}


void CloseAll ()
{
   int totalorders = OrdersTotal();
   int cnt = 0;
   for (cnt = totalorders ; cnt >=0 ; cnt--)
      {
         OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
         if(Symbol()!=OrderSymbol()) RefreshRates();
         if(OrderType()==OP_BUY) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),Slippage,Violet);
         if(OrderType()==OP_SELL) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),Slippage,Violet);
         if(OrderType()>OP_SELL) OrderDelete(OrderTicket());
      }
   //return (0);   
}

Recommend