Break_out_trend_EA

Author: Copyright 2018, MetaQuotes Software Corp.
Orders Execution
It automatically opens orders when conditions are reached
0 Views
0 Downloads
0 Favorites
Break_out_trend_EA
ÿþ//+------------------------------------------------------------------+

//|                                           Break_out_trend_EA.mq5 |

//|                        Copyright 2020, MetaQuotes Software Corp. |

//|                                             https://www.mql5.com |

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

#property copyright "Copyright 2018, MetaQuotes Software Corp."

#property link      "https://www.mql5.com/ru/users/s22aa"

#property version   "1.0"

#property description  "B:@K205B A45;:8 ?@8 ?@>1>5 B@5=4>2>9 ;8=88"



#define _ask  SymbolInfoDouble(_Symbol, SYMBOL_ASK)

#define _bid  SymbolInfoDouble(_Symbol, SYMBOL_BID)

#define _time SymbolInfoInteger(_Symbol, SYMBOL_TIME)



input string PPB = "PPB"; //8<O B@5=4>2>9 ?>78F88 Byu

input string TPB = "TPB"; //8<O B@5=4>2>9 B59:?@>D8B Byu

input string PPS = "PPS"; //8<O B@5=4>2>9 ?>78F88 Sell

input string TPS = "TPS"; //8<O B@5=4>2>9 B59:?@>D8B Sell

input color color_Byu  = clrBlue;  //F25B B@5=4>2>9 Byu

input color color_Sell = clrRed;  //F25B B@5=4>2>9 Sell

sinput ulong Magic     = 12345;

sinput ulong Slippage  = 30;



input double InpLots = 1;



double price_Sell, price_SL_Sell, price_TP_Sell, price_Buy, price_SL_Buy, price_TP_Buy;

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

int OnInit()

  {

   Trend_line("PPB", _ask + 20 * _Point, _time - 6000, _time + 600, color_Byu); // 8AC5< B@5=4>2K5

   Trend_line("TPB", _ask + 40 * _Point, _time - 6000, _time + 600, color_Byu);

   Trend_line("PPS", _bid - 20 * _Point, _time - 6000, _time + 600, color_Sell);

   Trend_line("TPS", _bid - 40 * _Point, _time - 6000, _time + 600, color_Sell);

   return(INIT_SUCCEEDED);

  }

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

void OnDeinit(const int reason) {}

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

void OnTick()

  {

   int x = 0;

   static int tempBuy = 0, tempSell = 0;

   static double price = 0;

   static string Name;

   static datetime last_time = 0;

   datetime cur_time = iTime(_Symbol, PERIOD_CURRENT, 0);



   int Total = ObjectsTotal(0, 0, OBJ_TREND); // >?@545;O5< :>;8G5AB2> B@5=4>2KE ;8=89 =0 3@0D8:5



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

     {

      Name = ObjectName(0, i, 0, OBJ_TREND);                 //=0E>48B 8<O

      price = ObjectGetValueByTime(0, Name, cur_time);       // F5=0 >1L5:B0 2> 2@5<O  >B:@KB8O =>2>3> 10@0



      if(Name == PPS)

         price_Sell = price; //F5=0 ;8=88 Sell

      if(Name == TPS)

         price_TP_Sell = price; //F5=0 ;8=88 B59: ?@>D8B Sell



      if(Name == PPB)

         price_Buy = price;

      if(Name == TPB)

         price_TP_Buy = price;





      if(PositionsTotal() == 0 && _bid >= price_Sell)            //5A;8 F5=0 =0E>48;0AL 2KH5 ;8=88 Sell

         tempSell = 1;                                           // 70?><8=05< GB> F5=0 1K;0 2KH5 ;8=88 Sell

      if(_bid <= price_Sell && Name == PPS && tempSell != 0 && price_Sell != 0 && price_TP_Sell != 0)// 5A;8 F5=0 >?CAB8;0AL =865 ;8=88 Sell

        {

         PositionOpen(ORDER_TYPE_SELL, _bid, price_Buy, price_TP_Sell, "Sell"); //>B:@K205< ?@>406C

         tempSell = 0;

         return;

        }



      if(PositionsTotal() == 0 && _ask <= price_Buy)

         tempBuy = 1;

      if(_ask >= price_Buy && Name == PPB && tempBuy != 0 && price_TP_Buy != 0)

        {

         PositionOpen(ORDER_TYPE_BUY, _ask, price_Sell, price_TP_Buy, "Buy");

         tempBuy = 0;

         return;

        }

     }



   if(PositionsTotal() > 0 && cur_time != last_time) //=0 =>2>9 A25G5 ?@>25@O5< C@>2=8 B59: ?@>D8B 8 AB>? ;>AA

     {

      if(PositionSelect(Symbol())) // 2K18@05< ?>78F8N

        {

         if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)

           {

            Change(price_Buy, price_TP_Sell);

           }

         else

           {

            Change(price_Sell, price_TP_Buy);

           }

        }

      last_time = cur_time;

     }

  }

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

double NormalizePrice(const double price)

  {

   double tick_size = SymbolInfoDouble(Symbol(), SYMBOL_TRADE_TICK_SIZE);

   if(tick_size != 0)

      return(NormalizeDouble(MathRound(price / tick_size) * tick_size, _Digits));

//---

   return(NormalizeDouble(price, _Digits));

  }

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

void PositionOpen(const ENUM_ORDER_TYPE order_type,

                  double price, double sl, double tp, const string comment)

  {

   ResetLastError();

   MqlTradeRequest request;

   MqlTradeResult result;

   ZeroMemory(request);

   price = NormalizePrice(price);

   sl = NormalizePrice(sl);

   tp = NormalizePrice(tp);



   if(CheckStopLoss_Takeprofit_ORDER(order_type, sl, tp))

     {

      if(CheckMoneyForTrade(Symbol(), InpLots, order_type))

        {

         request.action   = TRADE_ACTION_DEAL;

         // request.type_filling = ORDER_FILLING_RETURN;

         request.symbol   = Symbol();

         request.magic    = Magic;

         request.volume   = InpLots;

         request.type     = order_type;

         request.price    = price;

         request.sl       = sl;

         request.tp       = tp;

         request.deviation = Slippage;



         if(!OrderSend(request, result))

            Print("error ", GetLastError());

        }

     }

  }

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

bool CheckMoneyForTrade(string symb, double lots, ENUM_ORDER_TYPE type)

  {

//--- ?>;CG8< F5=C >B:@KB8O

   MqlTick mqltick;

   SymbolInfoTick(symb, mqltick);

   double price = mqltick.ask;

   if(type == ORDER_TYPE_SELL)

      price = mqltick.bid;

//--- 7=0G5=8O =5>1E>48<>9 8 A2>1>4=>9 <0@68

   double margin, free_margin = AccountInfoDouble(ACCOUNT_MARGIN_FREE);

//--- 2K7>25< DC=:F8N ?@>25@:8

   if(!OrderCalcMargin(type, symb, lots, price, margin))

     {

      //--- GB>-B> ?>H;> =5 B0:, A>>1I8< 8 25@=5< false

      Print("Error in ", __FUNCTION__, " code=", GetLastError());

      return(false);

     }

//--- 5A;8 =5 E20B05B A@54AB2 =0 ?@>2545=85 >?5@0F88

   if(margin > free_margin)

     {

      //--- A>>1I8< >1 >H81:5 8 25@=5< false

      Print("Not enough money for ", EnumToString(type), " ", lots, " ", symb, " Error code=", GetLastError());

      return(false);

     }

//--- ?@>25@:0 ?@>H;0 CA?5H=>

   return(true);

  }

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

void Change(double sl, double tp)

  {

   ResetLastError();

   MqlTradeRequest request;  // ?0@0<5B@K B>@3>2>3> 70?@>A0

   MqlTradeResult result;    // @57C;LB0B B>@3>2>3> 70?@>A0

   ZeroMemory(request);



   if(PositionSelect(Symbol())) // 2K18@05< ?>78F8N

     {

      double PositionSL = PositionGetDouble(POSITION_SL);                          // 70?8AK205< 2 ?5@5<5==CN C@>25=L AB>?;>AA0

      double PositionTP = PositionGetDouble(POSITION_TP);                          // 70?8AK205< 2 ?5@5<5==CN C@>25=L B59:?@>D8B0

      sl = NormalizePrice(sl);

      tp = NormalizePrice(tp);



      if(PositionSL != sl || PositionTP != tp)

        {

         ENUM_POSITION_TYPE type = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE); //>?@545;O5< B8? ?>78F88 Buy 8;8 Sell

         if(CheckStopLoss_Takeprofit_POSITION(type, sl, tp)) // ?@>25@O5< =0 <8=8<0;L=>5 @0AAB>O=85 >B F5=K

           {

            request.action    = TRADE_ACTION_SLTP;

            request.position  = PositionGetInteger(POSITION_TICKET);

            request.symbol    = Symbol();

            request.sl        = sl;

            request.tp        = tp;

            if(!OrderSend(request, result))

               Print("error ", GetLastError());

           }

        }

     }

  }

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

bool CheckStopLoss_Takeprofit_POSITION(ENUM_POSITION_TYPE type, double SL, double TP)

  {

//--- ?>;CG8< C@>25=L SYMBOL_TRADE_STOPS_LEVEL

   int stops_level = (int)SymbolInfoInteger(_Symbol, SYMBOL_TRADE_STOPS_LEVEL);

   if(stops_level != 0)

     {

      PrintFormat("SYMBOL_TRADE_STOPS_LEVEL=%d: StopLoss 8 TakeProfit 4>;6=K 1KBL" +

                  " =5 1;865 %d ?C=:B>2 >B F5=K 70:@KB8O", stops_level, stops_level);

     }

//---

   bool SL_check = false, TP_check = false;

//--- ?@>25@O5< B>;L:> 420 B8?0 >@45@>2

   switch(type)

     {

      //--- >?5@0F8O ?>:C?:0

      case  POSITION_TYPE_BUY:

        {

         //--- ?@>25@8< StopLoss

         SL_check = (_bid - SL > stops_level * _Point);

         if(!SL_check)

            PrintFormat("For order %s StopLoss=%.5f must be less than %.5f" +

                        " (_bid=%.5f - SYMBOL_TRADE_STOPS_LEVEL=%d ?C=:B>2)",

                        EnumToString(type), SL, _bid - stops_level * _Point, _bid, stops_level);

         //--- ?@>25@8< TakeProfit

         TP_check = (TP - _bid > stops_level * _Point);

         if(!TP_check)

            PrintFormat("For order %s TakeProfit=%.5f must be greater than %.5f" +

                        " (_bid=%.5f + SYMBOL_TRADE_STOPS_LEVEL=%d ?C=:B>2)",

                        EnumToString(type), TP, _bid + stops_level * _Point, _bid, stops_level);

         //--- 25@=5< @57C;LB0B ?@>25@:8

         return(SL_check && TP_check);

        }

      //--- >?5@0F8O ?@>4060

      case  POSITION_TYPE_SELL:

        {

         //--- ?@>25@8< StopLoss

         SL_check = (SL - _ask > stops_level * _Point);

         if(!SL_check)

            PrintFormat("For order %s StopLoss=%.5f must be greater than %.5f " +

                        " (_ask=%.5f + SYMBOL_TRADE_STOPS_LEVEL=%d ?C=:B>2)",

                        EnumToString(type), SL, _ask + stops_level * _Point, _ask, stops_level);

         //--- ?@>25@8< TakeProfit

         TP_check = (_ask - TP > stops_level * _Point);

         if(!TP_check)

            PrintFormat("For order %s TakeProfit=%.5f must be less than %.5f " +

                        " (_ask=%.5f - SYMBOL_TRADE_STOPS_LEVEL=%d ?C=:B>2)",

                        EnumToString(type), TP, _ask - stops_level * _Point, _ask, stops_level);

         //--- 25@=5< @57C;LB0B ?@>25@:8

         return(TP_check && SL_check);

        }

      break;

     }

   return false;

  }

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

bool CheckStopLoss_Takeprofit_ORDER(ENUM_ORDER_TYPE type, double SL, double TP)

  {

//--- ?>;CG8< C@>25=L SYMBOL_TRADE_STOPS_LEVEL

   int stops_level = (int)SymbolInfoInteger(_Symbol, SYMBOL_TRADE_STOPS_LEVEL);

   if(stops_level != 0)

     {

      PrintFormat("SYMBOL_TRADE_STOPS_LEVEL=%d: StopLoss 8 TakeProfit 4>;6=K 1KBL" +

                  " =5 1;865 %d ?C=:B>2 >B F5=K 70:@KB8O", stops_level, stops_level);

     }

//---

   bool SL_check = false, TP_check = false;

//--- ?@>25@O5< B>;L:> 420 B8?0 >@45@>2

   switch(type)

     {

      //--- >?5@0F8O ?>:C?:0

      case  ORDER_TYPE_BUY:

        {

         //--- ?@>25@8< StopLoss

         SL_check = (_bid - SL > stops_level * _Point);

         if(!SL_check)

            PrintFormat("For order %s StopLoss=%.5f must be less than %.5f" +

                        " (_bid=%.5f - SYMBOL_TRADE_STOPS_LEVEL=%d ?C=:B>2)",

                        EnumToString(type), SL, _bid - stops_level * _Point, _bid, stops_level);

         //--- ?@>25@8< TakeProfit

         TP_check = (TP - _bid > stops_level * _Point);

         if(!TP_check)

            PrintFormat("For order %s TakeProfit=%.5f must be greater than %.5f" +

                        " (_bid=%.5f + SYMBOL_TRADE_STOPS_LEVEL=%d ?C=:B>2)",

                        EnumToString(type), TP, _bid + stops_level * _Point, _bid, stops_level);

         //--- 25@=5< @57C;LB0B ?@>25@:8

         return(SL_check && TP_check);

        }

      //--- >?5@0F8O ?@>4060

      case  ORDER_TYPE_SELL:

        {

         //--- ?@>25@8< StopLoss

         SL_check = (SL - _ask > stops_level * _Point);

         if(!SL_check)

            PrintFormat("For order %s StopLoss=%.5f must be greater than %.5f " +

                        " (_ask=%.5f + SYMBOL_TRADE_STOPS_LEVEL=%d ?C=:B>2)",

                        EnumToString(type), SL, _ask + stops_level * _Point, _ask, stops_level);

         //--- ?@>25@8< TakeProfit

         TP_check = (_ask - TP > stops_level * _Point);

         if(!TP_check)

            PrintFormat("For order %s TakeProfit=%.5f must be less than %.5f " +

                        " (_ask=%.5f - SYMBOL_TRADE_STOPS_LEVEL=%d ?C=:B>2)",

                        EnumToString(type), TP, _ask - stops_level * _Point, _ask, stops_level);

         //--- 25@=5< @57C;LB0B ?@>25@:8

         return(TP_check && SL_check);

        }

      break;

     }

   return false;

  }

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

void Trend_line(string name, double price, datetime Time1, datetime Time2, color col)

  {

   ResetLastError();

   NormalizePrice(price);

   if(ObjectFind(0, name))

     {

      ObjectDelete(0, name);

     }

   if(!ObjectCreate(0, name, OBJ_TREND, 0, Time1, price, Time2, price)) //--- A>74048< ;8=8N ?> 7040==>9 F5=5

     {

      Print(__FUNCTION__,

            ": =5 C40;>AL A>740BL ;8=8N! >4 >H81:8 = ", GetLastError());

     }

   ObjectSetInteger(0, name, OBJPROP_COLOR, col); //--- CAB0=>28< F25B ;8=88

   ObjectSetInteger(0, name, OBJPROP_STYLE, STYLE_SOLID); //--- CAB0=>28< AB8;L ;8=88

   ObjectSetInteger(0, name, OBJPROP_WIDTH, 1); //--- CAB0=>28< B>;I8=C ;8=88

   ObjectSetInteger(0, name, OBJPROP_BACK, false); //--- >B>1@078< =0 ?5@54=5< (false) 8;8 704=5< (true) ?;0=5

   ObjectSetInteger(0, name, OBJPROP_SELECTABLE, true); //--- 2:;NG8< (true) 8;8 >B:;NG8< (false) @568< 2K45;5=8O 4;O ?5@5<5I5=89 (4>ABC?=>ABL 2K45;5=8O)

   ObjectSetInteger(0, name, OBJPROP_SELECTED, true); //---  true, GB> ?>72>;O5B 2K45;OBL 8 ?5@5<5I0BL MB>B >1J5:B

   ObjectSetInteger(0, name, OBJPROP_HIDDEN, false); //--- A:@>5< (true) 8;8 >B>1@078< (false) 8<O 3@0D8G5A:>3> >1J5:B0 2 A?8A:5 >1J5:B>2

   ObjectSetInteger(0, name, OBJPROP_RAY_RIGHT, true); //--- ;CG 2?@02>

   return;

  }

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

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---