EA_PriceDeviation_v1.00

Author: Developer: Andrey Minaev
Profit factor:
0.00
1 Views
0 Downloads
0 Favorites
EA_PriceDeviation_v1.00
ÿþ//+-----------------------------------------------------------------------------------------------+

//| Developer:        Andrey Minaev                                                               |

//| Expert Advisor:   EA_PriceDeviation_v1.00.mq4                                                 |

//| MQL5:             mql5.com/ru/users/id.scorpion                                               |

//| Mail:             id.scorpion@mail.ru                                                         |

//| Skype:            id.scorpion                                                                 |

//+-----------------------------------------------------------------------------------------------+

#property copyright "Developer: Andrey Minaev"

#property link      "http://www.mql5.com/ru/users/id.scorpion"

#property strict



extern string EASettings  = "";     // EA Settings

extern double fixLot      = 0.01;   // FixLot

extern int    stopLoss    = 50;     // StopLoss

extern int    takeProfit  = 50;     // TakeProfit

extern int    deviation   = 80;     // Deviation

extern int    magic       = 123;    // Magic



extern string             MASettings = "";            // Moving Average Settings

extern int                MAPeriod   = 50;            // Period

extern ENUM_MA_METHOD     MAMethod   = MODE_SMA;      // Method

extern ENUM_APPLIED_PRICE MAPrice    = PRICE_CLOSE;   // Price



//--- 3;>10;L=K5 ?5@5<5==K5

double MAValue;     // B5:CI55 7=0G5=85 

double openPrice;   // F5=0 >B:@KB8O ?>78F88

long   chartID;     // 845=B8D8:0B>@ 3@0D8:0

bool   upTrend;     // true - B5:CI0O B5=45=F8O 2>AE>4OI0O

bool   dnTrend;     // true - B5:CI0O B5=45=F8O =8AE>4OI0O

bool   posOpen;     // true - ?>78F8O >B:@KB0

//+-----------------------------------------------------------------------------------------------+

int OnInit()

{  

   //--- ?@>25@8< :>@@5:B=>ABL =0AB@>5:

   if(!CheckSettings()) ExpertRemove();

   chartID = ChartID();

   if(GetOrdersNumber() > 0) posOpen = true;

   return INIT_SUCCEEDED;

}

//+-----------------------------------------------------------------------------------------------+

void OnDeinit(const int reason)

{

   ObjectDelete("OpenPrice");

}

//+-----------------------------------------------------------------------------------------------+

void OnTick()

{

   if(GetOrdersNumber() == 0) posOpen = false;

   

   //--- C7=05< B5:CICN B5=45=F8N

   MAValue = NormPrice(iMA(_Symbol, PERIOD_CURRENT, MAPeriod, 0, MAMethod, MAPrice, 0));

   

   if(MAValue < Bid) { upTrend = true; dnTrend = false; }

   if(MAValue > Bid) { dnTrend = true; upTrend = false; }

   

   //--- B5:CI0O B5=45=F8O 2>AE>4OI0O

   if(upTrend)

   {

      //--- =5B >B:@KBKE ?>78F89

      if(!posOpen)

      {

         openPrice = MAValue + deviation * _Point;

         CreateLine("OpenPrice", "@>4060", openPrice, clrDimGray);

      }

      //--- >B:@>5< ?>78F8N =0 ?@>406C

      if(!posOpen && Bid >= openPrice)

         OpenMarketOrder(OP_SELL, fixLot, Bid);

   }

   

   //--- B5:CI0O B5=45=F8O =8AE>4OI0O

   if(dnTrend)

   {

      //--- =5B >B:@KBKE ?>78F89

      if(!posOpen)

      {

         openPrice = MAValue - deviation * _Point;

         CreateLine("OpenPrice", ">:C?:0", openPrice, clrDimGray);

      }

      //--- >B:@>5< ?>78F8N =0 ?>:C?:C

      if(!posOpen && Ask <= openPrice)

         OpenMarketOrder(OP_BUY, fixLot, Ask);

   }

}

//+-----------------------------------------------------------------------------------------------+

//| $C=:F8O >B:@K205B @K=>G=K9 >@45@                                                              |

//+-----------------------------------------------------------------------------------------------+

void OpenMarketOrder(int type, double lots, double price)

{

   int ticket = OrderSend(_Symbol, type, lots, price, 0, 0, 0, "", magic, 0);

   

   if(ticket > 0) { SetSLTP(ticket); posOpen = true; ObjectDelete("OpenPrice"); }

}

//+-----------------------------------------------------------------------------------------------+

//| $C=:F8O CAB0=02;8205B StopLoss 8 TakeProfit                                                   |

//+-----------------------------------------------------------------------------------------------+

void SetSLTP(int ticket)

{

   double sl = 0;

   double tp = 0;

   

   if(OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))

   {

      int    type = OrderType();

      double oop  = OrderOpenPrice();

      

      if(type == OP_BUY)

      {

         sl = NormPrice(oop - stopLoss   * _Point);

         tp = NormPrice(oop + takeProfit * _Point);

      }

      if(type == OP_SELL)

      {

         sl = NormPrice(oop + stopLoss   * _Point);

         tp = NormPrice(oop - takeProfit * _Point);

      }

      if(OrderModify(ticket, oop, sl, tp, 0)) return;

   }

}

//+-----------------------------------------------------------------------------------------------+

//| $C=:F8O A>7405B ;8=8N =0 3@0D8:5                                                              |

//+-----------------------------------------------------------------------------------------------+

void CreateLine(string name, string descrip, double price, color clr)

{

   ObjectDelete(name);

   

   ObjectCreate(chartID, name, OBJ_HLINE, 0, 0, price);

   ObjectSetInteger(chartID, name, OBJPROP_WIDTH, 1);

   ObjectSetInteger(chartID, name, OBJPROP_STYLE,STYLE_SOLID);

   ObjectSetInteger(chartID, name, OBJPROP_COLOR, clr);

   ObjectSetInteger(chartID, name, OBJPROP_BACK, false);

   ObjectSetInteger(chartID, name, OBJPROP_SELECTABLE, false);

   ObjectSetInteger(chartID, name, OBJPROP_SELECTED, false);

   ObjectSetInteger(chartID, name, OBJPROP_HIDDEN, true);

   ObjectSetString(chartID, name, OBJPROP_TEXT, descrip);

}

//+-----------------------------------------------------------------------------------------------+

//| $C=:F8O 2>72@0I05B :>;8G5AB2> >B:@KBKE >@45@>2                                                |

//+-----------------------------------------------------------------------------------------------+

int GetOrdersNumber(void)

{

   int number = 0;

   for(int i = 0; i < OrdersTotal(); i++)

      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))

         if(OrderSymbol() == _Symbol && OrderMagicNumber() == magic)

            number++;

   return number;

}

//+-----------------------------------------------------------------------------------------------+

//| $C=:F8O =>@<0;87C5B F5=C                                                                      |

//+-----------------------------------------------------------------------------------------------+

double NormPrice(double price)

{

   return NormalizeDouble(price, _Digits);

}

//+-----------------------------------------------------------------------------------------------+

//| $C=:F8O ?@>25@O5B :>@@5:B=>ABL =0AB@>5: A>25B=8:0                                             |

//+-----------------------------------------------------------------------------------------------+

bool CheckSettings(void)

{

   double maxLot = MarketInfo(_Symbol, MODE_MAXLOT);

   double minLot = MarketInfo(_Symbol, MODE_MINLOT);

   

   if(fixLot > maxLot)

   {

      Alert(">;8G5AB2> ;>B>2 4>;6=> 1KBL <5=LH5 8;8 @02=> ", maxLot);

      return false;

   }

   if(fixLot < minLot)

   {

      Alert(">;8G5AB2> ;>B>2 4>;6=> 1KBL 1>;LH5 8;8 @02=> ", minLot);

      return false;

   }

   

   double stopLevel = MarketInfo(_Symbol, MODE_STOPLEVEL);

   double spread    = MarketInfo(_Symbol, MODE_SPREAD);

   

   if(stopLoss < stopLevel + spread)

   {

      Alert("#1KB>: 4>;65= 1KBL 1>;LH5 8;8 @025= ", stopLevel + spread, " ?C=:B0<");

      return false;

   }

   if(takeProfit < stopLevel + spread)

   {

      Alert("@81K;L 4>;6=0 1KBL 1>;LH5 8;8 @02=0 ", stopLevel + spread, " ?C=:B0<");

      return false;

   }

   

   int pip = 2;

   if(_Digits == 3 || _Digits == 5) pip *= 10;

   

   if(deviation < pip)

   {

      Alert("B:;>=5=85 4>;6=> 1KBL 1>;LH5 8;8 @02=> ", pip, " ?C=:B0<");

      return false;

   }

   return true;

}

//+-----------------------------------------------------------------------------------------------+

Comments